Keras:ValueError:“concat”模式只能合并具有匹配输出形状的图层

时间:2017-05-01 09:41:12

标签: tensorflow keras theano keras-layer

我在Keras 2中遇到此错误。我该如何解决? 我已导入

from keras.layers import Input, merge

[...]

 up1 = merge([UpSampling2D(size=(2, 2))(conv3), conv2], mode='concat', concat_axis=1)
    /usr/local/python/3.5.2-gcc4/externalmodules/lib/python3.5/site-packages/keras/legacy/layers.py:456: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.
      name=name)
Traceback (most recent call last):
  File "./src/retinaNN_training.py", line 171, in <module>
    model = get_unet(n_ch, patch_height, patch_width)  #the U-net model
  File "./src/retinaNN_training.py", line 53, in get_unet
    up1 = merge([UpSampling2D(size=(2, 2))(conv3), conv2], mode='concat', concat_axis=1)
  File "/usr/local/python/3.5.2-gcc4/externalmodules/lib/python3.5/site-packages/keras/legacy/layers.py", line 456, in merge
    name=name)
  File "/usr/local/python/3.5.2-gcc4/externalmodules/lib/python3.5/site-packages/keras/legacy/layers.py", line 107, in __init__
    node_indices, tensor_indices)
  File "/usr/local/python/3.5.2-gcc4/externalmodules/lib/python3.5/site-packages/keras/legacy/layers.py", line 187, in _arguments_validation
    'Layer shapes: %s' % (input_shapes))
ValueError: "concat" mode can only merge layers with matching output shapes except for the concat axis. Layer shapes: [(None, 2, 24, 128), (None, 1, 24, 64)]

如何将合并功能转换为可在Keras 2.x.x上读取?感谢

1 个答案:

答案 0 :(得分:0)

您似乎正在使用上面1.0.3的Keras版本。 1.0.3之后的版本默认使用tensorflow作为后端Github Issues。所以你可以做两件事:

  1. 安装1.0.3版本的Keras。 [OR]
  2. pip install https://github.com/fchollet/keras/tarball/1.0.3

    1. 修改~/.keras/keras.json,使其看起来像
    2. { "image_dim_ordering": "th", "epsilon": 1e-07, "floatx": "float32", "backend": "tensorflow" }