JackYumCha / QA

Question and Answer
0 stars 0 forks source link

CNN MNIST example #18

Open JackYumCha opened 6 years ago

JackYumCha commented 6 years ago
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

import tensorflow as tf

# 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]) # batchsize * 28 * 28 * 1

h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1)+b_conv1) # batchsize * 28 * 28 * 32
h_pool1 = max_pool_2x2(h_conv1) # batchsize * 12 * 12 * 32

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) # batchsize * 14 * 14 * 64
h_pool2 = max_pool_2x2(h_conv2) # batchsize * 7 * 7 * 64

W_fc1 = weight_variable([7*7*64, 1024])
b_fc1 = bias_variable([1024])

h_pool2_flat = tf.reshape(h_pool2, [-1,7*7*64]) # 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_drop, W_fc2) + b_fc2 # batchsize * 10

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()

sess.run(tf.global_variables_initializer())

saver = tf.train.Saver()

valid_count = 0
index = 0

while index < 20000 and valid_count < 10:
    batch = mnist.train.next_batch(50)
    train_step.run(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
    if index % 50 == 0:
        train_accuracy =accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
        print('step %d, training accuracy %g' % (index, train_accuracy), "valid count {}".format(valid_count))
        if train_accuracy > 0.99:
            valid_count += 1
    index += 1

# for _ in range(20000):

# print the accuracy with the test data
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

# accuracy will be around 98%

# save the model

save_path = saver.save(sess, "./conv/model2")

print('save path: ', save_path)
JackYumCha commented 6 years ago

Beginner

from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
from PIL import Image

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# this shows what are in mnist
print(mnist)

import tensorflow as tf

# placeholder is the input
x = tf.placeholder(tf.float32, [None, 784]) # 784 = 28 * 28
# this is the placeholder for the labels. y is not y_. y is the inference output.
y_ = tf.placeholder(tf.float32, [None, 10])

# variables are used to hold the parameters 
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x, W) + b)

# this is the cross entropy.
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

# let SGD optimizer to minimize the cost function. we don't even need to explicitly calculate the gradients. It does all for us.
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

sess = tf.InteractiveSession()

# initialize variables
# this works only for InteractiveSession. If using Session, this will fail as there is not default session
tf.global_variables_initializer().run()

# run the minimization for 1000 times
for _ in range(500):
    batch_xs, batch_ys = mnist.train.next_batch(100) # this defines the size of your batch

    #print(sess.run(tf.reshape(x[0],[28,28]), feed_dict={x: batch_xs, y_: batch_ys}))
    ## [0,0,0,1,0,0,0,0,0,0]
    #print(sess.run(y_[0], feed_dict={x: batch_xs, y_: batch_ys}))
    #x_slice = sess.run(tf.reshape(x[0],[28,28]), feed_dict={x: batch_xs, y_: batch_ys})
    #x_arr = np.array(x_slice)
    #x_min = np.min(x_arr)
    #x_max = np.max(x_arr)
    #x_image_data = ((x_arr-x_min)/(x_max-x_min) * 255).astype(np.uint8)
    #img = Image.fromarray(x_image_data)
    #img.show()
    #img.save(".\mnist_images\slice{}.jpg".format(_),"JPEG")

    #print("xs: ", np.shape(batch_xs))
    #print(batch_xs)
    #print("ys: ", np.shape(batch_ys))
    #print(batch_ys)
    print('dimensions of [x,y,y_]:', sess.run([tf.shape(x),tf.shape(y),tf.shape(y_)], feed_dict={x: batch_xs, y_: batch_ys}))
    # feeding placeholder x and target y_ to the training
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
    print(_)

# build the correct prediction computation graph
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))

# calculate the accuracy with mean
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# print the accuracy with the test data
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

saver = tf.train.Saver()

saver.save(sess, "./lr/model2")
JackYumCha commented 6 years ago

beginner infer


from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
from PIL import Image

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# this shows what are in mnist
print(mnist)

import tensorflow as tf

# placeholder is the input
x = tf.placeholder(tf.float32, [None, 784]) # 784 = 28 * 28
# this is the placeholder for the labels. y is not y_. y is the inference output.
y_ = tf.placeholder(tf.float32, [None, 10])

# variables are used to hold the parameters 
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x, W) + b)

# this is the cross entropy.
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

# let SGD optimizer to minimize the cost function. we don't even need to explicitly calculate the gradients. It does all for us.
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

sess = tf.InteractiveSession()

# initialize variables
# this works only for InteractiveSession. If using Session, this will fail as there is not default session
tf.global_variables_initializer().run()

saver = tf.train.Saver()
saver.restore(sess, "./lr/model2")

number = Image.open('./tests/4.png').resize((28,28),Image.ANTIALIAS).convert('L')

pix = 1. - np.array(number, dtype=np.float32) / 255.

x_data = np.reshape(pix, [1,784])

print(sess.run(tf.arg_max(y,1),feed_dict={x: x_data, y_: np.zeros([1,10])}))

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# build the correct prediction computation graph
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))

# calculate the accuracy with mean
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# print the accuracy with the test data
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
JackYumCha commented 6 years ago

conv 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)
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.matmul(h_fc1_drop, W_fc2) + b_fc2

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/2.png').resize((28,28),Image.ANTIALIAS).convert('L')

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

print('infer:', np.argmax(inference), inference)
JackYumCha commented 6 years ago

show constitutional layer results

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_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.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)