2017-10-12 59 views
-1

我的代碼在這裏。我使用python 3.6.2。每個文件夾有100個圖像,例如negativa_peaton_1,negativa_peaton_2,直到negativa_peaton_100在沒有文件夾的情況下。FIFOQueue'_1_batch/fifo_queue'已關閉且元素不足(請求20,當前大小爲0)

import tensorflow as tf 
import numpy 
import numpy as np 
import math 
from PIL import Image 
from six.moves import xrange 


# config 
learning_rate = 0.01 
training_epochs = 100 
num_examples = 1000 
num_train = int(0.8*num_examples) 
num_test = int(0.2*num_examples) 

IMAGE_WIDTH = 40 
IMAGE_HEIGHT = 80 
IMAGE_DEPTH = 1 
IMAGE_PIXELS = IMAGE_WIDTH * IMAGE_HEIGHT 
NUM_CLASSES = 2 
BATCH_SIZE = 20 

# function to read image names 
def read_my_list(minId, maxId, folder): 


    filenames = [] 
    labels = [] 
    for num in range(minId, maxId+1): 

     filenames.append("/Users/RetailAdmin/Documents/Inteligencia Artificial/Python/Ejemplo" + folder + "/si/" + name_si(num) + ".jpg") 
     labels.append(int(1)) 

     filenames.append("/Users/RetailAdmin/Documents/Inteligencia Artificial/Python/Ejemplo" + folder + "/no/" + name_no(num) + ".jpg") 
     labels.append(int(0)) 

     print(num_name(num)) 

    # return list with all filenames 
    print("number of labels: " + str(len(labels))) 
    print("number of images: " + str(len(filenames))) 
    return filenames, labels 

def num_name(id): 



    ret = str(id) 
    while (len(ret) < 5): 
     ret = "0" + ret; 

    return ret; 

def name_si(id): 

    ret = str(id) 
    ret = "peaton_" + ret; 

    return ret; 

def name_no(id): 

    ret = str(id) 
    ret = "negativa_peaton_" + ret; 

    return ret; 

# read and prepare images 
def read_images_from_disk(input_queue): 

    label = input_queue[1] 
    print("read file " ) 
    file_contents = tf.read_file(input_queue[0]) 
    example = tf.image.decode_jpeg(file_contents, channels = 1) 
    print(example) 
    example = tf.image.resize_images(example,[IMAGE_HEIGHT, IMAGE_WIDTH]) 
    print(example) 
    example = tf.reshape(example, [ IMAGE_PIXELS ]) 
    print(example) 
    example.set_shape([ IMAGE_PIXELS ]) 
    print(example) 

    example = tf.cast(example, tf.float32) 
    example = tf.cast(example, tf.float32) * (1./255) - 0.5 

    label = tf.cast(label, tf.int64) 

    label = tf.one_hot(label, 2, 0, 1) 
    label = tf.cast(label, tf.float32) 

    print("file read ") 
    return example, label 

def fill_feed_dict(image_batch, label_batch, imgs, lbls): 
    feed_dict = { 
     imgs: image_batch, 
     lbls: label_batch, 
    } 
    return feed_dict 

# input images 
# None -> batch size can be any size, IMAGE_PIXELS -> image size 
x = tf.placeholder(tf.float32, shape=[None, IMAGE_PIXELS], name="x-input") 
# target 2 output classes 
y_ = tf.placeholder(tf.float32, shape=[None, NUM_CLASSES], name="y-input") 

# model parameters will change during training so we use tf.Variable 
W = tf.Variable(tf.zeros([IMAGE_PIXELS, NUM_CLASSES])) 

# bias 
b = tf.Variable(tf.zeros([NUM_CLASSES])) 

# implement model 
# y is our prediction 
y = tf.nn.softmax(tf.matmul(x,W) + b) 

# specify cost function 
# this is our cost --> y is the net output, y_ is the target 
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) 

# Accuracy --> y is the net output, y_ is the target 
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 

# specify optimizer 
# optimizer is an "operation" which we can execute in a session 
train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy) 

