-
Notifications
You must be signed in to change notification settings - Fork 14
/
tf_keras.py
executable file
·198 lines (141 loc) · 7.49 KB
/
tf_keras.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
# %%
# Import Package
import os
import cv2 as cv
import numpy as np
import tensorflow as tf
from matplotlib import pyplot as plt
from tensorflow.keras import layers, models, losses, optimizers, datasets, utils
# %%
# Data Prepare
URL = 'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz'
path_to_zip = utils.get_file('flower_photos.tgz', origin=URL, extract=True)
PATH = os.path.join(os.path.dirname(path_to_zip), 'flower_photos')
category_list = [i for i in os.listdir(PATH) if os.path.isdir(os.path.join(PATH, i)) ]
print(category_list)
num_classes = len(category_list)
img_size = 150
def read_img(path, img_size):
img = cv.imread(path)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
img = cv.resize(img, (img_size, img_size))
return img
imgs_tr = []
labs_tr = []
imgs_val = []
labs_val = []
for i, category in enumerate(category_list):
path = os.path.join(PATH, category)
imgs_list = os.listdir(path)
print("Total '%s' images : %d"%(category, len(imgs_list)))
ratio = int(np.round(0.05 * len(imgs_list)))
print("%s Images for Training : %d"%(category, len(imgs_list[ratio:])))
print("%s Images for Validation : %d"%(category, len(imgs_list[:ratio])))
print("=============================")
imgs = [read_img(os.path.join(path, img),img_size) for img in imgs_list]
labs = [i]*len(imgs_list)
imgs_tr += imgs[ratio:]
labs_tr += labs[ratio:]
imgs_val += imgs[:ratio]
labs_val += labs[:ratio]
imgs_tr = np.array(imgs_tr)/255.
labs_tr = utils.to_categorical(np.array(labs_tr), num_classes)
imgs_val = np.array(imgs_val)/255.
labs_val = utils.to_categorical(np.array(labs_val), num_classes)
print(imgs_tr.shape, labs_tr.shape)
print(imgs_val.shape, labs_val.shape)
# %%
# Build Network
def middle_flow(input, name="middle_flow"):
x = layers.ReLU(name=name+"_Act_1")(input)
x = layers.SeparableConv2D(728, 3, padding='same', name=name+"_Separable_1")(x)
x = layers.BatchNormalization(name=name+"_BN_1")(x)
x = layers.ReLU(name=name+"_Act_2")(x)
x = layers.SeparableConv2D(728, 3, padding='same', name=name+"_Separable_2")(x)
x = layers.BatchNormalization(name=name+"_BN_2")(x)
x = layers.ReLU(name=name+"_Act_3")(x)
x = layers.SeparableConv2D(728, 3, padding='same', name=name+"_Separable_3")(x)
x = layers.BatchNormalization(name=name+"_BN_3")(x)
x = layers.Add(name=name+"_Add")([input, x])
return x
def build_xception(input_shape=(None, None, 3), num_classes=1, name='xception'):
last_act = 'sigmoid' if num_classes==1 else 'softmax'
input = layers.Input(shape=input_shape, name=name+"_input")
x = layers.Conv2D(32, 3, strides=2, name=name+"_Stem_Conv_1")(input)
x = layers.BatchNormalization(name=name+"_Stem_BN_1")(x)
x = layers.ReLU(name=name+"_Stem_Act_1")(x)
x = layers.Conv2D(64, 3, name=name+"_Stem_Conv_2")(x)
x = layers.BatchNormalization(name=name+"_Stem_BN_2")(x)
x = layers.ReLU(name=name+"_Stem_Act_2")(x)
identity = layers.Conv2D(128, 1, strides=2, padding='same', name=name+"_Entry_Identity_Conv_1")(x)
identity = layers.BatchNormalization(name=name+"_Entry_Identity_BN_1")(identity)
x = layers.SeparableConv2D(128, 3, padding='same', name=name+"_Entry_Separable_1")(x)
x = layers.BatchNormalization(name=name+"_Entry_BN_1")(x)
x = layers.ReLU(name=name+"_Entry_Act_1")(x)
x = layers.SeparableConv2D(128, 3, padding='same', name=name+"_Entry_Separable_2")(x)
x = layers.BatchNormalization(name=name+"_Entry_BN_2")(x)
x = layers.MaxPooling2D(3, strides=2, padding='same', name=name+"_Entry_Pool_1")(x)
x = layers.Add(name=name+"_Entry_Add_1")([identity, x])
identity = layers.Conv2D(256, 1, strides=2, padding='same', name=name+"_Entry_Identity_Conv_2")(x)
identity = layers.BatchNormalization(name=name+"_Entry_Identity_BN_2")(identity)
x = layers.ReLU(name=name+"_Entry_Act_2")(x)
x = layers.SeparableConv2D(256, 3, padding='same', name=name+"_Entry_Separable_3")(x)
x = layers.BatchNormalization(name=name+"_Entry_BN_3")(x)
x = layers.ReLU(name=name+"_Entry_Act_3")(x)
x = layers.SeparableConv2D(256, 3, padding='same', name=name+"_Entry_Separable_4")(x)
x = layers.BatchNormalization(name=name+"_Entry_BN_4")(x)
x = layers.MaxPooling2D(3, strides=2, padding='same', name=name+"_Entry_Pool_2")(x)
x = layers.Add(name=name+"_Entry_Add_2")([identity, x])
identity = layers.Conv2D(728, 1, strides=2, padding='same', name=name+"_Entry_Identity_Conv_3")(x)
identity = layers.BatchNormalization(name=name+"_Entry_Identity_BN_3")(identity)
x = layers.ReLU(name=name+"_Entry_Act_4")(x)
x = layers.SeparableConv2D(728, 3, padding='same', name=name+"_Entry_Separable_5")(x)
x = layers.BatchNormalization(name=name+"_Entry_BN_5")(x)
x = layers.ReLU(name=name+"_Entry_Act_5")(x)
x = layers.SeparableConv2D(728, 3, padding='same', name=name+"_Entry_Separable_6")(x)
x = layers.BatchNormalization(name=name+"_Entry_BN_6")(x)
x = layers.MaxPooling2D(3, strides=2, padding='same', name=name+"_Entry_Pool_3")(x)
x = layers.Add(name=name+"_Entry_Add_3")([identity, x])
for i in range(8):
x = middle_flow(x, name=name+"_Middle_%d"%(i+1))
identity = layers.Conv2D(1024, 1, strides=2, padding='same', name=name+"_Exit_Identity_Conv_1")(x)
identity = layers.BatchNormalization(name=name+"_Exit_Identity_BN_1")(identity)
x = layers.ReLU(name=name+"_Exit_Act_1")(x)
x = layers.SeparableConv2D(728, 3, padding='same', name=name+"_Exit_Separable_1")(x)
x = layers.BatchNormalization(name=name+"_Exit_BN_1")(x)
x = layers.ReLU(name=name+"_Exit_Act_2")(x)
x = layers.SeparableConv2D(1024, 3, padding='same', name=name+"_Exit_Separable_2")(x)
x = layers.BatchNormalization(name=name+"_Exit_BN_2")(x)
x = layers.MaxPooling2D(3, strides=2, padding='same', name=name+"_Exit_Pool_1")(x)
x = layers.Add(name=name+"_Exit_Add")([identity, x])
x = layers.SeparableConv2D(1536, 3, padding='same', name=name+"_Exit_Separable_3")(x)
x = layers.BatchNormalization(name=name+"_Exit_BN_3")(x)
x = layers.ReLU(name=name+"_Exit_Act_3")(x)
x = layers.SeparableConv2D(2048, 3, padding='same', name=name+"_Exit_Separable_4")(x)
x = layers.BatchNormalization(name=name+"_Exit_BN_4")(x)
x = layers.ReLU(name=name+"_Exit_Act_4")(x)
x = layers.GlobalAveragePooling2D(name=name+"_GAP")(x)
x = layers.Dense(num_classes, activation=last_act, name=name+"_Output")(x)
return models.Model(input, x)
input_shape = imgs_tr.shape[1:]
xception = build_xception(input_shape=input_shape, num_classes=num_classes, name="Xception")
xception.summary()
loss = 'binary_crossentropy' if num_classes==1 else 'categorical_crossentropy'
xception.compile(optimizer=optimizers.Adam(), loss=loss, metrics=['accuracy'])
# %%
# Training Network
epochs=100
batch_size=16
history=xception.fit(imgs_tr, labs_tr, epochs = epochs, batch_size=batch_size, validation_data=[imgs_val, labs_val])
plt.figure(figsize=(10, 4))
plt.subplot(121)
plt.title("Loss graph")
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.legend(['Train', 'Validation'], loc='upper right')
plt.subplot(122)
plt.title("Acc graph")
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.legend(['Train', 'Validation'], loc='upper right')
plt.show()