我在Mac OS上安装了Python 3.6。我有一个文本文件,里面存储了每张图像的名称及其对应的类别编号。
#label.txt: img0001.jpg 1 img0002.jpg 3 img0003.jpg 5 img0004.jpg 10 img0005.jpg 6 img0006.jpg 8 img0007.jpg 10 .....
我想将这些标签作为输入数据的标签,同时将图像输入到tensorflow的神经网络中,像这样
xs = tf.placeholder(tf.float32,[None,#size of my photo]) ys = tf.placeholder(tf.float32,[None,#size of my label if it is an array])
我找不到任何相关文档。有人能告诉我应该怎么做吗?
回答:
假设你想知道如何将图像及其相应的标签输入到神经网络中。
这涉及到两个方面:
- 读取图像并将其转换为numpy数组。
- 将这些图像及其对应的标签输入到网络中。
正如@***所说,一旦你计算出名称和标签,就可以创建标签的一热编码。
from PIL import Imagenumber_of_batches = len(names)/ batch_sizefor i in range(number_of_batches): batch_x = names[i*batch_size:i*batch_size+batch_size] batch_y = labels[i*batch_size:i*batch_size+batch_size] batch_image_data = np.empty([batch_size, image_height, image_width, image_depth], dtype=np.int) for ix in range(len(batch_x)): f = batch_x[ix] batch_image_data[ix] = np.array(Image.open(data_dir+f)) sess.run(train_op, feed_dict={xs:batch_image_data, ys:batch_y})