-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResNet-18
63 lines (43 loc) · 2.34 KB
/
ResNet-18
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
from keras.layers import *
def identity_block(input_tensor, kernel_size, filters, stage, block):
conv_name_base = 'res' + str(stage) + block + '_branch'
x = Conv2D(filters, (kernel_size, kernel_size), padding='same', name=conv_name_base + '2a')(input_tensor)
x = BatchNormalization()(x)
x = Activation('relu', name=conv_name_base + '2a_relu')(x)
x = Conv2D(filters, (kernel_size, kernel_size), padding='same',name=conv_name_base + '2b')(x)
x = BatchNormalization()(x)
x = Add(name='res' + str(stage) + block)([x, input_tensor])
x = Activation('relu', name=conv_name_base + '2b_relu')(x)
return x
def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)):
conv_name_base = 'res' + str(stage) + block + '_branch'
x = Conv2D(filters, (kernel_size, kernel_size), padding='same', strides=strides,
name=conv_name_base + '2a')(input_tensor)
x = BatchNormalization()(x)
x = Activation('relu', name=conv_name_base + '2a_relu')(x)
x = Conv2D(filters, (kernel_size, kernel_size), padding='same',
name=conv_name_base + '2b')(x)
x = BatchNormalization()(x)
shortcut = Conv2D(filters, (1, 1), padding='same', strides=strides,
name=conv_name_base + '1')(input_tensor)
x = Add(name='res' + str(stage) + block)([x, shortcut])
x = Activation('relu', name=conv_name_base + '2b_relu')(x)
return x
def resnet18(img_input):
base_filter = 64
if img_input == None:
img_input = Input(shape=(256, 256, 3))
x = Conv2D(base_filter, (7, 7), strides=(2, 2), name='conv1', padding='same')(img_input)
x = BatchNormalization()(x)
x = Activation('relu', name='conv1_relu')(x)
x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1', padding='same')(x)
x = conv_block(x, 3, base_filter, stage=2, block='a', strides=(1, 1))
x = identity_block(x, 3, base_filter, stage=2, block='b')
x = conv_block(x, 3, base_filter * 2, stage=3, block='a')
x = identity_block(x, 3, base_filter * 2, stage=3, block='b')
x = conv_block(x, 3, base_filter * 4, stage=4, block='a')
x = identity_block(x, 3, base_filter * 4, stage=4, block='b')
x = conv_block(x, 3, base_filter * 8, stage=5, block='a')
x = identity_block(x, 3, base_filter * 8, stage=5, block='b')
output = x
return output