In [1]:
%matplotlib inline
import os
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
In [2]:
local_dir = os.path.join("/DATA",os.environ.get("USER"),"MNIST_data")
os.makedirs(local_dir,mode=0o755, exist_ok=True)
In [ ]:
In [3]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets(local_dir, one_hot=True)
max_pool_2x2(x)
- obrazek jest juz 14x14max_pool_2x2(x)
- obrazek jest juz 7x7
In [4]:
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)
In [5]:
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')
In [6]:
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
In [ ]:
In [7]:
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
In [8]:
x_image = tf.reshape(x, [-1,28,28,1])
In [9]:
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
In [10]:
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)
In [11]:
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)
In [12]:
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
In [13]:
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
In [14]:
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
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))
In [15]:
config = tf.ConfigProto()
#config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.50
sess = tf.InteractiveSession(config=config)
tf.global_variables_initializer().run()
In [16]:
batch = mnist.train.next_batch(1)
print( batch[0].shape )
In [17]:
sess.run(x_image,feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}).shape
Out[17]:
In [18]:
sess.run(h_conv1,feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}).shape
Out[18]:
In [19]:
sess.run(h_pool1,feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}).shape
Out[19]:
In [20]:
sess.run(h_conv2,feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}).shape
Out[20]:
In [21]:
sess.run(h_pool2,feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}).shape
Out[21]:
In [22]:
sess.run(h_pool2_flat,feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}).shape
Out[22]:
In [23]:
sess.run(h_fc1_drop,feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}).shape
Out[23]:
In [24]:
sess.run(y_conv,feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}).shape
Out[24]:
In [ ]:
In [25]:
%%time
sess.run(tf.global_variables_initializer())
for i in range(1000):
batch = mnist.train.next_batch(150)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={ x:batch[0], y_: batch[1], keep_prob: 0.8})
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print("test accuracy %g"%accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
In [26]:
P = sess.run(correct_prediction,feed_dict={ x:mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})
P.shape
Out[26]:
In [27]:
Bad_x = mnist.test.images[np.where(P==False)[0],:]
Bad_x_truelabels = mnist.test.labels[np.where(P==False)[0],:]
Bad_x.shape
Out[27]:
In [28]:
prediction =sess.run( tf.argmax(y_conv,1),
feed_dict={ x:Bad_x, y_: Bad_x_truelabels, keep_prob: 1.0})
In [29]:
prediction
Out[29]:
In [30]:
Bad_x_true = np.argmax(Bad_x_truelabels,axis=1)
In [31]:
Bad_x.shape
Out[31]:
In [32]:
Bad_x_true.shape
Out[32]:
In [33]:
prediction.shape
Out[33]:
In [ ]:
In [ ]:
In [34]:
#Imports for visualization
import PIL.Image
from io import BytesIO
from IPython.display import clear_output, Image, display
def DisplayArray(a, fmt='jpeg', rng=[0,1]):
"""Display an array as a picture."""
a = (a - rng[0])/float(rng[1] - rng[0])*255
a = np.uint8(np.clip(a, 0, 255))
f = BytesIO()
PIL.Image.fromarray(a).save(f, fmt)
display(Image(data=f.getvalue()))
In [35]:
from time import sleep
In [ ]:
In [36]:
for ith in range(Bad_x_true.shape[0]//20):
print("zamiast ",Bad_x_true[ith], "sieć odczytała",prediction[ith],end="")
DisplayArray( Bad_x[ith].reshape((28,28)) )
#sleep(0.5)
#clear_output(wait=True)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: