-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniDeepFont.py
More file actions
107 lines (82 loc) · 4.47 KB
/
MiniDeepFont.py
File metadata and controls
107 lines (82 loc) · 4.47 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
from tensorflow.keras import Sequential
from tensorflow.keras.models import load_model
from tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization, Conv2DTranspose, UpSampling2D, Flatten, Dense, Dropout
from tensorflow.keras import optimizers
from tensorflow.keras import utils
from tensorflow.keras import metrics
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ReduceLROnPlateau
from CustomAugmentations import CustomAugmentations
from CustomTrainingCallbacks import EvaluteTrainSet
import numpy as np
class DeepFont:
def __init__(self, input_shape, opt_name='adam', loss='mean_squared_error', use_augmentations=False):
self.model = Sequential()
self.model.add(Conv2D(32, kernel_size=(5, 5), activation='relu', input_shape=input_shape, name='conv_1'))
self.model.add(BatchNormalization(name='norm_1'))
self.model.add(MaxPooling2D(pool_size=(2, 2), name='pooling_1'))
self.model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', name='conv_2'))
self.model.add(BatchNormalization(name='norm_2'))
self.model.add(MaxPooling2D(pool_size=(2, 2), name='pooling_2'))
self.model.add(Conv2DTranspose(64, (3, 3), strides=(2, 2), activation='relu', padding='same',
kernel_initializer='uniform', name='convT_3'))
self.model.add(UpSampling2D(size=(2, 2)))
self.model.add(Conv2D(128, kernel_size=(3, 3), activation='relu', name='conv_4'))
self.model.add(Conv2D(128, kernel_size=(3, 3), activation='relu', name='conv_5'))
self.model.add(Conv2D(128, kernel_size=(3, 3), activation='relu', name='conv_6'))
self.model.add(Flatten())
self.model.add(Dense(1024, activation='relu', name='fc_1'))
self.model.add(Dropout(0.5))
self.model.add(Dense(512, activation='relu', name='fc_2'))
self.model.add(Dense(3, activation='softmax', name='softmax_classifier'))
lr = 0.04
if opt_name == 'adam':
opt = optimizers.Adam(lr=lr, epsilon=1.0)
elif opt_name == 'sgd':
opt = optimizers.SGD(lr=lr, decay=1e-8, momentum=0.9, nesterov=True)
self.model.compile(loss=loss, optimizer=opt, metrics=['accuracy'])
self.datagen = CustomAugmentations() if use_augmentations else ImageDataGenerator()
def summarize(self):
self.model.summary()
def train(self, images, labels, epochs, batch_size, validate_x = None, validate_y = None):
# Preparing training set
x = images
y = utils.to_categorical(labels)
self.datagen.fit(x)
# Preparing validation set (if given)
validation_data = None
if validate_x is not None and validate_y is not None:
validation_data=(validate_x, utils.to_categorical(validate_y))
# Creating a callack which calculates the real training set
# metrics at the end of each epoch
train_set_evaluation_callback = EvaluteTrainSet(x, y)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.25,
patience=1, min_lr=0.001, mode='min')
# Training
history = self.model.fit(self.datagen.flow(x, y, batch_size=batch_size),
steps_per_epoch=len(x) / batch_size,
epochs=epochs,
callbacks=[train_set_evaluation_callback, reduce_lr],
validation_data=validation_data)
# Merging traing history with our custom evaluation callback's history
# (which is real training set accuracy & loss)
history = dict(list(history.history.items()) +
list(train_set_evaluation_callback.get_history().items()))
return { 'history': history,
'evaluation': self.model.evaluate(x, y, verbose=0) }
def evaluate(self, test_images, test_labels):
x = test_images
y = utils.to_categorical(test_labels)
return self.model.evaluate(x, y, verbose=0)
def predict(self, x):
return self.model.predict(x)
def save(self, filename):
self.model.save(filename + '.h5')
def load(self, filename):
self.model = load_model(filename)
def save_plot(self, filename):
utils.plot_model(self.model,
to_file=filename,
show_shapes=True,
show_layer_names=True,
expand_nested=False)