【问题标题】:ValueError: The two structures don't have the same sequence lengthValueError:两个结构的序列长度不同
【发布时间】:2020-07-01 15:32:36
【问题描述】:

最近我尝试将 repository 中的 mask rcnn 从 tensorflow 1 转换为 tensorflow 2。

重写代码后,当我运行示例“形状”并执行代码时

model.train(dataset_train, dataset_val, learning_rate=config.LEARNING_RATE, epochs=1, layers='heads')

我得到了这个 ValueError

(ValueError: 这两个结构没有相同的序列 长度。输入结构的长度为 0,而浅层结构的长度为 长度 14.)

不知道怎么解决。

我注意到这个模型的输出在model.py中的model.build()中有14个元素。

我将我的项目上传到this repo整个结构和上面的原始仓库一样,我只是把这个模型的不同部分分成不同的文件,然后检查两次以检查打字问题。我不能肯定这个问题不是由我目前找不到的打字问题。

以下是整个错误信息:

Traceback (most recent call last):

File "/Users/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

File "", line 1, in
runfile('/Users/PycharmProjects/Mask-Dence-Cap-R-CNN/Project/samples/test.py', wdir='/Users/PycharmProjects/Mask-Dence-Cap-R-CNN/Project/samples')

File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script

File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)

File "/Users/PycharmProjects/Mask-Dence-Cap-R-CNN/Project/samples/test.py", line 260, in
layers='heads')

File "/Users/PycharmProjects/Mask-Dence-Cap-R-CNN/Project/model.py", line 585, in train
use_multiprocessing=True,

File "/Users/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 108, in _method_wrapper
return method(self, *args, **kwargs)

File "/Users/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1098, in fit
tmp_logs = train_function(iterator)

File "/Users/.local/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 780, in call
result = self._call(*args, **kwds)

File "/Users/.local/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 823, in _call
self._initialize(args, kwds, add_initializers_to=initializers)

File "/Users/.local/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 697, in _initialize
*args, **kwds))

File "/Users/.local/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 2855, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)

File "/Users/.local/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 3213, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/Users/.local/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 3075, in _create_graph_function
capture_by_value=self._capture_by_value),

File "/Users/.local/lib/python3.6/site-packages/tensorflow/python/framework/func_graph.py", line 986, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)

File "/Users/.local/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 600, in wrapped_fn
return weak_wrapped_fn().wrapped(*args, **kwds)

File "/Users/.local/lib/python3.6/site-packages/tensorflow/python/framework/func_graph.py", line 973, in wrapper
raise e.ag_error_metadata.to_exception(e)


ValueError: in user code:

/Users/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:806 train_function *
return step_function(self, iterator)

/Users/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))

/Users/.local/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)

/Users/.local/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2652 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)

/Users/.local/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:3012 _call_for_each_replica
return fn(*args, **kwargs)

/Users.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:789 run_step **
outputs = model.train_step(data)

/Users/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:759 train_step
self.compiled_metrics.update_state(y, y_pred, sample_weight)

/Users/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/compile_utils.py:388 update_state
self.build(y_pred, y_true)

/Users/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/compile_utils.py:319 build
self._metrics, y_true, y_pred)

/Users/.local/lib/python3.6/site-packages/tensorflow/python/util/nest.py:1139 map_structure_up_to
**kwargs)

/Users/.local/lib/python3.6/site-packages/tensorflow/python/util/nest.py:1221 map_structure_with_tuple_paths_up_to
expand_composites=expand_composites)

/Users/.local/lib/python3.6/site-packages/tensorflow/python/util/nest.py:854 assert_shallow_structure
input_length=len(input_tree), shallow_length=len(shallow_tree)))

ValueError: The two structures don't have the same sequence length. Input structure has length 0, while shallow structure has length 14.

【问题讨论】:

  • 直接运行 samples/test.py 就会出现这个错误。或者运行 samples/shapes/train_shapes.ipynb 也可以解决这个问题。
  • 我目前使用原始格式的代码更新了我的 repo,并将 keras 更改为 tf.keras。我现在正在使用 tf2.1

标签: python tensorflow keras


【解决方案1】:

我通过参考这个repo解决了这个问题。

【讨论】:

  • "鼓励链接到外部资源,但请在链接周围添加上下文,以便您的其他用户了解它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防外部资源无法访问或永久脱机。” SO answer guidelines
猜你喜欢
  • 2022-07-04
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 2020-04-18
  • 1970-01-01
  • 2019-02-24
  • 2018-05-29
相关资源
最近更新 更多