-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfm_train.py
175 lines (137 loc) · 5.28 KB
/
fm_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.layers import Dropout, Flatten, Dense, Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array, load_img
from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from imutils import paths
import tensorflow
import matplotlib.pyplot as plt
import numpy as np
import argparse, os
# Argument parser
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dataset", required=True, help="Path of DATASET dir")
parser.add_argument("-p", "--plot", type=str, default="ploted.png", help="Path to output loss/acc plot")
parser.add_argument("-m", "--model", type=str, default="mask_detector.model", help="Path to output human face detector model")
args = vars(parser.parse_args())
# initialize the learning rate, number of epochs to train our model
init_lr= 1e-4 # because we'll apply a learning rate decay schedule
epochs = 20
bs = 32
lb = LabelBinarizer()
def pre_process():
imgPaths = list(paths.list_images(args["dataset"]))
data = []
labels = []
# walk over all image
for imgPath in imgPaths:
label = imgPath.split(os.path.sep)[-2]
# load image
image = load_img(imgPath, target_size=(224,224))
image = img_to_array(image)
image = preprocess_input(image)
# update data and label list
data.append(image)
labels.append(label)
# convert data and label to np array
data = np.array(data, dtype="float32")
labels = np.array(labels)
return data, labels
def one_hot(labels):
# one hot encoging on the labels
labels = lb.fit_transform(labels)
return to_categorical(labels) # labels
def data_augmentation():
return ImageDataGenerator(
rotation_range=20,
zoom_range=0.15,••••••
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.15,
horizontal_flip=True,
fill_mode="nearest")
def hModel():
# load the mobile network
baseModel = MobileNetV2(weights="imagenet", include_top=False, input_tensor=Input(shape=(224,224,3)))
# construction of the head of model htat will be placed on top of the model
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(7,7))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(128, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(2, activation="softmax")(headModel)
# place the FC model on top
model = Model(inputs=baseModel.input, outputs=headModel)
# add the rest o fmodel to the final model
for layer in baseModel.layers:
layer.trainable = False
return model
def compileModel(model):
opt = Adam(lr=init_lr, decay=init_lr/epochs)
model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"])
def fitModel(model):
history = model.fit(
aug.flow(trainX, trainY, batch_size=bs),
steps_per_epoch=len(trainX) // bs,
validation_data=(testX, testY),
validation_steps=len(testX) // bs,
epochs=epochs)
print("FIT already done")
return history
def evaluate():
pred = model.predict(testX, batch_size=bs)
pred = np.argmax(pred, axis=1)
print(classification_report(testY.argmax(axis=1), pred, target_names=lb.classes_))
def saveModel(model):
model.save(args["model"], save_format="h5")
def History(history):
n = epochs
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, n), history.history["loss"], label="train_loss")
plt.plot(np.arange(0, n), history.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, n), history.history["accuracy"], label="train_acc")
plt.plot(np.arange(0, n), history.history["val_accuracy"], label="val_acc")
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Acc")
plt.legend(loc="lower left")
plt.savefig(args["plot"])
if __name__ == '__main__':
#tensorflow.debugging.set_log_device_placement(True)
gpus = tensorflow.config.experimental.list_physical_devices('GPU')
if len(gpus) == 0:
gpus = tensorflow.config.experimental.list_physical_devices('CPU')
try:
tensorflow.config.experimental.set_virtual_device_configuration(gpus[0],
[tensorflow.config.experimental.VirtualDeviceConfiguration(memory_limit=2048)])
print(" - [x]: Loading images ... ->")
data, labels = pre_process()
print(" - [x]: One hot on labels ... ->")
labels = one_hot(labels)
print(" - [x]: Split dataset ... ->")
(trainX, testX, trainY, testY) = train_test_split(data, labels,
test_size=0.2, stratify=labels, random_state=42)
print(" - [x]: Data augmentation ... ->")
aug = data_augmentation()
print(" - [x]: Prepare the MobileNetV2 for fine-tuning ... ->")
model = hModel()
print(" - [x]: Compiling the model ... ->")
compileModel(model)
print(" - [x]: Train the model ... ->")
history = fitModel(model)
print(" - [x]: evaluate model ... ->")
evaluate()
print(" - [x]: Store the model on disk... ->")
saveModel(model)
print(" - [x]: Plot the training loss and accuracy ... ->")
History(history)
except RuntimeError as e:
print(e)