How to train images in CNN with Tensorflow - tensorflow

I am a beginner of TensorFlow, and I am trying to build to CNN model.
Here is the sample code I refer to:
https://github.com/MorvanZhou/tutorials/blob/master/tensorflowTUT/tf18_CNN3/full_code.py
Currently I am facing a problem. I don't know how to insert training data (Images) to my model.
The sample code is using :
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
While I need to use my own images as training data.
From the sample code:
batch_xs, batch_ys = mnist.train.next_batch(100)
I not really understand this, and how can I do in my code to achieve this function ? Thanks.
Below is my code:
from __future__ import print_function
import tensorflow as tf
def getTrainImages():
filenames=[]
for i in range(576,1151):
if i<1000:
filenames.append('data/Class1/Class1/Train/0'+str(i)+'.PNG')
else:
filenames.append('data/Class1/Class1/Train/'+str(i)+'.PNG')
# step 2
filename_queue = tf.train.string_input_producer(filenames)
# step 3: read, decode and resize images
reader = tf.WholeFileReader()
filename, content = reader.read(filename_queue)
image = tf.image.decode_jpeg(content, channels=1)
image = tf.cast(image, tf.float32)
resized_image = tf.image.resize_images(image, 512, 512)
# step 4: Batching
image_batch = tf.train.batch([resized_image], batch_size=100)
return image_batch
def getTrainLabels():
labels=[]
file = open('data/Class1/Class1/Train/Label/Labels.txt', 'r')
for line in file:
if len(line)<=25:
labels.append(0)
else:
labels.append(1)
return labels
def getTestImages():
filenames=[]
for i in range(1,576):
if i<10:
filenames.append('data/Class1/Class1/Test/000'+str(i)+'.PNG')
elif i<100:
filenames.append('data/Class1/Class1/Test/00'+str(i)+'.PNG')
elif i<1000:
filenames.append('data/Class1/Class1/Test/0'+str(i)+'.PNG')
else:
filenames.append('data/Class1/Class1/Test/'+str(i)+'.PNG')
# step 2
filename_queue = tf.train.string_input_producer(filenames)
# step 3: read, decode and resize images
reader = tf.WholeFileReader()
filename, content = reader.read(filename_queue)
image = tf.image.decode_jpeg(content, channels=1)
image = tf.cast(image, tf.float32)
resized_image = tf.image.resize_images(image, 512, 512)
# step 4: Batching
image_batch = tf.train.batch([resized_image], batch_size=100)
return image_batch
def getTestLabels():
labels=[]
file = open('data/Class1/Class1/Test/Label/Labels.txt', 'r')
for line in file:
if len(line)<=25:
labels.append(0)
else:
labels.append(1)
return labels
def compute_accuracy(v_xs, v_ys):
global prediction
y_pre = sess.run(prediction, feed_dict={xs: v_xs, keep_prob: 1})
correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys, keep_prob: 1})
return result
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
# stride [1, x_movement, y_movement, 1]
# Must have strides[0] = strides[3] = 1
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') #SAME or VALID
def max_pool_2x2(x):
# stride [1, x_movement, y_movement, 1]
return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 262144]) # 512x512
ys = tf.placeholder(tf.float32, [None, 2])
keep_prob = tf.placeholder(tf.float32)
x_image = tf.reshape(xs, [-1, 512, 512, 1])
# print(x_image.shape) # [n_samples, 512,512,1]
## conv1 layer ##
W_conv1 = weight_variable([5,5, 1,32]) # patch 5x5, in size 1, out size 32
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # output size 512x512x32
h_pool1 = max_pool_2x2(h_conv1) # output size 256x256x32
## conv2 layer ##
W_conv2 = weight_variable([5,5, 32, 64]) # patch 5x5, in size 32, out size 64
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) # output size 256x256x64
h_pool2 = max_pool_2x2(h_conv2) # output size 128x128x64
## func1 layer ##
W_fc1 = weight_variable([128*128*64, 256])
b_fc1 = bias_variable([256])
# [n_samples, 7, 7, 64] ->> [n_samples, 7*7*64]
h_pool2_flat = tf.reshape(h_pool2, [-1, 128*128*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
## func2 layer ##
W_fc2 = weight_variable([256, 2]) # only 2 class, defect or defect-free
b_fc2 = bias_variable([2])
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
# the error between prediction and real data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
reduction_indices=[1])) # loss
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
sess = tf.Session()
# important step
sess.run(tf.initialize_all_variables())
batch_xs = getTrainImages()
batch_ys = getTrainLabels()
test_images = getTestImages()
test_labels = getTestLabels()
for i in range(1000):
#batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob: 0.5})
if i % 50 == 0:
print(compute_accuracy(
test_images, test_labels))
Here is error screen after I run my code.

