当前位置:   article > 正文

运行LaneNet,基于Tensorflow2.0兼容Tensorflow1.0运行报错。TypeError: Dimension value must be integer or None , go_tensorflow 2.0兼容1.0

tensorflow 2.0兼容1.0

想要兼容代码,比较可行的是参考如下博主指出的方法

用tensorflow2运行tensorflow1.x代码的方法_tf2调用tf1代码-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/qq_28941587/article/details/128466526?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-2-128466526-blog-114885736.235%5Ev43%5Epc_blog_bottom_relevance_base5&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-2-128466526-blog-114885736.235%5Ev43%5Epc_blog_bottom_relevance_base5&utm_relevant_index=5

  1. import tensorflow.compat.v1 as tf
  2. tf.disable_v2_behavior()

比如如下的一长条代码中

  1. class CNNBaseModel(object):
  2. """
  3. Base model for other specific cnn ctpn_models
  4. """
  5. def __init__(self):
  6. pass
  7. @staticmethod
  8. def conv2d(inputdata, out_channel, kernel_size, padding='SAME',
  9. stride=1, w_init=None, b_init=None,
  10. split=1, use_bias=True, data_format='NHWC', name=None):
  11. """
  12. Packing the tensorflow conv2d function.
  13. :param name: op name
  14. :param inputdata: A 4D tensorflow tensor which ust have known number of channels, but can have other
  15. unknown dimensions.
  16. :param out_channel: number of output channel.
  17. :param kernel_size: int so only support square kernel convolution
  18. :param padding: 'VALID' or 'SAME'
  19. :param stride: int so only support square stride
  20. :param w_init: initializer for convolution weights
  21. :param b_init: initializer for bias
  22. :param split: split channels as used in Alexnet mainly group for GPU memory save.
  23. :param use_bias: whether to use bias.
  24. :param data_format: default set to NHWC according tensorflow
  25. :return: tf.Tensor named ``output``
  26. """
  27. with tf.variable_scope(name):
  28. in_shape = inputdata.get_shape().as_list()
  29. channel_axis = 3 if data_format == 'NHWC' else 1
  30. in_channel = in_shape[channel_axis]
  31. assert in_channel is not None, "[Conv2D] Input cannot have unknown channel!"
  32. assert in_channel % split == 0
  33. assert out_channel % split == 0
  34. padding = padding.upper()
  35. # if isinstance(kernel_size, list):
  36. # filter_shape = [kernel_size[0], kernel_size[1]] + [in_channel / split, out_channel]
  37. # else:
  38. # filter_shape = [kernel_size, kernel_size] + [in_channel / split, out_channel]
  39. if isinstance(kernel_size, list):
  40. filter_shape = [kernel_size[0], kernel_size[1]] + [in_channel // split, out_channel]
  41. else:
  42. filter_shape = [kernel_size, kernel_size] + [in_channel // split, out_channel]
  43. if isinstance(stride, list):
  44. strides = [1, stride[0], stride[1], 1] if data_format == 'NHWC' \
  45. else [1, 1, stride[0], stride[1]]
  46. else:
  47. strides = [1, stride, stride, 1] if data_format == 'NHWC' \
  48. else [1, 1, stride, stride]
  49. if w_init is None:
  50. #w_init = tf.contrib.layers.variance_scaling_initializer()
  51. #w_init = slim.variance_scaling_initializer()
  52. w_init = tf.compat.v1.variance_scaling_initializer()
  53. if b_init is None:
  54. b_init = tf.constant_initializer()
  55. w = tf.get_variable('W', filter_shape, initializer=w_init)
  56. b = None
  57. if use_bias:
  58. b = tf.get_variable('b', [out_channel], initializer=b_init)
  59. if split == 1:
  60. conv = tf.nn.conv2d(inputdata, w, strides, padding, data_format=data_format)
  61. else:
  62. inputs = tf.split(inputdata, split, channel_axis)
  63. kernels = tf.split(w, split, 3)
  64. outputs = [tf.nn.conv2d(i, k, strides, padding, data_format=data_format)
  65. for i, k in zip(inputs, kernels)]
  66. conv = tf.concat(outputs, channel_axis)
  67. ret = tf.identity(tf.nn.bias_add(conv, b, data_format=data_format)
  68. if use_bias else conv, name=name)
  69. return ret
'
运行

具体代码块如下:

  1. if w_init is None:
  2. #w_init = tf.contrib.layers.variance_scaling_initializer()
  3. #w_init = slim.variance_scaling_initializer()
  4. w_init = tf.compat.v1.variance_scaling_initializer()

其中的#w_init = tf.contrib.layers.variance_scaling_initializer()为最原始的代码,会发生报错如下的:module 'tensorflow._api.v2.compat.v1' has no attribute 'contrib'
               #w_init = slim.variance_scaling_initializer()
                w_init = tf.compat.v1.variance_scaling_initializer()

当遇见报错————参考这种方式不可取

python - Tensorflow 2.x - tf.contrib.layers.variance_scaling_initializer() - Stack Overflowicon-default.png?t=N7T8https://stackoverflow.com/questions/66213366/tensorflow-2-x-tf-contrib-layers-variance-scaling-initializerAttributeError: module 'tensorflow.python.keras.api._v1.keras.initializers' has no attribute 'variance_scaling'报错解决需要这样解决:

  1. depthwise_filter_shape = [kernel_size, kernel_size] + [in_channel, depth_multiplier]
  2. #w_init = tf.contrib.layers.variance_scaling_initializer()
  3. #w_init = tf.keras.initializers.variance_scaling()
  4. w_init = tf.compat.v1.variance_scaling_initializer()

注释掉错误的代码:

  1. #w_init = tf.contrib.layers.variance_scaling_initializer()
  2. #w_init = tf.keras.initializers.variance_scaling()
'
运行

正确修改的代码为:

w_init = tf.compat.v1.variance_scaling_initializer()

对于报错内容为:

  1. 报错内容为————laneNet) PS D:\Code\CarLane\lanenet-lane-detection\tools> python test_lanenet.py --weights_path D:\Code\CarLane\lanenet-lane-detection\model\BiseNetV2_LaneNet_Tusimple_Model_Weights\tusimple_lanenet.ckpt --image_path D:/Code/CarLane/lanenet-lane-detection/data/tusimple_test_image/0.jpg
  2. 2024-03-29 19:39:43.501079: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
  3. WARNING:tensorflow:From D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\compat\v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
  4. Instructions for updating:
  5. non-resource variables are not supported in the long term
  6. 2024-03-29 19:39:45.268 | INFO | __main__:test_lanenet:84 - Start reading image and preprocessing
  7. 2024-03-29 19:39:45.279 | INFO | __main__:test_lanenet:90 - Image load complete, cost time: 0.01000s
  8. Traceback (most recent call last):
  9. File "test_lanenet.py", line 163, in <module>
  10. test_lanenet(args.image_path, args.weights_path)
  11. File "test_lanenet.py", line 95, in test_lanenet
  12. binary_seg_ret, instance_seg_ret = net.inference(input_tensor=input_tensor, name='LaneNet')
  13. File "D:\Code/CarLane/lanenet-lane-detection\lanenet_model\lanenet.py", line 37, in inference
  14. extract_feats_result = self._frontend.build_model(
  15. File "D:\Code/CarLane/lanenet-lane-detection\lanenet_model\lanenet_front_end.py", line 34, in build_model
  16. return self._net.build_model(
  17. File "D:\Code/CarLane/lanenet-lane-detection\semantic_segmentation_zoo\bisenet_v2.py", line 1045, in build_model
  18. detail_branch_output = self.build_detail_branch(
  19. File "D:\Code/CarLane/lanenet-lane-detection\semantic_segmentation_zoo\bisenet_v2.py", line 864, in build_detail_branch
  20. result = block_op(
  21. File "D:\Code/CarLane/lanenet-lane-detection\semantic_segmentation_zoo\bisenet_v2.py", line 816, in _conv_block
  22. result = self.conv2d(
  23. File "D:\Code/CarLane/lanenet-lane-detection\semantic_segmentation_zoo\cnn_basenet.py", line 62, in conv2d
  24. w = tf.get_variable('W', filter_shape, initializer=w_init)
  25. File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1577, in get_variable
  26. return get_variable_scope().get_variable(
  27. File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1320, in get_variable
  28. return var_store.get_variable(
  29. File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 576, in get_variable
  30. return _true_getter(
  31. File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 529, in _true_getter
  32. return self._get_single_variable(
  33. File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 874, in _get_single_variable
  34. shape = tensor_shape.as_shape(shape)
  35. File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 1235, in as_shape
  36. return TensorShape(shape)
  37. File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 758, in __init__
  38. self._dims = [Dimension(d) for d in dims]
  39. File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 758, in <listcomp>
  40. self._dims = [Dimension(d) for d in dims]
  41. File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 203, in __init__
  42. six.raise_from(
  43. File "<string>", line 3, in raise_from

 具体核心报错为:

TypeError: Dimension value must be integer or None or have an __index__ method, got value '3.0' with type '<class 'float'>'

解决方法:问题出现在尝试将浮点数 (float) 用作张量维度的值。错在创建变量 w 时给定的 filter_shape 包含了浮点数,而 TensorFlow 期望张量的形状 (shape) 中的维度值必须是整数。

错误指向的是以下这行代码:

filter_shape = [kernel_size, kernel_size] + [in_channel / split, out_channel]

问题在于 in_channel / split 这个操作。在 Python 3 中,/ 操作符执行的是浮点除法,即使两个操作数都是整数,结果也会是浮点数。为了解决这个问题,你需要使用整数除法 //,它会返回操作数除法的整数部分,确保结果是整数类型,而不是浮点数。这在定义张量的形状时非常重要,因为形状参数必须是整数。

将相关代码行更改为:

  1. if isinstance(kernel_size, list):
  2. filter_shape = [kernel_size[0], kernel_size[1]] + [in_channel // split, out_channel]
  3. else:
  4. filter_shape = [kernel_size, kernel_size] + [in_channel // split, out_channel]

/单杠变斜杠//。通过这种方式,filter_shape 中的所有值都会是整数,这样就不会再抛出关于尝试将浮点数用作维度值的错误了。

最后成功运行代码如下:

  1. (laneNet) PS D:\Code\CarLane\lanenet-lane-detection\tools> python test_lanenet.py --weights_path D:\Code\CarLane\lanenet-lane-detection\model\BiseNetV2_LaneNet_Tusimple_Model_Weights\tusimple_lanenet.ckpt --image_path D:/Code/CarLane/lanenet-lane-detection/data/tusimple_test_image/0.jpg
  2. 2024-03-29 19:46:39.924422: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
  3. WARNING:tensorflow:From D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\compat\v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
  4. Instructions for updating:
  5. non-resource variables are not supported in the long term
  6. 2024-03-29 19:46:41.588 | INFO | __main__:test_lanenet:84 - Start reading image and preprocessing
  7. 2024-03-29 19:46:41.599 | INFO | __main__:test_lanenet:90 - Image load complete, cost time: 0.01060s
  8. W0329 19:46:41.608941 23284 warnings.py:109] D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\keras\legacy_tf_layers\normalization.py:307: UserWarning: `tf.lay
  9. ers.batch_normalization` is deprecated and will be removed in a future version. Please use `tf.keras.layers.BatchNormalization` instead. In particular, `tf.control_dependencies(tf.GraphKeys.UPDATE_OPS)` should not be used (consult the `tf.keras.layers.BatchNormalization` documentation).
  10. warnings.warn(
  11. W0329 19:46:41.611858 23284 warnings.py:109] D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\keras\engine\base_layer_v1.py:1719: UserWarning: `layer.apply` is deprecated and will be removed in a future version. Please use `layer.__call__` method instead.
  12. warnings.warn('`layer.apply` is deprecated and '
  13. 2024-03-29 19:46:42.446556: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2
  14. To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
  15. 2024-03-29 19:46:42.458132: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library nvcuda.dll
  16. 2024-03-29 19:46:42.536926: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties:
  17. pciBusID: 0000:01:00.0 name: NVIDIA GeForce RTX 3060 Laptop GPU computeCapability: 8.6
  18. coreClock: 1.68GHz coreCount: 30 deviceMemorySize: 6.00GiB deviceMemoryBandwidth: 312.97GiB/s
  19. 2024-03-29 19:46:42.537254: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
  20. 2024-03-29 19:46:42.592151: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
  21. 2024-03-29 19:46:42.710787: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
  22. 2024-03-29 19:46:42.711164: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0
  23. 2024-03-29 19:46:43.531290: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1261] Device interconnect StreamExecutor with strength 1 edge matrix:
  24. 2024-03-29 19:46:43.531475: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1267] 0
  25. 2024-03-29 19:46:43.531529: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1280] 0: N
  26. 2024-03-29 19:46:43.531983: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1406] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 5529 MB memory) -> physical GPU (device: 0, name: NVIDIA GeForce RTX 3060 Laptop GPU, pci bus id: 0000:01:00.0, compute capability: 8.6)
  27. 2024-03-29 19:46:43.533011: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
  28. INFO:tensorflow:Restoring parameters from D:\Code\CarLane\lanenet-lane-detection\model\BiseNetV2_LaneNet_Tusimple_Model_Weights\tusimple_lanenet.ckpt
  29. I0329 19:46:43.603782 23284 saver.py:1292] Restoring parameters from D:\Code\CarLane\lanenet-lane-detection\model\BiseNetV2_LaneNet_Tusimple_Model_Weights\tusimple_lanenet.ckpt
  30. 2024-03-29 19:46:43.619515: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:196] None of the MLIR optimization passes are enabled (registered 0 passes)
  31. 2024-03-29 19:46:43.909649: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
  32. 2024-03-29 19:46:48.369187: I tensorflow/core/platform/windows/subprocess.cc:308] SubProcess ended with return code: 0
  33. 2024-03-29 19:46:48.397593: I tensorflow/core/platform/windows/subprocess.cc:308] SubProcess ended with return code: 4294967295
  34. 2024-03-29 19:46:48.398103: W tensorflow/stream_executor/gpu/redzone_allocator.cc:314] Internal: ptxas exited with non-zero error code -1, output:
  35. Relying on driver to perform ptx compilation.
  36. Modify $PATH to customize ptxas location.
  37. This message will be only logged once.
  38. 2024-03-29 19:46:48.506866: I tensorflow/core/platform/windows/subprocess.cc:308] SubProcess ended with return code: 4294967295
  39. 08] SubProcess ended with return code: 4294967295
  40. 2024-03-29 19:47:01.250217: I tensorflow/core/platform/windows/subprocess.cc:308] SubProcess ended with return code: 4294967295
  41. 2024-03-29 19:47:12.115481: I tensorflow/core/platform/windows/subprocess.cc:308] SubProcess ended with return code: 4294967295
  42. 2024-03-29 19:47:16.902 | INFO | __main__:test_lanenet:128 - Single imgae inference cost time: 0.06624s

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/841148
推荐阅读
相关标签
  

闽ICP备14008679号