☁️主页 Nowl
专栏 《深度学习》
君子坐而论道,少年起而行之
ResNet是一种用于图像识别的深度残差网络,是卷积神经网络的一种重要模型,ResNet开创性地引入了残差连接,解决了深层网络在训练过程中梯度弥散的问题,使深层模型的训练更加简便,同时也验证了随着网络层次的加深模型能够获得更好的性能
ResNet有许多模型:如ResNet34, ResNet50,不过这些基本上都是根据层数来命名的,ResNet网络模型一般是开始有一层卷积层来提取图像特征,再经过池化,然后进入残差块中(ResNet的主要结构),最后再经过池化层与全连接层被输出出来,下图是一个ResNet34的模型示意图
下面是resnet的关键结构——残差块,它由两个卷积层和一个直连通路组成右侧曲线被称为直连通路,直连通路有助于解决梯度消失的问题,因为此时当神经网络反向传播求权重时,因为这个多项式即使前一部分的梯度消失了,后一部分还能保证梯度的存在
以下是一个简化的 ResNet 模型中,有以下主要组件:
import tensorflow as tf
from tensorflow.keras import layers, Model
def residual_block(x, filters, kernel_size=3, stride=1, conv_shortcut=False):
shortcut = x
if conv_shortcut:
shortcut = layers.Conv2D(filters, kernel_size=1, strides=stride, padding='same')(shortcut)
shortcut = layers.BatchNormalization()(shortcut)
x = layers.Conv2D(filters, kernel_size, strides=stride, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.Activation('relu')(x)
x = layers.Conv2D(filters, kernel_size, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.add([x, shortcut])
x = layers.Activation('relu')(x)
return x
def resnet(input_shape, num_classes=10):
inputs = tf.keras.Input(shape=input_shape)
x = layers.Conv2D(64, 7, strides=2, padding='same')(inputs)
x = layers.BatchNormalization()(x)
x = layers.Activation('relu')(x)
x = layers.MaxPooling2D(3, strides=2, padding='same')(x)
x = residual_block(x, 64)
x = residual_block(x, 64)
x = residual_block(x, 128, stride=2)
x = residual_block(x, 128)
x = residual_block(x, 256, stride=2)
x = residual_block(x, 256)
x = residual_block(x, 512, stride=2)
x = residual_block(x, 512)
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dense(num_classes, activation='softmax')(x)
model = Model(inputs, x)
return model
# 创建ResNet模型
model = resnet(input_shape=(224, 224, 3), num_classes=1000)
# 打印模型概要
model.summary()