In [1]:
import tensorflow as tf
import numpy as np
import matplotlib as plt
import matplotlib.pyplot as plt
In [2]:
%matplotlib inline
sess = tf.InteractiveSession()
image = np.array([[[[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]]]], dtype = np.float32)
print(image.shape)
plt.imshow(image.reshape(3,3), cmap='Greys') #시각화
#1 => n개의 이미지#
# 3*3 size
#1 color
Out[2]:
In [3]:
print("image.shape", image.shape)
weight = tf.constant([[[[1.]],[[1.]]],[[[1.]],[[1.]]]])
print("weight.shape", weight.shape)
conv2d = tf.nn.conv2d(image, weight, strides=[1,1,1,1],padding = 'SAME') #1*1 stride줌 #same을 했기때문에 input과 같은 사이즈나옴
conv2d_img = conv2d.eval()
print("conv2d_img.shape", conv2d_img.shape)
#시각화하기위한코드
conv2d_img = np.swapaxes(conv2d_img,0,3) #transposing 2D array
for i, one_img in enumerate(conv2d_img):
print(one_img.reshape(3,3))
plt.subplot(1,3,i+1), plt.imshow(one_img.reshape(3,3), cmap = 'gray')
In [4]:
print("image.shape", image.shape)
weight = tf.constant([[[[1.,10,-1.]],[[1.,10.,-1]]],[[[1.,10.,-1.]],[[1.,10.,-1.]]]])
print("weight.shape", weight.shape) #마지막 숫자 3은 필터의 갯수==나올 이미지의 갯수
#필터를 몇장을 쓰는가에 따라 하나의 이미지에 대하여 여러 이미지가 나옴.
conv2d = tf.nn.conv2d(image, weight, strides=[1,1,1,1],padding = 'SAME')
conv2d_img = conv2d.eval()
print("conv2d_img.shape", conv2d_img.shape)
#시각화 하기 위한 코드
conv2d_img = np.swapaxes(conv2d_img,0,3)
for i, one_img in enumerate(conv2d_img):
print(one_img.reshape(3,3))
plt.subplot(1,3,i+1), plt.imshow(one_img.reshape(3,3), cmap= 'gray')
In [5]:
#max pooling_어떤 데이터를 서브 샘플링한다.
%matplotlib inline
image = np.array([[[[4],[3]],[[2],[1]]]],dtype = np.float32)
pool = tf.nn.max_pool(image, ksize= [1,2,2,1], strides = [1,1,1,1], padding = 'SAME')
#필터사이즈 #max_pool이 CNN과 잘 동작함
print(pool.shape)
print(pool.eval())
#최댓값 뽑아내는 것
In [6]:
#max pooling2
%matplotlib inline
image = np.array([[[[4],[3]],[[2],[1]]]],dtype = np.float32)
pool = tf.nn.max_pool(image, ksize= [1,2,2,1], strides = [1,1,1,1], padding = 'VALID')
print(pool.shape)
print(pool.eval())
In [7]:
#실전 이미지에 넣기
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
#데이터 읽어오기
In [8]:
img = mnist.train.images[5].reshape(28,28)
#가장 첫번째에 있는 거 불러와서 쉐잎을 28*28로 잡
plt.imshow(img, cmap = 'gray') #출력
Out[8]:
In [9]:
sess = tf.InteractiveSession()
img = img.reshape(-1,28,28,1) #28*28의 한 색깔, n개의 이미지일 때는 -1=(컴퓨터에게 알아서 계산해~라는 reshape의 방법)
W1 = tf.Variable(tf.random_normal([3, 3, 1, 5], stddev=0.01)) #칼라에 신경써야함.(1) (3*3은 필터의 사이즈) (5개의 필터사용)
conv2d = tf.nn.conv2d(img, W1, strides=[1, 2, 2, 1], padding='SAME')# (2*2는 필터를 2칸씩 옮기겠다.그래서 출력이 14*14)
print(conv2d)
sess.run(tf.global_variables_initializer())
#그림 출력하기는 code
conv2d_img = conv2d.eval()
conv2d_img = np.swapaxes(conv2d_img, 0, 3)
for i, one_img in enumerate(conv2d_img):
plt.subplot(1,5,i+1), plt.imshow(one_img.reshape(14,14), cmap='gray')
#5개의 서로다른 필터를 사용했기 때문에 서로다른 5개의 이미지가 나옴.
In [10]:
#max pooling
pool = tf.nn.max_pool(conv2d, ksize=[1, 2, 2, 1], strides=[
1, 2, 2, 1], padding='SAME')
#입력 이미지 14*14인데 또 필터가 두탄씩 움직이니까 7*7로 또 줄어듦.
print(pool)
#실행코드
sess.run(tf.global_variables_initializer())
pool_img = pool.eval()
#출력 code
pool_img = np.swapaxes(pool_img, 0, 3)
for i, one_img in enumerate(pool_img):
plt.subplot(1,5,i+1), plt.imshow(one_img.reshape(7, 7), cmap='gray')
#7*7의 이미지가 나옴. 이미지가 서브 샘플링되어서 해상도가 떨어져있음. max pooling을 이용해서 간단한 서브 샘플링을 해봄.
In [11]:
import tensorflow as tf
import random
# import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
tf.set_random_seed(777) # reproducibility
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) #vector표현가능하게one_hot=True
# Check out https://www.tensorflow.org/get_started/mnist/beginners for
# more information about the mnist dataset
# hyper parameters
learning_rate = 0.001
training_epochs = 15
batch_size = 100
# dropout (keep_prob) rate 0.7~0.5 on training, but should be 1 for testing
keep_prob = tf.placeholder(tf.float32)
#Conv layer1
#input의 이미지를 우리가 원하는대로 만들어야
# input place holders
X = tf.placeholder(tf.float32, [None, 784]) #784인 이유는 MNIST가 28*28이기 때문
X_img = tf.reshape(X, [-1, 28, 28, 1]) # img 28x28x1(칼라) (black/white) #이미지로 넣기 위해서 #x_img가 입력이 될 것.
Y = tf.placeholder(tf.float32, [None, 10])
#첫번째 conv layer1
# L1 ImgIn shape=(?, 28, 28, 1)
W1 = tf.Variable(tf.random_normal([3, 3, 1, 32], stddev=0.01)) #3*3size의 필터 #1은 칼라 #32 필터
# Conv -> (?, 28, 28, 32)
# Pool -> (?, 14, 14, 32)
L1 = tf.nn.conv2d(X_img, 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')
#필터 2*2 # stride 2*2 # 28*28->14*14
L1 = tf.nn.dropout(L1, keep_prob=keep_prob)
'''
Tensor("Conv2D:0", shape=(?, 28, 28, 32), dtype=float32)
Tensor("Relu:0", shape=(?, 28, 28, 32), dtype=float32)
Tensor("MaxPool:0", shape=(?, 14, 14, 32), dtype=float32)
Tensor("dropout/mul:0", shape=(?, 14, 14, 32), dtype=float32)
'''
#conv layer2
# L2 ImgIn shape=(?, 14, 14, 32)
W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01)) #32는 필터의 갯수와 같아야함. 64개의 필터를 쓸 것
# Conv ->(?, 14, 14, 64)
# Pool ->(?, 7, 7, 64)
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')
L2 = tf.nn.dropout(L2, keep_prob=keep_prob)
'''
Tensor("Conv2D_1:0", shape=(?, 14, 14, 64), dtype=float32)
Tensor("Relu_1:0", shape=(?, 14, 14, 64), dtype=float32)
Tensor("MaxPool_1:0", shape=(?, 7, 7, 64), dtype=float32)
Tensor("dropout_1/mul:0", shape=(?, 7, 7, 64), dtype=float32)
'''
#conv layer3
# L3 ImgIn shape=(?, 7, 7, 64)
W3 = tf.Variable(tf.random_normal([3, 3, 64, 128], stddev=0.01))
# Conv ->(?, 7, 7, 128)
# Pool ->(?, 4, 4, 128)
# Reshape ->(?, 4 * 4 * 128) # Flatten them for FC
L3 = tf.nn.conv2d(L2, W3, strides=[1, 1, 1, 1], padding='SAME')
L3 = tf.nn.relu(L3)
L3 = tf.nn.max_pool(L3, ksize=[1, 2, 2, 1], strides=[
1, 2, 2, 1], padding='SAME')
L3 = tf.nn.dropout(L3, keep_prob=keep_prob)
#입체적인 모양을 쭉 펼쳐야됨. 128*4*4만크므이 길이를 갖는게 n개 있게됨.
L3_flat = tf.reshape(L3, [-1, 128 * 4 * 4])
'''
Tensor("Conv2D_2:0", shape=(?, 7, 7, 128), dtype=float32)
Tensor("Relu_2:0", shape=(?, 7, 7, 128), dtype=float32)
Tensor("MaxPool_2:0", shape=(?, 4, 4, 128), dtype=float32)
Tensor("dropout_2/mul:0", shape=(?, 4, 4, 128), dtype=float32)
Tensor("Reshape_1:0", shape=(?, 2048), dtype=float32)
'''
#FC를 2번하겠다.그럼 정확도가 높아짐.
#Conv layer4
# L4 FC 4x4x128 inputs -> 625 outputs
#벡터 입력의 값 = 128*4*4 출력의 값 625개
W4 = tf.get_variable("W4", shape=[128 * 4 * 4, 625],
initializer=tf.contrib.layers.xavier_initializer())
#bias를 출력의 값과 똑같게 줌 (625)
b4 = tf.Variable(tf.random_normal([625]))
#곱하고 더한다.
L4 = tf.nn.relu(tf.matmul(L3_flat, W4) + b4)
#학습할때는 dropout을 0.5나 0.7로
L4 = tf.nn.dropout(L4, keep_prob=keep_prob)
'''
Tensor("Relu_3:0", shape=(?, 625), dtype=float32)
Tensor("dropout_3/mul:0", shape=(?, 625), dtype=float32)
'''
#Fully connecter(fc,dense) layer
# L5 Final FC 625 inputs 입력받아서 -> 10 outputs
W5 = tf.get_variable("W5", shape=[625, 10],
initializer=tf.contrib.layers.xavier_initializer())
b5 = tf.Variable(tf.random_normal([10]))
logits = tf.matmul(L4, W5) + b5
'''
Tensor("add_1:0", shape=(?, 10), dtype=float32)
'''
# define cost/loss & optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
logits=logits, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
#Training and Evaluation
# initialize
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# 학습시키기
print('Learning started. It takes sometime.')
for epoch in range(training_epochs):
avg_cost = 0
total_batch = int(mnist.train.num_examples / batch_size)
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
feed_dict = {X: batch_xs, Y: batch_ys, keep_prob: 0.7}
c, _ = sess.run([cost, optimizer], feed_dict=feed_dict)
avg_cost += c / total_batch
print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))
print('Learning Finished!')
# 학습이 잘 되었는지 평가하기
# if you have a OOM error, please refer to lab-11-X-mnist_deep_cnn_low_memory.py
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print('Accuracy:', sess.run(accuracy, feed_dict={
X: mnist.test.images, Y: mnist.test.labels, keep_prob: 1}))
#test할 때는 반드시 dropout을 1로
# Get one and predict
r = random.randint(0, mnist.test.num_examples - 1)
print("Label: ", sess.run(tf.argmax(mnist.test.labels[r:r + 1], 1)))
print("Prediction: ", sess.run(
tf.argmax(logits, 1), feed_dict={X: mnist.test.images[r:r + 1], keep_prob: 1}))
# plt.imshow(mnist.test.images[r:r + 1].
# reshape(28, 28), cmap='Greys', interpolation='nearest')
# plt.show(
#cost가 떨어지면서99프로까지 정확성을 얻을 수 있음.
In [21]:
#위의 코드는 관리하기 불편함.그래서 python의 class으로 보다 효과적으로 관리.
# Lab 11 MNIST and Deep learning CNN
import tensorflow as tf
# import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
tf.set_random_seed(777) # reproducibility
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# Check out https://www.tensorflow.org/get_started/mnist/beginners for
# more information about the mnist dataset
# hyper parameters
learning_rate = 0.001
training_epochs = 15
batch_size = 100
class Model:
#초기화
def __init__(self, sess, name):
self.sess = sess
self.name = name
self._build_net()
#네트워크를 빌드하는 건 다 넣음.
def _build_net(self):
with tf.variable_scope(self.name):
# dropout (keep_prob) rate 0.7~0.5 on training, but should be 1
# for testing
self.keep_prob = tf.placeholder(tf.float32)
# input place holders
self.X = tf.placeholder(tf.float32, [None, 784])
# img 28x28x1 (black/white)
X_img = tf.reshape(self.X, [-1, 28, 28, 1])
self.Y = tf.placeholder(tf.float32, [None, 10])
# L1 ImgIn shape=(?, 28, 28, 1)
W1 = tf.Variable(tf.random_normal([3, 3, 1, 32], stddev=0.01))
# Conv -> (?, 28, 28, 32)
# Pool -> (?, 14, 14, 32)
L1 = tf.nn.conv2d(X_img, 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')
L1 = tf.nn.dropout(L1, keep_prob=self.keep_prob)
'''
Tensor("Conv2D:0", shape=(?, 28, 28, 32), dtype=float32)
Tensor("Relu:0", shape=(?, 28, 28, 32), dtype=float32)
Tensor("MaxPool:0", shape=(?, 14, 14, 32), dtype=float32)
Tensor("dropout/mul:0", shape=(?, 14, 14, 32), dtype=float32)
'''
# L2 ImgIn shape=(?, 14, 14, 32)
W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01))
# Conv ->(?, 14, 14, 64)
# Pool ->(?, 7, 7, 64)
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')
L2 = tf.nn.dropout(L2, keep_prob=self.keep_prob)
'''
Tensor("Conv2D_1:0", shape=(?, 14, 14, 64), dtype=float32)
Tensor("Relu_1:0", shape=(?, 14, 14, 64), dtype=float32)
Tensor("MaxPool_1:0", shape=(?, 7, 7, 64), dtype=float32)
Tensor("dropout_1/mul:0", shape=(?, 7, 7, 64), dtype=float32)
'''
# L3 ImgIn shape=(?, 7, 7, 64)
W3 = tf.Variable(tf.random_normal([3, 3, 64, 128], stddev=0.01))
# Conv ->(?, 7, 7, 128)
# Pool ->(?, 4, 4, 128)
# Reshape ->(?, 4 * 4 * 128) # Flatten them for FC
L3 = tf.nn.conv2d(L2, W3, strides=[1, 1, 1, 1], padding='SAME')
L3 = tf.nn.relu(L3)
L3 = tf.nn.max_pool(L3, ksize=[1, 2, 2, 1], strides=[
1, 2, 2, 1], padding='SAME')
L3 = tf.nn.dropout(L3, keep_prob=self.keep_prob)
L3_flat = tf.reshape(L3, [-1, 128 * 4 * 4])
'''
Tensor("Conv2D_2:0", shape=(?, 7, 7, 128), dtype=float32)
Tensor("Relu_2:0", shape=(?, 7, 7, 128), dtype=float32)
Tensor("MaxPool_2:0", shape=(?, 4, 4, 128), dtype=float32)
Tensor("dropout_2/mul:0", shape=(?, 4, 4, 128), dtype=float32)
Tensor("Reshape_1:0", shape=(?, 2048), dtype=float32)
'''
# L4 FC 4x4x128 inputs -> 625 outputs
W4 = tf.get_variable("W4", shape=[128 * 4 * 4, 625],
initializer=tf.contrib.layers.xavier_initializer())
b4 = tf.Variable(tf.random_normal([625]))
L4 = tf.nn.relu(tf.matmul(L3_flat, W4) + b4)
L4 = tf.nn.dropout(L4, keep_prob=self.keep_prob)
'''
Tensor("Relu_3:0", shape=(?, 625), dtype=float32)
Tensor("dropout_3/mul:0", shape=(?, 625), dtype=float32)
'''
# L5 Final FC 625 inputs -> 10 outputs
W5 = tf.get_variable("W5", shape=[625, 10],
initializer=tf.contrib.layers.xavier_initializer())
b5 = tf.Variable(tf.random_normal([10]))
self.logits = tf.matmul(L4, W5) + b5
'''
Tensor("add_1:0", shape=(?, 10), dtype=float32)
'''
# define cost/loss & optimizer
self.cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
logits=self.logits, labels=self.Y))
self.optimizer = tf.train.AdamOptimizer(
learning_rate=learning_rate).minimize(self.cost)
correct_prediction = tf.equal(
tf.argmax(self.logits, 1), tf.argmax(self.Y, 1))
self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#예측하는거
def predict(self, x_test, keep_prop=1.0):
return self.sess.run(self.logits, feed_dict={self.X: x_test, self.keep_prob: keep_prop})
#정확도를 얻는 것
def get_accuracy(self, x_test, y_test, keep_prop=1.0):
return self.sess.run(self.accuracy, feed_dict={self.X: x_test, self.Y: y_test, self.keep_prob: keep_prop})
#학습하는 서
def train(self, x_data, y_data, keep_prop=0.7):
return self.sess.run([self.cost, self.optimizer], feed_dict={
self.X: x_data, self.Y: y_data, self.keep_prob: keep_prop})
# initialize
sess = tf.Session()
#model1을 만듦.
m1 = Model(sess, "m1")
sess.run(tf.global_variables_initializer())
print('Learning Started!')
# train my model
for epoch in range(training_epochs):
avg_cost = 0
total_batch = int(mnist.train.num_examples / batch_size)
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
#session run할 필요없이 ml에 train 함수를 호출해버리면 됨.
#깔끔하게 관리가 됨.
c, _ = m1.train(batch_xs, batch_ys)
avg_cost += c / total_batch
print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))
print('Learning Finished!')
# Test model and check accuracy
print('Accuracy:', m1.get_accuracy(mnist.test.images, mnist.test.labels))