【发布时间】:2017-10-12 04:03:22
【问题描述】:
我的代码在这里。我正在使用 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: W C:\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: W C:\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: W C:\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: W C:\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: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\kernels\queue_base.cc:295] _0_input_producer/input_producer:跳过已取消队列未关闭的入队尝试 回溯(最近一次通话最后): _do_call 中的文件“C:\Program Files\Python36\lib\site-packages\tensorflow\python\client\session.py”,第 1327 行 返回 fn(*args) _run_fn 中的文件“C:\Program Files\Python36\lib\site-packages\tensorflow\python\client\session.py”,第 1306 行 状态,运行元数据) 退出中的文件“C:\Program Files\Python36\lib\contextlib.py”,第 88 行 下一个(self.gen) 文件“C:\Program Files\Python36\lib\site-packages\tensorflow\python\framework\errors_impl.py”,第 466 行,在 raise_exception_on_not_ok_status pywrap_tensorflow.TF_GetCode(状态)) tensorflow.python.framework.errors_impl.FailedPreconditionError:尝试使用未初始化的值Variable_1 [[节点:Variable_1/read = IdentityT=DT_FLOAT, _class=["loc:@Variable_1"], _device="/job:localhost/replica:0/task:0/cpu:0"]]
在处理上述异常的过程中,又发生了一个异常:
Traceback(最近一次调用最后一次): 文件“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) _run 中的文件“C:\Program Files\Python36\lib\site-packages\tensorflow\python\client\session.py”,第 1124 行 feed_dict_tensor、选项、run_metadata) _do_run 中的文件“C:\Program Files\Python36\lib\site-packages\tensorflow\python\client\session.py”,第 1321 行 选项,run_metadata) _do_call 中的文件“C:\Program Files\Python36\lib\site-packages\tensorflow\python\client\session.py”,第 1340 行 raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.FailedPreconditionError:尝试使用未初始化的值Variable_1 [[节点:Variable_1/read = IdentityT=DT_FLOAT, _class=["loc:@Variable_1"], _device="/job:localhost/replica:0/task:0/cpu:0"]]
由操作“Variable_1/read”引起,定义在: 文件“NeuralNet_L1.py”,第 113 行,在 b = tf.Variable(tf.zeros([NUM_CLASSES])) init 中的文件“C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\variables.py”,第 199 行 预期形状=预期形状) _init_from_args 中的文件“C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\variables.py”,第 330 行 self._snapshot = array_ops.identity(self._variable, name="read") 文件“C:\Program Files\Python36\lib\site-packages\tensorflow\python\ops\gen_array_ops.py”,第 1400 行,身份 结果 = _op_def_lib.apply_op(“身份”,输入=输入,名称=名称) 文件“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) init 中的文件“C:\Program Files\Python36\lib\site-packages\tensorflow\python\framework\ops.py”,第 1204 行 self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
FailedPreconditionError(参见上面的回溯):尝试使用未初始化的值 Variable_1 [[节点:Variable_1/read = IdentityT=DT_FLOAT, _class=["loc:@Variable_1"], _device="/job:localhost/replica:0/task:0/cpu:0"]]
【问题讨论】:
标签: python python-3.x tensorflow