Open JackYumCha opened 6 years ago
Infer
import tensorflow as tf
from PIL import Image
import numpy as np
# model
x = tf.placeholder(tf.float32, shape=[None,784])
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,1,32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x, [-1, 28, 28, 1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1)+b_conv1) # 1 * 28 * 28 * 32
h_conv1_t = tf.transpose(h_conv1, [3,1,2,0]) # 32 * 28 * 28 * 1
h_conv1_img = tf.reshape(h_conv1_t,[32,28,28]) # 32 * 28 * 28
h_pool1 = max_pool_2x2(h_conv1) # 1 * 14 * 14 * 32
h_pool1_t = tf.transpose(h_pool1, [3,1,2,0]) # 32 * 14 * 14 * 1
h_pool1_img = tf.reshape(h_pool1_t,[32,14,14]) # 32 * 14 * 14
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) # 1 * 14 * 14 * 64
h_conv2_t = tf.transpose(h_conv2, [3,1,2,0]) # 64 * 14 * 14 * 1
h_conv2_img = tf.reshape(h_conv2_t,[64,14,14]) # 64 * 14 * 14
h_pool2 = max_pool_2x2(h_conv2) # 1 * 7 * 7 * 64
h_pool2_t = tf.transpose(h_pool2, [3,1,2,0]) # 64 * 7 * 7 * 1
h_pool2_img = tf.reshape(h_pool2_t,[64,7,7]) # 64 * 7 * 7
W_conv3 = weight_variable([5,5,64,128])
b_conv3 = bias_variable([128])
h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3)+b_conv3) # batchsize * 7 * 7 * 64
W_fc1 = weight_variable([7*7*128, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_conv3, [-1,7*7*128]) # batchsize * 3136 (= 7 * 7 * 64)
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) # batchsize * 1024
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.matmul(h_fc1, W_fc2) + b_fc2 # do not use dropout
cross_entroy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entroy)
# build the correct prediction computation graph
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
# calculate the accuracy with mean
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# session
sess = tf.InteractiveSession()
saver = tf.train.Saver()
saver.restore(sess, './conv/model2')
print('session restored')
# white = Image.new('RGBA',(28,28),(255,255,255,255))
number = Image.open('./tests/5print.jpg').resize((28,28),Image.ANTIALIAS).convert('L')
number.save('converted.png')
#inputImage = Image.alpha_composite(white, number).convert('1', dither=Image.NONE)
pix = 1. - np.array(number, dtype=np.float32) / 255.
print(pix)
inference = sess.run(y_conv, feed_dict={x: [np.ndarray.flatten(pix)], keep_prob: 1.0})
c1 = sess.run(h_conv1_img, feed_dict={x: [np.ndarray.flatten(pix)], keep_prob: 1.0})
h1 = sess.run(h_pool1_img, feed_dict={x: [np.ndarray.flatten(pix)], keep_prob: 1.0})
c2 = sess.run(h_conv2_img, feed_dict={x: [np.ndarray.flatten(pix)], keep_prob: 1.0})
h2 = sess.run(h_pool2_img, feed_dict={x: [np.ndarray.flatten(pix)], keep_prob: 1.0})
#c1,h1,c2,h2 = sess.run([h_conv1_img, h_pool1_img, h_conv2_img, h_pool2_img], feed_dict={x: [np.ndarray.flatten(pix)], keep_prob: 1.0})
#for i in range(0,32):
# c_slice = c1[i]
# c_arr = np.array(c_slice)
# c_min = np.min(c_arr)
# c_max = np.max(c_arr)
# c_image_data = ((c_arr-c_min)/(c_max-c_min) * 255).astype(np.uint8)
# c_img = Image.fromarray(c_image_data)
# c_img.save(".\conv\c1\slice{}.jpg".format(i),"JPEG")
#for i in range(0,32):
# c_slice = h1[i]
# c_arr = np.array(c_slice)
# c_min = np.min(c_arr)
# c_max = np.max(c_arr)
# c_image_data = ((c_arr-c_min)/(c_max-c_min) * 255).astype(np.uint8)
# c_img = Image.fromarray(c_image_data)
# c_img.save(".\conv\h1\slice{}.jpg".format(i),"JPEG")
#for i in range(0,64):
# c_slice = c2[i]
# c_arr = np.array(c_slice)
# c_min = np.min(c_arr)
# c_max = np.max(c_arr)
# c_image_data = ((c_arr-c_min)/(c_max-c_min) * 255).astype(np.uint8)
# c_img = Image.fromarray(c_image_data)
# c_img.save(".\conv\c2\slice{}.jpg".format(i),"JPEG")
#for i in range(0,64):
# c_slice = h2[i]
# c_arr = np.array(c_slice)
# c_min = np.min(c_arr)
# c_max = np.max(c_arr)
# c_image_data = ((c_arr-c_min)/(c_max-c_min) * 255).astype(np.uint8)
# c_img = Image.fromarray(c_image_data)
# c_img.save(".\conv\h2\slice{}.jpg".format(i),"JPEG")
print('infer:', np.argmax(inference), inference)