我的代码在这里。我正在使用python 3.6.2。每个文件夹都有100个图像,例如negativa_peaton_1,negativa_peaton_2,无文件夹情况下为negativa_peaton_100。
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)
我得到了这个问题
2017-10-12 00:25:19.457738:WC:\ tf_jenkins \ home \ workspace \ rel-win \ M \ windows \ PY \ 36 \ tensorflow \ core \ platform \ cpu_feature_guard.cc:45] TensorFlow库未编译为使用AVX指令,但这些指令可在您的计算机上使用,并可加速CPU计算。 2017-10-12 00:25:19.457845:WC:\ tf_jenkins \ home \ workspace \ rel-win \ M \ windows \ PY \ 36 \ tensorflow \ core \ platform \ cpu_feature_guard.cc:45] TensorFlow库不是编译为使用AVX2指令,但这些指令可在您的机器上使用,并可加速CPU计算。 8 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:跳过已取消队列未关闭的队列排队 Traceback(最近一次调用最后一次): 文件“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 \ contextlib.py”,第88行,退出 下一个(self.gen) 在raise_exception_on_not_ok_status中输入文件“C:\ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ framework \ errors_impl.py”,第466行 pywrap_tensorflow.TF_GetCode(状态)) 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”]]
在处理上述异常期间,发生了另一个异常:
追踪(最近一次通话): 文件“NeuralNet_L1.py”,第176行,in 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-packages \ tensorflow \ python \ client \ session.py”,第1124行,在_run中 feed_dict_tensor,options,run_metadata) 文件“C:\ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ client \ session.py”,第1321行,在_do_run中 选项,run_metadata) 在_do_call中输入文件“C:\ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ client \ session.py”,第1340行 提升类型(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 / read'引起,定义于: 文件“NeuralNet_L1.py”,第113行,in 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) 在apply_op中的文件“C:\ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ framework \ op_def_library.py”,第767行 op_def = op_def) 在create_op中的文件“C:\ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ framework \ ops.py”,第2630行 original_op = self._default_original_op,op_def = op_def) 文件“C:\ Program Files \ Python36 \ lib \ site-packages \ tensorflow \ python \ framework \ ops.py”,第1204行, init self._traceback = self._graph._extract_stack()#pylint:disable = protected-access
FailedPreconditionError(参见上面的回溯):尝试使用未初始化的值Variable_1 [[Node:Variable_1 / read = IdentityT = DT_FLOAT,_ class = [“loc:@ Variable_1”],_ device =“/ job:localhost / replica:0 / task:0 / cpu:0”]]
答案 0 :(得分:0)
错误消息表明问题的原因:
2017-10-11 22:52:23.533465:WC:\ tf_jenkins \ home \ workspace \ rel-win \ M \ windows \ PY \ 36 \ tensorflow \ core \ framework \ op_kernel.cc:1192]未找到: NewRandomAccessFile无法创建/打开:/ 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"
(/
和Ejemplo
之间train
。
要避免此类问题,请使用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))