简体   繁体   中英

find number of convolutional and dense layers

I copy code from Kaggle and I can not count the numbers of layers in it. I am working on an image classification model. can anyone explain this. I try most solutions and I can not count the convolutional and dense layers.

model = Sequential()
inputShape = (height, width, depth)
chanDim = -1
if K.image_data_format() == "channels_first":
    inputShape = (depth, height, width)
    chanDim = 1
    

model.add(Conv2D(32, (3, 3), padding="same",input_shape=inputShape))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))

model.add(Conv2D(64, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(128, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))

model.add(Conv2D(128, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())

model.add(Dense(1024))
model.add(Activation("relu"))
model.add(BatchNormalization())
model.add(Dropout(0.5))

model.add(Dense(15))
model.add(Activation("softmax"))

model.summary()

can any one explain.

Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_5 (Conv2D)            (None, 256, 256, 32)      896       
_________________________________________________________________
activation_6 (Activation)    (None, 256, 256, 32)      0         
_________________________________________________________________
batch_normalization_6 (Batch (None, 256, 256, 32)      128       
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 85, 85, 32)        0         
_________________________________________________________________
dropout_4 (Dropout)          (None, 85, 85, 32)        0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 85, 85, 64)        18496     
_________________________________________________________________
activation_7 (Activation)    (None, 85, 85, 64)        0         
_________________________________________________________________
batch_normalization_7 (Batch (None, 85, 85, 64)        256       
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 85, 85, 64)        36928     
_________________________________________________________________
activation_8 (Activation)    (None, 85, 85, 64)        0         
_________________________________________________________________
batch_normalization_8 (Batch (None, 85, 85, 64)        256       
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 42, 42, 64)        0         
_________________________________________________________________
dropout_5 (Dropout)          (None, 42, 42, 64)        0         
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 42, 42, 128)       73856     
_________________________________________________________________
activation_9 (Activation)    (None, 42, 42, 128)       0         
_________________________________________________________________
batch_normalization_9 (Batch (None, 42, 42, 128)       512       
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 42, 42, 128)       147584    
_________________________________________________________________
activation_10 (Activation)   (None, 42, 42, 128)       0         
_________________________________________________________________
batch_normalization_10 (Batc (None, 42, 42, 128)       512       
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 21, 21, 128)       0         
_________________________________________________________________
dropout_6 (Dropout)          (None, 21, 21, 128)       0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 56448)             0         
_________________________________________________________________
dense_1 (Dense)              (None, 1024)              57803776  
_________________________________________________________________
activation_11 (Activation)   (None, 1024)              0         
_________________________________________________________________
batch_normalization_11 (Batc (None, 1024)              4096      
_________________________________________________________________
dropout_7 (Dropout)          (None, 1024)              0         
_________________________________________________________________
dense_2 (Dense)              (None, 15)                15375     
_________________________________________________________________
activation_12 (Activation)   (None, 15)                0         
=================================================================
Total params: 58,102,671
Trainable params: 58,099,791
Non-trainable params: 2,880
_________________________________________________________________
 

I can not count the numbers of convolutional and dense layers. I try model.layers as well. the output of this is 28. how?

How can I get the number of convolutional & dense layers programmatically?

First, the reason why the number of layers is 28 is because Flatten , BatchNormalization , Dropout , Activation and MaxPool2D are all counted in model.layers .

That being said, you can get the count of the layers using isinstance :

num_conv = 0
num_dense = 0
for layer in model.layers:
    if isinstance(layer, Conv2D):
        num_conv += 1
    elif isinstance(layer, Dense):
        num_dense += 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM