<jupyter_start><jupyter_text>18장. CNN 모델  Anaconda에서 개발환경 만들기```bashconda env create -f env_CH18.ymlconda activate mnist-classification```  18.3 콘볼루션(Convolution)<jupyter_code>import numpy as np
import tensorflow as tf
import matplotlib.pylab as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
from tensorflow.examples.tutorials.mnist import input_data
sess = tf.InteractiveSession()
mnist = input_data.read_data_sets("MNIST_data/")<jupyter_output>WARNING:tensorflow:From <ipython-input-1-06f736123259>:8: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
WARNING:tensorflow:From C:\Users\HanByul\Anaconda3\envs\mnist-classification\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.
WARNING:tensorflow:From C:\Users\HanByul\Anaconda3\envs\mnist-classification\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\base.py:252: _internal_retry.<locals>.wrap.<locals>.wrapped_fn (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Plea[...]<jupyter_text>18.3.1 커널(Kernel)/Filter<jupyter_code>kernel0 = np.array([[0,1,0], [0, -2, 0], [0, 1, 0]]) # y-edge detection
kernel1 = np.array([[1,1,1], [1, -8, 1], [1,1,1]]) # xy-edge detection
kernel2 = np.array([[1,1,1], [1, 1, 1], [1,1,1]]) # Box Blur
kernel3 = np.array([[-1,-1,0], [-1, 0, 1], [0,1,1]]) # Emboss

kernel_list = [('y-edge', kernel0), ('xy-edge', kernel1), \
               ('blur', kernel2), ('embos', kernel3)] # 커널들 모아놓은 list
def plot_conv_example(conv_input, images, kernel_list):
    import inflect
    p = inflect.engine()
    plt.figure(figsize=(8,8))
    for j in range(len(images)):
        img = images[j].reshape(28,28)
        plt.subplot(5,5,1 + 5*j)
        plt.imshow(img, cmap='gray')
        plt.axis('off')
        plt.title('{0} input'.format(p.ordinal(j)))
        for k, (name, kernel) in enumerate(kernel_list):
            conv_out = tf.nn.conv2d(conv_input, kernel.reshape([3,3,1,1]), strides=[1,1,1,1], padding='SAME')
            conv_out_value = conv_out.eval(feed_dict={x_input: images[j].reshape(-1, 784)})
            plt.subplot(5,5,2+k + 5*j)
            plt.imshow(conv_out_value[0, :, :, 0], cmap='gray')
            plt.axis('off')
            plt.title(name)
    plt.show()
x_input = tf.placeholder(tf.float32, [None, 784])
conv_in = tf.reshape(x_input, shape=[1, 28, 28, 1])

plot_conv_example(conv_in, mnist.train.images[:5], kernel_list)<jupyter_output><empty_output><jupyter_text>18.3.2 Strides<jupyter_code>identity_kernel = np.array([[0,0,0], [0,1,0], [0,0,0]])
x_input = tf.placeholder(tf.float32, [None, 784])
conv_in = tf.reshape(x_input, shape=[1, 28, 28, 1])
plt.figure(figsize=(8,8))
for k, stride_num in enumerate([1, 2, 4, 7, 14], 1):
    plt.subplot(1,5,k)
    conv_out = tf.nn.conv2d(conv_in, identity_kernel.reshape([3,3,1,1]), \
                            strides=[1,stride_num,stride_num,1], padding='SAME')
    val = conv_out.eval(feed_dict={x_input:mnist.train.images[:1]})
    plt.imshow(val[0, :, :, 0], cmap='gray')
    plt.title('stride : {0}\n shape=({1}, {2})'.format(stride_num, conv_out.shape[1], conv_out.shape[2]))
plt.show()<jupyter_output><empty_output><jupyter_text>18.3.3 Padding<jupyter_code>identity_kernel = np.array([[0,0,0], [0,1,0], [0,0,0]])
x_input = tf.placeholder(tf.float32, [None, 784])
conv_in = tf.reshape(x_input, shape=[1, 28, 28, 1])
plt.figure(figsize=(8,8))
for k, padding_style in enumerate(['VALID', 'SAME'], 1):
    plt.subplot(1,2,k)
    conv_out = tf.nn.conv2d(conv_in, identity_kernel.reshape([3,3,1,1]), \
                            strides=[1,1,1,1], padding=padding_style)
    val = conv_out.eval(feed_dict={x_input:mnist.train.images[:1]})
    plt.imshow(val[0, :, :, 0], cmap='gray')
    plt.title('padding : {0}\n shape=({1}, {2})'.format(padding_style, conv_out.shape[1], conv_out.shape[2]))
plt.show()<jupyter_output><empty_output><jupyter_text>18.4 Max-Pooling<jupyter_code>identity_kernel = np.array([[0,0,0], [0,1,0], [0,0,0]])
x_input = tf.placeholder(tf.float32, [None, 784])
conv_in = tf.reshape(x_input, shape=[1, 28, 28, 1])
plt.figure(figsize=(8,8))
for k, pooling_size in enumerate([2,3,4,5], 1):
    plt.subplot(1,4,k)
    conv_out = tf.layers.max_pooling2d(conv_in, pooling_size, pooling_size)
    val = conv_out.eval(feed_dict={x_input:mnist.train.images[:1]})
    plt.imshow(val[0, :, :, 0], cmap='gray')
    plt.title('pooling size : {0}\n shape=({1}, {2})'.format(pooling_size, conv_out.shape[1], conv_out.shape[2]))
plt.show()<jupyter_output><empty_output><jupyter_text>18.8.1 데이터 훓어보기<jupyter_code>import numpy as np
import matplotlib.pylab as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/")
print("="*40)
print("Train Data Shape : {0}".format(mnist.train.images.shape))
print("Train Label Shape : {0}".format(mnist.train.labels.shape))
print("Test Data Shape : {0}".format(mnist.test.images.shape))
print("Test Label Shape : {0}".format(mnist.test.labels.shape))

print("="*40)
for i in range(10):
    print("Train Label {0} : {1}".format(i, np.count_nonzero(mnist.train.labels == i)))
print("Train Total : {0}".format(len(mnist.train.labels)))

print("="*40)
for i in range(10):
    print("Test Label {0} : {1}".format(i, np.count_nonzero(mnist.test.labels == i)))
print("Test Total : {0}".format(len(mnist.test.labels)))

print("="*40)
print("Train > Min : {0}, Max : {1}".format(mnist.train.images.min(), mnist.train.images.max()))
print("Test > Min : {0}, Max : {1}".format(mnist.test.images.min(), mnist.test.images.max()))
img_rows, img_cols = 28, 28
plt.figure(figsize=(16,8))
for k in range(32):
    img = mnist.train.images[k].reshape(img_rows,img_cols)
    label = mnist.train.labels[k]
    plt.subplot(4,8,1+k)
    plt.imshow(img, cmap='gray')
    plt.axis('off')
    plt.title('label : {0}'.format(label))
plt.show()<jupyter_output><empty_output><jupyter_text>18.8.2 One-Hot 인코딩<jupyter_code>mnist = input_data.read_data_sets("MNIST_data/")
print(mnist.train.labels[:10])
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
print(mnist.train.labels[:10])
print(np.argmax(mnist.train.labels[:10], 1))
x_train, y_train = mnist.train.images, mnist.train.labels
x_test, y_test = mnist.test.images, mnist.test.labels<jupyter_output><empty_output><jupyter_text>18.8.3 CNN 모델 구축하기<jupyter_code>import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 784])

x_img = tf.reshape(x, shape=[-1, 28, 28, 1])
conv1 = tf.layers.conv2d(x_img, 32, 3, activation=tf.nn.relu)
conv1 = tf.layers.max_pooling2d(conv1, 2, 2)

conv2 = tf.layers.conv2d(conv1, 64, 3, activation=tf.nn.relu)
conv2 = tf.layers.max_pooling2d(conv2, 2, 2)

conv2 = tf.layers.dropout(conv2, 0.25)

fc1 = tf.contrib.layers.flatten(conv2)
fc1 = tf.layers.dense(fc1, 128, activation=tf.nn.relu)
conv2 = tf.layers.dropout(fc1, 0.5)

y = tf.placeholder(tf.float32, [None, 10])
model = tf.nn.softmax(tf.layers.dense(fc1, 10))<jupyter_output><empty_output><jupyter_text>18.8.4 최적화 문제 설정<jupyter_code>loss = tf.reduce_mean(tf.reduce_sum(-y * tf.log(model),1))
train = tf.train.AdamOptimizer().minimize(loss)

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(model,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))<jupyter_output><empty_output><jupyter_text>18.8.5 하이퍼 파라미터 설정<jupyter_code>batch_size = 64
MaxEpoch = 4<jupyter_output><empty_output><jupyter_text>18.8.6 학습 시작<jupyter_code>sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for epoch in range(MaxEpoch):
    loss_val = 0.0
    for step in range(len(mnist.train.images) // batch_size + 1):
        x_batch, y_batch = mnist.train.next_batch(batch_size)
        batch_loss, _ = sess.run([loss, train], feed_dict={x:x_batch, y:y_batch})
        loss_val += batch_loss * len(x_batch)
    test_loss, test_acc = sess.run([loss, accuracy], feed_dict={x:mnist.test.images, y:mnist.test.labels})
    loss_val /= len(mnist.train.images)
    print('Epoch {0} : loss = {1:4.2E}, test_loss = {2:4.2E}, test_acc = {3:4.4f}'.format(epoch, loss_val, test_loss, test_acc))<jupyter_output>Epoch 0 : loss = 1.68E-01, test_loss = 5.02E-02, test_acc = 0.9835
Epoch 1 : loss = 4.90E-02, test_loss = 3.86E-02, test_acc = 0.9872
Epoch 2 : loss = 3.22E-02, test_loss = 2.73E-02, test_acc = 0.9911
Epoch 3 : loss = 2.43E-02, test_loss = 3.02E-02, test_acc = 0.9895<jupyter_text>18.8.7 정확도 확인<jupyter_code>acc = sess.run(accuracy, feed_dict={x:mnist.test.images, y:mnist.test.labels})
print(acc)<jupyter_output>0.9895
{'fraction_non_alphanumeric': 0.17906976744186046, 'fraction_numerical': 0.05249169435215947, 'mean_word_length': 5.118563685636857, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '277110', 'license': 'MIT', 'n_tokens_mistral': 3939, 'n_tokens_neox': 3773, 'n_words': 604, 'path': 'notebooks/CH18.ipynb', 'repo_name': 'DNRY/dlopt', 'size': '9030'}