image_batch = tf.train.batch([resized_image], batch_size=100)
This is the main problem. When you are inserting image into input queue, you did not specify the label together with it.
If you look at Tensorflow tutorial example, https://github.com/tensorflow/tensorflow/blob/r0.11/tensorflow/models/image/cifar10/cifar10_input.py#L126
images, label_batch = tf.train.batch(
[image, label],
batch_size=batch_size,
num_threads=num_preprocess_threads,
capacity=min_queue_examples + 3 * batch_size)
So remove getTrainLabels() function and push the label together with resize_image into queue.

Related

Why does this simple CNN suddenly fail?

I've looked at some simple CNNs from this tutorial video https://www.youtube.com/watch?v=hSDrJM6fJFM and modified the code so that it uses a MomentumOptimizer instead of AdamOptimizer, and made it so the training runs indefinitely instead of just to 1000. Around the 9500 value of i, the accuracy always goes to 0.085 and remains stuck there permanently, never changing after this. I am very confused as to what could cause this. As it progresses fine, increasing the accuracy gradually, then suddenly drops to worse than random guessing and stays there forever.
from __future__ import print_function
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# number 1 to 10 data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
def compute_accuracy(v_xs, v_ys):
global prediction
y_pre = sess.run(prediction, feed_dict={xs: v_xs, keep_prob: 1})
correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys, keep_prob: 1})
return result
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
# stride [1, x_movement, y_movement, 1]
# Must have strides[0] = strides[3] = 1
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
# stride [1, x_movement, y_movement, 1]
return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 784])/255. # 28x28
ys = tf.placeholder(tf.float32, [None, 10])
keep_prob = tf.placeholder(tf.float32)
x_image = tf.reshape(xs, [-1, 28, 28, 1])
# print(x_image.shape) # [n_samples, 28,28,1]
## conv1 layer ##
W_conv1 = weight_variable([5,5, 1,32]) # patch 5x5, in size 1, out size 32
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # output size 28x28x32
h_pool1 = max_pool_2x2(h_conv1) # output size 14x14x32
## conv2 layer ##
W_conv2 = weight_variable([5,5, 32, 64]) # patch 5x5, in size 32, out size 64
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) # output size 14x14x64
h_pool2 = max_pool_2x2(h_conv2) # output size 7x7x64
## fc1 layer ##
W_fc1 = weight_variable([7*7*64, 1024])
b_fc1 = bias_variable([1024])
# [n_samples, 7, 7, 64] ->> [n_samples, 7*7*64]
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)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
## fc2 layer ##
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
# the error between prediction and real data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),reduction_indices=[1])) # loss
train_step = tf.train.MomentumOptimizer(1e-4, 0.999).minimize(cross_entropy)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(100000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob: 0.5})
if i % 50 == 0:
print(i, compute_accuracy(mnist.test.images[:1000], mnist.test.labels[:1000]))
This is the code I've run, I'd really appreciate any help solving this mystery thank you.

Tensorflow MNIST: ValueError: Shape must be rank 4 but is rank 1 for 'Conv2D' (op: 'Conv2D') with input shapes: [?,28,28,1], [4]

