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
'Visual Intelligence > Generative Model' 카테고리의 다른 글
[Generative Model] 오토인코더 (MNIST) (0) | 2022.12.08 |
---|---|
[Generative Model] 오토인코더 (AutoEncoder) (0) | 2022.12.08 |
생성 모델 (Generative Model) (0) | 2022.12.08 |
[Generative Model] 노이즈 제거 (MNIST) (0) | 2022.11.25 |
[Generative Model] 단일 이미지 오토인코더 (0) | 2022.11.25 |