【问题标题】:how to add label to image data set for classification?如何为图像数据集添加标签以进行分类?
【发布时间】:2017-01-12 11:27:16
【问题描述】:

我正在使用安装在 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
     .....

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

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

我找不到任何相关文档。有人能告诉我我该怎么做才能高兴吗?

【问题讨论】:

    标签: tensorflow python machine-learning tensorflow deep-learning


    【解决方案1】:

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

    有两件事:

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

    【讨论】:

      【解决方案2】:

      您可以使用直接的 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]))
      

      然后您可以使用名称数组加载图像和标签作为您的 y 数组。

      【讨论】:

        猜你喜欢
        • 2017-11-08
        • 2020-10-01
        • 2021-08-13
        • 1970-01-01
        • 1970-01-01
        • 2021-05-18
        • 2015-01-27
        • 2021-05-01
        • 2020-05-05
        相关资源
        最近更新 更多