是的,我搜索了SO,Reddit,GitHub,Google Plus等等。我在Windows 10 64位上运行TensorFlow,运行Python 3。我的目标是阅读一堆图像并为他们分配标签进行培训。TensorFlow:如何在创建批次时将标签分配给图像数据集?
我想把我的标签列表变成一个可用的“对象”sess.run(train_step, feed_dict={imgs:batchX,lbls:batchY})
。我的图像导入正常,因为在此之前我称这个函数为创建批次(下面的代码)。在函数中,我可以成功创建图像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
是类(在这种情况下,猫)。该文件被读入一个名为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的阵列。我试过Google搜索,但大多数解决方案只使用整数标签列表,但我需要使用文件的路径。
赞赏任何帮助,谢谢:)
代码:(和我的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,1”,“1,1”等,而不是图像数组?感谢回复。 –
我有一个像你一样的培训文件,最后是标签。我有一个单独的labelfile,我根据培训文件创建,其中包含每个唯一标签映射到我的网络的一个热编码输入。例如labelfile:https:// www.imageupload.co.uk/images/2017/07/11/examplefile.png'。 列'A'是我的标签,列'B:I'是我用作'batchY'的网络标签。 –