【问题标题】:TensorFlow: How to assign labels to image dataset when creating batches?TensorFlow:创建批次时如何为图像数据集分配标签?
【发布时间】:2017-07-11 15:08:17
【问题描述】:

是的,我搜索过 SO、Reddit、GitHub、Google Plus 等。我在 Windows 10 64 位上运行 Python 3 和 TensorFlow。我的目标是读取一堆图像并为它们分配标签以进行训练。

我正在尝试将我的标签列表转换为sess.run(train_step, feed_dict={imgs:batchX,lbls:batchY}) 的可用“对象”。我的图像导入正常,因为在此之前我调用该函数来创建批次(下面的代码)。在函数中,我可以成功创建 images numpy 数组。但是,我不知道从哪里开始分配我的标签。

我的 labels.txt 文件格式为

data/cats/cat (1) copy.png,1
data/cats/cat (2) copy.png,1
data/cats/cat (3) copy.png,1
and so on for about 300 lines

其中data/cats/cat (x) copy.png 是文件,1 是类(在本例中为 Cat)。该文件被读入一个名为labels_list 的常规数组(或列表?),每一行都是数组中的一个新元素。当我打印labels_list 时,它显示

['data/cats/cat (1) copy.png,1' 'data/cats/cat (2) copy.png,1'
 'data/cats/cat (3) copy.png,1' 'data/cats/cat (4) copy.png,1'
 'data/cats/cat (5) copy.png,1' 'data/cats/cat (6) copy.png,1'
  (alot more lines of this)
 'data/cats/cat (295) copy.png,1' 'data/cats/cat (296) copy.png,1'
 'data/cats/cat (297) copy.png,1' 'data/cats/cat (298) copy.png,1']

我不知道如何为我的 train_step 制作一个可用的 numpy 数组(代码如下)。我试过谷歌搜索,但大多数解决方案只使用带有整数的标签列表,但我需要使用文件的路径。

感谢任何帮助,谢谢:)

代码:(和我的 GitHub github.com/supamonkey2000/jm-uofa)

import tensorflow as tf
import numpy as np
import os
import sys
import cv2


content = [] # Where images are stored
labels_list = [] # Stores the image labels, still not 100% working


########## File opening function
with open("data/cats/files.txt") as ff:
    for line in ff:
        line = line.rstrip()
        content.append(line)
#################################

########## Labels opening function
with open("data/cats/labels.txt") as fff:
    for linee in fff:
        linee = linee.rstrip()
        labels_list.append(linee)
    labels_list = np.array(labels_list)
###############################


############ Function used to create batches for training
def create_batches(batch_size):
    images1 = [] # Array to hold images within the function
    for img1 in content: # Read the images from content[] in a loop
        thedata = cv2.imread(img1) # Load the image
        thedata = thedata.flatten() # Convert the image to a usable numpy array
        images1.append(thedata) # Append the image to the images1 array
    images1 = np.array(images1) # Convert images1[] to numpy array

    print(labels_list) # Debugging purposes

    while(True):
        for i in range(0,298,10):
            yield(images1[i:i+batch_size],labels_list[i:i+batch_size])
#########################################################


imgs = tf.placeholder(dtype=tf.float32,shape=[None,786432]) # Images placeholder
lbls = tf.placeholder(dtype=tf.float32,shape=[None,10]) # Labels placeholder

W = tf.Variable(tf.zeros([786432,10])) # Weights
b = tf.Variable(tf.zeros([10])) # Biases

y_ = tf.nn.softmax(tf.matmul(imgs,W) + b) # Something complicated

cross_entropy = tf.reduce_mean(-tf.reduce_sum(lbls * tf.log(y_),reduction_indices=[1])) # Cool spacey sounding thing that does cool stuff
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy) # When this is called use the GDO to train the model

sess = tf.InteractiveSession() # Setup the session
tf.global_variables_initializer().run() # Initialize the variables

############################## Training steps for teaching the model
for i in range(10000): # Run for 10,000 steps
    for (batchX,batchY) in create_batches(10): # Call a batch to be used
        sess.run(train_step, feed_dict={imgs:batchX, lbls: batchY}) # Train the model with the batch (THIS IS GIVING ME TONS OF ISSUES)
###################################################################


correct_prediction = tf.equal(tf.argmax(y_,1),tf.argmax(lbls,1)) # Find out if the program tested properly (I think?)
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) # Find the accuracy of the model

print(sess.run(accuracy, feed_dict={imgs:content, lbls:labels_list})) # Print the accuracy of the model !!! imgs:content may be incorrect, must look into it

【问题讨论】:

  • 你能解释一下为什么训练时需要文件的路径吗?并且根据您的correct_prediction,您使用一种热编码,因此您的 batchY 也应该是一种热编码值。 ps 我对 tensorflow 比较陌生,但我的网络很相似,所以我会尽力提供帮助
  • 嗯...我不知道为什么我需要路径,因为图像已经加载...我只需要将类(在本例中为 1)分配给 数组值?那可能没有意义。我是否需要更改我的 labels.txt 以显示为 0,11,1 等而不是 images 数组?感谢您的回复。
  • 我有一个和你一样的训练文件,最后有标签。我根据训练文件创建了一个单独的标签文件,其中包含每个唯一标签到我的网络的一个热编码输入的映射。标签文件https://www.imageupload.co.uk/images/2017/07/11/examplefile.png 的示例。 A 列是我的标签,B:I 列是我用作batchY 的网络标签。
  • @GertKommer “该页面不存在”。另外,我不知道如何进行一次性编码输入,因为我能找到的唯一有用的例子是 MNIST,但这对自定义数据集没有帮助 编辑:没关系找到图像
  • 好的,给我一分钟,我会尝试写一个答案而不是评论:)

标签: python arrays image numpy tensorflow


【解决方案1】:

据我了解,您有 2 个文件。
- data/cats/files.txt 其中包含文件的 url
- data/cats/labels.txt 其中还包含文件的 url 和相应的标签。

我建议只使用标签文件,因为 url 和标签在这里链接。

现在继续贴标签。

当您阅读 labels.txt 文件时,已经创建了输出标签。我在代码中添加了一些 cmets。

import re
import numpy
import cv2

label_map = [] # the map that holds the link between the label and the one_hot_encoded label
file_info = [] # holds all your file locations and the label of the file
#read all the lines. A line should look like this: mycatimage.png,1 
with open('labels.txt') as f:
    rows = [re.split(",", line.rstrip("\n")) for line in f]

for row in rows:
    file_info.append(row)

label_column = [line[1] for line in rows] # line[1] is based on your 'data/cats/cat (1) copy.png,1' example where the label is the second index

unique_labels = list(set(label_column)) # set gives unique values
# now the onehot encoding of the labels which basically means everything 0 except 1
for label in unique_labels:
    output_values = np.zeros(len(unique_labels), dtype=np.int)
    output_values [unique_labels.index(label)] = 1
    label_map.append({'name': label , 'value': output_values })


# Write the found labels to the label file if you want for later use. We will use the label_map variable for now
with open("mylabelfile.txt", 'w+') as lf:
    for label in label_map:
        lf.write(label['name'] + ',' + ','.join(str(x) for x in label['value']) + "\n")  # writes --> Christina,0,0,1,0\n

现在开始批处理功能:)

def get_one_hot_encoded_array_for_label(label):
    for existing in label_map:
        if existing['name'] == label:
            return existing['value']

def create_batches(batch_size):
    images1 = [] # Array to hold images within the function
    labels_list = []
    for img1 in file_info: # Read the images from file_info[] in a loop
        image_location = img1[0]
        image_label = img1[1]
        thedata = cv2.imread(image_location ) # Load the image
        thedata = thedata.flatten() # Convert the image to a usable numpy array
        images1.append(thedata) # Append the image to the images1 array
        outputvalues = get_one_hot_encoded_array_for_label(image_label )
        labels_list.append(outputvalues) # where we fill the labels list with the one hot encoded values.
    images1 = np.array(images1) # Convert images1[] to numpy array
    labels_list = np.array(labels_list)
    print(labels_list) # Debugging purposes

    while(True):
        for i in range(0,298,10):
            yield(images1[i:i+batch_size],labels_list[i:i+batch_size])

这应该为您的batchY 提供一个热编码值。这是基于我自己的网络编写的,但尚未使用图像数据对其进行测试。你能确认这是有效的还是告诉它在哪里坏了?如果有什么不清楚的地方请追问:)

【讨论】:

  • 代码的第 5 行在 .....rstrip("\n")) for line in row] 处引发错误。它说name 'row' is not defined
  • 好吧,我设法解决了代码中的问题(非常感谢您提供了那个顺便说一句)。现在我的错误是this
  • 好的,所以我的程序现在正在运行,我会将您的答案标记为正确。非常感谢您的帮助:)
  • 大家好,谁能告诉我是分别为训练文件和验证文件生成标签文件还是合并一个?我有 3 个数据集训练验证和测试..
猜你喜欢
  • 2021-06-08
  • 1970-01-01
  • 2020-09-23
  • 2017-11-14
  • 2017-11-08
  • 2020-08-13
  • 2018-10-31
  • 2020-12-08
  • 2021-12-20
相关资源
最近更新 更多