-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
286 lines (220 loc) · 11 KB
/
Copy pathmain.py
File metadata and controls
286 lines (220 loc) · 11 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
import Import_Data
import Data_Description
import Data_Augmentation
import Model_Evaluation
import random
import math
import json
import tensorflow as tf
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Activation, Dense, Dropout, Flatten, BatchNormalization
from tensorflow.keras.layers import RandomFlip, RandomRotation, RandomZoom
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
# from tensorflow.keras.regularizers import l1, l2
import warnings
warnings.filterwarnings('ignore')
# -----------------------------------------------------------
print('\n****** Import Data ******')
# Data_dir = 'dataset'
Data_dir = 'CNN_Model_HandWriting_Digits/dataset'
random.seed(42)
batch_size = input('Please enter batch size: *** it must be a positive Integer ***\n')
batch_size = Import_Data.evaluate_digit(batch_size)
train_indeces, test_indeces = Import_Data.number_images_class(Data_dir,
batch_size= batch_size)
print("--------------------------------------------------------------\n")
n_sample_in_each_class = input(
'Please enter the number of samples in each classes that you'
' want to train the model: *** it must be a positive Integer ***\n'
)
n_sample_in_each_class = Import_Data.evaluate_digit(n_sample_in_each_class)
n_total_imgs, differ_samples = Import_Data.n_samples_evaluation(n_sample_in_each_class,
train_indeces,
batch_size= batch_size)
train_paths, test_paths = Import_Data.load_Directory(Data_dir= Data_dir,
train_indeces= train_indeces,
test_indeces= test_indeces)
label_to_index = {label: idx for idx, label in enumerate(train_paths['label'].unique())}
train_dataset, test_dataset = Import_Data.create_tf_dataset(train_paths,
test_paths,
label_to_index,
n_total_imgs= n_total_imgs,
differ_samples= differ_samples,
show_sample= True)
# -----------------------------------------------------------
print('****** Data Description ******')
index_to_label = {value: key for key, value in label_to_index.items()}
print(f"\nlabel to index is:\n {label_to_index}")
print('\nTable of Train Dataset:')
Data_Description.label_count(train_dataset, index_to_label)
print('\n--------------------------------------------------------------\n')
print('Table of Test Dataset:')
Data_Description.label_count(test_dataset, index_to_label)
Data_Description.plot_dataset_sample(train_dataset,
index_to_label,
'Train',
n_classes= 10,
n_plot= 5,
img_size= (28, 28))
print('\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n')
# -----------------------------------------------------------
# If you want to have data augmentation seperatly, run this section
# Data Augmentation
#print('****** Data Augmentation ******')
# more_samples= int(input('How many samples do you want to produce?\n '))
# n_augmented_samples = len(train_dataset) + more_samples
# train_dataset = Data_Augmentation.data_augmentation(train_dataset,
# n_labels= 10,
# n_augmented_samples= n_augmented_samples,
# is_batched= 1,
# batch_size= batch_size
# )
# Data_Description.label_count(train_dataset, index_to_label)
# Data_Description.plot_dataset_sample(train_dataset,
# index_to_label,
# 'Train_Augmentation',
# n_classes= 10,
# is_batched= True,
# n_samples_each_label= 320320,
# n_plot= 5,
# img_size= (28, 28))
# print('\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n')
# -----------------------------------------------------------
print('****** Create Validation Dataset ******\n')
total_length = Data_Description.size_dataset(train_dataset)
validation_ratio = 0.2
validation_size = round(validation_ratio * total_length)
train = train_dataset.skip(validation_size)
validation = train_dataset.take(validation_size)
train_size = Data_Description.size_dataset(train)
validation_siz = Data_Description.size_dataset(validation)
steps_per_epoch = train_size
validation_steps = validation_siz
print(f'\nShape of Train set is:\n {train_size* batch_size}')
print(f'\nShape of validation set is:\n {validation_siz* batch_size}')
print(f'\nnumber of batches for train is:\n {train_size}')
print(f'\nnumber of batches for validation is:\n {validation_siz}')
print('\n==============================================================\n')
print('\nShape of Training samples:')
Data_Description.describe_dataset_sample(train)
print('----------------------------------------\n')
print('Shape of validation samples:')
Data_Description.describe_dataset_sample(validation)
print('----------------------------------------\n')
print('Shape of Test samples:')
Data_Description.describe_dataset_sample(test_dataset)
print('\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n')
# -----------------------------------------------------------
# print('\n****** Convert to categorical Values ******')
# If you want to convert labels to categorical values, run bellow.
# Consider that you must use Categorical Cross Entropy function as loss function when using categorical values in model.
# This function is defined as Loss_CCE in Model Architect section.
# anyway, in this surway numbers are used as labels so that Sparse Categorical Cross Entropy function is
# used as loss function by Loss_SCCE name in Model Architect section.
# def one_hot_labels(image, label):
# label = tf.cast(label, tf.int32)
# one_hot_label = to_categorical(label, num_classes=10)
# return image, one_hot_label
# train_dataset = train.map(one_hot_labels, num_parallel_calls = tf.data.AUTOTUNE
# ).prefetch(buffer_size=tf.data.AUTOTUNE)
# validation_dataset = validation.map(one_hot_labels, num_parallel_calls = tf.data.AUTOTUNE
# ).prefetch(buffer_size=tf.data.AUTOTUNE)
# test_dataset = test_dataset.map(one_hot_labels, num_parallel_calls = tf.data.AUTOTUNE
# ).prefetch(buffer_size=tf.data.AUTOTUNE)
# print("Train Dataset Sample:\n")
# describe_final_Dataset(train_dataset)
# print('\n-----------------------------------------\n')
# print("Validation Dataset Sample:\n")
# describe_final_Dataset(validation_dataset)
# print('\n-----------------------------------------\n')
# print("Test Dataset Sample:\n")
# describe_final_Dataset(test_dataset)
# -----------------------------------------------------------
print('\n****** CNN Model ******')
#Data Augmentation for modeling
data_augmentation = Sequential(
[
RandomFlip('horizontal'),
RandomRotation(0.1),
RandomZoom(height_factor=0.2, width_factor=0.2)
]
)
# Simple Model Definition
optimizer = tf.keras.optimizers.Adam(learning_rate=0.005)
loss_SCCE = tf.keras.losses.SparseCategoricalCrossentropy()
loss_CCE = tf.keras.losses.CategoricalCrossentropy()
input = tf.keras.Input(shape = (28, 28, 4))
x = (data_augmentation)(input)
x1 = (Conv2D(64, kernel_size=(3, 3), padding = 'same'))(x)
x1 = (BatchNormalization())(x1)
x1 = (Activation('relu'))(x1)
x1 = (Conv2D(64, kernel_size=(3, 3), padding = 'same'))(x1)
x1 = (BatchNormalization())(x1)
x1 = (Activation('relu'))(x1)
x1 = (MaxPooling2D(pool_size=(2, 2), padding = 'same'))(x1)
x1 = (Dropout(0.25))(x1)
x2 = (Conv2D(128, kernel_size=(3, 3)))(x1)
x2 = (BatchNormalization())(x2)
x2 = (Activation('relu'))(x2)
x2 = (Conv2D(128, kernel_size=(3, 3)))(x2)
x2 = (BatchNormalization())(x2)
x2 = (Activation('relu'))(x2)
x2 = (MaxPooling2D(pool_size=(2, 2), strides = (2, 2), padding = 'same'))(x2)
x2 = (Dropout(0.25))(x2)
x3 = (Conv2D(256, kernel_size=(3, 3)))(x2)
x3 = (BatchNormalization())(x3)
x3 = (Activation('relu'))(x3)
x3 = (Conv2D(256, kernel_size=(3, 3)))(x3)
x3 = (BatchNormalization())(x3)
x3 = (Activation('relu'))(x3)
x3 = (MaxPooling2D(pool_size=(2, 2), strides = (2, 2), padding = 'same'))(x3)
x3 = (Dropout(0.25))(x3)
x4 = (Flatten())(x3)
x4 = (Dense(512, activation='relu'))(x4)
x4 = (BatchNormalization())(x4)
x4 = (Dropout(0.5))(x4)
output = (Dense(10, activation='softmax'))(x4)
model_simple = tf.keras.Model(inputs = input, outputs = output)
model_simple.compile(loss= loss_SCCE, optimizer= optimizer, metrics=['accuracy'])
# If you want to see model Summary run code below
print(model_simple.summary())
checkpoint_filepath_simple = 'CNN_Model_HandWriting_Digits/checkpoints1/simple/model_simple.weights.h5'
checkpoint_callback = ModelCheckpoint(
filepath=checkpoint_filepath_simple,
monitor='val_loss',
mode='min',
save_weights_only=True,
save_best_only=True
)
early_stopping = EarlyStopping(monitor='val_loss',
patience=10,
mode='min',
verbose = 1,
start_from_epoch=5
)
epoch= 200
history_simple = model_simple.fit(
train,
verbose = 1,
epochs=epoch,
validation_data= validation,
batch_size = 512,
callbacks=[checkpoint_callback, early_stopping]
)
print('\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n')
# -----------------------------------------------------------
print('\n****** Model Evaluation ******')
Model_Evaluation.plotting_report(history_simple)
config_simple = model_simple.get_config()
with open('./CNN_Model_HandWriting_Digits/checkpoints1/simple/model_config_simple.json', 'w') as f:
json.dump(config_simple, f, indent = 4)
with open('./CNN_Model_HandWriting_Digits/checkpoints1/simple/model_config_simple.json', 'r') as f:
configoration = json.load(f)
model_conf_simple = tf.keras.Model.from_config(configoration)
model_conf_simple.compile(loss= loss_SCCE, optimizer= optimizer, metrics=['accuracy'])
model_conf_simple.load_weights(f'CNN_Model_HandWriting_Digits/checkpoints1/simple/model_simple.weights.h5')
Model_Evaluation.model_Evaluation(train, model_conf_simple, label_to_index)
Model_Evaluation.model_Evaluation(test_dataset, model_conf_simple, label_to_index, Mode= 'Test')
# tf.keras.utils.plot_model(model_conf_simple, show_shapes=True)
print('End of the code!\nGood luck :)')