-
Notifications
You must be signed in to change notification settings - Fork 2
/
ResNet.py
94 lines (71 loc) · 3.47 KB
/
ResNet.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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
# ResNet34
from keras.optimizers import Adam
from keras.layers import Dense, Activation, Conv2D, MaxPooling2D, Flatten,Dropout
from keras.layers import BatchNormalization,AveragePooling2D,concatenate
from keras.layers import Input, concatenate
from keras.layers import Add
from keras.layers import GlobalAveragePooling2D
from keras.models import Model
#Define convolution with batchnromalization
def Conv2d_BN(x, nb_filter,kernel_size, padding='same',strides=(1,1),name=None):
if name is not None:
bn_name = name + '_bn'
conv_name = name + '_conv'
else:
bn_name = None
conv_name = None
x = Conv2D(nb_filter,kernel_size,padding=padding,strides=strides,activation='relu',name=conv_name)(x)
x = BatchNormalization(axis=3,name=bn_name)(x)
return x
#Define Residual Block for ResNet34(2 convolution layers)
def Residual_Block(input_model,nb_filter,kernel_size,strides=(1,1), with_conv_shortcut =False):
x = Conv2d_BN(input_model,nb_filter=nb_filter,kernel_size=kernel_size,strides=strides,padding='same')
x = Conv2d_BN(x, nb_filter=nb_filter, kernel_size=kernel_size,padding='same')
#need convolution on shortcut for add different channel
if with_conv_shortcut:
shortcut = Conv2d_BN(input_model,nb_filter=nb_filter,strides=strides,kernel_size=kernel_size)
# x = add([x,shortcut])
x = Add()([x, shortcut])
return x
else:
x = Add()([x, input_model])
# x = add([x,input_model])
return x
#Built ResNet34
def ResNet34(width, height, depth, classes):
Img = Input(shape=(width,height,depth))
x = Conv2d_BN(Img,64,(7,7),strides=(2,2),padding='same')
x = MaxPooling2D(pool_size=(3,3),strides=(2,2),padding='same')(x)
#Residual conv2_x ouput 56x56x64
x = Residual_Block(x,nb_filter=64,kernel_size=(3,3))
x = Residual_Block(x,nb_filter=64,kernel_size=(3,3))
x = Residual_Block(x,nb_filter=64,kernel_size=(3,3))
#Residual conv3_x ouput 28x28x128
x = Residual_Block(x,nb_filter=128,kernel_size=(3,3),strides=(2,2),with_conv_shortcut=True)# need do convolution to add different channel
x = Residual_Block(x,nb_filter=128,kernel_size=(3,3))
x = Residual_Block(x,nb_filter=128,kernel_size=(3,3))
x = Residual_Block(x,nb_filter=128,kernel_size=(3,3))
#Residual conv4_x ouput 14x14x256
x = Residual_Block(x,nb_filter=256,kernel_size=(3,3),strides=(2,2),with_conv_shortcut=True)# need do convolution to add different channel
x = Residual_Block(x,nb_filter=256,kernel_size=(3,3))
x = Residual_Block(x,nb_filter=256,kernel_size=(3,3))
x = Residual_Block(x,nb_filter=256,kernel_size=(3,3))
x = Residual_Block(x,nb_filter=256,kernel_size=(3,3))
x = Residual_Block(x,nb_filter=256,kernel_size=(3,3))
#Residual conv5_x ouput 7x7x512
x = Residual_Block(x,nb_filter=512,kernel_size=(3,3),strides=(2,2),with_conv_shortcut=True)
x = Residual_Block(x,nb_filter=512,kernel_size=(3,3))
x = Residual_Block(x,nb_filter=512,kernel_size=(3,3))
#Using AveragePooling replace flatten
x = GlobalAveragePooling2D()(x)
x = Dense( classes ,activation='softmax')(x)
model=Model(input=Img,output=x)
return model
###
ResNet34_model = ResNet34(224,224,3,2)
ResNet34_model.summary()
ResNet34_model.compile(optimizer=Adam(lr=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-08),loss = 'categorical_crossentropy',metrics=['accuracy'])
model = ResNet34_model