본문 바로가기
Visual Intelligence/Generative Model

[Generative Model] 서로 다른 이미지 오토인코더

by goatlab 2022. 11. 25.
728x90
반응형
SMALL

라이브러리

 

from matplotlib.pyplot import imshow
import numpy as np
import cv2
from tensorflow.keras.utils import img_to_array
from tensorflow.keras.layers import Conv2D, MaxPooling2D, UpSampling2D
from tensorflow.keras.models import Sequential

SIZE=256  # Limiting to 256 size image as my laptop cannot handle larger images. 
img_data = []

img = cv2.imread('einstein.jpg', 1)   # Change 1 to 0 for Grey scale images
imshow(img)

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img,(SIZE, SIZE))
img_data.append(img_to_array(img))

img_array = np.reshape(img_data, (len(img_data), SIZE, SIZE, 3))
img_array = img_array.astype('float32') / 255.

img_data2 = []

img2 = cv2.imread('monalisa.jpg', 1)   #Change 1 to 0 for Grey scale images
imshow(img2)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
img2 = cv2.resize(img2,(SIZE, SIZE))
img_data2.append(img_to_array(img2))

img_array2 = np.reshape(img_data2, (len(img_data2), SIZE, SIZE, 3))
img_array2 = img_array2.astype('float32') / 255.

 

모델 구성

 

model = Sequential()

model.add(Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(SIZE, SIZE, 3)))
model.add(MaxPooling2D((2, 2), padding='same'))
model.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D((2, 2), padding='same'))
model.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D((2, 2), padding='same'))  
model.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
model.add(UpSampling2D((2, 2)))
model.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
model.add(UpSampling2D((2, 2)))
model.add(Conv2D(32, (3, 3), activation='relu', padding='same'))
model.add(UpSampling2D((2, 2)))
model.add(Conv2D(3, (3, 3), activation='relu', padding='same'))

model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])

model.summary()

 

모델 학습

 

model.fit(img_array, img_array2,
        epochs=5000,                  
        shuffle=True)
Epoch 5000/5000
1/1 [==============================] - 0s 283ms/step - loss: 0.0022 - accuracy: 0.8324

 

모델 평가

 

pred = model.predict(img_array)
imshow(pred[0].reshape(SIZE,SIZE,3), cmap="gray")

pred2 = model.predict(img_array2)
imshow(pred2[0].reshape(SIZE,SIZE,3), cmap="gray")

728x90
반응형
LIST