Tensorflow 错误 "shape Tensorshape() must have rank 1"
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
但是当我看到示例 here 时,图像仅采用 [batch_size,height,width,depth] 张量的形式。这个例子工作正常。
我错过了什么吗?
我认为错误来自这一行:
cross_entropy= -tf.reduce_sum(tf.cast(image_batch[1],tf.float32)*tf.log(y_conv))
image_batch
是一个 5 维张量,形状为 [2, 1, 32, 32, 3]
,其中 2 是 tf.train.batch()
的 batch_size
参数,而 1 是前面添加的image = tf.reshape(image, [-1, 32, 32, 3])
。 (N.B. 这种重塑是不必要的,因为 tf.train.batch()
已经添加了一个批次维度,你最终不得不 undo 稍后构建 image_4d
).
时重塑的效果
在 TensorFlow 中,切片操作(即 image_batch[1]
)的灵活性略低于 NumPy。切片中指定的维度数必须等于张量的等级:即您必须指定所有五个维度才能工作。您可以指定 image_batch[1, :, :, :, :]
以获得 image_batch
.
的 4-D 切片
不过,我注意到您的程序中还有一些其他问题:
cross_entropy
的计算看起来很奇怪。通常这会使用预测标签并将其与已知的 标签 进行比较,而不是图像数据。
训练步骤的提要似乎没有效果,因为占位符 x
和 y_
在您的程序中未使用。此外,您似乎正在提供 tf.Tensor
(实际上是 image_batch
的非法切片),因此当您执行该语句时,这将失败。如果您打算使用馈送,您应该馈送保存输入数据的 NumPy 数组。
如果您不使用喂食——即使用程序中显示的 tf.WholeFileReader
——您需要调用 tf.train.start_queue_runners()
才能开始。否则你的程序会挂起,等待输入。
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
但是当我看到示例 here 时,图像仅采用 [batch_size,height,width,depth] 张量的形式。这个例子工作正常。 我错过了什么吗?
我认为错误来自这一行:
cross_entropy= -tf.reduce_sum(tf.cast(image_batch[1],tf.float32)*tf.log(y_conv))
image_batch
是一个 5 维张量,形状为 [2, 1, 32, 32, 3]
,其中 2 是 tf.train.batch()
的 batch_size
参数,而 1 是前面添加的image = tf.reshape(image, [-1, 32, 32, 3])
。 (N.B. 这种重塑是不必要的,因为 tf.train.batch()
已经添加了一个批次维度,你最终不得不 undo 稍后构建 image_4d
).
在 TensorFlow 中,切片操作(即 image_batch[1]
)的灵活性略低于 NumPy。切片中指定的维度数必须等于张量的等级:即您必须指定所有五个维度才能工作。您可以指定 image_batch[1, :, :, :, :]
以获得 image_batch
.
不过,我注意到您的程序中还有一些其他问题:
cross_entropy
的计算看起来很奇怪。通常这会使用预测标签并将其与已知的 标签 进行比较,而不是图像数据。训练步骤的提要似乎没有效果,因为占位符
x
和y_
在您的程序中未使用。此外,您似乎正在提供tf.Tensor
(实际上是image_batch
的非法切片),因此当您执行该语句时,这将失败。如果您打算使用馈送,您应该馈送保存输入数据的 NumPy 数组。如果您不使用喂食——即使用程序中显示的
tf.WholeFileReader
——您需要调用tf.train.start_queue_runners()
才能开始。否则你的程序会挂起,等待输入。