In [1]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

In [2]:
%matplotlib inline

In [3]:
from tensorflow.examples.tutorials.mnist import input_data

In [4]:
mnist = input_data.read_data_sets('/Users/jaegyuhan/PythonEx_1/mnist_data', one_hot=True)


Extracting /Users/jaegyuhan/PythonEx_1/mnist_data/train-images-idx3-ubyte.gz
Extracting /Users/jaegyuhan/PythonEx_1/mnist_data/train-labels-idx1-ubyte.gz
Extracting /Users/jaegyuhan/PythonEx_1/mnist_data/t10k-images-idx3-ubyte.gz
Extracting /Users/jaegyuhan/PythonEx_1/mnist_data/t10k-labels-idx1-ubyte.gz

In [5]:
X_test = mnist.test.images
Y_test = mnist.test.labels

In [6]:
sample = X_test[9]

In [7]:
sample = sample.reshape(28,28)

In [8]:
for i in range(sample.shape[0]):
    for j in range(sample.shape[1]):
        print("{:4.0f}".format(sample[i,j]*255), end='')
    print()


   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0  36  56 137 201 199  95  37   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0  45 152 234 254 254 254 254 254 250 211 151   6   0   0   0   0   0
   0   0   0   0   0   0   0   0   0  46 153 240 254 254 227 166 133 251 200 254 229 225 104   0   0   0   0   0
   0   0   0   0   0   0   0   0 153 234 254 254 187 142   8   0   0 191  40 198 246 223 253  21   0   0   0   0
   0   0   0   0   0   0   8 126 253 254 233 128  11   0   0   0   0 210  43  70 254 254 254  21   0   0   0   0
   0   0   0   0   0   0  72 243 254 228  54   0   0   0   0   3  32 116 225 242 254 255 162   5   0   0   0   0
   0   0   0   0   0   0  75 240 254 223 109 138 178 178 169 210 251 231 254 254 254 232  38   0   0   0   0   0
   0   0   0   0   0   0   9 175 244 253 255 254 254 251 254 254 254 254 254 252 171  25   0   0   0   0   0   0
   0   0   0   0   0   0   0   0  16 136 195 176 146 153 200 254 254 254 254 150  16   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0 162 254 254 241  99   3   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0 118 250 254 254  90   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0 100 242 254 254 211   7   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0  54 241 254 254 242  59   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0 131 254 254 244  64   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0  13 249 254 254 152   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0  12 228 254 254 208   8   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0  78 255 254 254  66   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0 209 254 254 137   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0 227 255 233  25   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0 113 255 108   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

In [9]:
plt.imshow(sample)


Out[9]:
<matplotlib.image.AxesImage at 0x12a27eac8>

In [14]:
sample.shape


Out[14]:
(28, 28)

In [ ]:
test_xdata = X_test[0:1,]
test_ydata = Y_test[0]

In [ ]:
ll = test_xdata.reshape(784,)

In [10]:
im = Image.open("../넘파이 연습/temp.png")

In [11]:
im


Out[11]:

In [12]:
img2 = im.resize((28,28))

In [27]:
pixels = img2.resize((28, 28)).tobytes("raw", "A")

In [29]:
pixels_list = [i for i in pixels]

In [33]:
plt.imshow(np.array(pixels_list).reshape(28,28))


Out[33]:
<matplotlib.image.AxesImage at 0x12b099390>

In [13]:
plt.imshow(img2)


Out[13]:
<matplotlib.image.AxesImage at 0x12a4c59b0>

In [18]:
b = img2.tobytes("raw","RGBA")

In [26]:
b


Out[26]:
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       ..., 
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])

In [19]:
b = np.array([i for i in b])

In [20]:
np.sum(b)


Out[20]:
4070

In [ ]:


In [21]:
#b의 값을 784,3 로 reshape를 한다. 
b = b.reshape(784,4)

In [22]:
np.sum(b,axis=1)


Out[22]:
array([  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 255,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 255,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0, 255,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0, 255,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0, 255,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 255,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
       255,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0, 255,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0, 255,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0, 255,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 255,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 245,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
       255,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0])

In [23]:
b = np.array([i for i in b])

In [25]:
b


Out[25]:
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       ..., 
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])

In [24]:
b = b.reshape(28,28)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-e999d3f7b60b> in <module>()
----> 1 b = b.reshape(28,28)

ValueError: cannot reshape array of size 3136 into shape (28,28)

In [ ]:
for i in range(b.shape[0]):
    for j in range(b.shape[1]):
        print("{:4}".format(b[i,j]), end='')
    print()

In [ ]:
ll = [i/255 for i in b ]

In [ ]:
len(ll)

In [ ]:
ll = np.array(ll)

In [ ]:
ll.shape

In [ ]:
ll = ll.reshape(1,784)

In [ ]:
ll.shape

In [ ]:
test_xdata

In [ ]:
print(len(test_xdata[0]))
print(test_ydata)
print(test_xdata.shape)

In [ ]:
X = tf.placeholder(dtype=tf.float32, shape=[None, 784])
Y = tf.placeholder(dtype=tf.float32, shape=[None, 10])

In [ ]:
W1 = tf.Variable(tf.random_normal(shape=[784, 256], stddev=0.01))
B1 = tf.Variable(tf.random_normal(shape=[256], stddev=0.01))
L1 = tf.nn.relu(tf.add(tf.matmul(X, W1), B1))

In [ ]:
W2 = tf.Variable(tf.random_normal(shape=[256, 256], stddev=0.01))
B2 = tf.Variable(tf.random_normal(shape=[256], stddev=0.01))
L2 = tf.nn.relu(tf.add(tf.matmul(L1, W2),B2))

In [ ]:
W3 = tf.Variable(tf.random_normal([256, 10], stddev=0.01))
B3 = tf.Variable(tf.random_normal(shape=[10], stddev=0.01))
model = tf.add(tf.matmul(L2, W3),B3)

In [ ]:
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=model, labels=Y))
optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)

In [ ]:
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    
    batch_size = 100
    total_batch = int(mnist.train.num_examples / batch_size)
    
    for epoch in range(15):
        total_cost = 0
        
        for i in range(total_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            
            _, cost_val = sess.run([optimizer, cost], feed_dict = {X: batch_xs, Y: batch_ys})
            total_cost += cost_val
        
        print("Epoch:","%04d" % (epoch + 1), "Avg. cost =", "{:.3f}".format(total_cost / total_batch))
    
    print('최적화 완료!')
    
    is_correct = tf.equal(tf.argmax(model, 1), tf.argmax(Y,1))
    accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
    print("정확도:", sess.run(accuracy, feed_dict={X:mnist.test.images, Y:mnist.test.labels}))
    
#     predict = sess.run([model], feed_dict={X:ll})
#     print(predict)
#     predict = np.array(predict)
#     print("shape:", predict.shape)
#     print("result : ",np.argmax(predict[0], axis=1))

In [ ]:
int(mnist.train.num_examples)

In [ ]:
arr = [-12.51021767,  -5.7246809 ,  -0.31184155,  -1.74826264,
        -17.8037262 ,  -9.16267967, -26.60844612,  15.07253742,
        -11.10308647,  -5.45234251]

In [ ]:
arr = np.array(arr)

In [ ]:
arr

In [ ]:
arr = arr.reshape([1,10])

In [ ]:
print(np.argmax(arr, axis=1))

In [ ]:
arr.shape

In [ ]: