2017-07-11 24 views
0

是的,我搜索了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 
+0

你能解釋爲什麼在訓練時需要文件路徑嗎?根據你的'correct_prediction'你使用一個熱門編碼,所以你的batchY應該是一個熱門的編碼值。 ps我對tensorflow比較陌生,但是我的網絡很相似,所以我會盡力幫助 –

+0

嗯...我不知道爲什麼我需要路徑,因爲圖像已經加載了......我只需要分配該類(在這種情況下'1')的*數組*值?這可能沒有道理。我是否需要將labels.txt更改爲「0,1」,「1,1」等,而不是圖像數組?感謝回覆。 –

+0

我有一個像你一樣的培訓文件,最後是標籤。我有一個單獨的labelfile,我根據培訓文件創建,其中包含每個唯一標籤映射到我的網絡的一個熱編碼輸入。例如labelfile:https:// www.imageupload.co.uk/images/2017/07/11/examplefile.png'。 列'A'是我的標籤,列'B:I'是我用作'batchY'的網絡標籤。 –

回答

1

就一個,因爲我知道你有2個文件。
- data/cats/files.txt其中包含您的文件的URL
- data/cats/labels.txt其中還包含您的文件的URL和相應的標籤。

我建議只使用標籤文件,因爲URL和標籤鏈接在這裏。

現在繼續進行標籤。

當您讀取labels.txt文件時,已經創建了輸出標籤。我在代碼中添加了一些註釋。

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。這是基於我自己的網絡編寫的,但沒有用圖像數據進行測試。你能證實這是行之有效的,還是告訴它突破的地方?如果有什麼不清楚的地方,請詢問:)

+0

您的代碼的第五行在'..... rstrip(「\ n」))在行中引發錯誤]'。它說'名字'行'沒有定義'。 –

+0

好吧,我設法解決了代碼中的扭結(非常感謝提供這個順便說一句)。現在我的錯誤是[這](https://ibb.co/cUfxfF) –

+0

好,所以我的程序現在正在工作,我會標記你的答案是正確的。非常感謝你的幫助 :) –

相關問題