how to add label to image data set for classification?

∥☆過路亽.° 提交于 2020-05-14 18:24:05

问题


I am using python 3.6 installed on mac os. I have text file that store name of image and the class number of every single image on.

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

I want to give them to my neural network in tensorflow as label of input data and give the image to network in same time like this

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

I cannot find any related documentation. could some one tell me what should i do for this pleased ?


回答1:


Assuming that you wanted to know, how to feed image and its respective label into neural network.

There are two things:

  1. Reading the images and converting those in numpy array.
  2. Feeding the same and its corresponding label into network.

As said by Thomas Pinetz , once you calculated names and labels. Create one hot encoding of labels.

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:


You can use streight forward python i/o utilities (https://docs.python.org/3/tutorial/inputoutput.html) like:

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

Then you can use the names array to load in the images and the labels as your y array.



来源:https://stackoverflow.com/questions/41612057/how-to-add-label-to-image-data-set-for-classification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!