2016-08-31 7 views
0

2つの可能な出力を持つ画像を分類するためにCNNを設定しようとしていますが、これを行うにはTensorflowを使用しています。私はチュートリアルに続いて、自分の使っていたCNNを自分の問題に適応させましたが、うまくいきませんでした。TensorflowでのCNNの列車エラー

私が変更した最初のことは、問題の画像がどのように読み込まれたかということでした。私は、すべての画像の場所と期待される出力をスペースで区切って書きます。私はバッチ(label_batchとimage_batch)を与える画像にロードするために他の場所で見つけたコードを使用しました。ただし、この形式はチュートリアルとは異なりますので、私はトレーニングループをどのように行うのか分かりません。バッチのインデックスを取り込んでsess.run()を実行し、オンラインで見つけたものを試してみましたが、これまで何も手伝っていませんでした。

申し訳ありませんこれは非常にシンプルなものですが、私はこれをかなり新しくしています。

マイコード:

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

を更新: Tensorflow, train_step feed incorrect 私はこれを発見し、第二の答えを実装しようとしましたが、私はこのエラーを取得:

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

私は最初の解決策を試したとき、Iこれを持っています:

トレースバック(最新のコール最後):

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 

アップデート2 はだからは、xを実現し、yはそれぞれimage_batchとlabel_batchに等しくなければならず、それは私がtf.cast(image_batch、tf.float32)を使用してのfloat32にそれをキャスト一度働きました。これが2回続けて印刷してしかし、今の列車ラインがまだ失敗:

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

答えて

1

がtf.float32_refするconv2dの入力をキャストし、tf.float32

関連する問題