如何在图像数据集中添加标签进行分类?

2024-06-29 01:11:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用MacOS上安装的Python3.6。我有一个文本文件,它存储图像的名称和每个图像的类号。

     #label.txt:
     img0001.jpg  1
     img0002.jpg  3
     img0003.jpg  5
     img0004.jpg  10
     img0005.jpg  6
     img0006.jpg  8
     img0007.jpg  10
     .....

我想把它们作为输入数据的标签,在中交给我的神经网络,同时像这样把图像交给网络

    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])

我找不到任何相关文件。有人能告诉我,为了这个高兴我该怎么办吗?


Tags: of图像txt名称nonesizemytf
2条回答

假设你想知道,如何将图像及其各自的标签输入神经网络。

有两件事:

  1. 读取图像并转换numpy数组中的图像。
  2. 将同一标签及其对应标签送入网络。

正如Thomas Pinetz所说,一旦计算出名称和标签。创建一个标签的热编码。

from PIL import Image
number_of_batches = len(names)/ batch_size
for 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})

可以使用streight forward python i/o实用程序(https://docs.python.org/3/tutorial/inputoutput.html),如:

names = []
labels = []
with open('label.txt', 'r') as f:
    for line in f.readlines():
       tokens = line.split(' ')
       names.append(tokens[0])
       labels.append(int(tokens[1]))

然后可以使用names数组作为y数组加载到图像和标签中。

相关问题 更多 >