본문 바로가기
기계학습/이미지 머신러닝

MNIST 실습 - CNN 모델

by tryotto 2020. 2. 10.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
 
mnist = input_data.read_data_sets("./mnist/data/", one_hot=True)
 
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense
 
 
 
# 레이어 설정
= tf.placeholder(tf.float32, shape=[None, 28281])
= tf.placeholder(tf.float32, [None, 10])
 
W1 = tf.Variable(tf.random_normal([4,4,1,16], stddev=0.01))
L1 = tf.nn.conv2d(X, W1, strides=[1,1,1,1], padding='SAME')
L1 = tf.nn.relu(L1)
L1 = tf.nn.max_pool(L1, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
 
W2 = tf.Variable(tf.random_normal([4,4,16,32], stddev=0.01))
L2 = tf.nn.conv2d(L1, W2, strides=[1,1,1,1], padding='SAME')
L2 = tf.nn.relu(L2)
L2 = tf.nn.max_pool(L2, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
 
L3 = tf.reshape(L2, [-17*7*32])
W3 = tf.Variable(tf.random_normal([7*7*32256], stddev=0.01))
L3 = tf.matmul(L3, W3)
= tf.nn.relu(L3)
#L3 = tf.nn.dropout(L3,keep_prob=0.5)
 
W4 = tf.Variable(tf.random_normal([25610], stddev=0.01))
model = tf.matmul(L3, W4)
model = tf.nn.softmax(model)
 
 
 
# 손실함수, 옵티마이저
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=model, labels=Y),axis=0)
optimizer = tf.train.AdamOptimizer(0.01).minimize(cost)
 
 
 
# 정확도 
correct = tf.equal(tf.arg_max(model, 1), tf.arg_max(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32),axis=0)
 
 
 
import random
 
epoch = 5
batch_s = 1000
batch_num = int(mnist.train.num_examples/batch_s)
 
with tf.Session() as sess:
  initial = tf.global_variables_initializer()
  sess.run(initial)
 
  # 학습 과정
  for i in range(epoch):    
    avg_cost=0
    
    for j in range(batch_num):
      batch_X, batch_Y = mnist.train.next_batch(batch_s)
      batch_X = batch_X.reshape(-128281)
      
      tmp_cost, opt = sess.run([cost, optimizer], feed_dict={X: batch_X, Y: batch_Y})
      avg_cost += tmp_cost
      
    avg_cost = avg_cost/batch_num
    print("epoch: ", i+1"cost >> ", avg_cost)
 
 
  # 정확도 출력하기  
  input_test = mnist.test.images
  output_test = mnist.test.labels
 
  input_test = input_test.reshape(-128281)
  
  acc = sess.run(accuracy, feed_dict={X: input_test, Y: output_test})
  print("정확도 : ", acc)
 
 
  # 실제 라벨 예측하기
  idx = random.randint(0, mnist.test.num_examples-1)
 
  predict_X = mnist.test.images[idx, idx+1]
  predict_X = predict_X.reshape(-128281)
  predict_X = sess.run(model, feed_dict={X: predict_X})
 
  predict_Y = mnist.test.labels[idx, idx+1]
 
  label_real = tf.arg_max(predict_Y, 1)
  label_predict = tf.arg_max(predict_X, 1)
 
  print("실제 라벨 값 : ", label_real)
  print("예측 라벨 값 : ", label_predict)
 
 
cs

'기계학습 > 이미지 머신러닝' 카테고리의 다른 글

남자 여자 판독기 - CNN 모델  (0) 2020.02.25
MNIST 실습 - GAN  (0) 2020.02.11
MNIST - 일반 딥러닝 모델  (0) 2020.02.09
MNIST - 기본 1 layer 머신러닝  (0) 2020.02.08