基本介绍
ResNet通过建立前面层与后面层之间的“短路连接”来训练出更深的CNN模型和更高的准确度。而DenseNet与ResNet类似,但它建立的是前面所有层与后面层的密集连接。相比ResNet,DenseNet提出了更激进的密集连接方式,相互连接所有的层,每个层都会接受其前面所有层作为其额外的输入。
DenseNet代码实现
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from typing import List
import matplotlib.pyplot as plt
from tensorflow.keras.applications import imagenet_utils
# 实现DenseNet 根据blocks不同而产生不同的DenseNet(DenseNet161、DenseNet121)
def DenseNet(blocks: List, shape: tuple) -> keras.Model:
img_input = layers.Input(shape=shape)
x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input) # padding-top, bottom, left, right
x = layers.Conv2D(64, 7, strides=2, use_bias=False)(x)
x = layers.BatchNormalization(axis=3, epsilon=EPSILON)(x)
x = layers.Activation('relu')(x)
x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)))(x)
x = layers.MaxPooling2D(3, strides=2)(x)
x = dense_block(x, blocks[0])
x = transition_block(x, 0.5)
x = dense_block(x, blocks