I'm new to machine learning and tensorflow. I started by following the MNIST tutorial on the tensorflow site. I got the simple version to work, but when I was following along with the deep CNN, I found an error.
ValueError: Shape must be rank 4 but is rank 1 for 'Conv2D' (op:
'Conv2D') with input shapes: [?,28,28,1], [4].
The problem seems to lie in the line:
x_image = tf.reshape(x, [-1, 28, 28, 1])
Thanks for any help, I'm a bit lost here.
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNST_data/", one_hot=True)
import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.matmul(x, W) + b
y_ = tf.placeholder(tf.float32, [None, 10])
#improvements
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
#layer 1
W_conv1 = ([5,5,1,32])
b_conv1 = ([32])
x_image = tf.reshape(x, [-1, 28, 28, 1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
#layer 2
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)
#fully connected layer
W_fc1 = weight_variable([3136, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 3136])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
#dropout
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
#readout, similar to softmax
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
#optimization
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
#training
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
#evaluate
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
#the session
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(20000):
batch = mnist.train.next_batch(50)
if i%100==0:
training_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
print("step: %i accuracy: %a" % (i, training_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print("test accuracy: %s" % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
Your error is in your 1st convolutional layer - your variables W_conv1 and b_conv1 are just lists (hence rank 1) since you have not used the weight_variable() and bias_variable() functions that you created!
Might be relevant.
This error at least misleading and confusing for me.
As per the error its asking us to check "input shapes", whereas exactly the issue is in filters that you have specified.
That's why #Yuji asking above to use method weight_variable(), which is properly initializing the filters(weights).

Why is the loss taking multiple values for one epoch?

I am wondering about the smoothing option of tensorboard. When I set smoothing to 0, I get the following plot of the loss:
This seems strange to me, because I think I calculate the loss only once for each epoch. Why does it have multiple values for one epoch?
Code
The following code with tensorboard --logdir summary_dir created the image:
#!/usr/bin/env python
"""MNIST with Tensorflow."""
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import os
import numpy as np
epochs = 20000
model_checkpoint_path = 'checkpoints/mnist_tf_model.ckpt'
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.0001)
return tf.get_variable(initializer=initial, name='weights')
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.get_variable(initializer=initial, name='biases')
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
def eval_network(sess, summary_writer, dataset, correct_prediction, epoch):
correct_sum = 0
total_test = 0
training_summary = tf.get_default_graph().get_tensor_by_name("training_accuracy:0")
loss_summary = tf.get_default_graph().get_tensor_by_name("loss:0")
for i in range(dataset.labels.shape[0] / 1000):
feed_dict = {x: dataset.images[i * 1000:(i + 1) * 1000],
y_: dataset.labels[i * 1000:(i + 1) * 1000],
keep_prob: 1.0}
[test_correct, train_summ, loss_summ] = sess.run([correct_prediction,
training_summary,
loss_summary],
feed_dict=feed_dict)
summary_writer.add_summary(train_summ, epoch)
summary_writer.add_summary(loss_summ, epoch)
test_correct = correct_prediction.eval(feed_dict=feed_dict)
correct_sum += sum(test_correct)
total_test += len(test_correct)
return float(correct_sum) / total_test
def log_score(sess, summary_writer, filename, mnist, scoring, epoch):
with open(filename, "a") as myfile:
train = eval_network(sess, summary_writer, mnist.train, scoring, epoch)
test = eval_network(sess, summary_writer, mnist.test, scoring, epoch)
myfile.write("%i;%0.6f;%0.6f\n" % (epoch, train, test))
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
with tf.Session() as sess:
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
x_image = tf.reshape(x, [-1, 28, 28, 1])
with tf.variable_scope('conv1') as scope:
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1, name='ReLU1')
h_pool1 = max_pool_2x2(h_conv1)
with tf.variable_scope('conv2') as scope:
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, name='ReLU2')
h_pool2 = max_pool_2x2(h_conv2)
with tf.variable_scope('fc1'):
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)
with tf.variable_scope('dropout'):
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
with tf.variable_scope('softmax'):
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
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))
tf.scalar_summary("training_accuracy", accuracy, name="training_accuracy")
tf.scalar_summary("loss", cross_entropy, name="loss")
summary_writer = tf.train.SummaryWriter('summary_dir', sess.graph)
sess.run(tf.initialize_all_variables())
for i in range(epochs):
batch = mnist.train.next_batch(50)
if i % 100 == 0:
log_score(sess, summary_writer,
'validation-curve-accuracy.csv',
mnist, correct_prediction, i)
train_step.run(feed_dict={x: batch[0],
y_: batch[1],
keep_prob: 0.5})
log_score(sess, summary_writer, 'validation-curve-accuracy.csv',
mnist, correct_prediction, epochs)
log_score evaluates the summaries and it's called every 100 minibatches.