# DATA FOR TRAINING 
# get filelist and labels for training (num_train/2 examples of each class) 
image_list, label_list = read_my_list(1, int(num_train/2), "train") 

# create queue for training 
input_queue = tf.train.slice_input_producer([ image_list, label_list ]) 

# read files for training 
image, label = read_images_from_disk(input_queue) 

# `image_batch` and `label_batch` represent the "next" batch 
# read from the input queue. 
image_batch, label_batch = tf.train.batch([ image, label ], batch_size = BATCH_SIZE) 

# DATA FOR TESTING 
# get filelist and labels for tESTING 
image_list_test, label_list_test = read_my_list(int(num_train/2)+1, int(num_examples/2), "train") 

# create queue for training 
input_queue_test = tf.train.slice_input_producer([ image_list_test, label_list_test ]) 

# read files for training 
image_test, label_test = read_images_from_disk(input_queue_test) 

# read from the input queue. 
image_batch_test, label_batch_test = tf.train.batch([ image_test, label_test ], batch_size = num_test) 

with tf.Session() as sess: 
    # variables need to be initialized before we can use them 
    sess.run(tf.local_variables_initializer()) 

    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(coord=coord) 

    # perform training cycles 
    for epoch in range(training_epochs): 

     # number of batches in one epoch 
     batch_count = int(num_train/BATCH_SIZE) 

     for i in range(batch_count): 

      imgs, lbls = sess.run([image_batch, label_batch]) 

      sess.run([train_op], feed_dict={x:imgs, y_:lbls}) 

     print("Epoch: ", epoch) 
     imgs_test, lbls_test = sess.run([image_batch_test, label_batch_test]) 
     print ("Accuracy: ", accuracy.eval(feed_dict={x: imgs_test , y_: lbls_test})) 
    print ("done") 
    coord.request_stop() 
    coord.join(threads) 

我得到這個probrem

2017年10月12日00:25:19.457738:WC:\ tf_jenkins \家庭\工作區\ REL-WIN \中號\ WINDOWS \ PY \ 36 \ tensorflow \核心\ platform \ cpu_feature_guard.cc:45] TensorFlow庫沒有被編譯爲使用AVX指令,但是這些指令在您的機器上可用並且可以加速CPU計算。 TensorFlow圖書館wasn''tensorFlow圖書館wasn''tensorFlow圖書館wasn''tensorFlow圖書館wasn''tensorFlow圖書館wasn'編譯爲使用AVX2指令,但這些指令可在您的機器上使用,並可加速CPU計算。 2017-10-12 00:25:19.806878 WC:\ tf_jenkins \ home \ workspace \ rel-win \ M \ windows \ PY \ 36 \ tensorflow \ core \ kernels \ queue_base.cc:295] _3_batch_1/fifo_queue :跳過隊列未關閉的取消入隊嘗試 2017-10-12 00:25:19.807235:WC:\ tf_jenkins \ home \ workspace \ rel-win \ M \ windows \ PY \ 36 \ tensorflow \ core \ kernels \ queue_base。 cc:295] _2_input_producer_1/input_producer:跳過隊列未關閉的取消入隊嘗試 2017-10-12 00:25:19.811144:WC:\ tf_jenkins \ home \ workspace \ rel-win \ M \ windows \ PY \ 36 \ tensorflow \ core \ kernels \ queue_base.cc:295] _0_input_producer/input_producer:跳過隊列未關閉的取消排隊嘗試 回溯(最近調用最後一個): 文件「C:\ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ client \ session.py「,第1327行,在_do_call return fn(* args) 文件「C:\ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ client \ session.py」,行1306,在_run_fn中 status,run_metadata) 文件「C: \ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ framework \目錄下的\ Program Files \ Python36 \ lib \ contextlib.py「,第88行,在退出 下一個(self.gen) errors_impl.py」,線路466,在raise_exception_on_not_ok_status pywrap_tensorflow.TF_GetCode(狀態)) tensorflow.python.framework.errors_impl.FailedPreconditionError:試圖使用未初始化值Variable_1 [[節點:Variable_1 /讀取= IdentityT = DT_FLOAT,_class = [「loc:@ Variable_1」],_device =「/ job:localhost/replica:0/task:0/cpu:0」]]

