1. FCN에서 CNN(LeNet)으로
기존 FCN은 입력 이미지를 1차원 벡터로 Flatten 해서 처리했기 때문에, 이미지의 공간적 정보(위치, 구조 등) 를 전혀 활용하지 못했다. 따라서 공간 구조를 보존하면서 특징을 추출할 수 있는 모델, CNN이 등장하게 되었고, 그 대표적인 초기 구조가 바로 LeNet-5이다.
2. LeNet의 도입 이유
- 이미지의 공간적 패턴을 활용하기 위함
- 파라미터 수를 줄이기 위한 weight sharing
- 위치에 덜 민감한 특징 추출 (Pooling)
- Convolution -> Edge, shape, texture와 같은 특징을 자동으로 학습
- Pooling -> 불필요한 정보 축소 및 연산량 감소
- Fully Connected -> 최종 분류
3. LeNet 구조
# Input: 1x28x28 이미지 (MNIST)
[1] C1: Convolution (6 filters, 5x5 kernel) → 6x24x24
[2] S2: Average Pooling (2x2) → 6x12x12
[3] C3: Convolution (16 filters, 5x5) → 16x8x8
[4] S4: Average Pooling (2x2) → 16x4x4
[5] C5: Fully Connected Convolution (120 units) → flatten
[6] FC6: Fully Connected (84 units)
[7] Output Layer: 10-class softmax
4. 파이썬으로 LeNet 구현
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
print("NumPy Version :{}".format(np.__version__))
print("TensorFlow Version :{}".format(tf.__version__))
print("Matplotlib Version :{}".format(plt.matplotlib.__version__))
#########################################################################################
# Load MNIST
#########################################################################################
(train_data, train_labels), (test_data, test_labels) = mnist.load_data()
train_data = train_data.astype(np.float32)
train_data = np.expand_dims(train_data, axis=-1)
train_data = train_data / 255.0
test_data = test_data.astype(np.float32)
test_data = np.expand_dims(test_data, axis=-1)
test_data = test_data / 255.0
#########################################################################################
# Model Create
#########################################################################################
model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(28, 28, 1)))
model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=3, padding='same',activation='sigmoid'))
model.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=3, padding='same',activation='sigmoid'))
model.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=3, padding='same',activation='sigmoid'))
model.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(10, activation='softmax'))
model.summary()
#########################################################################################
# Training
#########################################################################################
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_data, train_labels, shuffle=True,
epochs=10, validation_data=(test_data, test_labels))
#########################################################################################
# Inference
#########################################################################################
def Make_Result_Plot(suptitle:str, data:np.ndarray, label:np.ndarray, y_max:np.ndarray):
fig_result, ax_result = plt.subplots(2,5,figsize=(18, 7))
fig_result.suptitle(suptitle)
for idx in range(10):
ax_result[idx//5,idx%5].imshow(data[idx].reshape((28,28)),cmap="binary")
ax_result[idx//5,idx%5].set_title("test_data[{}] (label : {} / y : {})".format(idx, label[idx], y_max[idx]))
y_out = model.predict(test_data)
y_max = np.argmax(y_out, axis=1).reshape((-1, 1))
Make_Result_Plot("After Training", test_data, test_labels, y_max)
'AI SOC COURSE > Python (Keras)' 카테고리의 다른 글
Python Course Review - 05. CNN 학습기법 (1) | 2025.04.10 |
---|---|
Python Course Review - 03. CNN(Convolutional Neural Network) (0) | 2025.04.10 |
Python Course Review - 02. Keras를 이용한 Sequential Model 생성 (1) | 2025.04.09 |
Python Course Review - 01. Machine Learning (0) | 2025.04.09 |
Python Course Review - 00. Machine Learning 개요 (1) | 2025.04.09 |