class BasicBlock(layers.Layer): expansion = 1 def init(self, in_channels, out_channels, stride=1): super(BasicBlock, self).init() # 1. BasicBlock模块中的共有2个卷积;BasicBlock模块中的第1个卷积层; self.conv1 = regularized_padded_conv(out_channels, kernel_size=3, strides=stride) self.bn1 = layers.BatchNormalization() # 2. 第2个;第1个卷积如果做stride就会有一个下采样,在这个里面就不做下采样了。这一块始终保持size一致,把stride固定为1 self.conv2 = regularized_padded_conv(out_channels, kernel_size=3, strides=1) self.bn2 = layers.BatchNormalization() # 3. 判断stride是否等于1,如果为1就是没有降采样。 if stride != 1 or in_channels != self.expansion * out_channels: self.shortcut = Sequential([regularized_padded_conv(self.expansion * out_channels, kernel_size=1, strides=stride), layers.BatchNormalization()]) else: self.shortcut = lambda x, _: x self.max= layers.GlobalMaxPooling2D(kernel_size=2, stride=2) def call(self, inputs, training=False): out = self.conv1(inputs) out = self.bn1(out, training=training) out = tf.nn.relu(out) out = self.conv2(out) out = self.bn2(out, training=training) out = out + self.shortcut(inputs, training) out = self.pool(out) out = tf.nn.relu(out) return out 改为def定义形式
时间: 2024-04-28 11:20:00 浏览: 112
def BasicBlock(inputs, in_channels, out_channels, stride=1):
expansion = 1
# 1. BasicBlock模块中的共有2个卷积;BasicBlock模块中的第1个卷积层;
conv1 = regularized_padded_conv(out_channels, kernel_size=3, strides=stride)(inputs)
bn1 = layers.BatchNormalization()(conv1)
# 2. 第2个;第1个卷积如果做stride就会有一个下采样,在这个里面就不做下采样了。这一块始终保持size一致,把stride固定为1
conv2 = regularized_padded_conv(out_channels, kernel_size=3, strides=1)(bn1)
bn2 = layers.BatchNormalization()(conv2)
# 3. 判断stride是否等于1,如果为1就是没有降采样。
if stride != 1 or in_channels != expansion * out_channels:
shortcut = Sequential([regularized_padded_conv(expansion * out_channels, kernel_size=1, strides=stride),
layers.BatchNormalization()])(inputs)
else:
shortcut = inputs
pool = layers.GlobalMaxPooling2D(kernel_size=2, strides=2)(bn2)
out = tf.nn.relu(shortcut + pool)
return out
阅读全文