赞
踩
想要兼容代码,比较可行的是参考如下博主指出的方法
- import tensorflow.compat.v1 as tf
- tf.disable_v2_behavior()
比如如下的一长条代码中
- class CNNBaseModel(object):
- """
- Base model for other specific cnn ctpn_models
- """
-
- def __init__(self):
- pass
-
- @staticmethod
- def conv2d(inputdata, out_channel, kernel_size, padding='SAME',
- stride=1, w_init=None, b_init=None,
- split=1, use_bias=True, data_format='NHWC', name=None):
- """
- Packing the tensorflow conv2d function.
- :param name: op name
- :param inputdata: A 4D tensorflow tensor which ust have known number of channels, but can have other
- unknown dimensions.
- :param out_channel: number of output channel.
- :param kernel_size: int so only support square kernel convolution
- :param padding: 'VALID' or 'SAME'
- :param stride: int so only support square stride
- :param w_init: initializer for convolution weights
- :param b_init: initializer for bias
- :param split: split channels as used in Alexnet mainly group for GPU memory save.
- :param use_bias: whether to use bias.
- :param data_format: default set to NHWC according tensorflow
- :return: tf.Tensor named ``output``
- """
- with tf.variable_scope(name):
- in_shape = inputdata.get_shape().as_list()
- channel_axis = 3 if data_format == 'NHWC' else 1
- in_channel = in_shape[channel_axis]
- assert in_channel is not None, "[Conv2D] Input cannot have unknown channel!"
- assert in_channel % split == 0
- assert out_channel % split == 0
-
- padding = padding.upper()
-
- # if isinstance(kernel_size, list):
- # filter_shape = [kernel_size[0], kernel_size[1]] + [in_channel / split, out_channel]
- # else:
- # filter_shape = [kernel_size, kernel_size] + [in_channel / split, out_channel]
- if isinstance(kernel_size, list):
- filter_shape = [kernel_size[0], kernel_size[1]] + [in_channel // split, out_channel]
- else:
- filter_shape = [kernel_size, kernel_size] + [in_channel // split, out_channel]
-
- if isinstance(stride, list):
- strides = [1, stride[0], stride[1], 1] if data_format == 'NHWC' \
- else [1, 1, stride[0], stride[1]]
- else:
- strides = [1, stride, stride, 1] if data_format == 'NHWC' \
- else [1, 1, stride, stride]
-
- if w_init is None:
- #w_init = tf.contrib.layers.variance_scaling_initializer()
- #w_init = slim.variance_scaling_initializer()
- w_init = tf.compat.v1.variance_scaling_initializer()
- if b_init is None:
- b_init = tf.constant_initializer()
-
- w = tf.get_variable('W', filter_shape, initializer=w_init)
- b = None
-
- if use_bias:
- b = tf.get_variable('b', [out_channel], initializer=b_init)
-
- if split == 1:
- conv = tf.nn.conv2d(inputdata, w, strides, padding, data_format=data_format)
- else:
- inputs = tf.split(inputdata, split, channel_axis)
- kernels = tf.split(w, split, 3)
- outputs = [tf.nn.conv2d(i, k, strides, padding, data_format=data_format)
- for i, k in zip(inputs, kernels)]
- conv = tf.concat(outputs, channel_axis)
-
- ret = tf.identity(tf.nn.bias_add(conv, b, data_format=data_format)
- if use_bias else conv, name=name)
-
- return ret
'运行
具体代码块如下:
- if w_init is None:
- #w_init = tf.contrib.layers.variance_scaling_initializer()
- #w_init = slim.variance_scaling_initializer()
- 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 Overflowhttps://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'报错解决需要这样解决:
- depthwise_filter_shape = [kernel_size, kernel_size] + [in_channel, depth_multiplier]
- #w_init = tf.contrib.layers.variance_scaling_initializer()
- #w_init = tf.keras.initializers.variance_scaling()
- w_init = tf.compat.v1.variance_scaling_initializer()
注释掉错误的代码:
- #w_init = tf.contrib.layers.variance_scaling_initializer()
- #w_init = tf.keras.initializers.variance_scaling()
'运行
正确修改的代码为:
w_init = tf.compat.v1.variance_scaling_initializer()
对于报错内容为:
- 报错内容为————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
- 2024-03-29 19:39:43.501079: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
- 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.
- Instructions for updating:
- non-resource variables are not supported in the long term
- 2024-03-29 19:39:45.268 | INFO | __main__:test_lanenet:84 - Start reading image and preprocessing
- 2024-03-29 19:39:45.279 | INFO | __main__:test_lanenet:90 - Image load complete, cost time: 0.01000s
- Traceback (most recent call last):
- File "test_lanenet.py", line 163, in <module>
- test_lanenet(args.image_path, args.weights_path)
- File "test_lanenet.py", line 95, in test_lanenet
- binary_seg_ret, instance_seg_ret = net.inference(input_tensor=input_tensor, name='LaneNet')
- File "D:\Code/CarLane/lanenet-lane-detection\lanenet_model\lanenet.py", line 37, in inference
- extract_feats_result = self._frontend.build_model(
- File "D:\Code/CarLane/lanenet-lane-detection\lanenet_model\lanenet_front_end.py", line 34, in build_model
- return self._net.build_model(
- File "D:\Code/CarLane/lanenet-lane-detection\semantic_segmentation_zoo\bisenet_v2.py", line 1045, in build_model
- detail_branch_output = self.build_detail_branch(
- File "D:\Code/CarLane/lanenet-lane-detection\semantic_segmentation_zoo\bisenet_v2.py", line 864, in build_detail_branch
- result = block_op(
- File "D:\Code/CarLane/lanenet-lane-detection\semantic_segmentation_zoo\bisenet_v2.py", line 816, in _conv_block
- result = self.conv2d(
- File "D:\Code/CarLane/lanenet-lane-detection\semantic_segmentation_zoo\cnn_basenet.py", line 62, in conv2d
- w = tf.get_variable('W', filter_shape, initializer=w_init)
- File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1577, in get_variable
- return get_variable_scope().get_variable(
- File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1320, in get_variable
- return var_store.get_variable(
- File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 576, in get_variable
- return _true_getter(
- File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 529, in _true_getter
- return self._get_single_variable(
- File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 874, in _get_single_variable
- shape = tensor_shape.as_shape(shape)
- File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 1235, in as_shape
- return TensorShape(shape)
- File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 758, in __init__
- self._dims = [Dimension(d) for d in dims]
- File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 758, in <listcomp>
- self._dims = [Dimension(d) for d in dims]
- File "D:\ProgramData\anaconda3\envs\laneNet\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 203, in __init__
- six.raise_from(
- 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 中,/
操作符执行的是浮点除法,即使两个操作数都是整数,结果也会是浮点数。为了解决这个问题,你需要使用整数除法 //
,它会返回操作数除法的整数部分,确保结果是整数类型,而不是浮点数。这在定义张量的形状时非常重要,因为形状参数必须是整数。
将相关代码行更改为:
- if isinstance(kernel_size, list):
- filter_shape = [kernel_size[0], kernel_size[1]] + [in_channel // split, out_channel]
- else:
- filter_shape = [kernel_size, kernel_size] + [in_channel // split, out_channel]
/单杠变斜杠//。通过这种方式,filter_shape
中的所有值都会是整数,这样就不会再抛出关于尝试将浮点数用作维度值的错误了。
最后成功运行代码如下:
- (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
- 2024-03-29 19:46:39.924422: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
- 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.
- Instructions for updating:
- non-resource variables are not supported in the long term
- 2024-03-29 19:46:41.588 | INFO | __main__:test_lanenet:84 - Start reading image and preprocessing
- 2024-03-29 19:46:41.599 | INFO | __main__:test_lanenet:90 - Image load complete, cost time: 0.01060s
- 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
- 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).
- warnings.warn(
-
- 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.
- warnings.warn('`layer.apply` is deprecated and '
-
- 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
- To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
- 2024-03-29 19:46:42.458132: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library nvcuda.dll
- 2024-03-29 19:46:42.536926: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties:
- pciBusID: 0000:01:00.0 name: NVIDIA GeForce RTX 3060 Laptop GPU computeCapability: 8.6
- coreClock: 1.68GHz coreCount: 30 deviceMemorySize: 6.00GiB deviceMemoryBandwidth: 312.97GiB/s
- 2024-03-29 19:46:42.537254: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
- 2024-03-29 19:46:42.592151: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
-
- 2024-03-29 19:46:42.710787: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
- 2024-03-29 19:46:42.711164: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0
- 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:
- 2024-03-29 19:46:43.531475: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1267] 0
- 2024-03-29 19:46:43.531529: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1280] 0: N
- 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)
- 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
- INFO:tensorflow:Restoring parameters from D:\Code\CarLane\lanenet-lane-detection\model\BiseNetV2_LaneNet_Tusimple_Model_Weights\tusimple_lanenet.ckpt
- 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
- 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)
- 2024-03-29 19:46:43.909649: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
- 2024-03-29 19:46:48.369187: I tensorflow/core/platform/windows/subprocess.cc:308] SubProcess ended with return code: 0
-
- 2024-03-29 19:46:48.397593: I tensorflow/core/platform/windows/subprocess.cc:308] SubProcess ended with return code: 4294967295
-
- 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:
- Relying on driver to perform ptx compilation.
- Modify $PATH to customize ptxas location.
- This message will be only logged once.
- 2024-03-29 19:46:48.506866: I tensorflow/core/platform/windows/subprocess.cc:308] SubProcess ended with return code: 4294967295
-
- 08] SubProcess ended with return code: 4294967295
-
- 2024-03-29 19:47:01.250217: I tensorflow/core/platform/windows/subprocess.cc:308] SubProcess ended with return code: 4294967295
-
-
-
- 2024-03-29 19:47:12.115481: I tensorflow/core/platform/windows/subprocess.cc:308] SubProcess ended with return code: 4294967295
-
- 2024-03-29 19:47:16.902 | INFO | __main__:test_lanenet:128 - Single imgae inference cost time: 0.06624s
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。