-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocks.py
156 lines (139 loc) · 4.79 KB
/
blocks.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
# locations of tf.keras implementations of ResNet blocks.
# from https://github.com/keras-team/keras-applications/blob/master/keras_applications/resnet50.py
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from typing import List, Tuple
def identity_block(
input_tensor: tf.Tensor,
kernel_size: int,
filters: List[int],
stage: int,
block: str,
regularizer=None,
):
"""The identity block is the block that has no conv layer at shortcut.
# Arguments
input_tensor: input tensor
kernel_size: default 3, the kernel size of
middle conv layer at main path
filters: list of integers, the filters of 3 conv layer at main path
stage: integer, current stage label, used for generating layer names
block: 'a','b'..., current block label, used for generating layer names
# Returns
Output tensor for the block.
"""
filters1, filters2, filters3 = filters
conv_name_base = "res" + str(stage) + block + "_branch"
bn_name_base = "bn" + str(stage) + block + "_branch"
x = layers.Conv2D(
filters1,
(1, 1),
kernel_initializer="he_normal",
name=conv_name_base + "2a",
kernel_regularizer=regularizer,
bias_regularizer=regularizer,
activity_regularizer=regularizer,
)(input_tensor)
x = layers.BatchNormalization(name=bn_name_base + "2a")(x)
x = layers.Activation("relu")(x)
x = layers.Conv2D(
filters2,
kernel_size,
padding="same",
kernel_initializer="he_normal",
name=conv_name_base + "2b",
kernel_regularizer=regularizer,
bias_regularizer=regularizer,
activity_regularizer=regularizer,
)(x)
x = layers.BatchNormalization(name=bn_name_base + "2b")(x)
x = layers.Activation("relu")(x)
x = layers.Conv2D(
filters3,
(1, 1),
kernel_initializer="he_normal",
name=conv_name_base + "2c",
kernel_regularizer=regularizer,
bias_regularizer=regularizer,
activity_regularizer=regularizer,
)(x)
x = layers.BatchNormalization(name=bn_name_base + "2c")(x)
x = layers.add([x, input_tensor])
x = layers.Activation("relu")(x)
return x
def conv_block(
input_tensor: tf.Tensor,
kernel_size: int,
filters: List[int],
stage: int,
block: str,
strides: Tuple = (2, 2),
regularizer=None,
):
"""A block that has a conv layer at shortcut.
# Arguments
input_tensor: input tensor
kernel_size: default 3, the kernel size of
middle conv layer at main path
filters: list of integers, the filters of 3 conv layer at main path
stage: integer, current stage label, used for generating layer names
block: 'a','b'..., current block label, used for generating layer names
strides: Strides for the first conv layer in the block.
# Returns
Output tensor for the block.
Note that from stage 3,
the first conv layer at main path is with strides=(2, 2)
And the shortcut should have strides=(2, 2) as well
"""
filters1, filters2, filters3 = filters
conv_name_base = "res" + str(stage) + block + "_branch"
bn_name_base = "bn" + str(stage) + block + "_branch"
x = layers.Conv2D(
filters1,
(1, 1),
strides=strides,
kernel_initializer="he_normal",
name=conv_name_base + "2a",
kernel_regularizer=regularizer,
bias_regularizer=regularizer,
activity_regularizer=regularizer,
)(input_tensor)
x = layers.BatchNormalization(name=bn_name_base + "2a")(x)
x = layers.Activation("relu")(x)
x = layers.Conv2D(
filters2,
kernel_size,
padding="same",
kernel_initializer="he_normal",
name=conv_name_base + "2b",
kernel_regularizer=regularizer,
bias_regularizer=regularizer,
activity_regularizer=regularizer,
)(x)
x = layers.BatchNormalization(name=bn_name_base + "2b")(x)
x = layers.Activation("relu")(x)
x = layers.Conv2D(
filters3,
(1, 1),
kernel_initializer="he_normal",
name=conv_name_base + "2c",
kernel_regularizer=regularizer,
bias_regularizer=regularizer,
activity_regularizer=regularizer,
)(x)
x = layers.BatchNormalization(name=bn_name_base + "2c")(x)
shortcut = layers.Conv2D(
filters3,
(1, 1),
strides=strides,
kernel_initializer="he_normal",
name=conv_name_base + "1",
kernel_regularizer=regularizer,
bias_regularizer=regularizer,
activity_regularizer=regularizer,
)(input_tensor)
shortcut = layers.BatchNormalization(name=bn_name_base + "1")(shortcut)
x = layers.add([x, shortcut])
x = layers.Activation("relu")(x)
return x