trying to adapt the tensorflow cnn tutorial to SVHN data Placeholder issue

i am new to cnn and to tensorflow, so i tried to adapt their example of cnn that they run on the mnist dataset onto the SVHN dataset. the images are 32x32 instead of 28x28 and they have 3 color channels instead of one. I get an error message :
tensorflow.python.framework.errors.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
I cannot yet make heads or tails of the whole thing. Maybe someone spots a few glaring mistakes that i surely made. I encourage you to be brutal :)
Here is my code in its entirety :
import urllib
import os.path
import numpy as np
testfile = urllib.URLopener()
testfile2=urllib.URLopener()
import scipy.io as scp
if not os.path.isfile("test.mat"):
testfile.retrieve("http://ufldl.stanford.edu/housenumbers/test_32x32.mat", "test.mat")
if not os.path.isfile("train.mat"):
testfile.retrieve("http://ufldl.stanford.edu/housenumbers/train_32x32.mat", "train.mat")
testdata=scp.loadmat('test.mat')
traindata=scp.loadmat('train.mat')
trainDataX = traindata['X']
trainDataY = traindata['y']
testDataX = testdata['X']
testDataY = testdata['y']
def OnehotEndoding(Y):
Ytr=[]
for el in Y:
temp=np.zeros(10)
if el==10:
temp[0]=1
elif el==1:
temp[1]=1
elif el==2:
temp[2]=1
elif el==3:
temp[3]=1
elif el==4:
temp[4]=1
elif temp[5]==1:
temp[5]=1
elif temp[6]==1:
temp[6]=1
elif temp[7]==1:
temp[7]=1
elif temp[8]==1:
temp[8]=1
elif temp[9]==1:
temp[9]=1
Ytr.append(temp)
return np.asarray(Ytr)
trainDataY = OnehotEndoding(trainDataY)
testDataY = OnehotEndoding(testDataY)
def transposeArray(data):
print 'started'
xtrain = []
trainLen = data.shape[3]
print trainLen
for x in xrange(trainLen):
xtrain.append(data[:,:,:,x])
xtrain = np.asarray(xtrain)
return xtrain
trainDataX = transposeArray(trainDataX)
testDataX = transposeArray(testDataX)
print trainDataX.shape
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=[None, 32,32,3])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
W_conv1 = weight_variable([5, 5, 3, 32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x, [-1,32,32,3])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
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)
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)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
x = tf.placeholder(tf.float32, shape=[None, 32,32,3])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
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))
with tf.Session() as sess:
epoch=10000
batch_size=100
sess.run(tf.initialize_all_variables())
p = np.random.permutation(range(len(trainDataX)))
trX, trY = trainDataX[p], trainDataY[p]
print len(trainDataX)
start = 0
end = 0
for step in range(epoch):
start = end
end = start + batch_size
if start >= len(trainDataX):
start = 0
end = start + batch_size
if end >= len(trainDataX):
end = len(trainDataX) - 1
inX, outY = trX[start:end], trY[start:end]
#sess.run(optimizer, feed_dict= {tf_X: inX, tf_Y: outY, keep_prob:0.75})
if step % 100 == 0:
train_accuracy = accuracy.eval(feed_dict={x: inX, y_: outY, keep_prob:1})
print("step %d, training accuracy %g"%(step, train_accuracy))
train_step.run(feed_dict={x: inX, y_: outY, keep_prob: 0.5})
print("test accuracy %g"%accuracy.eval(feed_dict={
x: testDataX, y_:testDataY , keep_prob: 1.0}))
The warning is quite explicit: you didn't pass any value for a required Placeholder.
First, you define twice the placeholders with:
x = tf.placeholder(tf.float32, shape=[None, 32,32,3])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
So TensorFlow expects you to feed 4 values instead of just 2.
You can delete one of them, this will hopefully remove the error.

