-
[TensorFlow] 03-1. 합성곱 신경망으로 이미지 분류하기Machine Learning 2024. 5. 9. 11:34
구현 모델: 합성곱 신경망(CNN)을 이용한 28x28 흑백 이미지를 입력받아 셔츠, 바지, 드레스 등의 종류 구별
모듈 불러오기
import tensorflow as tf
데이터 준비하기
data = tf.keras.datasets.fashion_mnist (training_images, training_labels), (test_images, test_labels) = data.load_data()
TensorFlow의 합성곱 신경망은 R,G,B 3가지의 채널이 존재하는 이미지에 대해 설계되었기 때문에 채널 차원(dimenssion)을 추가하여야한다.
ndarray.reshape 메소드를 통해 기존 3차원(이미지 개수, 가로 픽셀, 세로 픽셀)이었던 데이터를 4차원(이미지 개수, 가로 픽셀, 세로 픽셀, 채널)으로 데이터를 재정의해준다.
기존 데이터는 흑백이므로 채널은 1개만 존재한다.
0-255의 값을0-1로 정규화하는 작업도 진행해준다.
training_images = training_images.reshape(60000, 28, 28, 1) training_images = training_images / 255.0 test_images = test_images.reshape(10000, 28, 28, 1) test_images = test_images / 255.0
모델 구성하기
레이어 구성:
- 입력 (28,28,1)
Conv2D(64개의 (3x3)필터 채널) + Relu
Input: $(28, 28, 1)$
Output: $(26, 26, 64)$, 이미지 4면의 끝쪽 픽셀이 잘림
Params: $640=(inputChannels\times filtersize+bias)\times channels=(1\times3\times3+1)\times64$MaxPooling2D(2x2 픽셀을 1x1로 압축)
Input: $(26, 26, 64)$
Output: $(13, 13, 64)$Conv2D(64개의 (3x3)필터 채널) + Relu
Input: $(13, 13, 64)$
Output: $(11, 11, 64)$, 이미지 4면의 끝쪽 픽셀이 잘림
Params: $36928=(inputChannels\times filtersize+bias)\times channels=(64\times3\times3+1)\times64$MaxPooling2D(2x2 픽셀을 1x1로 압축)
Input: $(11, 11, 64)$
Output: $(5, 5, 64)$,Flatten(입력된 3차원 데이터를 1차원 배열로 재구성), 784
Input: $(5, 5, 64)$
Output: $1600=5\times5\times64$Dense(입력된 배열을 연산 후 128 길이의 배열로 출력) + Relu, 128
Input: $1600$
Output: $128$
Params: $204928=(weights + bias) \times neurons=(1600+1)\times128$Dense(입력된 배열을 연산 후 softmax를 통해 10개의 확률로 출력) + SoftMax, 10
Input: $128$
Output: $10$
Params: $1290=(weights + bias) \times neurons=(1600+1)\times10$- 출력(10)
손실 계산:
- 옵티마이저(optimizer): Adam(Adaptive Moment Estimation)
- 손실함수(loss, Loss Function): sparse_categorical_crossentropy(0,1,2,3 등의 정수 값의 라벨에 대한 다중 분류 손실함수)
모델 학습하기tf.keras.Model.fit(x, y)model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(64, (3, 3), activation='relu', input_shape=(28,28,1)), tf.keras.layers.MaxPooling2D((2,2)), tf.keras.layers.Conv2D(64, (3, 3), activation='relu'), tf.keras.layers.MaxPooling2D((2,2)), tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation=tf.nn.relu), tf.keras.layers.Dense(10, activation=tf.nn.softmax) ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
- Input = training_images
- ouput = training_labels
- epochs = 학습의 반복 수
model.fit(training_images, training_labels, epochs=50)
예측하기
model.evaluate(test_images, test_labels)
313/313 [==============================] - 4s 11ms/step - loss: 1.0087 - accuracy: 0.9072 [1.0086950063705444, 0.9071999788284302]
- loss(error, 손실) = 1.0087
- accuracy(정확도) = 90.72%
모델 출력 살펴보기
classifications = model.predict(test_images) print(classifications[0]) print(test_labels[0])
313/313 [==============================] - 3s 11ms/step [0.0000000e+00 2.4038821e-33 2.5273516e-33 4.5973653e-35 4.2456518e-24 4.0134513e-31 0.0000000e+00 6.5647037e-20 8.0693845e-38 9.9999994e-01] 9
모델은 라벨 0-9 에대한 확률을 출력한다. 라벨 9의 확률이 99.9%로 가장 높아 실제 정답인 9와 일치한다.
완전 연결 계층으로만 이루어진 모델로 학습하였을 경우 86%였던 정확도가 90.72%로 크게 오른 모습을 보인다. 레이어 구조를 더 강화하고 에폭 수를 조절하면 더 좋은 결과를 보일 것으로 예측된다.'Machine Learning' 카테고리의 다른 글
[Tensorflow] 06. 순환 신경망 & LSTM (2) 2024.07.21 [Tensorflow] 05. 자연어 처리 - Embedding (2) 2024.07.10 [Tensorflow] 04. 자연어 처리 - Tokneizer (2) 2024.05.16 [TensorFlow] 02. 이미지 분류하기 (0) 2024.04.27 [TensorFlow] 01. 1차 방정식 학습하기 (0) 2024.04.10