我正在尝试设置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
当我尝试第一个解决方案时,我得到了这个:
追踪(最近一次呼叫最后一次):
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就可以工作。然而,现在火车线仍然失败,连续两次打印:
W tensorflow/core/framework/op_kernel.cc:936] Unimplemented: File system scheme Tensor("input_producer/Gather not implemented
答案 0 :(得分:1)
将conv2d的输入转换为tf.float32_ref,tf.float32