[英]Tensorflow error “shape Tensorshape() must have rank 1”
import tensorflow as tf
import numpy as np
import os
from PIL import Image
cur_dir = os.getcwd()
def modify_image(image):
#resized = tf.image.resize_images(image, 180, 180, 3)
image.set_shape([32,32,3])
flipped_images = tf.image.flip_up_down(image)
return flipped_images
def read_image(filename_queue):
reader = tf.WholeFileReader()
key,value = reader.read(filename_queue)
image = tf.image.decode_jpeg(value)
return key,image
def inputs():
filenames = ['standard_1.jpg', 'standard_2.jpg' ]
filename_queue = tf.train.string_input_producer(filenames)
filename,read_input = read_image(filename_queue)
reshaped_image = modify_image(read_input)
reshaped_image = tf.cast(reshaped_image, tf.float32)
label=tf.constant([1])
return reshaped_image,label
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
x = tf.placeholder(tf.float32, shape=[None,32,32,3])
y_ = tf.placeholder(tf.float32, shape=[None, 1])
image,label=inputs()
image=tf.reshape(image,[-1,32,32,3])
label=tf.reshape(label,[-1,1])
image_batch=tf.train.batch([image],batch_size=2)
label_batch=tf.train.batch([label],batch_size=2)
W_conv1 = weight_variable([5, 5, 3, 32])
b_conv1 = bias_variable([32])
image_4d=x_image = tf.reshape(image, [-1,32,32,3])
h_conv1 = tf.nn.relu(conv2d(image_4d, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_fc1 = weight_variable([8 * 8 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 8*8*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024, 2])
b_fc2 = bias_variable([2])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
cross_entropy= -tf.reduce_sum(tf.cast(image_batch[1],tf.float32)*tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(20000):
sess.run(train_step,feed_dict={x:image_batch[0:1],y_:label_batch[0:1]})
我試圖在我自己的[32x32x3]圖像維度圖像上運行張量流卷積模型。 在訓練期間,圖像被正確讀取並被分配給占位符。 在運行train_step操作期間出現問題。 當我執行圖表時,我收到以下錯誤。
TensorShape([Dimension(2), Dimension(1), Dimension(32), Dimension(32), Dimension(3)]) must have rank 1
但是當我在這里看到示例時,圖像僅以[batch_size,height,width,depth]張量的形式出現。 這個例子很好用。 我錯過了什么嗎?
我認為錯誤來自這條線:
cross_entropy= -tf.reduce_sum(tf.cast(image_batch[1],tf.float32)*tf.log(y_conv))
image_batch
是一個5-D張量,形狀為[2, 1, 32, 32, 3]
tf.train.batch()
[2, 1, 32, 32, 3]
,其中2是tf.train.batch()
的batch_size
參數,1是由早期的image = tf.reshape(image, [-1, 32, 32, 3])
。 ( 注意,這種重塑是不必要的,因為tf.train.batch()
已經添加了一個批量維度,並且在以后構造image_4d
時最終必須撤消重塑的效果)。
在TensorFlow中,切片操作(即image_batch[1]
)的靈活性略低於NumPy。 切片中指定的維數必須等於張量的等級:即,您必須指定所有五個維度才能使其起作用。 您可以指定image_batch[1, :, :, :, :]
來獲取image_batch
的4-D切片。
我注意到你的程序中還有一些其他問題:
cross_entropy
計算似乎很奇怪。 通常,這使用預測標簽並將其與已知標簽進行比較,而不是圖像數據。
訓練步驟中的Feed似乎沒有效果,因為占位符x
和y_
在程序中未使用。 此外,你似乎是喂tf.Tensor
(實際上,非法片image_batch
),所以當你執行該語句這將失敗。 如果您打算使用喂食,您應該輸入包含輸入數據的NumPy數組。
如果您沒有使用tf.WholeFileReader
即使用程序中顯示的tf.WholeFileReader
- 您需要調用tf.train.start_queue_runners()
才能開始使用。 否則你的程序會掛起,等待輸入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.