Deep learning 扩展keras VGG16模型的异常:self.assert\u输入\u兼容性(x)抛出

Deep learning 扩展keras VGG16模型的异常:self.assert\u输入\u兼容性(x)抛出,deep-learning,keras,Deep Learning,Keras,为了将VGG16网络用于回归任务,我以以下方式对其进行了扩展: keras.applications.vgg16.VGG16(input_tensor=input_tensor, include_top=False) x = model.output x = Flatten()(x) x = Dense(1024, activation='relu')(x) x = Dropout(0.5)(x) x = Dense

为了将VGG16网络用于回归任务,我以以下方式对其进行了扩展:

keras.applications.vgg16.VGG16(input_tensor=input_tensor, include_top=False)
        x = model.output
        x = Flatten()(x)
        x = Dense(1024, activation='relu')(x)
        x = Dropout(0.5)(x)
        x = Dense(512,activation='relu')(x)
        x = Dropout(0.5)(x)
        x = Dense(256,activation='relu')(x)
        x = Dropout(0.5)(x)
        x = Dense(1)(x)
        model = Model(model.input, x)
这会立即引发以下异常:

  File "C:\Users\Ralph\Documents\GitHub\CarND-Behavioral-Cloning-P3\model.py", line 116, in <module>
    tf.app.run()
  File "C:\Users\Ralph\Miniconda3\envs\carnd-term1\lib\site-packages\tensorflow\python\platform\app.py", line 43, in run
    sys.exit(main(sys.argv[:1] + flags_passthrough))
  File "C:\Users\Ralph\Documents\GitHub\CarND-Behavioral-Cloning-P3\model.py", line 101, in main
    model = create_model()
  File "C:\Users\Ralph\Documents\GitHub\CarND-Behavioral-Cloning-P3\model.py", line 60, in create_model
    x = Dense(1024)(x)
  File "C:\Users\Ralph\Miniconda3\envs\carnd-term1\lib\site-packages\keras\engine\topology.py", line 529, in __call__
    self.assert_input_compatibility(x)
  File "C:\Users\Ralph\Miniconda3\envs\carnd-term1\lib\site-packages\keras\engine\topology.py", line 457, in assert_input_compatibility
    if K.ndim(x) < ndim:
  File "C:\Users\Ralph\Miniconda3\envs\carnd-term1\lib\site-packages\keras\backend\tensorflow_backend.py", line 396, in ndim
    dims = x.get_shape()._dims
AttributeError: 'function' object has no attribute 'get_shape'
Press any key to continue . . .
我是否必须更改架构,或者输入维度的推断是否存在问题?

对我来说很有用:

input_tensor = Input((3, 224, 224))
model = keras.applications.vgg16.VGG16(input_tensor=input_tensor, include_top=False)
x = model.output
x = model.output
x = Flatten()(x)
x = Dense(1024, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(512,activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(256,activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(1)(x)
model2 = Model(model.input, x)
import numpy as np
model2.predict(np.zeros((1, 3, 224,224)))

输出:数组[[0.11504173]],dtype=float32

;我相信这是最新版本中的一个bug,当你称之为set_learning_phase时;我提交了


您可以回滚到1.2.0作为临时修复。

输入张量的形状是什么?原始形状是160、320、3,但在预处理期间,张量的大小调整为224、224、3。你认为这是投入吗?例外情况发生在网络的构建过程中,就在我上面发布的代码片段中。应该对我也适用,但它不适用-我的环境一定有问题。。。