zzh8829 / yolov3-tf2

YoloV3 Implemented in Tensorflow 2.0
MIT License
2.51k stars 907 forks source link

Error when initializing model using TF1.12 #33

Closed Hugh0120 closed 5 years ago

Hugh0120 commented 5 years ago

I just wanna try your Keras version model in TF1.12 and added two lines into 'yolov3_tf2/models.py'

if __name__ == '__main__':
    model = YoloV3(training=True, size=418)

Things go well in TF2.0, however, when I run it in TF1.12, the following error occurred:

I just wanna try your Keras version model in TF1.12 and added two lines into 'yolov3_tf2/models.py'

if __name__ == '__main__':
    model = YoloV3(training=True, size=418)

However, the following error occurred:

Traceback (most recent call last): File "/Users/xxx/Code/GitOA/xxx/YoloV3/src/yolov3_tf2/models.py", line 312, in model = YoloV3(training=True, size=418) File "/Users/xxx/Code/GitOA/xxx/YoloV3/src/yolov3_tf2/models.py", line 207, in YoloV3 x = YoloConv(256, name='yolo_conv_1')((x, x_61)) File "/Users/xxx/Code/GitOA/xxx/YoloV3/src/yolov3_tf2/models.py", line 112, in yolo_conv return Model(inputs, x, name=name)(x_in) File "/Users/xxx/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 554, in call outputs = self.call(inputs, *args, kwargs) File "/Users/xxx/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 815, in call mask=masks) File "/Users/xxx/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1002, in _run_internal_graph output_tensors = layer.call(computed_tensor, kwargs) File "/Users/xxx/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/layers/convolutional.py", line 194, in call outputs = self._convolution_op(inputs, self.kernel) File "/Users/xxx/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 966, in call return self.conv_op(inp, filter) File "/Users/xxx/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 591, in call return self.call(inp, filter) File "/Users/xxx/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_ops.py", line 208, in call name=self.name) File "/Users/xxx/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 1026, in conv2d data_format=data_format, dilations=dilations, name=name) File "/Users/xxx/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 529, in _apply_op_helper (input_name, err)) ValueError: Tried to convert 'input' to a tensor and failed. Error: Dimension 1 in both shapes must be equal, but are 13 and 26. Shapes are [?,13,13,512] and [?,26,26,512]. From merging shape 0 with other shapes. for 'yolo_conv_1/conv2d_59/Conv2D/packed' (op: 'Pack') with input shapes: [?,13,13,512], [?,26,26,512].

I think it's because of the version of Tensorflow but I'm not sure which specific part caused this, does anybody knows?

zzh8829 commented 5 years ago

maybe there some api difference in tf2 and tf1.12 I never tested this on tf1.12 so can't really answer

hollowgalaxy commented 5 years ago

I found same issue in tf 1.13. I ll I did was run python convert.py after using your command to download the weights.

W0712 13:05:21.441559 140197088499456 deprecation.py:323] From /home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
Traceback (most recent call last):
  File "/home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1659, in _create_c_op
    c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 4 but is rank 5 for 'yolo_conv_1/conv2d_59/Conv2D' (op: 'Conv2D') with input shapes: [2,?,?,?,512], [1,1,512,256].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "convert.py", line 33, in <module>
    app.run(main)
  File "/home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/absl/app.py", line 300, in run
    _run_main(main, args)
  File "/home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/absl/app.py", line 251, in _run_main
    sys.exit(main(argv))
  File "convert.py", line 16, in main
    yolo = YoloV3()
  File "/home/hollowgalaxy/Github/amazon_demo/recognition_docker/yolov3_tf2/yolov3_tf2/models.py", line 207, in YoloV3
    x = YoloConv(256, name='yolo_conv_1')((x, x_61))
  File "/home/hollowgalaxy/Github/amazon_demo/recognition_docker/yolov3_tf2/yolov3_tf2/models.py", line 112, in yolo_conv
    return Model(inputs, x, name=name)(x_in)
  File "/home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 554, in __call__
    outputs = self.call(inputs, *args, **kwargs)
  File "/home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/tensorflow/python/keras/engine/network.py", line 815, in call
    mask=masks)
  File "/home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/tensorflow/python/keras/engine/network.py", line 1002, in _run_internal_graph
    output_tensors = layer.call(computed_tensor, **kwargs)
  File "/home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/tensorflow/python/keras/layers/convolutional.py", line 194, in call
    outputs = self._convolution_op(inputs, self.kernel)
  File "/home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py", line 966, in __call__
    return self.conv_op(inp, filter)
  File "/home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py", line 591, in __call__
    return self.call(inp, filter)
  File "/home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py", line 208, in __call__
    name=self.name)
  File "/home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 1026, in conv2d
    data_format=data_format, dilations=dilations, name=name)
  File "/home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
    op_def=op_def)
  File "/home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "/home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 3300, in create_op
    op_def=op_def)
  File "/home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1823, in __init__
    control_input_ops)
  File "/home/hollowgalaxy/anaconda3/envs/py36/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1662, in _create_c_op
    raise ValueError(str(e))
ValueError: Shape must be rank 4 but is rank 5 for 'yolo_conv_1/conv2d_59/Conv2D' (op: 'Conv2D') with input shapes: [2,?,?,?,512], [1,1,512,256].
hollowgalaxy commented 5 years ago

corrected this line and shape issue went away. However this is a terrible duct tape solution, and I am not sure how best to report this for tensorflow team. Here is the code to fix it.

return Model(inputs, x, name=name)([x_in[0],x_in[1]] if isinstance(x_in, tuple) else x_in)

*I haven't tested the full pipeline with this change as there are other issues with using tf 1.13 with tf 2 code

iamsaksham commented 5 years ago

@hollowgalaxy I faced the same issue on mac. What was happening was that tensorflow-gpu==2.0.0a0 from the environment.yml was not getting installed as tensorflow-gpu is not supported in mac. So as a result there was no tensorflow 2 installed. To fix this I replaced the tensorflow-gpu==2.0.0a0 with tensorflow==2.0.0alpha0

environment.yml

name: yolov3-tf2
channels:
  - conda-forge

dependencies:
  - python==3.6
  - pip
  - matplotlib
  - pip:
    - absl-py
    - numpy
    - opencv-python==4.0.0.21
    - tensorflow==2.0.0alpha0
    # - tensorflow-gpu==2.0.0a0