在将Caffe模型转换为Keras时出现以下错误:“dillation\u rate”参数必须是2个整数的元组。收到:[2]

2024-06-16 12:51:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用caffe2keraspre-trained Caffe model转换为Keras。
我得到以下错误:

ValueError: The dilation_rate argument must be a tuple of 2 integers. Received: [2]

这是终端的完整输出:

Traceback (most recent call last):
  File "/usr/local/var/pyenv/versions/3.5.1/lib/python3.5/runpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)

  File "/usr/local/var/pyenv/versions/3.5.1/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)

  File "/Users/R/tensorflow/lib/python3.5/site-packages/caffe2keras/__main__.py", line 32, in <module>
    main()

  File "/Users/R/tensorflow/lib/python3.5/site-packages/caffe2keras/__main__.py", line 22, in main
    model = convert.caffe_to_keras(args.prototxt, args.caffemodel, args.debug)

  File "/Users/R/tensorflow/lib/python3.5/site-packages/caffe2keras/convert.py", line 416, in caffe_to_keras
    tuple(config.input_dim[1:]))

  File "/Users/R/tensorflow/lib/python3.5/site-packages/caffe2keras/convert.py", line 533, in create_model
    out_blobs = converter(layer, layer_bottom_blobs)

  File "/Users/R/tensorflow/lib/python3.5/site-packages/caffe2keras/convert.py", line 53, in wrapper
    rv = converter_func(spec, bot_arg)

  File "/Users/R/tensorflow/lib/python3.5/site-packages/caffe2keras/convert.py", line 147, in handle_conv
    data_format='channels_first')(bottom)

  File "/Users/R/tensorflow/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)

  File "/Users/R/tensorflow/lib/python3.5/site-packages/keras/layers/convolutional.py", line 490, in __init__
    **kwargs)

  File "/Users/R/tensorflow/lib/python3.5/site-packages/keras/layers/convolutional.py", line 114, in __init__
    'dilation_rate')

  File "/Users/R/tensorflow/lib/python3.5/site-packages/keras/utils/conv_utils.py", line 40, in normalize_tuple
    str(n) + ' integers. Received: ' + str(value))

ValueError: The `dilation_rate` argument must be a tuple of 2 integers. Received: [2]

这是失败的代码:

if isinstance(value, int):

        return (value,) * n
    else:
        try:
            value_tuple = tuple(value)
        except TypeError:
            raise ValueError('The `' + name + '` argument must be a tuple of ' +
                             str(n) + ' integers. Received: ' + str(value))
        if len(value_tuple) != n:
            raise ValueError('The `' + name + '` argument must be a tuple of ' +
                             str(n) + ' integers. Received: ' + str(value))
        for single_value in value_tuple:
            try:
                int(single_value)
            except ValueError:
                raise ValueError('The `' + name + '` argument must be a tuple of ' +
                                 str(n) + ' integers. Received: ' + str(value) + ' '
                                 'including element ' + str(single_value) + ' of '
                                 'type ' + str(type(single_value)))
    return value_tuple

我已经修改了代码,让它打印出传递给'type(value)''value''n''name'函数的参数。你知道吗

下面是一个工作时打印内容的示例:

type: <class 'tuple'> value: (1, 1) n: 2 name: strides

以下是失败时打印的内容:

type: <class 'google.protobuf.pyext._message.RepeatedScalarContainer'> value: [2] n: 2 name: dilation_rate

我在终端输入以下内容:

python -m caffe2keras deploy_DilatedNet.prototxt DilatedNet_iter_120000.caffemodel keras_DilatedNet_from_Caffe.h5

如果有人能帮我解决这个错误或者建议一个更好的方法将Caffe模型转换成Keras,我将不胜感激。你知道吗


Tags: inpyvaluelibpackagestensorflowlinesite