wangermeng2021 / Scaled-YOLOv4-tensorflow2

A Tensorflow2.x implementation of Scaled-YOLOv4 as described in Scaled-YOLOv4: Scaling Cross Stage Partial Network
Apache License 2.0
47 stars 18 forks source link

Duplicate name in graph `ones` #1

Open OndrejSzekely opened 3 years ago

OndrejSzekely commented 3 years ago

Hello, using TF 2.1 training fails during model export. It throws an error in box_decode function in box_coder.py. See traceback:

Traceback (most recent call last):
  File "/Users/ondra/opt/anaconda3/envs/tf_21_py37/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 1619, in _create_c_op
    c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Duplicate node name in graph: 'ones'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/ondra/Downloads/ScaledYOLOv4-tensorflow2-main/train.py", line 272, in <module>
    main(args)
  File "/Users/ondra/Downloads/ScaledYOLOv4-tensorflow2-main/train.py", line 266, in main
    model = Yolov4(args, training=False)
  File "/Users/ondra/Downloads/ScaledYOLOv4-tensorflow2-main/model/yolov4.py", line 21, in Yolov4
    pre_nms_decoded_boxes, pre_nms__scores = postprocess(outputs,args)
  File "/Users/ondra/Downloads/ScaledYOLOv4-tensorflow2-main/model/postprocess.py", line 22, in postprocess
    decoded_boxes = box_decode(output[..., 0:4], args, index)
  File "/Users/ondra/Downloads/ScaledYOLOv4-tensorflow2-main/model/box_coder.py", line 22, in box_decode
    grid_xy = tf.stack(tf.meshgrid(tf.range(grid_width), tf.range(grid_height)), axis=-1)
  File "/Users/ondra/opt/anaconda3/envs/tf_21_py37/lib/python3.7/site-packages/tensorflow_core/python/ops/array_ops.py", line 3065, in meshgrid
    mult_fact = ones(shapes, output_dtype)
  File "/Users/ondra/opt/anaconda3/envs/tf_21_py37/lib/python3.7/site-packages/tensorflow_core/python/ops/array_ops.py", line 2671, in ones
    output = fill(shape, constant(one, dtype=dtype), name=name)
  File "/Users/ondra/opt/anaconda3/envs/tf_21_py37/lib/python3.7/site-packages/tensorflow_core/python/ops/array_ops.py", line 233, in fill
    result = gen_array_ops.fill(dims, value, name=name)
  File "/Users/ondra/opt/anaconda3/envs/tf_21_py37/lib/python3.7/site-packages/tensorflow_core/python/ops/gen_array_ops.py", line 3247, in fill
    "Fill", dims=dims, value=value, name=name)
  File "/Users/ondra/opt/anaconda3/envs/tf_21_py37/lib/python3.7/site-packages/tensorflow_core/python/framework/op_def_library.py", line 742, in _apply_op_helper
    attrs=attr_protos, op_def=op_def)
  File "/Users/ondra/opt/anaconda3/envs/tf_21_py37/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py", line 595, in _create_op_internal
    compute_device)
  File "/Users/ondra/opt/anaconda3/envs/tf_21_py37/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 3322, in _create_op_internal
    op_def=op_def)
  File "/Users/ondra/opt/anaconda3/envs/tf_21_py37/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 1786, in __init__
    control_input_ops)
  File "/Users/ondra/opt/anaconda3/envs/tf_21_py37/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 1622, in _create_c_op
    raise ValueError(str(e))
ValueError: Duplicate node name in graph: 'ones'

The problem is in repeated call of tf.meshgrid function which calls ones function with same naming which works for the first YOLO head, but second time it fails because of already existing name.

Maybe in higher TF version the problem is handled on framework level. My solution is to run postprocess block in for loop in postprocess.py (for loop stats on line 18) under unique name space for each head (iteration).

wangermeng2021 commented 3 years ago

Thanks for the feedback, My test environment is Tensorflow2.3. I will check it. It should generate new different tensor name in every loop

OndrejSzekely commented 3 years ago

Please do that, thank you!