-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain.py
More file actions
165 lines (133 loc) · 5.1 KB
/
train.py
File metadata and controls
165 lines (133 loc) · 5.1 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
# let matplotlib plot the figures in the background
import matplotlib
matplotlib.use('Agg')
# import packages
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.layers import Dropout, Flatten, Dense, Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import SGD
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import pickle
import cv2
import os
PATH_TO_DIR = os.getcwd()
# parse CLI args
ap = argparse.ArgumentParser()
ap.add_argument('-d', '--dataset', required=True, help='path to input dataset')
ap.add_argument('-m', '--model', required=True, help='path to output serialized model')
ap.add_argument('-l', '--label-bin', required=True, help='path to output label binarizer')
ap.add_argument('-e', '--epochs', type=int, default=25, help='number of epochs')
ap.add_argument('-p', '--plot', type=str, default='plot.png', help='path to loss plot')
args = vars(ap.parse_args())
# lets initialize the labels and load the data after parsing our CLI
LABELS = set(['negative', 'neutral', 'positive'])
print('loading images...')
# imagePath = [paths.list_images(args['dataset'])]
imagePath = os.listdir(PATH_TO_DIR + '/' + args['dataset'])
imagePath.remove('.DS_Store')
data, labels = [], []
i = 0
# loop over the image paths
for img_path in imagePath:
# extract the class label from the file name
if img_path.find('positive') != -1:
label = 'positive'
elif img_path.find('negative') != -1:
label = 'negative'
else:
label = 'neutral'
# label = img_path.split(os.path.sep)[-2]
# load the image
image = cv2.imread(PATH_TO_DIR + '/' + args['dataset'] + '/' + img_path)
# convert from BGR to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# print(img_path, image.shape, label, i)
# fix image size to 224x224
image = cv2.resize(image, (224, 224))
data.append(image)
labels.append(label)
i += 1
# convert the data and labels into numpy arrays
data, labels = np.array(data), np.array(labels)
# one-hot encode the labels
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
# partition data into train/test
(trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.25, stratify=labels)
# initialize the training data augmentation object
trainAug = ImageDataGenerator(
rotation_range=30,
zoom_range=0.15,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.15,
horizontal_flip=True,
fill_mode='nearest'
)
# validation/testing data augmentation object
valAug = ImageDataGenerator()
# TODO - Look into these values
mean = np.array([123.68, 116.779, 103.939], dtype='float32')
trainAug.mean = mean
valAug.mean = mean
# load the ResNet50
baseModel = ResNet50(weights='imagenet',
include_top=False,
input_tensor=Input(shape=(224, 224, 3)))
# head of model that will go on top of baseModel
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
headModel = Flatten(name='flatten')(headModel)
headModel = Dense(512, activation='relu')(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(len(lb.classes_), activation='softmax')(headModel)
model = Model(inputs=baseModel.input, outputs=headModel)
# freeze weights on the base model so they do not get updated
for layer in baseModel.layers:
layer.trainable = False
# start compiling + training process
print('compiling model...')
# TODO - Look into these values
opt = SGD(lr=1e-4, momentum=0.9, decay=(1e-4 / args['epochs']))
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
print('training model head...')
H = model.fit(
x=trainAug.flow(trainX, trainY, batch_size=32),
steps_per_epoch=len(trainX)//32,
validation_data=valAug.flow(testX, testY),
validation_steps=len(testX)//32,
epochs=args['epochs']
)
# evaluation process
print('evaluating model...')
predictions = model.predict(x=testX.astype('float32'), batch_size=32)
print(classification_report(testY.argmax(axis=1),
predictions.argmax(axis=1),
target_names=lb.classes_))
# plot training loss/accuracy
N = args['epochs']
plt.style.use('ggplot')
plt.figure()
plt.plot(np.arange(0, N), H.history['loss'], label='training loss')
plt.plot(np.arange(0, N), H.history['val_loss'], label='validation loss')
plt.plot(np.arange(0, N), H.history['accuracy'], label='accuracy')
plt.plot(np.arange(0, N), H.history['val_accuracy'], label='validation accuracy')
plt.title('Loss/Accuracy on Dataset')
plt.xlabel('Nbr of Epochs')
plt.ylabel('Loss/Accuracy')
plt.legend(loc='lower left')
plt.savefig(args['plot'])
# wrap up - serialize model & label binarizer
print('serializing model to disc...')
model.save(args['model'], save_format='h5')
f = open(args['label_bin'], 'wb')
f.write(pickle.dumps(lb))
f.close()