-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_val_doc_class.py
More file actions
453 lines (371 loc) · 14.6 KB
/
train_val_doc_class.py
File metadata and controls
453 lines (371 loc) · 14.6 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# -*- coding: utf-8 -*-
"""
Spyder Editor
Based on the VGG16 example on Keras Blog, some tutorials, adjustments, this
script can function as a document classifier, based on the 'shape'of the
header of an image, It takes the upper third of a page, to train the model
and to evaluate it.
______________
| * * * * * * |
| THIS AREA |
|_*_*_*_*_*_* _|
| |
| |
|______________|
| |
| |
|______________|
It was trained with over 40000 documents (Invoices, Credit Notes, Proformas and
other types of documents)
To train the network from Scratch:
1.- Define a root path, ie, "E:/drm_classes15"
2.- Inside of E:/drm_classes15, create a directory for each class (type) of
document you want the DNN to learn to classify.
For example:
E:/drm_classes15
/Invoices
/CreditNotes
/Contracts
/Formulars
/Others
Try to provide as many as possible documents for each category, let's say,
a minimum of 5000 of each kind. Be careful to not over biase the network,
don't put many more samples of one kind, and much less from others, otherwise
the training will be skewed and your accuracy will be bad.
3.- Evaluate the model, check the Confussion Matrix and Losses
4.- If needed, play around with the network definition, or hyperparameters.
5.- Use the saved model and load it for production use
Time to train:
Using Tensorflow with GPU and ~40000 training samples, takes about 3 hours,
on a CPU Intel(R) Core(TM) i7-5820K CPU @ 3.30GHz (6 cores), 32Gb of RAM
and a GPU NVIDIA 980 (The GPU is the one helpful here).
"""
import datetime
import itertools
import os
from keras import backend as K
from keras.utils import np_utils
from keras.models import Sequential
from keras.models import model_from_json
from keras.models import load_model
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import MaxPooling2D, Conv2D
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
import cv2
K.clear_session()
K.set_image_dim_ordering("tf") #Use TensorFlow
DATA_PATH = "E:/drm_classes15"
#os.chdir("E:\drm_classes")
DATA_DIR_LIST = os.listdir(DATA_PATH)
#defining the dimensions of the resized image in similar proportion as the one third
IMG_COLS = 362
IMG_ROWS = 170
NUM_CHANNEL = 1 #black and white
NUM_EPOCH = 15
IMG_DATA_LIST = []
IMG_TYPES = []
START = 0
OFFSET = 0
CLASS_ID = 0
def show_timestamp(text):
'''
Acts a log on on the console, including timestamp
'''
print(datetime.datetime.now().time().strftime("%H:%M:%S.%f") + " " + str(text))
def get_image(path):
'''
Loads and pre-processes an image, for training and evaluation
'''
input_img = cv2.imread(path)
height, _, _ = input_img.shape
input_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
# Here I'm trying just to get the upper third part of the document
input_img = input_img[0:int(height/3), :]
return cv2.resize(input_img, (IMG_COLS, IMG_ROWS))
def plot_results(hist):
'''
Plots the graphs for loss and accuracy for training and validation
'''
train_loss = hist.history["loss"]
val_loss = hist.history["val_loss"]
train_acc = hist.history["acc"]
val_acc = hist.history["val_acc"]
x_c = range(NUM_EPOCH)
#Visualizing the loss and the accuracy
plt.figure(1, figsize=(7, 5))
plt.plot(x_c, train_loss)
plt.plot(x_c, val_loss)
plt.xlabel("Epochs")
plt.ylabel("loss")
plt.title("train_loss vs val_loss")
plt.grid(True)
plt.legend(["train", "val"])
plt.style.use(["classic"])
plt.figure(2, figsize=(7, 5))
plt.plot(x_c, train_acc)
plt.plot(x_c, val_acc)
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.title("train_acc vs val_acc")
plt.grid(True)
plt.legend(["train", "val"], loc=4)
plt.style.use(["classic"])
def build_model(input_shape):
'''
Builds our variant of VGG16
'''
model = Sequential()
#32 filters of 3x3, using same border model and the input shape of the model
model.add(Conv2D(32, (3, 3), padding="same", input_shape=input_shape))
model.add(Activation("relu"))
model.add(Conv2D(32, (3, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.4))#0.5
model.add(Flatten())
model.add(Dense(128))
model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation("softmax"))
model.summary()
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
return model
def plot_confusion_matrix(cm, classes,
normalize=False,
title="Confusion matrix",
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
"""
plt.imshow(cm, interpolation="nearest", cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype("float") / cm.sum(axis=1)[:, np.newaxis]
show_timestamp("Normalized confusion matrix")
else:
show_timestamp("Confusion matrix, without normalization")
show_timestamp(cm)
thresh = cm.max() / 2.0
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel("True label")
plt.xlabel("Predicted label")
# Visualizing the intermediate layer
def get_featuremaps(model_tmp, layer_idx, X_batch):
'''
Extracts the features of a given layer
'''
get_activations = K.function([model_tmp.layers[0].input,
K.learning_phase()],
[model_tmp.layers[layer_idx].output,])
activations = get_activations([X_batch, 0])
return activations
def reshape_for_backend_framework(img_data_temp):
'''
Process the image according on the backend, if its TF or Theano
as they use different ordering for color channels.
'''
show_timestamp("Adapting the structure to be compatible with: " + K.image_dim_ordering())
if NUM_CHANNEL == 1:
if K.image_dim_ordering == "th":
#if using theano as backend, we then have to add the column at index 1
img_data_temp = np.expand_dims(img_data_temp, axis=1)
#just to confirm the shape is correct
show_timestamp(img_data.shape)
else:
#if using tensorflow as backend, then the channel's dimension is added at the end
img_data_temp = np.expand_dims(img_data_temp, axis=4)
#just to confirm the shape is correct
show_timestamp(img_data_temp.shape)
else:
if K.image_dim_ordering == "th":
#if using TF backend, and loading a RGB image, then just swap
#the dimension from index 3 to index 1
img_data_temp = np.rollaxis(img_data_temp, 3, 1)
#just to confirm the shape is correct
show_timestamp(img_data_temp.shape)
return img_data_temp
def show_feature_map_at_layer(layer_num):
'''
Shows the network's learned features at a given layer
'''
show_timestamp("Feature map for layer : " + str(layer_num))
filter_num = 0
activations = get_featuremaps(model, int(layer_num), TEST_IMAGE)
show_timestamp("Activations shape : " + str(np.shape(activations)))
feature_maps = activations[0][0]
show_timestamp("Feature maps shape : " + str(np.shape(feature_maps)))
if K.image_dim_ordering() == "th":
feature_maps = np.rollaxis((np.rollaxis(feature_maps, 2, 0)), 2, 0)
show_timestamp("Feature maps shape : " + str(feature_maps.shape))
fig = plt.figure(figsize=(16, 16))
plt.imshow(feature_maps[:, :, filter_num], cmap="gray")
plt.savefig("featuremaps-layer-{}".format(layer_num) +
"-filternum-{}".format(filter_num)+".jpg")
num_of_featuremaps = feature_maps.shape[2]
fig = plt.figure(figsize=(16, 16))
plt.title("Featuremaps-Layer-{}".format(layer_num))
subplot_num = int(np.ceil(np.sqrt(num_of_featuremaps)))
for i in range(int(num_of_featuremaps)):
a_x = fig.add_subplot(subplot_num, subplot_num, i+1)
a_x.imshow(feature_maps[:, :, i], cmap="gray")
plt.xticks([])
plt.yticks([])
plt.tight_layout()
plt.show()
fig.savefig("Featuremaps-Layer-{}".format(layer_num) + ".jpg")
img_data = None
IMG_TYPES = []
show_timestamp("Checking if we have precomputed images")
#it might take some gigabytes if the training is too big
if os.path.exists("nparray.npy"):
show_timestamp("Loading precomputed numpy array")
for dataset in DATA_DIR_LIST:
img_list = os.listdir(DATA_PATH + "/" + dataset)
show_timestamp("Loading images from dataset: " + dataset)
for img in img_list:
if img.endswith(".png"):
OFFSET = OFFSET + 1
IMG_TYPES.append((dataset, CLASS_ID, START, OFFSET+ START))
START = START + OFFSET
OFFSET = 0
CLASS_ID = CLASS_ID + 1
show_timestamp("Items loaded " + str(len(img_list)))
img_data = np.load("nparray.npy")
else:
show_timestamp("Loading images from disk")
for dataset in DATA_DIR_LIST:
img_list = os.listdir(DATA_PATH + "/" + dataset)
show_timestamp("Loading images from dataset: " + dataset)
for img in img_list:
if img.endswith(".png"):
input_img_resize = get_image(DATA_PATH + "/" + dataset + "/"+ img)
IMG_DATA_LIST.append(input_img_resize)
OFFSET = OFFSET + 1
IMG_TYPES.append((dataset, CLASS_ID, START, OFFSET + START))
START = START + OFFSET
OFFSET = 0
CLASS_ID = CLASS_ID + 1
show_timestamp("Items loaded :" + str(len(IMG_DATA_LIST)))
show_timestamp("Converting into array")
img_data = np.array(IMG_DATA_LIST)
img_data = img_data.astype("float32")
show_timestamp("Normalizing")
img_data /= 255
np.save("nparray.npy", img_data)
num_classes = len(set(IMG_TYPES))
show_timestamp("Going to classiffy: " + str(num_classes) + " classes")
show_timestamp("Image Types : " + str(IMG_TYPES))
show_timestamp("Image data shape : " + str(img_data.shape))
img_data = reshape_for_backend_framework(img_data)
num_of_samples = img_data.shape[0] #we take the amount of items on the first dimension
labels = np.ones((num_of_samples,), dtype="int64")
names = []
show_timestamp("Generating class ranges")
for cname, cid, cstart, cend in IMG_TYPES:
labels[cstart:cend] = cid
names.append(cname)
show_timestamp("Labels: " + str(labels))
show_timestamp("Names: " + str(names))
show_timestamp("Converting into categorical (into one hot enconding)")
Y = np_utils.to_categorical(labels, num_classes)
show_timestamp("Shuffling the dataset")
x, y = shuffle(img_data, Y, random_state=2)
show_timestamp("Split the dataset into train and test")
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2)
show_timestamp("Input shape: " + str(img_data[0].shape))
show_timestamp("Creating the DNN")
model = build_model(img_data[0].shape)
hist = model.fit(X_train,
y_train,
batch_size=8,
epochs=NUM_EPOCH,
verbose=1,
validation_data=(X_test, y_test))
plot_results(hist)
# Evaluating the model
score = model.evaluate(X_test, y_test, verbose=0)
show_timestamp("Test Loss : " + str(score[0]))
show_timestamp("Test accuracy : " + str(score[1]))
TEST_IMAGE = X_test[0:1]
show_timestamp("Test image shape : " + str(TEST_IMAGE.shape))
show_timestamp("Model.predict test_image : " + str(model.predict(TEST_IMAGE)))
show_timestamp("Model.predict_classes : " + str(model.predict_classes(TEST_IMAGE)))
show_timestamp("y_test : " + str(y_test[0:1]))
# Testing a new image
TEST_IMAGE = get_image("E:/drm_classes/FD_D/00222048.png")
TEST_IMAGE = TEST_IMAGE.astype("float32")
TEST_IMAGE /= 255
show_timestamp("Test image shape : " + str(TEST_IMAGE.shape))
if NUM_CHANNEL == 1:
if K.image_dim_ordering() == "th":
TEST_IMAGE = np.expand_dims(TEST_IMAGE, axis=0)
TEST_IMAGE = np.expand_dims(TEST_IMAGE, axis=0)
show_timestamp(TEST_IMAGE.shape)
else:
TEST_IMAGE = np.expand_dims(TEST_IMAGE, axis=3)
TEST_IMAGE = np.expand_dims(TEST_IMAGE, axis=0)
show_timestamp(TEST_IMAGE.shape)
else:
if K.image_dim_ordering() == "th":
TEST_IMAGE = np.rollaxis(TEST_IMAGE, 2, 0)
TEST_IMAGE = np.expand_dims(TEST_IMAGE, axis=0)
show_timestamp(TEST_IMAGE.shape)
else:
TEST_IMAGE = np.expand_dims(TEST_IMAGE, axis=0)
show_timestamp(TEST_IMAGE.shape)
# Predicting the test image
show_timestamp(model.predict(TEST_IMAGE))
show_timestamp(model.predict_classes(TEST_IMAGE))
#layer_num=3
show_feature_map_at_layer(1)
show_feature_map_at_layer(2)
show_feature_map_at_layer(3)
show_feature_map_at_layer(4)
Y_pred = model.predict(X_test)
show_timestamp("Y_pred : " + str(Y_pred))
y_pred = np.argmax(Y_pred, axis=1)
show_timestamp("y_pred : " + str(y_pred))
target_names = names
show_timestamp("class report: " + classification_report(
np.argmax(y_test, axis=1),
y_pred,
target_names=target_names))
show_timestamp("conf matrix : " + str(confusion_matrix(np.argmax(y_test, axis=1), y_pred)))
# Compute confusion matrix
cnf_matrix = confusion_matrix(np.argmax(y_test, axis=1), y_pred)
np.set_printoptions(precision=2)
plt.figure()
# Plot non-normalized confusion matrix
plot_confusion_matrix(cnf_matrix, classes=target_names, title="Confusion matrix")
plt.show()
# serialize model to JSON
model_json = model.to_json()
with open("model_dm.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model_dm.h5")
show_timestamp("Saved model to disk")
# load json and create model
json_file = open("model_dm.json", "r")
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model_dm.h5")
show_timestamp("Loaded model from disk")
model.save("model_dm.hdf5")
loaded_model = load_model("model_dm.hdf5")
K.clear_session()