2016-07-25 13 views
1

Ich versuche ein Softmax-Regressionsmodell für die CIFAR-Klassifikation zu erstellen. Als ich zuerst versuchte, meine Bilder und Etiketten in das Feed-Wörterbuch einzutragen, bekam ich einen Fehler, der besagte, dass Feed-Wörterbücher keine Tensoren akzeptieren. Ich konvertierte sie dann in nummerische Arrays mit .eval(), aber das Programm hängt an der .eval() Zeile und geht nicht weiter. Wie kann ich diese Daten an das feed_dict übergeben?Tensorflow: Tensor in numpy Array konvertieren und dann in ein feed_dict übergeben

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()})) 

Antwort

0

Theres ein paar Dinge, die Sie nicht wirklich gut sind, zu verstehen. In Ihrer Grafik arbeiten Sie mit Tensoren. Sie definieren Tensors entweder mit tf.placeholder und füttern sie in session.run(feed_dict{}) oder mit tf.Variable und initialisieren sie mit session.run(tf.initialize_all_variables()). Sie müssen Ihre Eingabe auf diese Weise füttern, und es sollten numplige Arrays in derselben Form sein, wie Sie es von den Platzhaltern erwarten. Hier ist ein einfaches Beispiel:

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}) 

Sie nicht füttern tf.functions mit numpy Arrays, Sie füttern sie immer mit Tensoren. Und die feed_dict wird immer mit numpy Arrays gefüttert. Die Sache ist: Sie müssen niemals Tensoren für die Eingabe in nackte Arrays umwandeln, das macht keinen Sinn. Ihre Eingabe muss numpy Arrays sein, wenn es eine Liste ist, können Sie np.asarray(list) verwenden, wenn es ein Tensor ist, tun Sie das falsch.

Ich weiß nicht, was CIFARImageLoading.loadimagesandlabels zu Ihnen zurückkehrt, aber ich stelle mir vor, es ist kein Tensor, es ist wahrscheinlich ein numpy Array bereits, also nur diese .eval() loswerden.

+0

danke michel, ich habe den Post bearbeitet, um zu zeigen, wie ich das Modell gebaut habe. Sagen Sie, dass meine Platzhaltergrößen nicht korrekt sind? – araman

+0

Also ist die Sache: Tensor.eval() == session.run (Tensor). Sie können Tensor.eval() also nicht einfach so aufrufen. Versuchen Sie: images_numpy, labels_numpy = session.run (entrolled_images, Argumente), session.run (one_hot_labels, Argumente) – Michel

+0

Und ja, das numpy Array, das Sie füttern müssen die gleiche Form wie Ihr Platzhalter haben, stellen Sie sicher, dass das der Fall ist . – Michel

Verwandte Themen