-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresnet.py
More file actions
71 lines (53 loc) · 1.91 KB
/
resnet.py
File metadata and controls
71 lines (53 loc) · 1.91 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
from keras.layers import (
Activation,
Add,
AveragePooling2D,
BatchNormalization,
Conv2D,
Dense,
Flatten,
Input,
MaxPooling2D,
ZeroPadding2D,
)
from keras.models import Model
def convolutional_block(x, filters, strides=(2, 2)):
x_shortcut = Conv2D(filters, kernel_size=(1, 1), strides=strides, padding="same")(x)
x_shortcut = BatchNormalization(axis=3)(x_shortcut)
x = Conv2D(filters, kernel_size=(3, 3), strides=strides, padding="same")(x)
x = BatchNormalization(axis=3)(x)
x = Activation("relu")(x)
x = Conv2D(filters, kernel_size=(3, 3), padding="same")(x)
x = BatchNormalization(axis=3)(x)
x = Add()([x, x_shortcut])
x = Activation("relu")(x)
return x
def identity_block(x, filters):
x_shortcut = x
x = Conv2D(filters, kernel_size=(3, 3), padding="same")(x)
x = BatchNormalization(axis=3)(x)
x = Activation("relu")(x)
x = Conv2D(filters, kernel_size=(3, 3), padding="same")(x)
x = BatchNormalization(axis=3)(x)
x = Add()([x, x_shortcut])
x = Activation("relu")(x)
return x
def ResNet34(input_shape=(28, 28, 1), classes=345):
x_input = Input(input_shape)
x = ZeroPadding2D((3, 3))(x_input)
x = Conv2D(64, (7, 7), strides=(2, 2), padding="same")(x)
x = BatchNormalization(axis=3)(x)
x = Activation("relu")(x)
x = MaxPooling2D((3, 3), strides=(2, 2), padding="same")(x)
stages = [(64, 3), (128, 4), (256, 6), (512, 3)]
for filters, blocks in stages:
strides = (1, 1) if filters == 64 else (2, 2)
x = convolutional_block(x, filters, strides=strides)
for _ in range(blocks - 1):
x = identity_block(x, filters)
x = AveragePooling2D((2, 2), padding="same")(x)
x = Flatten()(x)
x = Dense(512, activation="relu")(x)
x = Dense(classes, activation="softmax")(x)
model = Model(inputs=x_input, outputs=x, name="ResNet34")
return model