输入层从深度学习模型的结构中消失了

时间:2019-02-02 20:54:49

标签: machine-learning keras neural-network deep-learning

我使用以下代码使用VGG16创建了CNN模型,但是在创建模型之后,模型的输入层从结构中消失了(请参见图片)。

为什么输入层从结构消失?

Model

模型结构

enter image description here

2 个答案:

答案 0 :(得分:3)

使用Sequential API时,这只是Keras模型表示的产物,并且没有任何实际效果:Input层隐式存在,但是它不被认为是合适的层,并且不适用显示在model.summary()中。如果使用了Functional API,它会显示

请考虑使用以下两种不同的API编写的以下两个相同的模型:

顺序API

from keras.models import Sequential
from keras.layers import Dense      # notice that we don't import Input here...

model_seq = Sequential([
    Dense(64, input_shape=(784,),activation='relu'),
    Dense(64, activation='relu'),
    Dense(10, activation='softmax')
])    

model_seq.summary()

# result:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 64)                50240     
_________________________________________________________________
dense_2 (Dense)              (None, 64)                4160      
_________________________________________________________________
dense_3 (Dense)              (None, 10)                650       
=================================================================
Total params: 55,050
Trainable params: 55,050
Non-trainable params: 0
_________________________________________________________________

功能性API

from keras.models import Model
from keras.layers import Input, Dense  # explicitly import Input layer

inputs = Input(shape=(784,))
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)

model_func = Model(inputs=inputs, outputs=predictions)

model_func.summary()

    # result:

Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 784)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 64)                50240     
_________________________________________________________________
dense_2 (Dense)              (None, 64)                4160      
_________________________________________________________________
dense_3 (Dense)              (None, 10)                650       
=================================================================
Total params: 55,050
Trainable params: 55,050
Non-trainable params: 0
_________________________________________________________________

这两个模型相同;使用顺序API时Input层未在model.summary()中明确显示的事实并不意味着有关模型功能的任何内容。编辑:正如丹尼尔·莫勒(DanielMöller)在下面的评论中正确指出的那样,它甚至不是真正的图层,除了定义输入形状外,什么也不做(请注意上面model_func.summary中的0训练参数)。

换句话说,不用担心...

此相关线程也可能有用:Keras Sequential model input layer

答案 1 :(得分:0)

您需要显式添加InputLayer ...

示例代码:

vgg16_model = keras.applications.vgg16.VGG16()
model = Sequential()
inp = InputLayer(input_shape=(224, 224, 3))
model.add(inp)

for layer in vgg16_model.layers[:-1]:
    model.add(layer)

for layer in model.layers:
    layer.trainable = False

model.add(Dense(2, activation='softmax'))
model.summary()

输出:

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 224, 224, 3)       0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 224, 224, 64)      36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 112, 112, 64)      0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 112, 112, 128)     73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 112, 112, 128)     147584    
_________________________________________________________________
block2_pool (MaxPooling2D)   (None, 56, 56, 128)       0         
_________________________________________________________________
block3_conv1 (Conv2D)        (None, 56, 56, 256)       295168    
_________________________________________________________________
block3_conv2 (Conv2D)        (None, 56, 56, 256)       590080    
_________________________________________________________________
block3_conv3 (Conv2D)        (None, 56, 56, 256)       590080    
_________________________________________________________________
block3_pool (MaxPooling2D)   (None, 28, 28, 256)       0         
_________________________________________________________________
block4_conv1 (Conv2D)        (None, 28, 28, 512)       1180160   
_________________________________________________________________
block4_conv2 (Conv2D)        (None, 28, 28, 512)       2359808   
_________________________________________________________________
block4_conv3 (Conv2D)        (None, 28, 28, 512)       2359808   
_________________________________________________________________
block4_pool (MaxPooling2D)   (None, 14, 14, 512)       0         
_________________________________________________________________
block5_conv1 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_conv2 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_conv3 (Conv2D)        (None, 14, 14, 512)       2359808   
_________________________________________________________________
block5_pool (MaxPooling2D)   (None, 7, 7, 512)         0         
_________________________________________________________________
flatten (Flatten)            (None, 25088)             0         
_________________________________________________________________
fc1 (Dense)                  (None, 4096)              102764544 
_________________________________________________________________
fc2 (Dense)                  (None, 4096)              16781312  
_________________________________________________________________
dense_1 (Dense)              (None, 2)                 8194      
=================================================================
Total params: 134,268,738
Trainable params: 8,194
Non-trainable params: 134,260,544