-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
168 lines (134 loc) · 4.67 KB
/
Copy pathtrain.py
File metadata and controls
168 lines (134 loc) · 4.67 KB
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
from setup import config
from tensorflow.keras.applications import VGG16
from tensorflow.keras.layers import Flatten, Dense, Input, Dropout
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
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 imutils import paths
import matplotlib.pyplot as plt
from tqdm import tqdm
import numpy as np
import pickle
import cv2
import os
print("[INFO] loading dataset...")
data = []
labels = []
bboxes = []
imagePaths = []
for csvPath in tqdm(paths.list_files(config.ANNOTS_PATH, validExts=(".csv"))):
rows = open(csvPath).read().strip().split('\n')
for row in tqdm(rows):
row = row.split(",")
(filename, startX, startY, endX, endY, label) = row
imagePath = os.path.join(config.IMAGES_PATH, label, filename)
image = cv2.imread(imagePath)
(h, w) = image.shape[:2]
startX = float(startX) / w
startY = float(startY) / h
endX = float(endX) / w
endY = float(endY) / h
image = load_img(imagePath, target_size=(224, 224))
image = img_to_array(image)
data.append(image)
labels.append(label)
bboxes.append((startX, startY, endX, endY))
imagePaths.append(filename)
data = np.array(data, dtype="float32") / 255.0
labels = np.array(labels)
bboxes = np.array(bboxes, dtype="float32")
imagePaths = np.array(imagePaths)
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
if len(lb.classes_) == 2:
labels = to_categorical(labels)
split = train_test_split(data, labels, bboxes, imagePaths,
test_size=0.20, random_state=42)
(trainImages, testImages) = split[:2]
(trainLabels, testLabels) = split[2:4]
(trainBBoxes, testBBoxes) = split[4:6]
(trainPaths, testPaths) = split[6:]
print("[INFO] saving testing image paths...")
f = open(config.TEST_PATHS, "w")
f.write("\n".join(testPaths))
f.close()
vgg = VGG16(weights="imagenet", include_top=False,
input_tensor=Input(shape=(224, 224, 3)))
vgg.trainable = False
flatten = vgg.output
flatten = Flatten()(flatten)
bboxHead = Dense(128, activation="relu")(flatten)
bboxHead = Dense(64, activation="relu")(bboxHead)
bboxHead = Dense(32, activation="relu")(bboxHead)
bboxHead = Dense(4, activation="sigmoid", name="bounding_box")(bboxHead)
softmaxHead = Dense(512, activation="relu")(flatten)
softmaxHead = Dropout(0.5)(softmaxHead)
softmaxHead = Dense(512, activation="relu")(softmaxHead)
softmaxHead = Dropout(0.5)(softmaxHead)
softmaxHead = Dense(len(lb.classes_), activation="softmax",
name="class_label")(softmaxHead)
model = Model(inputs=vgg.input, outputs=(bboxHead, softmaxHead))
losses = {
"class_label": "categorical_crossentropy",
"bounding_box": "mean_squared_error",
}
lossWeights = {
"class_label": 1.0,
"bounding_box": 1.0
}
opt = Adam(learning_rate=config.INIT_LR)
model.compile(loss=losses, optimizer=opt, metrics=[
"accuracy"], loss_weights=lossWeights)
print(model.summary())
trainTargets = {
"class_label": trainLabels,
"bounding_box": trainBBoxes
}
testTargets = {
"class_label": testLabels,
"bounding_box": testBBoxes
}
print("[INFO] training model...")
H = model.fit(
trainImages, trainTargets,
validation_data=(testImages, testTargets),
batch_size=config.BATCH_SIZE,
epochs=config.NUM_EPOCHS,
verbose=1)
print("[INFO] saving object detector model...")
model.save(config.MODEL_PATH, save_format="h5")
print("[INFO] saving label binarizer...")
f = open(config.LB_PATH, "wb")
f.write(pickle.dumps(lb))
f.close()
lossNames = ["loss", "class_label_loss", "bounding_box_loss"]
N = np.arange(0, config.NUM_EPOCHS)
plt.style.use("ggplot")
(fig, ax) = plt.subplots(3, 1, figsize=(13, 13))
for (i, l) in enumerate(lossNames):
title = f"Loss for {l}" if l != "loss" else "Total loss"
ax[i].set_title(title)
ax[i].set_xlabel("Epoch #")
ax[i].set_ylabel("Loss")
ax[i].plot(N, H.history[l], label=l)
ax[i].plot(N, H.history["val_" + l], label="val_" + l)
ax[i].legend()
plt.tight_layout()
plotPath = os.path.join(config.PLOTS_PATH, "losses.png")
plt.savefig(plotPath)
plt.close()
plt.style.use("ggplot")
plt.figure()
plt.plot(N, H.history["class_label_accuracy"],
label="class_label_train_acc")
plt.plot(N, H.history["val_class_label_accuracy"],
label="val_class_label_acc")
plt.title("Class Label Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Accuracy")
plt.legend(loc="lower left")
plotPath = os.path.join(config.PLOTS_PATH, "accs.png")
plt.savefig(plotPath)