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

3
我使用以下代码使用VGG16创建CNN模型,但是在创建模型后,模型的输入层从结构中消失了(请参见图像)。 为什么输入层会从结构中消失?
vgg16_model = keras.applications.vgg16.VGG16()
model = Sequential([])
 for layer in vgg16_model.layers[:-1]:
   model.add(layer)
   model.add(Dropout(0.5))
   model.add(Dense(2, activation='softmax', name = 'prediction'))

模型结构

enter image description here

2个回答

7
这只是在使用Sequential API时Keras模型表示的一种假象,它实际上没有任何实际效果:Input层在隐式存在,但它不被认为是一个真正的层,也不会出现在model.summary()中。如果使用Functional API,则会显示出来。 考虑以下两个相同的模型,使用了两种不同的API编写: Sequential 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()中没有明确显示,并不意味着模型的功能有任何问题。编辑:正如Daniel Möller在下面的评论中正确指出的那样,它甚至不是一个真正的层,除了定义输入形状之外什么也不做(请注意其在model_func.summary中的0个训练参数)。 换句话说,不用担心... 这个相关的主题也可能很有用:Keras Sequential model input layer

3
此外,输入层并不是真正的一层。它除了定义输入的形状外什么也不做。一个层需要对数据执行操作,而输入只是数据。 - Daniel Möller
1
@DanielMöller 确实 - 感谢您的有用建议(已添加到答案并标注出处) - desertnaut

1
您需要显式添加一个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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,