1. Convolution Neural Network
convolution neural network는 이미지 분류에 가장 많이 쓰이는 인공지능 설계 방식이다.
특이한 점으로는 convolution layer와 pooling layer을 가지고 있다는 것이다.
convolution layer는 이미지의 각각의 부분을 나누어 분석하면서 각 부분의 특징을 분류한다고 할 수 있다.
pooling layer는 convolution layer에서 분류한 요소 중 유의미한 정보를 뽑아내는 것 같고
그렇기에 max pooling을 많이 사용하는 거 같다.
2. 예제
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
|
import numpy as np
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
mnist = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data() # 0~9의 손글씨 데이터를 받아온다.
train_images = train_images.astype(np.float32) / 255.
test_images = test_images.astype(np.float32) / 255.
train_labels = tf.keras.utils.to_categorical(train_labels, 10)
test_labels = tf.keras.utils.to_categorical(test_labels, 10)
train_images = train_images.reshape(-1, 28, 28, 1)
train_images = tf.convert_to_tensor(train_images)
test_images = test_images.reshape(-1, 28, 28, 1)
test_images = tf.convert_to_tensor(test_images)
print('train_images.shape', train_images.shape)
print('test_images.shape', test_images.shape)
weight_init = keras.initializers.RandomNormal()
conv2d = keras.layers.Conv2D(filters = 5, kernel_size=3, strides=(2, 2), padding='SAME', kernel_initializer=weight_init) #주로 쓰이는 layer 정의
pool = keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='VALID')
model = tf.keras.Sequential([ #model 정의
conv2d,
pool,
conv2d,
pool,
keras.layers.Flatten(),
keras.layers.Dense(256, use_bias=True, kernel_initializer=keras.initializers.he_uniform()),
keras.layers.Activation(tf.keras.activations.relu),
keras.layers.Dense(256, use_bias=True, kernel_initializer=keras.initializers.he_uniform()),
keras.layers.Activation(tf.keras.activations.relu),
keras.layers.Dense(10, use_bias=True, kernel_initializer=keras.initializers.he_uniform())
])
def loss_func(model, images, labels): #비용 함수
logits = model(images, training=True)
loss = tf.reduce_mean(tf.keras.losses.categorical_crossentropy(y_true=labels, y_pred=logits, from_logits=True))
return loss
def accuracy_func(model, images, labels): #정확도 함수
logits = model(images, training=False)
prediction = tf.equal(tf.argmax(logits, -1), tf.argmax(labels, -1))
accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32))
return accuracy
def grad_func(model, images, labels): #기울기 함수
with tf.GradientTape() as tape:
loss = loss_func(model, images, labels)
return tape.gradient(loss, model.variables)
learning_rate = 0.001
batch_size = 1000
training_iterations = len(train_images)
label_dim = 10
optimizer = tf.keras.optimizers.legacy.Adam(learning_rate=learning_rate)
print(train_labels.shape)
for epoch in range(1): #실제 learning 부분
for i in range(6000):
grads = grad_func(model, tf.reshape(train_images[i], [1, 28, 28, 1]), tf.reshape(train_labels[i], [1, 10]))
optimizer.apply_gradients(grads_and_vars=zip(grads, model.variables))
if i%10==0 :
print("step: [{}], trainloss: {:.8f}, train_accuracy: {:.4f}, test_Accuracy: {:.4f}".format(i, loss_func(model, train_images, train_labels), accuracy_func(model, train_images, train_labels), accuracy_func(model, test_images, test_labels)))
#test
images = test_images[0:100]
labels = model(images, training=False)
labels = np.argmax(labels)
print(images.shape)
for i in range(100):
print(np.argmax(model(images[i:i+1], training=False)))
plt.imshow(images[i], cmap='gray')
plt.show()
|
cs |
1~4 import
1~18 데이터의 전처리
20~21 결과 :
train_images.shape (60000, 28, 28, 1)
test_images.shape (10000, 28, 28, 1)
24 convolution layer 정의 : filters 필터의 갯수, kernelsize 필터의 크기, strides 필터 적용 간격, padding ‘SAME’ input과 output의 크기가 같게 ‘VALID’ padding 없이, kernel_initializer 필터 weight 정의
25 pooling layer 정의
26 model 정의
40~54 비용, 정확도, 기울기 함수 정의
61 optimizer 정의
63 결과 :
(60000, 10)
65~72 실제로 learning을 진행함
결과 :
Test accuracy로 0.9이상의 정확도
75~84 상위 10개 결과 :
'Deep Learning' 카테고리의 다른 글
RHO-1: Not All Tokens are What You Need 논문 리뷰 (0) | 2024.05.03 |
---|---|
Grokking: Generalization Beyond Overfitting on Small Algorithmic Datasets 논문 리뷰 (0) | 2024.04.01 |
손으로 마우스 조종 with Mediapipe (0) | 2024.01.10 |
Recurrent Neural Network in Tensorflow (2) | 2024.01.07 |
Gradient descent algorithm in Tensor flow 경사하강법 (0) | 2023.11.05 |