GoeunLee
GoeunLee

Reputation: 23

tf.nn.conv2d occurs InvalidArgumentError: Value for attr 'T' of uint8 is not in the list of allowed values

I'm just studying how to use tensorflow 2.x version. But my code occurs error, and I don't know the reason.

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')

error is

--------------------------------------------------------------------------- InvalidArgumentError Traceback (most recent call last) in ----> 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 in wrapper(*args, **kwargs) 199 """Call target, and fall back on dispatchers if there is a TypeError.""" 200 try: --> 201 return target(*args, **kwargs) 202 except (TypeError, ValueError): 203 # Note: convert_to_eager_tensor currently raises a ValueError, not a

c:\users\goeun\miniconda3\envs\tf\lib\site-packages\tensorflow\python\ops\nn_ops.py in conv2d_v2(input, filters, strides, padding, data_format, dilations, name) 2164 data_format=data_format, 2165
dilations=dilations, -> 2166 name=name) 2167 2168

c:\users\goeun\miniconda3\envs\tf\lib\site-packages\tensorflow\python\util\dispatch.py in wrapper(*args, **kwargs) 199 """Call target, and fall back on dispatchers if there is a TypeError.""" 200 try: --> 201 return target(*args, **kwargs) 202 except (TypeError, ValueError): 203 # Note: convert_to_eager_tensor currently raises a ValueError, not a

c:\users\goeun\miniconda3\envs\tf\lib\site-packages\tensorflow\python\ops\nn_ops.py in conv2d(input, filter, strides, padding, use_cudnn_on_gpu, data_format, dilations, name, filters) 2272
data_format=data_format, 2273 dilations=dilations, -> 2274 name=name) 2275 return squeeze_batch_dims( 2276 input,

c:\users\goeun\miniconda3\envs\tf\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py in conv2d(input, filter, strides, padding, use_cudnn_on_gpu, explicit_paddings, data_format, dilations, name) 935 return _result 936 except _core._NotOkStatusException as e: --> 937 _ops.raise_from_not_ok_status(e, name) 938 except _core._FallbackException: 939 pass

c:\users\goeun\miniconda3\envs\tf\lib\site-packages\tensorflow\python\framework\ops.py in raise_from_not_ok_status(e, name) 6841 message = e.message + (" name: " + name if name is not None else "") 6842 # pylint: disable=protected-access -> 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 in raise_from(value, from_value)

InvalidArgumentError: Value for attr 'T' of uint8 is not in the list of allowed values: half, bfloat16, float, double, int32 ; NodeDef: {{node Conv2D}}; Op<name=Conv2D; signature=input:T, filter:T -> output: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]

what is the problem..? Please help me..

Upvotes: 1

Views: 843

Answers (1)

Kaveh
Kaveh

Reputation: 4980

You should convert your input tensor to a one of supported data type of tf.nn.conv2d. Based on the doc here, the supported types are half, bfloat16, float32, float64.

Your input tensor (img) is in uint8 format, and is not supported as input data type. So, just convert your tensor before feeding it to the layer:

img = tf.cast(img, dtype=tf.float32)

Upvotes: 1

Related Questions