在尝试跑通tf-Faster-RCNN的时候,遇到了这个问题。
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1334, in _do_call
return fn(*args)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1319, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1407, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: assertion failed: [] [Condition x == y did not hold element-wise:] [x (losses/fast_rcnn_cls_loss/SparseSoftmaxCrossEntropyWithLogits/Shape_1:0) = ] [101 1 1] [y (losses/fast_rcnn_cls_loss/SparseSoftmaxCrossEntropyWithLogits/strided_slice:0) = ] [101]
[[{{node losses/fast_rcnn_cls_loss/SparseSoftmaxCrossEntropyWithLogits/assert_equal/Assert/Assert}}]]
[[{{node losses/mul}}]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "faster_rcnn_conv5.py", line 297, in
main()
File "faster_rcnn_conv5.py", line 290, in main
model.train()
File "faster_rcnn_conv5.py", line 170, in train
summary = self._run_train_iter(feed_dict)
File "faster_rcnn_conv5.py", line 138, in _run_train_iter
summary, _ = self.sess.run([self.merged, self.optimizer], feed_dict=feed_dict)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 929, in run
run_metadata_ptr)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1152, in _run
feed_dict_tensor, options, run_metadata)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1328, in _do_run
run_metadata)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 1348, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: assertion failed: [] [Condition x == y did not hold element-wise:] [x (losses/fast_rcnn_cls_loss/SparseSoftmaxCrossEntropyWithLogits/Shape_1:0) = ] [101 1 1] [y (losses/fast_rcnn_cls_loss/SparseSoftmaxCrossEntropyWithLogits/strided_slice:0) = ] [101]
[[node losses/fast_rcnn_cls_loss/SparseSoftmaxCrossEntropyWithLogits/assert_equal/Assert/Assert (defined at ../Lib/loss_functions.py:91) ]]
[[node losses/mul (defined at faster_rcnn_conv5.py:114) ]]
Caused by op 'losses/fast_rcnn_cls_loss/SparseSoftmaxCrossEntropyWithLogits/assert_equal/Assert/Assert', defined at:
File "faster_rcnn_conv5.py", line 297, in
main()
File "faster_rcnn_conv5.py", line 288, in main
model = FasterRcnnConv5(flags, dictionary)
File "faster_rcnn_conv5.py", line 40, in __init__
super().__init__(flags_input, flags_input['run_num'], vram=cfg.VRAM, restore=flags_input['restore_num'])
File "../Lib/TensorBase/tensorbase/base.py", line 677, in __init__
self._optimizer()
File "faster_rcnn_conv5.py", line 113, in _optimizer
self.fast_rcnn_cls_loss = self.fast_rcnn_net['TRAIN'].get_fast_rcnn_cls_loss()
File "../Networks/faster_rcnn_networks.py", line 245, in get_fast_rcnn_cls_loss
return fast_rcnn_cls_loss(fast_rcnn_cls_score, labels)
File "../Lib/loss_functions.py", line 91, in fast_rcnn_cls_loss
fast_rcnn_cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=tf.squeeze(fast_rcnn_cls_score), labels=labels))
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/nn_ops.py", line 2670, in sparse_softmax_cross_entropy_with_logits
array_ops.shape(logits)[:-1]))
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/check_ops.py", line 512, in assert_equal
return control_flow_ops.Assert(condition, data, summarize=summarize)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/tf_should_use.py", line 193, in wrapped
return _add_should_use_warning(fn(*args, **kwargs))
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 160, in Assert
return gen_logging_ops._assert(condition, data, summarize, name="Assert")
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_logging_ops.py", line 72, in _assert
name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 3300, in create_op
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1801, in __init__
self._traceback = tf_stack.extract_stack()
InvalidArgumentError (see above for traceback): assertion failed: [] [Condition x == y did not hold element-wise:] [x (losses/fast_rcnn_cls_loss/SparseSoftmaxCrossEntropyWithLogits/Shape_1:0) = ] [101 1 1] [y (losses/fast_rcnn_cls_loss/SparseSoftmaxCrossEntropyWithLogits/strided_slice:0) = ] [101]
[[node losses/fast_rcnn_cls_loss/SparseSoftmaxCrossEntropyWithLogits/assert_equal/Assert/Assert (defined at ../Lib/loss_functions.py:91) ]]
[[node losses/mul (defined at faster_rcnn_conv5.py:114) ]]
一开始闭着眼就开始上网搜索,结果啥也没找到,然后看了一下问题原因,发现还挺简单的。
错误的地方在于,计算loss的时候数组维度对不上,导致他计算不了,到对应的位置改一下数组维度就完事了。
修改位置:Lib/loss_function.py
修改函数:fast_rcnn_cls_loss,大约在80行。
修改内容:将函数的
fast_rcnn_cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=tf.squeeze(fast_rcnn_cls_score), labels=labels))
修改为:
fast_rcnn_cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=tf.expand_dims(tf.squeeze(fast_rcnn_cls_score),1), labels=labels))
好了,其实就是增加了一维。
以后要先分析错误,不能闭着眼就想剽窃别人的成果,这样不好。