加载Resnet权重时出错

时间:2018-03-20 04:23:16

标签: python tensorflow resnet

我试图在代码后加载Resnet加载的权重。但是在Resnet方法中加载权重时

base_model = ResNet50(input_shape=(224, 224, 3), include_top=False,weights='imagenet',pooling='avg')


# load weights
if weights == 'imagenet':
    if include_top:
        weights_path = get_file('resnet50_weights_tf_dim_ordering_tf_kernels.h5',
                                WEIGHTS_PATH,
                                cache_subdir='models',
                                md5_hash='a7b3fe01876f51b976af0dea6bc144eb')
    '''else:
        weights_path = get_file('resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
                                WEIGHTS_PATH_NO_TOP,
                                cache_subdir='models',
                                md5_hash='a268eb855778b3df3c7506639542a6af')'''

    print ('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    model.load_weights('C:/Users/hirplk/.keras/models/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')
    print ('cccccccccccccccccccccccccccccccccccccccc')

我收到以下错误。

Traceback (most recent call last):
  File "C:\CT_SCAN_IMAGE_SET\resnet_50\dbs2017\resnet_fineTuning.py", line 391, in <module>
    base_model = ResNet50(input_shape=(224, 224, 3), include_top=False,weights='imagenet',pooling='avg')
  File "C:\CT_SCAN_IMAGE_SET\resnet_50\dbs2017\resnet_fineTuning.py", line 298, in ResNet50
    model.load_weights('C:/Users/hirplk/.keras/models/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')
  File "C:\Research\Python_installation\lib\site-packages\keras\engine\topology.py", line 2622, in load_weights
    load_weights_from_hdf5_group(f, self.layers)
  File "C:\Research\Python_installation\lib\site-packages\keras\engine\topology.py", line 3143, in load_weights_from_hdf5_group
    K.batch_set_value(weight_value_tuples)
  File "C:\Research\Python_installation\lib\site-packages\keras\backend\tensorflow_backend.py", line 2247, in batch_set_value
    assign_op = x.assign(assign_placeholder)
  File "C:\Research\Python_installation\lib\site-packages\tensorflow\python\ops\variables.py", line 527, in assign
    return state_ops.assign(self._variable, value, use_locking=use_locking)
  File "C:\Research\Python_installation\lib\site-packages\tensorflow\python\ops\state_ops.py", line 274, in assign
    validate_shape=validate_shape)
  File "C:\Research\Python_installation\lib\site-packages\tensorflow\python\ops\gen_state_ops.py", line 43, in assign
    use_locking=use_locking, name=name)
  File "C:\Research\Python_installation\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "C:\Research\Python_installation\lib\site-packages\tensorflow\python\framework\ops.py", line 2632, in create_op
    set_shapes_for_outputs(ret)
  File "C:\Research\Python_installation\lib\site-packages\tensorflow\python\framework\ops.py", line 1911, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "C:\Research\Python_installation\lib\site-packages\tensorflow\python\framework\ops.py", line 1861, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "C:\Research\Python_installation\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 595, in call_cpp_shape_fn
    require_shape_fn)
  File "C:\Research\Python_installation\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 659, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Dimension 0 in both shapes must be equal, but are 1 and 512 for 'Assign_78' (op: 'Assign') with input shapes: [1,1,128,512], [512,256,1,1].

我的图片尺寸为[224,224,3]。 有人可以帮我确定一下我的问题是什么吗?

1 个答案:

答案 0 :(得分:0)

您似乎正在使用Keras加载RESNET权重。 Keras需要指定频道顺序,有时会混淆。尝试使用:

K.set_image_data_format('channels_last')

点击此处了解更多详情:https://keras.io/backend/#set_image_data_format