-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelBuild.py
More file actions
277 lines (209 loc) · 8.51 KB
/
modelBuild.py
File metadata and controls
277 lines (209 loc) · 8.51 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
from tensorflow.keras.applications.resnet50 import ResNet50
from keras.models import Model
from keras.layers import Input, Activation
input_shape = (224, 224, 3)
resnet = ResNet50(
include_top = True, # fully_connet_layer를 top_of_the_network에 사용여부
weights = None, # 사전에 학습된 가중치를 사용할지 말지, None : random initialize, imagenet : pre_trained (on imagenet) 가중치를 사용함 If using `weights='imagenet'` with `include_top=True`, `classes` should be 1000. (capable of recognizing 1,000 different object categories, we encounter in our day-to-day lives )
input_tensor = None,
input_shape = (224,224,3),
pooling = None, # include top이 false일 경우에만 pooling을 avg로 할 지 max로 할지 결정
classes = 8, #최종 분류할 label의 개수
classifier_activation = "softmax" # fully connected layer에서 사용할 activation 함수
)
model = Model(inputs=resnet.input, outputs=resnet.output)
model.summary()
from tensorflow.keras.applications.resnet50 import ResNet50
from keras.models import Model
from keras.layers import Dense,Input, Activation
### 위와는 다른 모델
### 최종 pooling 과정에서 max pooling을 사용,
### 초기 가중치로 imagenet data를 학습한 resnet 50의 가중치를 가져옴.
input_shape = (224, 224, 3)
resnet = ResNet50(
include_top = None,
weights = "imagenet",
input_tensor = None,
input_shape = (224,224,3),
pooling = 'max',
classes = None,
classifier_activation = None
)
x= resnet.output
x = Dense(8, activation='softmax')(x)
model = Model(inputs=resnet.input, outputs=x)
model.summary()
from keras.layers import Conv2D as Conv
from keras import layers
from keras.models import Model
### resnet의 conv layer중 conv 1,2 layer까지만 대략 구현해본 모델
### skip_connection은 구현되어 있지 않음
def conv1 (x):
x = Conv(filters = 64, kernel_size = (7,7), strides = 2, padding = 'same')(x)
x = layers.BatchNormalization()(x)
x = layers.Activation('relu')(x)
x = layers.MaxPool2D(pool_size = (3,3), strides = 2, padding = 'same')(x)
return x
def conv2 (x):
for i in range(3):
x = Conv(filters = 64, kernel_size = (1,1), strides = 1, padding = 'same')(x)
x = layers.BatchNormalization()(x)
x = layers.Activation('relu')(x)
x = Conv(filters = 64, kernel_size = (3,3), strides = 1, padding = 'same')(x)
x = layers.BatchNormalization()(x)
x = layers.Activation('relu')(x)
x = layers.Conv2D(filters = 256, kernel_size = (1,1), strides = 1, padding = 'same')(x)
x = layers.BatchNormalization()(x)
x = layers.Activation('relu')(x)
return x
def conv (x):
x = conv1(x)
x = conv2(x)
return x
inputs_layer = layers.Input(shape=(224,224,3))
conv_layer = conv(inputs_layer)
pooling_layer = layers.GlobalAveragePooling2D()(conv_layer)
outputs_layer = layers.Dense(8, activation='softmax')(pooling_layer)
model = Model(inputs=inputs_layer, outputs=outputs_layer, name = 'naive_resnet')
model.summary()
### Custom ResnNet
### Skip connection , Batch Normalization 모두 포함.
### 우선 직접 구현한 모델이 기존 import한 resnet과 유사한 정확도를 보이는지 확인해보기 위함.
from keras.layers import Conv2D as Conv
from keras.layers import BatchNormalization as BN
from keras.layers import Activation
from keras import layers
from keras.models import Model
def conv1 (x):
x = Conv(filters = 64, kernel_size = (7,7), strides = 2, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
return x
def conv2 (x):
x = layers.MaxPool2D(pool_size = (3,3), strides = 2, padding = 'same')(x)
shortcut = x
for i in range(3):
x = Conv(filters = 64, kernel_size = (1,1), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = Conv(filters = 64, kernel_size = (3,3), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = layers.Conv2D(filters = 256, kernel_size = (1,1), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
# i=0 즉 첫번째 iteration인 경우에만 shortcut(이전 output)에 대하여 차원을 맞춰줄 필요가 있음.
if i==0:
shortcut = layers.Conv2D(filters = 256, kernel_size = (1,1), strides = 1, padding = 'same')(shortcut)
shortcut = BN()(shortcut)
shortcut = Activation('relu')(shortcut)
x = layers.Add()([x,shortcut])
shortcut = x
return x
def conv3(x):
shortcut = x
for i in range(4):
if i==0:
x = Conv(filters = 128, kernel_size = (1,1), strides = 2, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = Conv(filters = 128, kernel_size = (3,3), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = layers.Conv2D(filters = 512, kernel_size = (1,1), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
shortcut = layers.Conv2D(filters = 512, kernel_size = (1,1), strides = 2, padding = 'same')(shortcut)
shortcut = BN()(shortcut)
shortcut = Activation('relu')(shortcut)
#x = layers.Add()([x,shortcut])
#shortcut = x
else :
x = Conv(filters = 128, kernel_size = (1,1), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = Conv(filters = 128, kernel_size = (3,3), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = layers.Conv2D(filters = 512, kernel_size = (1,1), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = layers.Add()([x,shortcut])
shortcut = x
return x
def conv4(x):
shortcut = x
for i in range(6):
if i==0:
x = Conv(filters = 256, kernel_size = (1,1), strides = 2, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = Conv(filters = 256, kernel_size = (3,3), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = layers.Conv2D(filters = 1024, kernel_size = (1,1), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
shortcut = layers.Conv2D(filters = 1024, kernel_size = (1,1), strides = 2, padding = 'same')(shortcut)
shortcut = BN()(shortcut)
shortcut = Activation('relu')(shortcut)
else :
x = Conv(filters = 256, kernel_size = (1,1), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = Conv(filters = 256, kernel_size = (3,3), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = layers.Conv2D(filters = 1024, kernel_size = (1,1), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = layers.Add()([x,shortcut])
shortcut = x
return x
def conv5(x):
shortcut = x
for i in range(3):
if i==0 :
x = Conv(filters = 512, kernel_size = (1,1), strides = 2, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = Conv(filters = 512, kernel_size = (3,3), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = Conv(filters = 2048, kernel_size = (1,1), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
shortcut = layers.Conv2D(filters = 2048, kernel_size = (1,1), strides = 2, padding = 'same')(shortcut)
shortcut = BN()(shortcut)
shortcut = Activation('relu')(shortcut)
else :
x = Conv(filters = 512, kernel_size = (1,1), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = Conv(filters = 512, kernel_size = (3,3), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = layers.Conv2D(filters = 2048, kernel_size = (1,1), strides = 1, padding = 'same')(x)
x = BN()(x)
x = Activation('relu')(x)
x = layers.Add()([x,shortcut])
shortcut = x
return x
def conv_stage (x):
x = conv1(x)
x = conv2(x)
x = conv3(x)
x = conv4(x)
x = conv5(x)
return x
inputs_layer = layers.Input(shape=(224,224,3))
conv_layer = conv_stage(inputs_layer)
pooling_layer = layers.GlobalAveragePooling2D()(conv_layer)
outputs_layer = layers.Dense(8, activation='softmax')(pooling_layer)
model = Model(inputs=inputs_layer, outputs=outputs_layer, name = 'naive_resnet')
model.summary()
model.compile(
loss='categorical_crossentropy', #target이 one hot vector 인 경우가 아닌 경우에는 sparse
optimizer='adam',
metrics=['accuracy']
)