ADCNN: A Deep Convolutional Neural Network for Image Classification
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
nb_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
This code defines a function called ADCNN that creates a DenseNet-based deep convolutional neural network (DCNN) model for image classification tasks.
**Parameters:**
- img_dim: The size of the input image (width, height, channels)
- classNum: The number of classes for classification
- depth: The network depth, i.e., number of layers (default: 40)
- nb_dense_block: Number of dense blocks in DenseNet (default: 3)
- growth_rate: Number of convolution kernels in each dense block (default: 12)
- nb_filter: Number of convolution kernels in the initial convolution layer (default: 16)
- dropout_rate: Dropout rate (default: None)
- weight_decay: Weight decay (default: 1E-4)
- verbose: Whether to print model information (default: True)
**Function Flow:**
- Creates an input layer based on the image size
- Performs an initial convolution on the input
- Adds an attention mechanism module
- Adds several dense blocks
- Includes transition modules between each dense block
- The last dense block is followed by a global average pooling layer and a fully connected layer, outputting the classification results
- Creates the model and returns it
The dense block consists of multiple convolutional and batch normalization layers, which enhance the network's non-linearity and feature extraction capabilities. Transition modules reduce the size and number of feature maps to decrease parameter count and computational cost. The attention mechanism module focuses the model's attention on important features, improving classification performance.
原文地址: https://www.cveoy.top/t/topic/no89 著作权归作者所有。请勿转载和采集!