python - Keras InceptionV3 类型错误 : unhashable type: 'Dimension'

标签 python machine-learning keras deep-learning computer-vision

我正在尝试实现一个模型,它将灰度图像作为输入并返回一个数值作为输出。我使用 InceptionV3(从头开始训练)作为特征提取器,然后使用一些密集层进行最后阶段的回归。

这是我的代码:

from keras.applications.inception_v3 import InceptionV3
from keras.layers import Input, GlobalAveragePooling2D, Dense, Dropout, Flatten, BatchNormalization
from keras.models import Model
from keras.metrics import mean_absolute_error
from keras.utils import plot_model

inputs = Input(shape=(256, 256, 1))
x = BatchNormalization()(inputs)
x = InceptionV3(include_top = False, weights = None, input_shape=inputs.shape[1:])(x)
x = BatchNormalization()(x)
x = GlobalAveragePooling2D()(x)
x = Dense(1000, activation = 'relu' )(x)
x = Dense(1000, activation = 'relu' )(x)
outputs = Dense(1, activation = 'linear' )(x)
model = Model(inputs=inputs, outputs=outputs)

model.compile(optimizer = 'adam', loss = 'mse', metrics = [mae])

model.summary()

现在我在运行代码时收到此错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-36-50041eb640cc> in <module>()
      7 inputs = Input(shape=(256, 256, 1))
      8 x = BatchNormalization()(inputs)
----> 9 x = InceptionV3(include_top = False, weights = None, input_shape=inputs.shape[1:])(x)
     10 x = BatchNormalization()(x)
     11 x = GlobalAveragePooling2D()(x)

3 frames
/usr/local/lib/python3.6/dist-packages/keras_applications/imagenet_utils.py in _obtain_input_shape(input_shape, default_size, min_size, data_format, require_flatten, weights)
    273             default_shape = (input_shape[0], default_size, default_size)
    274         else:
--> 275             if input_shape[-1] not in {1, 3}:
    276                 warnings.warn(
    277                     'This model usually expects 1 or 3 input channels. '

TypeError: unhashable type: 'Dimension'

我不明白是什么导致了错误,因为当我使用顺序模型时它绝对没问题。但它不适用于此功能模型。

最佳答案

inputs.shape 不是列表,因此它会引发错误。它为您提供类型为 tensorflow.python.framework.tensor_shape.TensorShape 的形状,其中包含类型为 Dimension

的每个维度的列表
print(inputs.shape)
# output TensorShape([Dimension(None), Dimension(256), Dimension(256), Dimension(1)])

您可以使用 as_list() 将形状获取为列表:

# inputs.shape.as_list()
# output [None, 256, 256, 1]

x = InceptionV3(include_top = False, weights = None, input_shape=inputs.shape.as_list()[1:])(x)

关于python - Keras InceptionV3 类型错误 : unhashable type: 'Dimension' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60017583/

相关文章:

python - 输入多个数据集到tensorflow模型

machine-learning - 单浮点标签,caffe中的LMDB格式

python - Keras model.summary() 对象到字符串

machine-learning - Keras 中具有有限输出的多目标回归

python-3.x - 如何匹配 cv2.imread 到 keras image.img_load 输出

Python 数据框。根据行索引向左移动行值

python - 特定的 Python 版本字节码可以在不同的解释器上运行吗?

python - PyQt5 和 Django : How to Upload Image using HTTP Request ( Multipart-form )?

python - 对于相同的 Keras 模型和数据,精度低于 AUC

machine-learning - 构建决策树时的停止条件