TensorFlow TypeError:传递给参数输入的值的数据类型为uint8不在允许值列表中:float16,float32 [英] TensorFlow TypeError: Value passed to parameter input has DataType uint8 not in list of allowed values: float16, float32

查看:1040
本文介绍了TensorFlow TypeError:传递给参数输入的值的数据类型为uint8不在允许值列表中:float16,float32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去3天里,我正在尝试获取一个简单的CNN进行训练.

I am trying to get a simple CNN to train for the past 3 days.

首先,我设置了输入管道/队列配置,该配置从目录树中读取图像并准备批处理.

First, I have setup an input pipeline/queue configuration that reads images from a directory tree and prepares batches.

我在链接中获得了此代码.因此,我现在需要输入 train_image_batch train_label_batch .

I got the code for this at this link. So, I now have train_image_batch and train_label_batch that I need to feed to my CNN.

train_image_batch, train_label_batch = tf.train.batch(
        [train_image, train_label],
        batch_size=BATCH_SIZE
        # ,num_threads=1
    )

我不知道怎么做.我正在使用此链接中提供的CNN代码

And I am unable to figure out how. I am using the code for CNN given at this link.

# Input Layer
input_layer = tf.reshape(train_image_batch, [-1, IMAGE_HEIGHT, IMAGE_WIDTH, NUM_CHANNELS])

# Convolutional Layer #1
conv1 = new_conv_layer(input_layer, NUM_CHANNELS, 5, 32, 2)

 # Pooling Layer #1
pool1 = new_pooling_layer(conv1, 2, 2)

正在打印的input_layer显示了这一点

The input_layer on printing shows this

Tensor("Reshape:0",shape =(5,120,120,3),dtype = uint8)

Tensor("Reshape:0", shape=(5, 120, 120, 3), dtype=uint8)

下一行因TypeError崩溃; conv1 = new_conv_layer(...). new_conv_layer函数的主体在下面给出

The next line crashes with TypeError; conv1 = new_conv_layer(...). The body of new_conv_layer function is given below

def new_conv_layer(input,              # The previous layer.
               num_input_channels, # Num. channels in prev. layer.
               filter_size,        # Width and height of each filter.
               num_filters,        # Number of filters.
               stride):

# Shape of the filter-weights for the convolution.
# This format is determined by the TensorFlow API.
shape = [filter_size, filter_size, num_input_channels, num_filters]

# Create new weights aka. filters with the given shape.
weights = tf.Variable(tf.truncated_normal(shape, stddev=0.05))

# Create new biases, one for each filter.
biases = tf.Variable(tf.constant(0.05, shape=[num_filters]))

# Create the TensorFlow operation for convolution.
# Note the strides are set to 1 in all dimensions.
# The first and last stride must always be 1,
# because the first is for the image-number and
# the last is for the input-channel.
# But e.g. strides=[1, 2, 2, 1] would mean that the filter
# is moved 2 pixels across the x- and y-axis of the image.
# The padding is set to 'SAME' which means the input image
# is padded with zeroes so the size of the output is the same.
layer = tf.nn.conv2d(input=input,
                     filter=weights,
                     strides=[1, stride, stride, 1],
                     padding='SAME')

# Add the biases to the results of the convolution.
# A bias-value is added to each filter-channel.
layer += biases

# Rectified Linear Unit (ReLU).
# It calculates max(x, 0) for each input pixel x.
# This adds some non-linearity to the formula and allows us
# to learn more complicated functions.
layer = tf.nn.relu(layer)

# Note that ReLU is normally executed before the pooling,
# but since relu(max_pool(x)) == max_pool(relu(x)) we can
# save 75% of the relu-operations by max-pooling first.

# We return both the resulting layer and the filter-weights
# because we will plot the weights later.
return layer, weights

精确地,它在 tf.nn.conv2d 崩溃,并显示此错误

Precisely it crashes at tf.nn.conv2d with this error

TypeError:传递给参数'input'的值的数据类型uint8不在允许的值列表中:float16,float32

TypeError: Value passed to parameter 'input' has DataType uint8 not in list of allowed values: float16, float32

推荐答案

输入管道中的图像类型为'uint8',您需要将其类型转换为'float32',您可以在图像jpeg解码器之后执行此操作:

The image from your input pipeline is of type 'uint8', you need to type cast it to 'float32', You can do this after the image jpeg decoder:

image = tf.image.decode_jpeg(...
image = tf.cast(image, tf.float32)

这篇关于TensorFlow TypeError:传递给参数输入的值的数据类型为uint8不在允许值列表中:float16,float32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