2016-07-25 310 views
1

我正在尝试为CIFAR分类构建softmax回归模型。起初,当我试图将我的图像和标签传入Feed词典时,出现错误,表示Feed词典不接受张量。然后,我使用.eval()将它们转换为numpy数组,但程序挂在.eval()行,并且不再继续。我如何将这些数据传递给feed_dict?Tensorflow:将Tensor转换为numpy数组然后传递给feed_dict

CIFARIMAGELOADING.PY

import tensorflow as tf 
import os 
import tensorflow.models.image.cifar10 as cf 


IMAGE_SIZE = 24 
BATCH_SIZE = 128 


def loadimagesandlabels(size): 
    # Load the images from the CIFAR data directory 
    FLAGS = tf.app.flags.FLAGS 
    data_dir = os.path.join(FLAGS.data_dir, 'cifar-10-batches-bin') 
    filenames = [os.path.join(data_dir, 'data_batch_%d.bin' % i) for i in xrange(1, 6)] 
    filename_queue = tf.train.string_input_producer(filenames) 
    read_input = cf.cifar10_input.read_cifar10(filename_queue) 

    # Reshape and crop the image 
    height = IMAGE_SIZE 
    width = IMAGE_SIZE 
    reshaped_image = tf.cast(read_input.uint8image, tf.float32) 
    cropped_image = tf.random_crop(reshaped_image, [height, width, 3]) 

    # Generate a batch of images and labels by building up a queue of examples 
    print('Filling queue with CIFAR images') 
    num_preprocess_threads = 16 
    min_fraction_of_examples_in_queue = 0.4 
    min_queue_examples = int(BATCH_SIZE*min_fraction_of_examples_in_queue) 

    images, label_batch = tf.train.batch([cropped_image,read_input.label],batch_size=BATCH_SIZE, num_threads=num_preprocess_threads, capacity=min_queue_examples+3*BATCH_SIZE) 
    print(images) 
    print(label_batch) 
    return images, tf.reshape(label_batch, [BATCH_SIZE]) 

CIFAR.PY

#Set up placeholder vectors for image and labels 
x = tf.placeholder(tf.float32, shape = [None, 1728]) 
y_ = tf.placeholder(tf.float32, shape = [None,10]) 
W = tf.Variable(tf.zeros([1728,10])) 
b = tf.Variable(tf.zeros([10])) 


#Implement regression model. Multiply input images x by weight matrix W, add the bias b 
#Compute the softmax probabilities that are assigned to each class 
y = tf.nn.softmax(tf.matmul(x,W) + b) 

#Define cross entropy 
#tf.reduce sum sums across all classes and tf.reduce_mean takes the average over these sums 
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y), reduction_indices = [1])) 

#Train the model 
#Each training iteration we load 128 training examples. We then run the train_step operation 
#using feed_dict to replace the placeholder tensors x and y_ with the training examples 
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) 

#Open up a Session 
init = tf.initialize_all_variables() 
sess = tf.Session() 
sess.run(init) 
for i in range(1000) : 
    images, labels = CIFARImageLoading.loadimagesandlabels(size=BATCH_SIZE) 
    unrolled_images = tf.reshape(images, (1728, BATCH_SIZE)) 

    #convert labels to their one_hot representations 
    # should produce [[1,0,0,...],[0,1,0...],...] 
    one_hot_labels = tf.one_hot(indices= labels, depth=NUM_CLASSES, on_value=1.0, off_value= 0.0, axis=-1) 

    print(unrolled_images) 
    print(one_hot_labels) 
    images_numpy, labels_numpy = unrolled_images.eval(session=sess), one_hot_labels.eval(session=sess) 
    sess.run(train_step, feed_dict = {x: images_numpy, y_:labels_numpy}) 




#Evaluate the model 
#.equal returns a tensor of booleans, we want to cast these as floats and then take their mean 
#to get percent correctness (accuracy) 
print("evaluating") 
test_images, test_labels = CIFARImageLoading.loadimagesandlabels(TEST_SIZE) 
test_images_unrolled = tf.reshape(test_images, (1728, TEST_SIZE)) 
test_images_one_hot = tf.one_hot(indices= test_labels, depth=NUM_CLASSES, on_value=1.0, off_value= 0.0, axis=-1) 
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
print(accuracy.eval(feed_dict = {x: unrolled_images.eval(), y_ : test_images_one_hot.eval()})) 

回答

0

即使世界一对夫妇的事情,你不会是理解得很好。在整个图表中,您将使用张量。您通过使用tf.placeholder并将它们送入session.run(feed_dict{})tf.Variable并使用session.run(tf.initialize_all_variables())对其进行初始化来定义张量。您必须以这种方式提供您的输入,并且它应该与您在占位符中所期望的形状相同的numpy数组。这里有一个简单的例子:

images = tf.placeholder(type, [1728, BATCH_SIZE]) 
labels = tf.placeholder(type, [size]) 

''' 
    Build your network here so you have the variable: Output 
''' 

images_feed, labels_feed = CIFARImageLoading.loadimagesandlabels(size=BATCH_SIZE) 
# here you can see your output 
print sess.run(Output, feed_dict = {x: images_feed, y_:labels_feed}) 

你不numpy的阵列喂tf.functions,你总是与张量养活他们。并且feed_dict总是与numpy数组一起提供。事情是:你将永远不必将张量转换为输入的numpy数组,这是没有意义的。你的输入必须是numpy数组,如果它是一个列表,你可以使用np.asarray(list),如果它是一个张量,你做错了。

我不知道什么CIFARImageLoading.loadimagesandlabels回报给你,但我想它不是一个张量,它可能是一个numpy阵列,所以只是摆脱这个.eval()

+0

感谢米歇尔,我编辑了帖子,展示了我如何构建模型。你是说我的占位符大小不正确? – araman

+0

所以事情是:Tensor.eval()== session.run(Tensor)。所以你不能像这样真正地调用tensor.eval()。尝试: images_numpy,labels_numpy = session.run(unrolled_images,arguments),session.run(one_hot_labels,arguments) – Michel

+0

是的,您提供的numpy数组必须与您的占位符具有相同的形状,请确保“ 。 – Michel

相关问题