2017-06-29 50 views
1

我正在嘗試獲取簡單的CNN來訓練過去3天。TensorFlow TypeError:傳遞給參數輸入的值具有DataType uint8不在允許值列表中:float16,float32

首先,我設置了一個輸入管道/隊列配置,從目錄樹讀取圖像並準備批處理。

我在link得到了這個代碼。所以,我現在有train_image_batchtrain_label_batch,我需要餵給我的CNN。

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

而我無法弄清楚如何。我正在使用此link給出的CNN代碼。

# 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示出了該

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

下一行與類型錯誤崩潰; conv1 = new_conv_layer(...)。 new_conv_layer函數體下方

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與此錯誤

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

回答

4

從輸入管道中的圖像類型爲「UINT8」的,則需要鍵入它轉換爲「FLOAT32」,您可以將圖像JPEG解碼器之後:

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

OMG!從來沒有人提到我在任何地方閱讀過的TF教程。非常感謝! – azmath

+0

爲什麼不使用'tf.float16'?這個大小對於8位數值就足夠了。 float32更快嗎? – Czechnology

+1

@Czechnology,你也可以使用float16。 tf.nn.conv的輸出返回與輸入相同的類型。所以在這種情況下,輸出也將是float16,這是一個精度降低,不推薦(除非你需要它的內存足跡較低,但精度較低)。 –

相關問題