-
Notifications
You must be signed in to change notification settings - Fork 19
/
train.py
225 lines (182 loc) · 7.3 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Importing Libraries
import numpy as np
import cv2
import matplotlib.pyplot as plt
import random
import os
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import classification_report, confusion_matrix
from tensorflow.keras.layers import Input, Conv2D, Activation, MaxPool2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam, SGD
from tensorflow.keras.regularizers import l2
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import LearningRateScheduler, ModelCheckpoint
from tensorflow.keras.utils import to_categorical
from tensorflow.keras import regularizers
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
import seaborn as sns
sns.set(rc={'figure.figsize':(11.7,8.27)})
palette = sns.color_palette("bright", 28)
# Importing dataset
def extractImages(datadir):
# Get the data
imagesData = []
imagesLabel = []
for folder in os.listdir(datadir):
path = os.path.join(datadir, folder)
for images in os.listdir(path):
img = cv2.imread(os.path.join(path, images), cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (32, 32))
imagesData.append(img)
imagesLabel.append(folder)
# Shuffle data
combined = list(zip(imagesData, imagesLabel))
random.shuffle(combined)
imagesData, imagesLabel = zip(*combined)
return (imagesData, imagesLabel)
# Import train data
imagesData = []
imagesLabel = []
train_data_dir = 'data/extracted_images'
imagesData, imagesLabel = extractImages(train_data_dir)
print("number of image: ",len(imagesData))
print("shape of image: ",imagesData[1].shape)
print("labels: ",list(set(imagesLabel)))
imagesTrainData, imagesTestData, imagesTrainLabel, imagesTestLabel = train_test_split(
imagesData,imagesLabel,
shuffle=True,
test_size=0.2,
random_state=42,
stratify= imagesLabel )
# Data exploration
def showImage (images,label,part):
figure = plt.figure(figsize=((len(part)/10 + 1)*10, (len(part)/10 + 1)*2))
j = 0
for i in part:
lbl = label[i]
img = images[i]
img = cv2.resize(img, (256, 256))
figure.add_subplot(int(len(part)/10)+1, 10, j+1)
plt.imshow(img,cmap='gray')
plt.axis('off')
plt.title(lbl)
j += 1
showImage(imagesTrainData,imagesTrainLabel, range(20))
unique_idx = [imagesTrainLabel.index(i) for i in list(set(imagesTrainLabel))]
showImage(imagesTrainData,imagesTrainLabel, unique_idx)
sns.countplot(x= list(imagesTrainLabel))
# Preprocessing
label_encoder = LabelEncoder()
Y_train = label_encoder.fit_transform(imagesTrainLabel)
Y_test = label_encoder.transform(imagesTestLabel)
Y_train = to_categorical(Y_train)
Y_test = to_categorical(Y_test)
X_train = np.array(imagesTrainData)
X_test = np.array(imagesTestData)
Y_train = np.array(Y_train)
Y_test = np.array(Y_test)
X_train = np.expand_dims(X_train, axis=-1)
X_test = np.expand_dims(X_test, axis=-1)
X_train = X_train/255.
X_test = X_test/255.
print(X_train.shape)
print(X_test.shape)
print(Y_train.shape)
print(Y_test.shape)
X_train[0].shape
# CNN Model
def detect_text(input_shape=(32, 32, 1)):
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), padding='same', activation='relu', input_shape=input_shape))
model.add(BatchNormalization())
model.add(MaxPooling2D(2, 2))
model.add(Dropout(0.25))
model.add(
Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same', kernel_regularizer=regularizers.l2(0.01)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(82, activation='softmax'))
model.compile(optimizer=Adam(learning_rate=0.0001, ),
loss='categorical_crossentropy',
metrics=['accuracy'])
return model
model = detect_text()
model.summary()
def step_decay(epoch):
initial_learning_rate = 0.001
dropEvery = 10
factor = 0.5
lr = initial_learning_rate*(factor**np.floor((1 + epoch)/dropEvery))
return float(lr)
checkpoint = ModelCheckpoint('HandwrittenMathEquationModel.keras',
monitor='val_accuracy',
save_best_only=True,
verbose=1,
mode='min')
earlyStopping = EarlyStopping(monitor='val_accuracy',
mode='auto',
verbose=1,
patience=10,
restore_best_weights=True)
reduceLr = ReduceLROnPlateau(monitor='val_accuracy',
factor=0.2,
patience=6,
verbose=1,
min_delta=0.0001)
callbacks = [checkpoint,earlyStopping,reduceLr, LearningRateScheduler(step_decay)]
# Image Augmentation
aug = ImageDataGenerator(zoom_range=0.1,
rotation_range=5,
width_shift_range=0.05,
height_shift_range=0.05)
hist = model.fit(aug.flow(X_train, Y_train, batch_size=64),
batch_size=64,
epochs=50,
validation_data=(X_test, Y_test),
callbacks=callbacks)
model.save('/Trained Model/HandwrittenMathEquationModel.keras')
plt.figure(figsize=(14,5))
plt.subplot(1,2,2)
plt.plot(hist.history['accuracy'])
plt.plot(hist.history['val_accuracy'])
plt.title('Model Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend(['train', 'test'], loc='upper left')
plt.subplot(1,2,1)
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.title('model Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# Model Evaluation
train_loss, train_accu = model.evaluate(X_train, Y_train)
test_loss, test_accu = model.evaluate(X_test, Y_test)
print("final train accuracy = {:.2f} , validation accuracy = {:.2f}".format(train_accu*100, test_accu*100))
def confusion_mat(X, y):
y_pred = model.predict(X)
y_pred = np.argmax(y_pred, axis=1)
y_target = np.argmax(y, axis=1)
target_names = label_encoder.classes_
print('Classification Report')
target_names = label_encoder.classes_
print(classification_report(y_target, y_pred, target_names=target_names))
print('Confusion Matrix')
cm_train = confusion_matrix(y_target, y_pred)
print(cm_train)
plt.figure(figsize=(5, 5))
plt.imshow(cm_train, interpolation='nearest')
plt.colorbar()
tick_mark = np.arange(len(target_names))
_ = plt.xticks(tick_mark, target_names, rotation=90)
_ = plt.yticks(tick_mark, target_names)
confusion_mat(X_train,Y_train)
confusion_mat(X_test,Y_test)