How to input last layer of pretrained network as tensor to flatten in Keras

时间:2019-04-27 11:04:15

标签: python tensorflow keras multipleoutputs

I'm trying to make a model with two outputs, one for classification and other for regression. I'm using pre-trained VGG16 from Keras as the convolutional feature extractor. But when I try to build the next flatten layer in both the output sequence, I get error that the input is not a tensor. How do I make sure I'm feeding only the last layer of the convolutional base as input to the next layers?

conv_base = VGG16(include_top = False,
                  weights = 'imagenet')
conv_base.trainable = False
with tf.variable_scope('classification_head', reuse=tf.AUTO_REUSE):
    x = Flatten()(conv_base.layers[-1])
    x = Dense()(x)

Error :

ValueError: Layer flatten_1 was called with an input that isn't a symbolic tensor. Received type: class 'keras.layers.pooling.MaxPooling2D'. Full input: [keras.layers.pooling.MaxPooling2D object at 0xc2855c9b0]. All inputs to the layer should be tensors.

1 个答案:

答案 0 :(得分:0)

将所需的张量设为:

<*>
  

Tensor(“ block5_pool / MaxPool:0”,shape =(?, 11,14,512),dtype = float32)