2016-08-31 7 views
0

Ich versuche, ein CNN einzurichten, um Bilder mit zwei möglichen Ausgaben zu klassifizieren, und ich verwende Tensorflow, um dies zu tun. Ich folgte den Tutorials und ging dann zur Anpassung des CNN über, das sie an mein Problem gewöhnt hatten, aber es hat nicht sehr gut funktioniert.Zug Fehler mit CNN in Tensorflow

Die erste Sache, die ich änderte, war, wie die Bilder für mein Problem geladen wurden. Ich habe ein anderes Skript, das die Position aller Bilder zusammen mit ihrer erwarteten Ausgabe schreibt (getrennt durch ein Leerzeichen). Ich habe etwas Code verwendet, den ich an anderer Stelle gefunden habe, um die Bilder zu laden, die die Stapel ausgeben (label_batch und image_batch). Da dieses Format jedoch anders ist als das Tutorial, weiß ich nicht, wie ich die Trainingsschleife machen soll. Ich habe eine ganze Reihe von Dingen ausprobiert, angefangen bei den Chargenindizes bis hin zur Ausführung von sess.run() und habe eine Menge Dinge ausprobiert, die ich online gefunden habe, aber bis jetzt hat noch nichts geholfen.

Tut mir leid, wenn das etwas sehr einfaches ist, ich bin ziemlich neu und fange an, mich durch zu fühlen.

Mein Code:

#imports tensorflow 
import tensorflow as tf 
sess = tf.InteractiveSession() 

#weight generation with small amount of noise in normal dist & slight + bias bc ReLU nuerons 
def weight_variable(shape): 
    initial = tf.truncated_normal(shape, stddev=0.1) 
    return tf.Variable(initial) 

#bias generation with small amount of noise in normal dist & slight + bias bc ReLU nuerons 
def bias_variable(shape): 
    initial = tf.constant(0.1, shape=shape) 
    return tf.Variable(initial) 

#creates conv layer with stride 1 and 0 padding 
def conv2d(x, W): 
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 

#creates max_pool layer thats 2x2 
def max_pool_2x2(x): 
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')  

#reads image file names and respective labels 
def read_labeled_image_list(image_list_file): 
    f = open(image_list_file, 'r') 
    filenames = [] 
    labels = [] 
    for line in f: 
    filename, label = line[:-1].split(' ') 
    filenames.append(filename) 
    labels.append(int(label)) 
    return filenames, labels 

#image name to image 
def read_images_from_disk(input_queue): 
    label = input_queue[1] 
    file_contents = tf.read_file(str(input_queue[0])) 
    example = tf.image.decode_png(file_contents, channels=1) 
    return example, label 

# Reads paths of images together with their labels 
image_list, label_list = read_labeled_image_list("images.txt") 

images = tf.convert_to_tensor(image_list) 
labels = tf.convert_to_tensor(label_list) 

# Makes an input queue 
input_queue = tf.train.slice_input_producer([images, labels], shuffle=True) 

image, label = read_images_from_disk(input_queue) 
image.set_shape([28,28,1]) 

#Image and Label Batching 
image_batch, label_batch = tf.train.batch([image, label],batch_size=50, allow_smaller_final_batch = True) 

#placeholder define vars? 
x = tf.placeholder(tf.float32, shape=[None, 784]) 
y_ = tf.placeholder(tf.float32, shape=[None, 2]) 

#conv layer 1, 5x5 patch with 32 features 
W_conv1 = weight_variable([5, 5, 1, 32]) 
b_conv1 = bias_variable([32]) 

#4D tensor, 2 and 3 is w and h, 4th is color channels 
x_image = tf.reshape(x, [-1,28,28,1]) 

#sets up forward for x_image 
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) 
h_pool1 = max_pool_2x2(h_conv1) 

#second conv layer, 64 feature extraction 
W_conv2 = weight_variable([5, 5, 32, 64]) 
b_conv2 = bias_variable([64]) 
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) 
h_pool2 = max_pool_2x2(h_conv2) 

#converts from feature to 1024 
W_fc1 = weight_variable([7 * 7 * 64, 1024]) 
b_fc1 = bias_variable([1024]) 
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) 
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 

#prevents overfitting, disabled during testing (todo: potentially  remove for us, is complex) 
keep_prob = tf.placeholder(tf.float32) 
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 

#softmax layer with 10 outputs 
W_fc2 = weight_variable([1024, 2]) 
b_fc2 = bias_variable([2]) 
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) 


#training: ADAM optimizer with overfitting help and logging every 100th  iteration 
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1])) 
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) 
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
sess.run(tf.initialize_all_variables()) 

for i in range(20000): 
    #What do I do here!?!??! 
    #imgs, lbls = sess.run([image_batch, label_batch]) 
    #imgs = image_batch[i] 
    #lbls = label_batch[i] 
    if i%100 == 0: 
    train_accuracy = accuracy.eval(feed_dict={ x:imgs, y_: lbls, keep_prob: 1.0}) 
    print("step %d, training accuracy %g"%(i, train_accuracy)) 
    train_step.run(feed_dict={x: imgs, y_: lbls, keep_prob: 0.5}) 

#prints final accuracy, to be updated 
print("test accuracy %g"%accuracy.eval(feed_dict={ 
    x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})) 

Update: Tensorflow, train_step feed incorrect Ich fand dies und versuchte, die zweite Antwort zu implementieren, aber ich bekomme diese Fehlermeldung:

W tensorflow/core/framework/op_kernel.cc:936] Unimplemented: File system scheme Tensor("input_producer/Gather not implemented 

Als ich die erste Lösung versucht, ich Getes dies:

Traceback (letzten Anruf zuletzt):

File "Tester.py", line 77, in <module> 
    h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) 
    File "Tester.py", line 22, in conv2d 
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') 
    TypeError: DataType uint8 for attr 'T' not in list of allowed values: float16, float32, float64 

Update 2 So erkannte ich, x und y müssen gleich sein, die jeweils mit image_batch und label_batch und es funktionierte einmal gegossen ich es mit tf.cast (image_batch, tf.float32) float32. Jedoch jetzt die Zuglinie immer noch nicht mit diesem zweimal in Folge gedruckt:

W tensorflow/core/framework/op_kernel.cc:936] Unimplemented: File system scheme Tensor("input_producer/Gather not implemented 

Antwort

1

Guss die Eingänge zu conv2d tf.float32_ref, tf.float32

Verwandte Themen