-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
151 lines (117 loc) · 5.82 KB
/
train.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from tensorflow.keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Flatten, AveragePooling2D, Input
from tensorflow.keras.optimizers import Adam
import os
import logging
import sys
from collections import Counter
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
parent_path = os.path.dirname(__file__)
src_path = os.path.dirname(parent_path)
sys.path.append(parent_path)
sys.path.append(src_path)
class CustomModel:
def __init__(self, train_dir, val_dir, test_dir):
self.batchsize = 48
self.img_height = 160 # 224
self.img_width = 160 # 224
self.channels = 3
self.train_dir = train_dir
self.val_dir = val_dir
self.test_dir = test_dir
self.train_datagen = None
self.val_datagen = None
self.train_generator = None
self.validation_generator = None
self.test_generator = None
self.class_weight = None
def import_augmented_data(self):
"""data augmentation on train/val sets"""
self.train_datagen = ImageDataGenerator(rotation_range=20.0,
horizontal_flip=True,
shear_range=0.1,
rescale=1./255)
self.val_datagen = ImageDataGenerator(rescale=1./255)
self.train_generator = self.train_datagen.flow_from_directory(self.train_dir,
target_size=(self.img_height, self.img_width),
batch_size= self.batchsize,
class_mode='categorical',
shuffle=True)
self.validation_generator = self.val_datagen.flow_from_directory(self.val_dir,
target_size=(self.img_height, self.img_width),
batch_size=self.batchsize,
class_mode='categorical')
self.test_generator = self.val_datagen.flow_from_directory(self.test_dir,
target_size=(self.img_height, self.img_width),
batch_size=self.batchsize,
class_mode='categorical')
counter = Counter(self.train_generator.classes)
total = float(sum(counter.values()))
self.class_weight = {class_id: (1 / num_images) * (total) / 2.0 for class_id, num_images in counter.items()}
def create_model(self):
"""https://github.com/atulapra/Emotion-detection/blob/master/src/emotions.py"""
baseModel = MobileNetV2(weights="imagenet", include_top=False,
input_tensor=Input(shape=(160, 160, 3)))
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(5, 5))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(128, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(3, activation="softmax")(headModel)
self.model = Model(inputs=baseModel.input, outputs=headModel)
for layer in baseModel.layers:
layer.trainable = False
# compile our model
self.model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=0.0003, decay=1e-6),
metrics=['accuracy'])
return self.model.summary()
def train_model(self, model_output_path, epochs=1):
""" commence model training"""
early_stopping = EarlyStopping(
monitor='val_accuracy',
mode='max',
verbose=1,
patience=3,
min_delta=0.00001)
checkpoint = ModelCheckpoint(
filepath=model_output_path,
monitor='val_accuracy',
verbose=1,
save_best_only=True,
mode='max')
learning_rate_reduction = ReduceLROnPlateau(
monitor='val_accuracy',
patience=1,
verbose=1,
factor=0.2,
min_lr=0.00001)
self.model.fit(self.train_generator,
steps_per_epoch=self.train_generator.samples // self.batchsize,
validation_data=self.validation_generator,
validation_steps=self.validation_generator.samples // self.batchsize,
epochs=epochs,
callbacks=[early_stopping, checkpoint, learning_rate_reduction],
class_weight=self.class_weight)
def evaluate_model(self):
"""compute test accuracy"""
test_loss, test_accuracy = self.model.evaluate(self.test_generator)
return test_loss, test_accuracy
if __name__ == '__main__':
# define the directories
train_dir = os.path.join('images', 'train')
val_dir = os.path.join('images', 'valid')
test_dir = os.path.join('images', 'test')
# define the model
model = CustomModel(train_dir, val_dir, test_dir)
model.import_augmented_data()
model.create_model()
# train model
model.train_model('src/emotions.h5', epochs=20)
test_loss, test_accuracy = model.evaluate_model()
logger.info(f"the test loss is {test_loss}")
logger.info(f"the test acc is {test_accuracy}")