tensorflow accuracy goes to NAN, adaptation from google tutorial with ADAMOPT

I have been trying to adapt the mnist cnn tutorial to my SVHN data.
i dont get an error ( besides something getting ignored)
But the training accuracy just goes to NAN after a few batches.
I am new to cnn and to tensorflow, so i am a bit lost why that is.
( imho it cannot be the learning rate, since i useed the Adam optimizer and the doc said it dynamically adapts the learning rate)
Here is my code:
import urllib
import os.path
import numpy as np
testfile = urllib.URLopener()
testfile2=urllib.URLopener()
import scipy.io as scp
if not os.path.isfile("test.mat"):
testfile.retrieve("http://ufldl.stanford.edu/housenumbers/test_32x32.mat", "test.mat")
if not os.path.isfile("train.mat"):
testfile.retrieve("http://ufldl.stanford.edu/housenumbers/train_32x32.mat", "train.mat")
testdata=scp.loadmat('test.mat')
traindata=scp.loadmat('train.mat')
trainDataX = traindata['X']
trainDataY = traindata['y']
testDataX = testdata['X']
testDataY = testdata['y']
def OnehotEndoding(Y):
Ytr=[]
for el in Y:
temp=np.zeros(10)
if el==10:
temp[0]=1
elif el==1:
temp[1]=1
elif el==2:
temp[2]=1
elif el==3:
temp[3]=1
elif el==4:
temp[4]=1
elif temp[5]==1:
temp[5]=1
elif temp[6]==1:
temp[6]=1
elif temp[7]==1:
temp[7]=1
elif temp[8]==1:
temp[8]=1
elif temp[9]==1:
temp[9]=1
Ytr.append(temp)
return np.asarray(Ytr)
trainDataY = OnehotEndoding(trainDataY)
testDataY = OnehotEndoding(testDataY)
def transposeArray(data):
print 'started'
xtrain = []
trainLen = data.shape[3]
print trainLen
for x in xrange(trainLen):
xtrain.append(data[:,:,:,x])
xtrain = np.asarray(xtrain)
return xtrain
trainDataX = transposeArray(trainDataX)
testDataX = transposeArray(testDataX)
print trainDataX.shape
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=[None, 32,32,3])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
W_conv1 = weight_variable([5, 5, 3, 32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x, [-1,32,32,3])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
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)
W_fc1 = weight_variable([8 * 8* 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 8*8*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
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))
with tf.Session() as sess:
epoch=10000
batch_size=100
sess.run(tf.initialize_all_variables())
p = np.random.permutation(range(len(trainDataX)))
trX, trY = trainDataX[p], trainDataY[p]
print len(trainDataX)
start = 0
end = 0
for step in range(epoch):
start = end
end = start + batch_size
if start >= len(trainDataX):
start = 0
end = start + batch_size
if end >= len(trainDataX):
end = len(trainDataX) - 1
inX, outY = trX[start:end], trY[start:end]
#sess.run(optimizer, feed_dict= {tf_X: inX, tf_Y: outY, keep_prob:0.75})
if step % 100 == 0:
train_accuracy = accuracy.eval(feed_dict={x: inX, y_: outY, keep_prob:1})
print("step %d, training accuracy %g"%(step, train_accuracy))
train_step.run(feed_dict={x: inX, y_: outY, keep_prob: 0.5})
print("test accuracy %g"%accuracy.eval(feed_dict={
x: testDataX, y_:testDataY , keep_prob: 1.0}))
Your cross entropy formula seems wrong. Instead use the built in tf.nn.softmax_cross_entropy_with_logits:
logits = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
y_conv = tf.nn.softmax(logits)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, y_))