Hello and Greetings People from around the world,
I was using the original source code from:
https://github.com/martin-gorner/tensorflow-mnist-tutorial/blob/master/mnist_1.0_softmax.py
And modified it into:
import tensorflow as tf
import numpy
from PIL import Image
tf.set_random_seed(0)
#Image
filenames = 'gray_kitten.jpg'
img = Image.open(filenames)
num_maps = 3
img = numpy.asarray(img, dtype='float32') / 256.
img_shape = img.shape
img_reshaped = img.reshape(1, img_shape[0], img_shape[1], num_maps)
#Label
label = numpy.zeros((10))
#Manual label value assignment
label[0] = 1
label = numpy.array(label,dtype = numpy.float32)
label = tf.convert_to_tensor(label,tf.float32)
X = tf.placeholder(tf.float32, [1, None, None, 3])
Y_ = tf.placeholder(tf.float32, [None, 10])
W = tf.Variable(tf.zeros([(img_shape[0] * img_shape[1]), 10]))
b = tf.Variable(tf.zeros([10]))
XX = tf.reshape(X, [-1, (img_shape[0] * img_shape[1])])
# The model
Y = tf.nn.softmax(tf.matmul(XX, W) + b)
cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y)) * 10.0
correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
train_step = tf.train.GradientDescentOptimizer(0.005).minimize(cross_entropy)
allweights = tf.reshape(W, [-1])
allbiases = tf.reshape(b, [-1])
# init
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
label = sess.run([label])
# You can call this function in a loop to train the model
def training_step(i, update_test_data, update_train_data):
if update_train_data:
a, c, w, b = sess.run([accuracy, cross_entropy, allweights, allbiases], feed_dict={X: img_reshaped, Y_: label})
print(str(i) + ": accuracy:" + str(a) + " loss: " + str(c))
if update_test_data:
a, c = sess.run([accuracy, cross_entropy], feed_dict={X: img_reshaped, Y_: label})
print("test accuracy:" + str(a) + " test loss: " + str(c))
# the backpropagation training step
sess.run(train_step, feed_dict={X: img_reshaped, Y_: label})
for i in range(100+1):
training_step(i, i % 50 == 0, i % 10 == 0)
When I execute the program, it will produce the following output:
0: accuracy:1.0 loss: 2.30259
test accuracy:1.0 test loss: 2.30259
10: accuracy:1.0 loss: nan
20: accuracy:1.0 loss: nan
30: accuracy:1.0 loss: nan
40: accuracy:1.0 loss: nan
50: accuracy:1.0 loss: nan
test accuracy:1.0 test loss: nan
60: accuracy:1.0 loss: nan
70: accuracy:1.0 loss: nan
80: accuracy:1.0 loss: nan
90: accuracy:1.0 loss: nan
100: accuracy:1.0 loss: nan
test accuracy:1.0 test loss: nan
What I am actually looking at is that I am hope for someone who can tell me on whether I have any mistake on image classification using soft-max algorithm with tensorflow.....? Because the the loss section produce "nan" value instead of "0"? and I am not sure my on whether my labeling was correct or wrong.
Thanks