在處理上述例外的,另一個異常:

回溯(最後最近一次調用): 文件 「NeuralNet_L1.py」,線路176,在 sess.run([train_op],feed_dict = {x:imgs,y_:lbls}) 運行中的文件「C:\ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ client \ session.py」,行895,運行 run_metadata_ptr) 文件「C \ Program Files \ Python36 \ lib \ site \ Program Files \ Python36 \ lib \ site-packages \ -packages \ tensorflow \ python的\客戶\會話.py「,第1321行,在_do_run 選項,run_metadata) 文件」C:\ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ client \ session.py「,行1340,_do_call raise type (e)(node_def,op,message) tensorflow.python.framework.errors_impl。FailedPreconditionError:試圖使用未初始化的值Variable_1 [[Node:Variable_1/read = IdentityT = DT_FLOAT,_class = [「loc:@ Variable_1」],_device =「/ job:localhost/replica:0/task:0/cpu: 0" ]]

引起OP 'Variable_1 /讀',在定義: 文件 「NeuralNet_L1.py」,線路113,在 b = tf.Variable(tf.zeros([NUM_CLASSES])) 文件「C:\ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ ops \ variables.py」,第199行,在init expected_shape = expected_shape) 文件「C:\ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ ops \ variables.py「,第330行,位於_init_from_args中 self._snapshot = array_ops.identity(self._variable,name =「read」) 文件「C:\ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ ops \ gen_array_ops.py」,第1400行,標識爲 result = _op_def_lib.apply_op(「Identity」,input = input,name = name) 文件「C:\ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ framework \ op_def_library.py」,第767行,在apply_op op_def = op_def) 文件「C:\ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ framework \ ops.py」,第2630行,在create_op中 original_op = self._default_original_op,op_def = op_def) File 「C:\ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ framework \ ops.py」,行1204,在init self._traceback = self._graph._extract_stack() #pylint的:禁用=受保護的訪問

FailedPreconditionError(參見上述用於回溯):試圖使用未初始化值Variable_1 [[節點:Variable_1 /讀取= IdentityT = DT_FLOAT,_class = [ 「LOC:@ Variable_1」], _device = 「/職業:本地主機/副本:0 /任務:0/CPU:0」]]

回答

0

該錯誤消息表示了問題的原因:

2017-10-11 22:52:23.533465: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\framework\op_kernel.cc:1192] Not found: NewRandomAccessFile failed to Create/Open: /Users/RetailAdmin/Documents/Inteligencia Artificial/Python/Ejemplotrain/si/peaton_457.jpg : El sistema no puede encontrar la ruta especificada.

沒有名爲"/Users/RetailAdmin/Documents/Inteligencia Artificial/Python/Ejemplotrain/si/peaton_457.jpg"文件。據猜測,我會說路徑構造不正確,應該是"/Users/RetailAdmin/Documents/Inteligencia Artificial/Python/Ejemplo/train/si/peaton_457.jpg"(在Ejemplotrain之間的/)。

爲了避免這種問題,可以利用os.path.join()而不是字符串連接到構建路徑:

for num in range(minId, maxId+1): 

    filenames.append(os.path.join(
     "/Users/RetailAdmin/Documents/Inteligencia Artificial/Python/Ejemplo", 
     folder, "si", name_si(num) + ".jpg") 
    labels.append(int(1)) 

    filenames.append(
     "/Users/RetailAdmin/Documents/Inteligencia Artificial/Python/Ejemplo", 
     folder, "no", name_no(num) + ".jpg") 
    labels.append(int(0)) 

    print(num_name(num)) 
+0

我固定的路徑和錯誤不斷出現 –

+0

你可以更新新的完整的錯誤消息的問題? – mrry

+0

我剛剛更新了 –

相關問題