在张量流中加载自定义图像

问题描述 投票:0回答:1

Im new to tensorflow and python and I created a feed forward neural network with tensorflow that will help me to classify two groups of images. One group represents images of myself and another group represents images of a different person (I know convolutional network is better for this kind of problem but for the sake of learning I approached the FF network). All my images are stored in two separate directories. Im试图加载图像进行训练并将它们输入NN。我的图像是272x272px RGB,所以输入层应该有73984个神经元。我无法加载图像并通过网络提供它们。

我尝试过使用这种方法:

filename_queue = tf.train.string_input_producer(tf.train.match_filenames_once("images/train/resized/*.jpg"))

reader = tf.WholeFileReader()
filename, content = reader.read(filename_queue)
image = tf.image.decode_jpeg(content, channels=3) 
image = tf.cast(image, tf.float32) 
resized_image = tf.image.resize_images(image, [272, 272]) 

然后,当我跑:

sess.run([optimizer], feed_dict={x: resized_image, y: 1})

我gen“feed的值不能是tf.Tensor对象”错误

有没有更好的方法去做,或者我在这里缺少什么?谢谢!

python image networking tensorflow
1个回答
1
投票

你的resized_image变量是一个张量,因为你把它初始化为tf.image.resize_images(image, [272, 272]) ...你的feed必须是一个numpy数组,其形状必须与你在代码中定义的张量x匹配例如,如果在你的情况下x = tf.placeholder(tf.float32, (None, 272, 272, 3)) 然后你有给它的形象必须是形状的bacth(bacth_number,272,272,3)

我建议您使用以下代码来读取图像...以此代码为例

import matplotlib.image as mpimg
image = mpimg.imread(path_to_the_image)
x = tf.placeholder(tf.float32, (None, 272, 272, 3))

并在会话中运行时:

sess.run([optimizer], feed_dict={x: image.reshape((1, image.shape[0], image.shape[1], image.shape[2])), y: 1})
© www.soinside.com 2019 - 2024. All rights reserved.