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
np.random.seed(42)
SIZE = 256
img_data = []
img = cv2.imread('monalisa.jpg', 1) # Change 1 to 0 for grey images
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 이미지를 BGR에서 RGB로 변환
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.
모델 구성
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: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 256, 256, 32) 896
max_pooling2d (MaxPooling2D (None, 128, 128, 32) 0
)
conv2d_1 (Conv2D) (None, 128, 128, 8) 2312
max_pooling2d_1 (MaxPooling (None, 64, 64, 8) 0
2D)
conv2d_2 (Conv2D) (None, 64, 64, 8) 584
max_pooling2d_2 (MaxPooling (None, 32, 32, 8) 0
2D)
conv2d_3 (Conv2D) (None, 32, 32, 8) 584
up_sampling2d (UpSampling2D (None, 64, 64, 8) 0
)
conv2d_4 (Conv2D) (None, 64, 64, 8) 584
up_sampling2d_1 (UpSampling (None, 128, 128, 8) 0
2D)
conv2d_5 (Conv2D) (None, 128, 128, 32) 2336
up_sampling2d_2 (UpSampling (None, 256, 256, 32) 0
2D)
conv2d_6 (Conv2D) (None, 256, 256, 3) 867
=================================================================
Total params: 8,163
Trainable params: 8,163
Non-trainable params: 0
_________________________________________________________________
모델 학습
model.fit(img_array, img_array,
epochs = 5000,
shuffle = True)
Epoch 5000/5000
1/1 [==============================] - 0s 286ms/step - loss: 0.0013 - accuracy: 0.9187
pred = model.predict(img_array)
imshow(pred[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] 서로 다른 이미지 오토인코더 (0) | 2022.11.25 |
[Generative Model] 노이즈 제거 (MNIST) (0) | 2022.11.25 |