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
|
""" # @File : model_resnet.py # @Time : # @Author :0399 # @version :python 3.9 # @Software : PyCharm # @Description: """
from tensorflow.keras import layers, Model, Sequential
class BasicBlock(layers.Layer): expansion = 1
def __init__(self, out_channel, strides=1, downsample=None, **kwargs): super(BasicBlock, self).__init__() self.conv1 = layers.Conv2D(out_channel, kernel_size=3, strides=strides, padding="SAME", use_bias=False) self.bn1 = layers.BatchNormalization(momentum=0.9, epsilon=1e-5) self.conv2 = layers.Conv2D(out_channel, kernel_size=3, strides=1, padding="SAME", use_bias=False) self.bn2 = layers.BatchNormalization(momentum=0.9, epsilon=1e-5) self.downsample = downsample self.relu = layers.ReLU() self.add = layers.Add()
def call(self, inputs, training=False): identity = inputs if self.downsample is not None: identity = self.downsample(inputs)
x = self.conv1(inputs) x = self.bn1(x, training=training) x = self.relu(x)
x = self.conv2(x) x = self.bn2(x, training=training)
x = self.add([x, identity]) x = self.relu(x) return x
class Bottlenect(layers.Layer): expansion = 4
def __init__(self, out_channel, strides=1, downsample=None, **kwargs): super(Bottlenect, self).__init__() self.conv1 = layers.Conv2D(out_channel, kernel_size=1, use_bias=False, name="conv1") self.bn1 = layers.BatchNormalization(momentum=0.9, epsilon=1e-5, name="conv1/BatchNorm") self.conv2 = layers.Conv2D(out_channel, kernel_size=3, use_bias=False, strides=strides, padding="SAME", name="conv2") self.bn2 = layers.BatchNormalization(momentum=0.9, epsilon=1e-5, name="conv2/BatchNorm") self.conv3 = layers.Conv2D(out_channel * self.expansion, kernel_size=1, use_bias=False, name="conv3") self.bn3 = layers.BatchNormalization(momentum=0.9, epsilon=1e-5, name="conv3/BatchNorm") self.relu = layers.ReLU() self.downsample = downsample self.add = layers.Add()
def call(self, inputs, training=False): identity = inputs if self.downsample is not None: identity = self.downsample(inputs)
x = self.conv1(inputs) x = self.bn1(x, training=training) x = self.relu(x)
x = self.conv2(x) x = self.bn2(x, training=training) x = self.relu(x)
x = self.conv3(x) x = self.bn3(x, training=training) x = self.add([identity, x]) x = self.relu(x) return x
def _make_layer(block, in_channel, channel, block_num, name, strides=1): downsample = None if strides != 1 or in_channel != channel * block.expansion: downsample = Sequential([ layers.Conv2D(channel * block.expansion, kernel_size=1, strides=strides, use_bias=False, name="conv1"), layers.BatchNormalization(momentum=0.9, epsilon=1.001e-5, name="BatchNorm") ], name="shortcut") layers_list = [] layers_list.append(block(channel, downsample=downsample, strides=strides, name="unit_1")) for index in range(1, block_num): layers_list.append(block(channel, name="unit_" + str(index + 1))) return Sequential(layers_list, name=name)
def _resnet(block, block_num, im_width, im_height, num_classes=1000, include_top=True): input_image = layers.Input(shape=(im_height, im_width, 3), dtype="float32") x = layers.Conv2D(filters=64, kernel_size=7, strides=2, padding="SAME", use_bias=False, name="conv1")(input_image) x = layers.BatchNormalization(momentum=0.9, epsilon=1e-5, name="conv1/BatchNorm")(x) x = layers.ReLU()(x) x = layers.MaxPool2D(pool_size=3, strides=2, padding="SAME")(x)
x = _make_layer(block, x.shape[-1], 64, block_num[0], name="block1")(x) x = _make_layer(block, x.shape[-1], 128, block_num[1], strides=2, name="block2")(x) x = _make_layer(block, x.shape[-1], 256, block_num[2], strides=2, name="block3")(x) x = _make_layer(block, x.shape[-1], 512, block_num[3], strides=2, name="block4")(x)
if include_top: x = layers.GlobalAvgPool2D()(x) x = layers.Dense(num_classes, name="logits")(x) predict = layers.Softmax()(x) else: predict = x model = Model(inputs=input_image, outputs=predict) return model
def resnet34(im_width=224, im_height=224, num_classes=1000, include_top=True): return _resnet(BasicBlock, [3, 4, 6, 3], im_height, im_width, num_classes, include_top)
def resnet50(im_width=224, im_height=224, num_classes=1000, include_top=True): return _resnet(Bottlenect, [3, 4, 6, 3], im_height, im_width, num_classes, include_top)
def resnet101(im_width=224, im_height=224, num_classes=1000, include_top=True): return _resnet(Bottlenect, [3, 4, 23, 3], im_height, im_width, num_classes, include_top)
import tensorflow as tf input = tf.random.uniform((8, 224, 224, 3)) model = resnet34() print(model(input))
|