이전에 DNN 으로 했던 이미지 분류를 CNN 을 이용해 분류
import tensorflow as tf
from tensorflow.keras.datasets import fashion_mnist
# 데이터 가져와서 학습용과 테스트용으로 분리
(X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()
# 컬러 이미지든 그레이 스케일 이미지든 전부 처리할 수 있는 인공지능 개발을 위해서
# 4차원으로 reshape 한다.
>>> X_train.shape
(60000, 28, 28)
>>> X_train = X_train.reshape(60000,28,28,1)
>>> X_test.shape
(10000, 28, 28)
>>> X_test = X_test.reshape(10000,28,28,1)
모델링
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
>>> def build_model():
model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu', input_shape=(28,28,1)))
model.add(MaxPooling2D(pool_size=(2, 2), strides=2))
model.add(Conv2D(filters=64, kernel_size=(2, 2), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=2))
model.add(Flatten())
model.add(Dense(128, 'relu'))
model.add(Dense(10, 'softmax'))
model.compile('adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
return model
>>> model = build_model()
학습 후 테스트
>>> early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=10)
>>> epoch_history = model.fit(X_train, y_train,
epochs=1000,
validation_split=0.2,
callbacks=[early_stopping])
>>> model.evaluate(X_test, y_test)
'인공지능 > Deep Learning' 카테고리의 다른 글
[Deep Learning] Transfer Learning Fine tuning (0) | 2024.04.19 |
---|---|
[Deep Learning] 이미지 분류 딥러닝 Transfer Learning, MobileNetV2 활용 (1) | 2024.04.19 |
[Deep Learning] 이미지 파일 학습 데이터로 만들기(+이미지 증강) ImageDataGenerator (0) | 2024.04.18 |
[Deep Learning] 파이썬 압축 파일 풀기 zipfile (0) | 2024.04.18 |
[Deep Learning] CNN Pooling (0) | 2024.04.18 |