Amazing-Python-Scripts
288 строк · 8.0 Кб
1# -*- coding: utf-8 -*-
2
3import os
4from tensorflow.keras.regularizers import l2
5from tensorflow.keras.optimizers import Adam
6from tensorflow.keras.layers import Dense
7from tensorflow.keras.layers import Dropout
8from tensorflow.keras.layers import Flatten
9from tensorflow.keras.layers import Activation
10from tensorflow.keras.layers import MaxPooling2D
11from tensorflow.keras.layers import Conv2D
12from tensorflow.keras.layers import BatchNormalization
13from tensorflow.keras.models import Sequential
14from tensorflow import keras
15import tensorflow as tf
16from imgaug import augmenters as iaa
17import matplotlib.pyplot as plt
18from skimage.color import rgb2gray
19import numpy as np
20import cv2 as cv
21import csv
22import random
23PATH = '/content/Dataset'
24TESTING_PATH = '/content/Dataset/Test'
25
26# Minimal number of samples for each class for data augmentation
27MIN_IMGS_IN_CLASS = 500
28
29# Learning parameters
30EPOCHS = 100
31INIT_LR = 0.001
32BATCH_SIZE = 256
33SET_DECAY = True
34
35# Imports
36
37# images and corresponding labels
38images = []
39labels = []
40
41# loop over all 43 classes
42gtFile = open(PATH + '/Train.csv')
43gtReader = csv.reader(gtFile, delimiter=',') # csv parser for annotations file
44next(gtReader)
45
46# loop over all images in current annotations file
47for row in gtReader:
48img = cv.imread(PATH + '/' + row[7])
49images.append(cv.resize(img, (28, 28)))
50labels.append(row[6]) # the 6th column is the label
51gtFile.close()
52
53print('Number of loaded images: ' + str(len(images)))
54print('Number of loaded labels: ' + str(len(labels)))
55
56train_X = np.asarray(images)
57train_X = train_X / 255
58train_X = np.asarray(train_X, dtype="float32")
59train_Y = np.asarray(labels, dtype="float32")
60
61print('Shape of training array: ' + str(train_X.shape))
62
63
64def count_images_in_classes(lbls):
65"""
66Determining number of images in a class for Graph
67:param lbls: Labels (different classes)
68:return: Dictionary of labels and count
69"""
70dct = {}
71for i in lbls:
72if i in dct:
73dct[i] += 1
74else:
75dct[i] = 1
76return dct
77
78
79samples_distribution = count_images_in_classes(train_Y)
80print(samples_distribution)
81
82
83def distribution_diagram(dct):
84"""
85Histogram of Count and Label
86:param dct: Dictionary of labels and count
87:return: Histogram
88"""
89plt.bar(range(len(dct)), list(dct.values()), align='center')
90plt.xticks(range(len(dct)), list(dct.keys()), rotation=90, fontsize=7)
91plt.show()
92
93
94distribution_diagram(samples_distribution)
95
96
97def preview(images, labels):
98"""
99Preview of a Images rom a label
100:param images: Image from a Label
101:param labels: Label
102"""
103plt.figure(figsize=(16, 16))
104for c in range(len(np.unique(labels))):
105i = random.choice(np.where(labels == c)[0])
106plt.subplot(10, 10, c + 1)
107plt.axis('off')
108plt.title('class: {}'.format(c))
109plt.imshow(images[i])
110
111
112preview(train_X, train_Y)
113
114
115def augment_imgs(imgs, p, imgaug=None):
116"""
117Performs a set of augmentations with with a probability p
118:param imgs: Performs Data Augmentation
119:param p: Probability for augmentation
120"""
121from imgaug import augmenters as iaa
122augs = iaa.SomeOf(
123(2, 4),
124[
125# crop images from each side by 0 to 4px (randomly chosen)
126iaa.Crop(px=(0, 4)),
127iaa.Affine(scale={
128"x": (0.8, 1.2),
129"y": (0.8, 1.2)
130}),
131iaa.Affine(translate_percent={
132"x": (-0.2, 0.2),
133"y": (-0.2, 0.2)
134}),
135# rotate by -45 to +45 degrees)
136iaa.Affine(rotate=(-45, 45)),
137# shear by -10 to +10 degrees
138iaa.Affine(shear=(-10, 10))
139])
140
141seq = iaa.Sequential([iaa.Sometimes(p, augs)])
142res = seq.augment_images(imgs)
143return res
144
145
146def augmentation(imgs, lbls):
147"""
148Data Augmentation for a Label
149:param imgs: Images
150:param lbls: Labels
151"""
152classes = count_images_in_classes(lbls)
153for i in range(len(classes)):
154if classes[i] < MIN_IMGS_IN_CLASS:
155# Number of samples to be added
156add_num = MIN_IMGS_IN_CLASS - classes[i]
157imgs_for_augm = []
158lbls_for_augm = []
159for j in range(add_num):
160im_index = random.choice(np.where(lbls == i)[0])
161imgs_for_augm.append(imgs[im_index])
162lbls_for_augm.append(lbls[im_index])
163augmented_class = augment_imgs(imgs_for_augm, 1)
164augmented_class_np = np.array(augmented_class)
165augmented_lbls_np = np.array(lbls_for_augm)
166imgs = np.concatenate((imgs, augmented_class_np), axis=0)
167lbls = np.concatenate((lbls, augmented_lbls_np), axis=0)
168return imgs, lbls
169
170
171train_X, train_Y = augmentation(train_X, train_Y)
172
173print(train_X.shape)
174print(train_Y.shape)
175
176augmented_samples_distribution = count_images_in_classes(train_Y)
177print(augmented_samples_distribution)
178
179distribution_diagram(augmented_samples_distribution)
180
181preview(train_X, train_Y)
182
183train_X = rgb2gray(train_X)
184
185
186def build(width, height, depth, classes):
187"""
188Initialize the model and its dimension
189"""
190model = keras.Sequential()
191inputShape = (height, width, depth)
192chanDim = -1
193
194# CONV => RELU => BN => POOL
195model.add(Conv2D(8, (5, 5), padding="same", input_shape=inputShape))
196model.add(Activation("relu"))
197model.add(BatchNormalization(axis=chanDim))
198model.add(MaxPooling2D(pool_size=(2, 2)))
199
200# first set of (CONV => RELU => CONV => RELU) * 2 => POOL
201model.add(Conv2D(16, (3, 3), padding="same"))
202model.add(Activation("relu"))
203model.add(BatchNormalization(axis=chanDim))
204model.add(Conv2D(16, (3, 3), padding="same"))
205model.add(Activation("relu"))
206model.add(BatchNormalization(axis=chanDim))
207model.add(MaxPooling2D(pool_size=(2, 2)))
208
209# second set of (CONV => RELU => CONV => RELU) * 2 => POOL
210model.add(Conv2D(32, (3, 3), padding="same"))
211model.add(Activation("relu"))
212model.add(BatchNormalization(axis=chanDim))
213model.add(Conv2D(32, (3, 3), padding="same"))
214model.add(Activation("relu"))
215model.add(BatchNormalization(axis=chanDim))
216model.add(MaxPooling2D(pool_size=(2, 2)))
217
218# first set of FC => RELU layers
219model.add(Flatten())
220model.add(Dense(128))
221model.add(Activation("relu"))
222model.add(BatchNormalization())
223model.add(Dropout(0.5))
224
225# second set of FC => RELU layers
226model.add(Flatten())
227model.add(Dense(128))
228model.add(Activation("relu"))
229model.add(BatchNormalization())
230model.add(Dropout(0.5))
231
232# softmax classifier
233model.add(Dense(classes))
234model.add(Activation("softmax"))
235
236# return the constructed network architecture
237return model
238
239
240model = build(28, 28, 1, 43)
241
242if SET_DECAY:
243opt = Adam(lr=INIT_LR, decay=INIT_LR / (EPOCHS * 0.5))
244else:
245opt = Adam(lr=INIT_LR)
246model.compile(loss="sparse_categorical_crossentropy",
247optimizer=opt,
248metrics=["accuracy"])
249
250test_images = []
251test_labels = []
252test = '/content/Dataset'
253# loop over all 43 classes
254gtFile = open('/content/Dataset/Test.csv') # annotations file
255gtReader = csv.reader(gtFile, delimiter=',') # csv parser for annotations file
256next(gtReader)
257
258# loop over all images in current annotations file
259for row in gtReader:
260img = cv.imread(PATH + '/' + row[7])
261test_images.append(cv.resize(img, (28, 28)))
262test_labels.append(row[6])
263gtFile.close()
264
265test_X = np.asarray(test_images)
266test_X = test_X / 255
267test_X = np.asarray(test_X, dtype="float32")
268
269test_X = rgb2gray(test_X)
270
271test_Y = np.asarray(test_labels, dtype="float32")
272
273print(train_X.shape)
274train_X_ext = np.expand_dims(train_X, axis=3)
275print(train_X_ext.shape)
276print(train_Y.shape)
277train_Y_ext = np.expand_dims(train_Y, axis=1)
278print(train_Y_ext.shape)
279
280with tf.device('/device:GPU:0'):
281os.mkdir("models_Xception")
282for i in range(EPOCHS):
283model.save("models_Xception/model_" + str(i) + ".h5")
284H = model.fit(train_X_ext, train_Y, epochs=1, batch_size=BATCH_SIZE)
285
286print(model.summary())
287
288model.save_weights('model_final.h5')
289