keras层的name必须独一无二

哈喽,大家好,我回来了。

在家办公先用Keras写个CNN,然后复制粘贴几层,发现没改每层的命名,发现报错。

ValueError: The name "BN" is used 3 times in the model. All layer names should be unique.

因此都修改后没毛病了。

import keras

inputs=keras.Input(shape=(229,229,3),name='input')
x=keras.layers.Conv2D(32,kernel_size=(3,3),strides=(2,2),padding='valid',name='Conv2D1')(inputs)
x=keras.layers.BatchNormalization(name='BN1')(x)
x=keras.layers.MaxPool2D(pool_size=(2,2),strides=(2,2),padding='valid',name='MaxPool1')(x)

x=keras.layers.Conv2D(64,kernel_size=(3,3),strides=(2,2),padding='valid',name='Conv2D2')(x)
x=keras.layers.BatchNormalization(name='BN2')(x)
x=keras.layers.MaxPool2D(pool_size=(2,2),strides=(2,2),padding='valid',name='MaxPool2')(x)

x=keras.layers.Conv2D(128,kernel_size=(3,3),strides=(2,2),padding='valid',name='Conv2D3')(x)
x=keras.layers.BatchNormalization(name='BN3')(x)
outputs=keras.layers.MaxPool2D(pool_size=(2,2),strides=(2,2),padding='valid',name='MaxPool3')(x)


model=keras.Model(inputs=inputs,outputs=outputs)
model.summary()
Layer (type)                 Output Shape              Param #   
=================================================================
input (InputLayer)           (None, 229, 229, 3)       0         
_________________________________________________________________
Conv2D1 (Conv2D)             (None, 114, 114, 32)      896       
_________________________________________________________________
BN1 (BatchNormalization)     (None, 114, 114, 32)      128       
_________________________________________________________________
MaxPool1 (MaxPooling2D)      (None, 57, 57, 32)        0         
_________________________________________________________________
Conv2D2 (Conv2D)             (None, 28, 28, 64)        18496     
_________________________________________________________________
BN2 (BatchNormalization)     (None, 28, 28, 64)        256       
_________________________________________________________________
MaxPool2 (MaxPooling2D)      (None, 14, 14, 64)        0         
_________________________________________________________________
Conv2D3 (Conv2D)             (None, 6, 6, 128)         73856     
_________________________________________________________________
BN3 (BatchNormalization)     (None, 6, 6, 128)         512       
_________________________________________________________________
MaxPool3 (MaxPooling2D)      (None, 3, 3, 128)         0         
=================================================================
Total params: 94,144
Trainable params: 93,696
Non-trainable params: 448

显而易见,每次卷积后的厚度(channel)都会改变,而BN层和Pool层不改变厚度;并且卷积核(filters)个数恰好是该层的厚度,除非有卷积出现或RNN

此外也可查看模型的输入和输出是什么,包括节点名和shape

>>> model.inputs
[]
>>> model.outputs
[]

 

另外有相关问题可以加入QQ群讨论,不设微信群

QQ群:868373192 

语音图像视频深度-学习群

你可能感兴趣的:(python)