Keras展平层-无效参数错误,矩阵无法展平?

时间:2018-07-14 13:09:03

标签: python tensorflow machine-learning keras keras-layer

解释,然后是代码,然后是输出,然后是错误:

看起来扁平层没有完成其工作,并且输出形状取决于批处理大小(当我设置BATCH_SIZE=32时,[1,32768]变成了[1,16384])。我一生无法理解自己在做什么或如何解决。我看过有关Flatten和Dense层的Keras文档。另外,我正在使用Tensorflow后端,这实际上反映在keras.json文件中。

这是我的代码:

BATCH_SIZE = 64
EPOCHS = 1000
EPOCH_STEP = 50

vgg = keras.applications.vgg16.VGG16(include_top=False,weights='imagenet',input_shape=(48,48,3))
vgg_input = vgg.inputs
vgg_output = vgg.outputs


#freeze the vgg layers
for layer in vgg.layers:
    layer.trainable = False

print('~~~~~~~~~~~~~~~~~~~~~~Tensors~~~~~~~~~~~~~~')
print('vgg_output tensor:')
print(vgg_output)
print()
model_tensor = Flatten()(vgg_output)
print('flattened vgg_output tensor:')
print(model_tensor)
print()
model_tensor = Dense(32, activation='relu')(model_tensor)
print('dense FC flattened vgg_output tensor:')
print(model_tensor)
print('~~~~~~~~~~~~~~~~~~~~~~Tensors~~~~~~~~~~~~~~')
model_tensor = Dense(2, activation='softmax')(model_tensor)

model = Model(inputs=vgg_input,outputs=model_tensor)
print('Model architecture made')
#CHOSEN ARBITRARILY FOR NOW
model.compile(optimizer='rmsprop',
            loss='binary_crossentropy',
            metrics=['accuracy'])
print('Model Compiled')             
print(model.summary())

#train top model
val_batch, val_labels = dataGenerator.generateDataBatch(256)
print('validation batch loaded')
batch, labels = dataGenerator.generateDataBatch(2048)
print('training batch loaded')
print('t-batch shape: ' + str(batch.shape))
print('t-batch lable shape: ' + str(labels.shape))
        model.fit(x=batch,y=labels,batch_size=BATCH_SIZE,epochs=EPOCHS,verbose=2,validati    on_data=(val_batch,val_labels),shuffle=True)

打印输出:

Printed Info:
~~~~~~~~~~~~~~~~~~~~~~Tensors~~~~~~~~~~~~~~
vgg_output tensor:
[<tf.Tensor 'block5_pool/MaxPool:0' shape=(?, 1, 1, 512) dtype=float32>]

flattened vgg_output tensor:
Tensor("flatten_1/Reshape:0", shape=(?, ?), dtype=float32)

dense FC flattened vgg_output tensor:
Tensor("dense_1/Relu:0", shape=(?, 32), dtype=float32)
~~~~~~~~~~~~~~~~~~~~~~Tensors~~~~~~~~~~~~~~
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         (None, 48, 48, 3)         0
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 48, 48, 64)        1792
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 48, 48, 64)        36928
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 24, 24, 64)        0
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 24, 24, 128)       73856
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 24, 24, 128)       147584
_________________________________________________________________
block2_pool (MaxPooling2D)   (None, 12, 12, 128)       0
_________________________________________________________________
block3_conv1 (Conv2D)        (None, 12, 12, 256)       295168
_________________________________________________________________
block3_conv2 (Conv2D)        (None, 12, 12, 256)       590080
_________________________________________________________________
block3_conv3 (Conv2D)        (None, 12, 12, 256)       590080
_________________________________________________________________
block3_pool (MaxPooling2D)   (None, 6, 6, 256)         0
_________________________________________________________________
block4_conv1 (Conv2D)        (None, 6, 6, 512)         1180160
_________________________________________________________________
block4_conv2 (Conv2D)        (None, 6, 6, 512)         2359808
_________________________________________________________________
block4_conv3 (Conv2D)        (None, 6, 6, 512)         2359808
_________________________________________________________________
block4_pool (MaxPooling2D)   (None, 3, 3, 512)         0
_________________________________________________________________
block5_conv1 (Conv2D)        (None, 3, 3, 512)         2359808
_________________________________________________________________
block5_conv2 (Conv2D)        (None, 3, 3, 512)         2359808
_________________________________________________________________
block5_conv3 (Conv2D)        (None, 3, 3, 512)         2359808
_________________________________________________________________
block5_pool (MaxPooling2D)   (None, 1, 1, 512)         0
_________________________________________________________________
flatten_1 (Flatten)          (None, 512)               0
_________________________________________________________________
dense_1 (Dense)              (None, 32)                16416
_________________________________________________________________
dense_2 (Dense)              (None, 2)                 66
=================================================================
Total params: 14,731,170
Trainable params: 16,482
Non-trainable params: 14,714,688
_________________________________________________________________
None
Model Compiled
Model architecture made
Model Compiled
validation batch loaded
training batch loaded
t-batch shape: (2048, 48, 48, 3)
t-batch lable shape: (2048, 2)

错误消息的最后一部分(我可以全部添加,但其余部分似乎无济于事):

tensorflow.python.framework.errors_impl.InvalidArgumentError: Matrix size-incompatible: In[0]: [1,32768], In[1]: [512,32]
     [[Node: dense_1/MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](flatten_1/Reshape, dense_1/kernel/read)]]

编辑:在print(model.summary())之后立即添加model.compile()的输出。

1 个答案:

答案 0 :(得分:1)

Keras模型的outputs(和inputs)属性返回模型(张量)输出的列表。您可以在日志中确认这一点:

vgg_output tensor:
[<tf.Tensor 'block5_pool/MaxPool:0' shape=(?, 1, 1, 512) dtype=float32>] <-- this is a list

VGG16有一个输出(即是一个顺序模型),您需要将此一个输出张量(即返回列表的第一个元素)显式传递给下一层,即Flatten:

model_tensor = Flatten()(vgg_output[0])  # pass the first element of output

请注意,如果您想查看训练的进度条,请不要将verbose参数传递给fit方法,也不要将其值传递为1(即{{1 }}。