这是一个使用 Keras 框架实现的 ADCNN 模型,用于图像分类任务。

函数的参数:

  • img_dim:输入图像的维度
  • classNum:分类的类别数
  • depth:网络层数,必须是 3N+4 的形式,其中 N 为正整数
  • nb_dense_block:Dense Block 的数量
  • growth_rate:每个 Dense Block 中的卷积层输出通道数
  • nb_filter:网络中第一个卷积层的卷积核数量
  • dropout_rate:Dropout 层的 dropout 率,为 None 则不使用
  • weight_decay:L2 正则化的权重
  • verbose:是否打印模型创建信息

该函数首先对输入图像进行初始化卷积,然后添加了一个 Attention Block。接着通过循环添加了多个 Dense Block 和 Transition Block。最后通过全局平均池化和全连接层,输出分类结果。

注意事项:

  • 在使用该函数时,需要先导入 Keras 和 numpy 库
  • 该函数中的 attention_blockdense_blocktransition_block 为自定义函数,需要在代码中定义
def ADCNN(img_dim, classNum, depth=40, nb_dense_block=3, growth_rate=12, nb_filter=16, dropout_rate=None,
          weight_decay=1E-4, verbose=True):
   
n_channels = 3
model_input = Input(shape=img_dim)
# model_input = input_size

concat_axis = 1 if K.image_data_format() == "th" else -1
   
assert (depth - 4) % 3 == 0, "Depth must be 3 N + 4"

# layers in each dense block
b_layers = int((depth - 4) / 3)

# Initial convolution
x = Convolution2D(nb_filter, (3, 3), kernel_initializer="he_uniform", padding="same", name="initial_conv2D",
                      use_bias=False, kernel_regularizer=l2(weight_decay))(model_input)

x = BatchNormalization(axis=concat_axis, gamma_regularizer=l2(weight_decay),
                            beta_regularizer=l2(weight_decay))(x)

# Add attention block
x = attention_block(x, encoder_depth=1)
   
# Add dense blocks
for block_idx in range(nb_dense_block - 1):
    x, nb_filter = dense_block(x, nb_layers, nb_filter, growth_rate, dropout_rate=dropout_rate,
                               weight_decay=weight_decay)
    # add transition_block
    x = transition_block(x, nb_filter, dropout_rate=dropout_rate, weight_decay=weight_decay)

# The last dense_block does not have a transition_block
x, nb_filter = dense_block(x, nb_layers, nb_filter, growth_rate, dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

x = Activation('relu')(x)
x = GlobalAveragePooling2D()(x)
x = Dense(classNum, activation='softmax', kernel_regularizer=l2(weight_decay),
              bias_regularizer=l2(weight_decay))(x)

adcnn = Model(inputs=model_input, outputs=x)
   
if verbose:
    print("ADCNN-%d-%d created." % (depth, growth_rate))

return adcnn
ADCNN 模型:基于密集连接网络和注意力机制的图像分类模型

原文地址: https://www.cveoy.top/t/topic/no82 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录