-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtraining_code_original.py
189 lines (162 loc) · 6.09 KB
/
training_code_original.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory
import os
for dirname, _, filenames in os.walk('/kaggle/input/face-mask-detection/images'):
for filename in filenames:
print(os.path.join(dirname, filename))
import os
import xml.etree.ElementTree as et
import re
import pandas as pd
dic = {"image": [],"Dimensions": []}
for i in range(1,116):
dic[f'Object {i}']=[]
print("Generating data in CSV format....")
for file in os.listdir("/kaggle/input/face-mask-detection/annotations"):
row = []
xml = et.parse("/kaggle/input/face-mask-detection/annotations/"+file)
root = xml.getroot()
img = root[1].text
row.append(img)
h,w = root[2][0].text,root[2][1].text
row.append([h,w])
for i in range(4,len(root)):
temp = []
temp.append(root[i][0].text)
for point in root[i][5]:
temp.append(point.text)
row.append(temp)
for i in range(len(row),119):
row.append(0)
for i,each in enumerate(dic):
dic[each].append(row[i])
df = pd.DataFrame(dic)
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
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import 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
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt
import numpy as np
import argparse
import pandas as pd
import glob
import os
import cv2
import random as rand
image_directories = sorted(glob.glob(os.path.join("/kaggle/input/face-mask-detection/images", "*.png")))
j = 0
classes = ["without_mask", "mask_weared_incorrect", "with_mask"]
labels = []
data = []
print("Extracting each data into respective label folders....")
for idx, image in enumerate(image_directories):
img = cv2.imread(image)
# scale to dimension
X, Y = df["Dimensions"][idx]
cv2.resize(img, (int(X), int(Y)))
# find the face in each object
for obj in df.columns[3:]:
info = df[obj][idx]
if info != 0:
label = info[0]
info[0] = info[0].replace(str(label), str(classes.index(label)))
info = [int(each) for each in info]
face = img[info[2]:info[4], info[1]:info[3]]
if ((info[3] - info[1]) > 40 and (info[4] - info[2]) > 40):
try:
face = cv2.resize(face, (224, 224))
face = img_to_array(face)
face = preprocess_input(face)
data.append(face)
labels.append(label)
if (label == "mask_weared_incorrect"):
data.append(face)
labels.append(label)
except:
pass
print("Done!")
data = np.array(data, dtype="float32")
labels = np.array(labels)
lb = LabelEncoder()
labels = lb.fit_transform(labels)
labels = to_categorical(labels)
aug = ImageDataGenerator(
zoom_range=0.1,
rotation_range=25,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.15,
horizontal_flip=True,
fill_mode="nearest"
)
baseModel = MobileNetV2(weights="imagenet", include_top=False,
input_tensor=Input(shape=(224, 224, 3)))
# construct the head of the model that will be placed on top of the
# the base model
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(64, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(3, activation="softmax")(headModel)
# place the head FC model on top of the base model (this will become
# the actual model we will train)
model = Model(inputs=baseModel.input, outputs=headModel)
# loop over all layers in the base model and freeze them so they will
# *not* be updated during the first training process
for layer in baseModel.layers:
layer.trainable = False
INIT_LR = 1e-4
EPOCHS = 50
BS = 1
(trainX, testX, trainY, testY) = train_test_split(data, labels,
test_size=0.3, stratify=labels, random_state=42)
print("[INFO] compiling model...")
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="categorical_crossentropy", optimizer=opt,
metrics=["accuracy"])
# train the head of the network
print("[INFO] training head...")
H = 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("[INFO] evaluating network...")
predIdxs = model.predict(testX, batch_size=32)
# for each image in the testing set we need to find the index of the
# label with corresponding largest predicted probability
predIdxs = np.argmax(predIdxs, axis=1)
# show a nicely formatted classification report
print(classification_report(testY.argmax(axis=1), predIdxs,
target_names=lb.classes_))
# serialize the model to disk
print("[INFO] saving mask detector model...")
# plot the training loss and accuracy
N = EPOCHS
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, N), H.history["loss"], label="train_loss")
plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, N), H.history["accuracy"], label="train_acc")
plt.plot(np.arange(0, N), H.history["val_accuracy"], label="val_acc")
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")
plt.show()