-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai_image_classifier.py
121 lines (90 loc) · 3.27 KB
/
ai_image_classifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import gc
import os
import pandas
from keras.layers import Dense, Conv2D, Activation, MaxPooling2D, Dropout, Flatten
from keras.models import Sequential
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import EarlyStopping
def main():
gc.collect()
data_dir = os.path.abspath('.')
german_df = pandas.read_csv(os.path.join(data_dir, 'german_index.csv'))
engie_df = pandas.read_csv(os.path.join(data_dir, 'engie_index.csv'))
dataframe = pandas.concat([german_df]) # , engie_df
num_classes = 7
train = dataframe.iloc[:round(len(dataframe) * .8)]
test = dataframe.iloc[round(len(dataframe) * .8):]
train_datagen = ImageDataGenerator(rescale=1. / 255., validation_split=0.25)
train_generator = train_datagen.flow_from_dataframe(
dataframe=train,
directory=data_dir,
x_col="file_path",
y_col="emotion",
batch_size=32,
seed=42,
shuffle=True,
subset="training",
class_mode="categorical",
target_size=(64, 64))
validation_generator = train_datagen.flow_from_dataframe(
dataframe=train,
directory=data_dir,
x_col="file_path",
y_col="emotion",
batch_size=32,
seed=42,
shuffle=True,
subset="validation",
class_mode="categorical",
target_size=(64, 64))
gc.collect()
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same', input_shape=(64, 64, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('hard_sigmoid'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(optimizer=Adam(lr=0.001), loss="categorical_crossentropy", metrics=["accuracy"])
model.summary()
gc.collect()
step_size_generator = train_generator.n // train_generator.batch_size
step_size_validate = validation_generator.n // validation_generator.batch_size
model.fit_generator(
generator=train_generator,
validation_data=validation_generator,
steps_per_epoch=step_size_generator,
validation_steps=step_size_validate,
epochs=100,
use_multiprocessing=True,
callbacks=[EarlyStopping(monitor='loss', mode='min', verbose=1, patience=5)]
)
gc.collect()
print('Model Done Training!')
model.save('model.h5')
print('Model Saved!')
print('Evaluating model...')
test_datagen = ImageDataGenerator(rescale=1. / 255.)
test_generator = test_datagen.flow_from_dataframe(
dataframe=test,
directory=data_dir,
x_col="file_path",
y_col="emotion",
batch_size=32,
seed=42,
shuffle=True,
class_mode="categorical",
target_size=(64, 64))
test_steps = test_generator.n / test_generator.batch_size
scores = model.evaluate_generator(generator=test_generator, steps=test_steps, verbose=1, use_multiprocessing=True)
print("Model accuracy %.2f%%" % (scores[1] * 100))
del model
gc.collect()
if __name__ == "__main__":
main()