728x90
반응형
SMALL
합성곱 오토인코더 (Convolutional Autoencoder)
합성곱 오토인코더 (Convolutional Autoencoder)는 오토인코더 (Autoencoder)의 일종으로, 이미지와 같은 고차원 데이터를 저차원으로 압축하고 복원하는 데 사용된다. 합성곱 오토인코더는 합성곱 계층 (convolutional layer)과 풀링 계층 (pooling layer)을 사용하여 입력 데이터의 공간적 정보를 보존하면서 압축한다. 이러한 합성곱 오토인코더는 이미지 처리 분야에서 많이 사용되며, 이미지 노이즈 제거, 이미지 생성 등에 활용된다.
라이브러리
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras import layers
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Model
def preprocess(array):
"""
Normalizes the supplied array and reshapes it into the appropriate format.
"""
array = array.astype("float32") / 255.0
array = np.reshape(array, (len(array), 28, 28, 1))
return array
def noise(array):
"""
Adds random noise to each image in the supplied array.
"""
noise_factor = 0.4
noisy_array = array + noise_factor * np.random.normal(
loc=0.0, scale=1.0, size=array.shape
)
return np.clip(noisy_array, 0.0, 1.0)
def display(array1, array2):
"""
Displays ten random images from each one of the supplied arrays.
"""
n = 10
indices = np.random.randint(len(array1), size=n)
images1 = array1[indices, :]
images2 = array2[indices, :]
plt.figure(figsize=(20, 4))
for i, (image1, image2) in enumerate(zip(images1, images2)):
ax = plt.subplot(2, n, i + 1)
plt.imshow(image1.reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax = plt.subplot(2, n, i + 1 + n)
plt.imshow(image2.reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
데이터 로드
(train_data, _), (test_data, _) = mnist.load_data()
# Normalize and reshape the data
train_data = preprocess(train_data)
test_data = preprocess(test_data)
# Create a copy of the data with added noise
noisy_train_data = noise(train_data)
noisy_test_data = noise(test_data)
# Display the train data and a version of it with added noise
display(train_data, noisy_train_data)
합성곱 오토인코더 정의
input = layers.Input(shape=(28, 28, 1))
# Encoder
x = layers.Conv2D(32, (3, 3), activation="relu", padding="same")(input)
x = layers.MaxPooling2D((2, 2), padding="same")(x)
x = layers.Conv2D(32, (3, 3), activation="relu", padding="same")(x)
x = layers.MaxPooling2D((2, 2), padding="same")(x)
# Decoder
x = layers.Conv2DTranspose(32, (3, 3), strides=2, activation="relu", padding="same")(x)
x = layers.Conv2DTranspose(32, (3, 3), strides=2, activation="relu", padding="same")(x)
x = layers.Conv2D(1, (3, 3), activation="sigmoid", padding="same")(x)
# Autoencoder
autoencoder = Model(input, x)
autoencoder.compile(optimizer="adam", loss="binary_crossentropy")
autoencoder.summary()
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 28, 28, 1)] 0
_________________________________________________________________
conv2d (Conv2D) (None, 28, 28, 32) 320
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 32) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 14, 14, 32) 9248
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 7, 7, 32) 0
_________________________________________________________________
conv2d_transpose (Conv2DTran (None, 14, 14, 32) 9248
_________________________________________________________________
conv2d_transpose_1 (Conv2DTr (None, 28, 28, 32) 9248
_________________________________________________________________
conv2d_2 (Conv2D) (None, 28, 28, 1) 289
=================================================================
Total params: 28,353
Trainable params: 28,353
Non-trainable params: 0
_________________________________________________________________
모델 학습
autoencoder.fit(
x=train_data,
y=train_data,
epochs=50,
batch_size=128,
shuffle=True,
validation_data=(test_data, test_data),
)
predictions = autoencoder.predict(test_data)
display(test_data, predictions)
autoencoder.fit(
x=noisy_train_data,
y=train_data,
epochs=100,
batch_size=128,
shuffle=True,
validation_data=(noisy_test_data, test_data),
)
predictions = autoencoder.predict(noisy_test_data)
display(noisy_test_data, predictions)
https://keras.io/examples/vision/autoencoder/
728x90
반응형
LIST
'Visual Intelligence > Image Deep Learning' 카테고리의 다른 글
ImageDataGenerator 훈련 및 검증 데이터 분할 (0) | 2023.09.06 |
---|---|
[시각 지능] Roboflow (0) | 2022.09.04 |
[시각 지능] TPU (Tensor Processing Unit) (0) | 2022.09.03 |
[시각 지능] Coffee Classification (0) | 2022.08.28 |
[시각 지능] COVID-19 Radiography (0) | 2022.08.27 |