【发布时间】:2021-08-30 23:30:43
【问题描述】:
我只是在研究如何使用 tensorflow 2.x 版本。 但是我的代码出现错误,不知道是什么原因。
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('dataset/bird_pic_by_benjamin_planche.png', cv2.IMREAD_GRAYSCALE)
import tensorflow as tf
import tensorflow.keras as keras
img = tf.constant(img)
img = tf.reshape(img, [1, 680, 608])
img = tf.reshape(img, [1, 680, 608, 1])
parameters = [[1/16, 2/16, 1/16],
[2/16, 4/16, 2/16],
[1/16, 2/16, 1/16]]
kernel = tf.constant(parameters)
print("Kernel shape : {}".format(kernel.shape))
kernel = tf.reshape(kernel, [3, 3, 1, 1])
print("Kernel shape : {}".format(kernel.shape))
img = tf.nn.conv2d(img, kernel, strides=[1, 1, 1, 1], padding='SAME')
错误是
----------------------------------- ---------------------------- InvalidArgumentError Traceback(最近调用 最后)在 ----> 1 img = tf.nn.conv2d(img, kernel, strides=[1, 1, 1, 1], padding='SAME')
c:\users\goeun\miniconda3\envs\tf\lib\site-packages\tensorflow\python\util\dispatch.py 在包装器中(*args,**kwargs) 199 """调用目标,如果有TypeError,则返回调度程序。""" 200 次尝试: --> 201 返回目标(*args, **kwargs) 202 除外(类型错误、值错误): 203 # 注意:convert_to_eager_tensor 当前引发 ValueError,而不是
c:\users\goeun\miniconda3\envs\tf\lib\site-packages\tensorflow\python\ops\nn_ops.py 在 conv2d_v2(输入,过滤器,步幅,填充,data_format,扩张, 名称)2164 数据格式=数据格式,2165
膨胀=膨胀, -> 2166 名称=名称)2167 2168c:\users\goeun\miniconda3\envs\tf\lib\site-packages\tensorflow\python\util\dispatch.py 在包装器中(*args,**kwargs) 199 """调用目标,如果有TypeError,则返回调度程序。""" 200 次尝试: --> 201 返回目标(*args, **kwargs) 202 除外(类型错误、值错误): 203 # 注意:convert_to_eager_tensor 当前引发 ValueError,而不是
c:\users\goeun\miniconda3\envs\tf\lib\site-packages\tensorflow\python\ops\nn_ops.py 在 conv2d(输入、过滤器、步幅、填充、use_cudnn_on_gpu、 数据格式、膨胀、名称、过滤器)2272
数据格式=数据格式,2273 膨胀=膨胀, -> 2274 名称=名称)2275 返回挤压批次尺寸(2276 输入,c:\users\goeun\miniconda3\envs\tf\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py 在 conv2d(输入、过滤器、步幅、填充、use_cudnn_on_gpu、 显式填充,数据格式,膨胀,名称) 第935章 936 除了 _core._NotOkStatusException 作为 e: --> 937 _ops.raise_from_not_ok_status(e,名称) 938 除了 _core._FallbackException: 939通过
c:\users\goeun\miniconda3\envs\tf\lib\site-packages\tensorflow\python\framework\ops.py 在 raise_from_not_ok_status(e, name) 6841 message = e.message + (" name: " + name if name is not None else "") 6842 # pylint: 禁用=受保护的访问 -> 6843 Six.raise_from(core._status_to_exception(e.code, message), None) 6844 # pylint: enable=protected-access 6845
c:\users\goeun\miniconda3\envs\tf\lib\site-packages\six.py 在 raise_from(value, from_value)
InvalidArgumentError:uint8 的 attr 'T' 的值不在列表中 允许值:half、bfloat16、float、double、int32;节点定义: {{node Conv2D}};操作 输出:T; attr=T:type,allowed=[DT_HALF, DT_BFLOAT16, DT_FLOAT, DT_DOUBLE,DT_INT32]; attr=strides:list(int); attr=use_cudnn_on_gpu:bool,default=true; attr=padding:string,allowed=["SAME", "VALID", "EXPLICIT"]; attr=explicit_paddings:list(int),default=[]; attr=data_format:string,default="NHWC",allowed=["NHWC", "NCHW"]; attr=dilations:list(int),default=[1, 1, 1, 1]> [Op:Conv2D]
有什么问题..?请帮帮我..
【问题讨论】:
-
img张量的数据类型是什么? -
它的类型是
-
因此,将其转换为 float32 以供 conv2d 支持。使用
img=tf.cast(img, dtype=tf.float32)之前将其提供给图层 -
哇,非常感谢!现在可以了!
标签: python-3.x tensorflow