import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, Sequential
第一个残差模块
class BasicBlock(layers.Layer):
def init(self, filter_num, stride=1):
super(BasicBlock, self).init()
self.conv1 = layers.Conv2D(filter_num, (3, 3), strides=stride, padding=‘same’)
self.bn1 = layers.BatchNormalization()
self.relu = layers.Activation(‘relu’)
self.conv2 = layers.Conv2D(filter_num, (3, 3), strides=1, padding=‘same’)
self.bn2 = layers.BatchNormalization()
if stride != 1:
self.downsample = Sequential()
self.downsample.add(layers.Conv2D(filter_num, (1, 1), strides=stride))
else:
self.downsample = lambda x: x
def call(self, input, training=None):
out = self.conv1(input)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
identity = self.downsample(input)
output = layers.add([out, identity])
output = tf.nn.relu(output)
return output
class ResNet(keras.Model):
def init(self, layer_dims, num_classes=10):
super(ResNet, self).init()
预处理层
self.padding = keras.layers.ZeroPadding2D((3, 3))
self.stem = Sequential([
layers.Conv2D(64, (7, 7), strides=(2, 2)),
layers.BatchNormalization(),
layers.Activation(‘relu’),
layers.MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding=‘same’)
])
resblock
self.layer1 = self.build_res