๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[Kaggle]Super Image Resolution_๊ณ ํ์ง ์ด๋ฏธ์ง ๋ง๋ค๊ธฐ ๋ณธ๋ฌธ
[Kaggle]Super Image Resolution_๊ณ ํ์ง ์ด๋ฏธ์ง ๋ง๋ค๊ธฐ
์ง์ง์ํ์นด 2022. 2. 7. 18:57220207 ์์ฑ
<๋ณธ ๋ธ๋ก๊ทธ๋ Kaggle ์ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค>
https://www.kaggle.com/theblackmamba31/low-resolution-images-to-high-resolution
http://dmqm.korea.ac.kr/activity/seminar/318
1. Image super resolution
: ์ ํ์ง์ ์ด๋ฏธ์ง๋ฅผ ๊ณ ํ์ง๋ก ๋ฐ๊พธ์ด์ฃผ๋ ๊ฒ
: Image Super-Resolution ์ ์ ํด์๋ ์ด๋ฏธ์ง๋ฅผ ๊ณ ํด์๋๋ก ๋ณํ์ํค๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐ
: ๋ฏธ๋์ด, ์๋ฃ, ๋ณด์ ๋ฑ ๋ค์ํ ์ฐ์ ๋ถ์ผ์์ ์ค์ํ ๋ฌธ์ ๋ก ๋๋
- ํด์๋๋ฅผ ๋์ฌ์ฃผ๋ Super Resolution
- SR = Super-Resolution (์ด๊ณ ํด์๋)
: ๋ชจ๋ธ๋ก ์์ฑ๋ image ์๋ฏธ
- HR = High-Resolution (๊ณ ํด์๋)
: Output Image ์๋ฏธ
- LR = Low-Resolution (์ ํด์๋)
: Input Image
- ํด์๋(resolution)๋
: ๋์งํธ ํ๋ฉด์ ๊ตฌ์ฑํ๋ ๊ฐ๊ฐ์ ๋ฉด ์์ ์ผ๋ง๋ ๋ง์ ์์ ํฝ์ (pixel)์ ๋ด์๋๊ฐ
: ํฝ์ ์ ๋์งํธ ์ด๋ฏธ์ง๋ฅผ ์ด๋ฃจ๋ ๊ฐ์ฅ ์์ ์์
: ํด์๋๊ฐ ๋์ ์๋ก ํฝ์ ์ ์๊ฐ ์ฆ๊ฐ
2. Image Super Resolution ๋ฅ๋ฌ๋ ๋ชจ๋ธ ๊ฐ๋ฐ
: ๋ฎ์ ํ์ง์ ์ด๋ฏธ์ง๋ฅผ ๋์ ํ์ง์ ์ด๋ฏธ์ง๋ก ๋ณํํ๋๋ก ๋ชจ๋ธ์ ํ๋ จ์ํค๋ ๋ฐ์ดํฐ ์ธํธ
1) CNN_๋ฅ๋ฌ๋์ ํตํ ์ด๋ฏธ์ง ํฅ์
- Conv ์ฐ์ฐ
: ๋ฐ์ดํฐ์์ ํน์ง๋ง์ด ๋จ๊ณ ์ฐจ์์ด ์ค์ด๋๋๋ฐ, ๋ง์ผ ํจ๋ฉ์ ๋ถ์ฌ์ ์ฐจ์ ์ถ์๋ฅผ ๋ง๋๋ค๊ณ ํ๋๋ผ๋,
pooling์ ํตํด ๋ํ์ ํน์ง์ ์ถ์ถํ๋ ๊ฒ
- Downsampling : ์ฐจ์์ด ์ถ์๋์ด Denseํ ๋ฐ์ดํฐ๋ฅผ ๋ง๋๋ ๊ฒ
- Upsampling : ์ด๊ฒ์ ๋ค์ ํด๋ ํด์ ์๋ณธ์ sparseํ ๋ฐ์ดํฐ๋ฅผ ๋ง๋๋ ๊ฒ
=> CNN์ผ๋ก ๋ฐ์ดํฐ๊ฐ ์ถ์ฝ๋๊ณ , upsampling ์ ํตํ์ฌ ์ฐจ์์ด ๋์ด๋๋ฉฐ,
๋ชจ๋ธ์ด ์ด sampling ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ , ๊ฐ์ฅ ํ๋ฅ ์ด ๋์ ๋ฐ์ดํฐ๋ฅผ ์ถ๋ก ํด๋ด๋๋ก, ํํฐ๋ค์ ํ์ต
2) CNN
- HR ์ด๋ฏธ์ง๋ฅผ ์์ง
- Downsampling ํํฐ๋ฅผ ์ ์ฉํ์ฌ LR ์ด๋ฏธ์ง๋ฅผ ์ป๊ธฐ
- CNN์ HR ์ด๋ฏธ์ง์ ์์ค๋ ์ธ๋ถ ์ ๋ณด๋ฅผ ๋ณต์ํ๊ธฐ ์ํด Downsampling ํํฐ์ ์ญํจ์ Upsampling ๋ก ํ์ต
+) ์ ํด์๋์ธ Input ์ด๋ฏธ์ง๋ฅผ ๊ณ ํ์ง ์ด๋ฏธ์ง ํฌ๊ธฐ์ ๋ง์ถฐ์ผ ํจ
+) ํฌ๊ธฐ๋ฅผ ๋ง์ถ ๋ค, ๋น ์์ญ์ ๋ํ ๊ฐ์ ์ด๋ป๊ฒ ์ฑ์ธ ๊ฒ์ธ๊ฐ์ ๋ํ ๊ณ ๋ฏผ ํ์
- 1) Interpolation-based upsampling
- Nearest-neighbor : ์ธ์ ํฝ์ ๊ฐ
- Bilnear : ์ธ์ 4๊ฐ ํฝ์ ๊ฐ์ liner interpolation, ๋น ์์ญ์ ๋ํด ๊ฑฐ๋ฆฌ ๊ฐ์ค์น๋ฅผ ๋ ํ๊ท ๊ฐ์ผ๋ก ์ฑ์
-
- Bicubic : ์ธ์ 16๊ฐ ํฝ์ ๊ฐ์ cubic interpolation
- 2) Learning-based upsampling
- Transpose Convolution Layer : ์ ํด์๋ ์ด๋ฏธ์ง ํ์ฅ ํ (ํ์ฅํ ์์ญ์ zero padding ์งํ) conv ์ฐ์ฐ
-
- Sub-pixel Convolution Layer : conv ์ฐ์ฐ ํ pixel ๋ณ๋ก ๋ชจ์
3. CNN ๊ธฐ๋ฐ ๋ฅ๋ฌ๋ ๋ชจ๋ธ
- ๋ผ์ด๋ธ๋ฌ๋ฆฌ Import
import numpy as np
import tensorflow as tf
import keras
import cv2
from keras.models import Sequential
from keras.preprocessing.image import img_to_array
import os
from tqdm import tqdm # dataloader ์ ํตํด data๋ฅผ download ํ ๋, ์ํ๋ฐ ๋ฃ์ด๋๊ธฐ (์ํ ์งํ ๋ณด์ฌ์ฃผ๊ธฐ)
import re
import matplotlib.pyplot as plt
from keras import layers
- load Data
def sorted_alphanumeric(data):
# isdigit() : ๋ฌธ์์ด์ด ์ซ์๋ก ๊ตฌ์ฑ๋์ด ์๋์ง ํ๋ณ
convert = lambda text : int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key : [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(data, key = alphanum_key)
SIZE = 256
high_img = []
path = 'dataset/Raw Data/high_res'
files = os.listdir(path)
files = sorted_alphanumeric(files)
for i in tqdm(files) :
if i == '855.jpg' :
break
else :
img = cv2.imread(path + '/' + i, 1)
# open bgr to rgb
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# resize
img = cv2.resize(img, (SIZE, SIZE))
img = img.astype('float') / 255.0
high_img.append(img_to_array(img))
low_img = []
path = "dataset/Raw Data/low_res"
files = os.listdir(path)
files = sorted_alphanumeric(files)
for i in tqdm(files) :
if i == "855.jpg" :
break
else :
img = cv2.imread(path + '/' + i, 1)
# resize
img = cv2.resize(img, (SIZE, SIZE))
img = img.astype("float32") / 255.0
low_img.append(img_to_array(img))
- Data ์๊ฐํ
for i in range(4) :
a = np.random.randint(0, 855)
plt.figure(figsize=(10, 10,))
plt.subplot(1,2,1)
plt.title("High Resolution", color = "pink", fontsize = 20)
plt.imshow(high_img[a])
plt.axis('off')
plt.subplot(1,2,2)
plt.title("Low Resolution", color = "black", fontsize = 20)
plt.imshow(low_img[a])
plt.axis("off")
ํ ์๋ ๋๋ค ํ์ง์ด ๋ณ๋ก์ธ๊ฐ,, ์๊น ๋นผ๊ณ
- slicing & reshaping images
train_high_image = high_img[:700]
train_low_image = low_img[:700]
train_high_image = np.reshape(train_high_image,(len(train_high_image),SIZE,SIZE,3))
train_low_image = np.reshape(train_low_image,(len(train_low_image),SIZE,SIZE,3))
print(train_high_image.shape)
print(train_low_image.shape)
validation_high_image = high_img[700:830]
validation_low_image = low_img[700:830]
validation_high_image= np.reshape(validation_high_image,(len(validation_high_image),SIZE,SIZE,3))
validation_low_image = np.reshape(validation_low_image,(len(validation_low_image),SIZE,SIZE,3))
print(validation_high_image.shape)
print(validation_low_image.shape)
test_high_image = high_img[830:]
test_low_image = low_img[830:]
test_high_image= np.reshape(test_high_image,(len(test_high_image),SIZE,SIZE,3))
test_low_image = np.reshape(test_low_image,(len(test_low_image),SIZE,SIZE,3))
print(test_high_image.shape)
print(test_low_image.shape)
print("Shape of training images:",train_high_image.shape)
print("Shape of test images:",test_high_image.shape)
print("Shape of validation images:",validation_high_image.shape)
- Model
def down(filters , kernel_size, apply_batch_normalization = True):
downsample = tf.keras.models.Sequential()
downsample.add(layers.Conv2D(filters,kernel_size,padding = 'same', strides = 2))
if apply_batch_normalization:
downsample.add(layers.BatchNormalization())
downsample.add(keras.layers.LeakyReLU())
return downsample
- batch_normalization : ๊ฐ ๋ ์ด์ด๋ง๋ค ์ ๊ทํ ํ๋ ๋ ์ด์ด๋ฅผ ๋์ด, ๋ณํ๋ ๋ถํฌ๊ฐ ๋์ค์ง ์๋๋ก ์กฐ์
- LeakyReLU : ์ ๋ ฅ ๊ฐ์ด ์์์ผ ๋ ์๋งํ ์ ํ ํ์ฑํ ํจ์
def up(filters, kernel_size, dropout = False):
upsample = tf.keras.models.Sequential()
upsample.add(layers.Conv2DTranspose(filters, kernel_size,padding = 'same', strides = 2))
if dropout:
upsample.dropout(0.2)
upsample.add(keras.layers.LeakyReLU())
return upsample
- Interpolation : ์ธ์ ํฝ์ ๊ฐ์ ํ์ฉํด์ ๋น ์์ญ์ ์ฑ์ฐ๋ ๋ฐฉ๋ฒ
- dropout : ๊ณผ๋์ ํฉ์ด ๋ฐ์ํ๊ธฐ ์ฌ์ด ์ฌ์ธต ์ ๊ฒฝ๋ง ๊ตฌ์กฐ์์ ์ด๋ฅผ ๋ฐฉ์งํ ์ ์๋ ์ ๊ทํ ๊ธฐ๋ฒ์ ํ๋
def model():
inputs = layers.Input(shape= [SIZE,SIZE,3])
d1 = down(128,(3,3),False)(inputs)
d2 = down(128,(3,3),False)(d1)
d3 = down(256,(3,3),True)(d2)
d4 = down(512,(3,3),True)(d3)
d5 = down(512,(3,3),True)(d4)
#upsampling
u1 = up(512,(3,3),False)(d5)
u1 = layers.concatenate([u1,d4])
u2 = up(256,(3,3),False)(u1)
u2 = layers.concatenate([u2,d3])
u3 = up(128,(3,3),False)(u2)
u3 = layers.concatenate([u3,d2])
u4 = up(128,(3,3),False)(u3)
u4 = layers.concatenate([u4,d1])
u5 = up(3,(3,3),False)(u4)
u5 = layers.concatenate([u5,inputs])
output = layers.Conv2D(3,(2,2),strides = 1, padding = 'same')(u5)
return tf.keras.Model(inputs=inputs, outputs=output)
model = model()
model.summary()
- compile
model.compile(optimizer= tf.keras.optimizers.Adam(learning_rate= 0.001),
loss = "mean_absolute_error",
metrics = ['acc'])
- fit model
model.fit(train_low_image, train_high_image, epochs=7, batch_size= 1,
validation_data = (validation_low_image, validation_high_image))
- ์์ธกํ๊ฑฐ ์๊ฐํ
def plot_images(high,low,predicted):
plt.figure(figsize=(15,15))
plt.subplot(1,3,1)
plt.title('High Image', color = 'pink', fontsize = 20)
plt.imshow(high)
plt.subplot(1,3,2)
plt.title('Low Image ', color = 'orange', fontsize = 20)
plt.imshow(low)
plt.subplot(1,3,3)
plt.title('Predicted Image ', color = 'green', fontsize = 20)
plt.imshow(predicted)
plt.show()
for i in range(1,10):
predicted = np.clip(model.predict(test_low_image[i].reshape(1,SIZE, SIZE,3)),0.0,1.0).reshape(SIZE, SIZE,3)
plot_images(test_high_image[i],test_low_image[i],predicted)
+) ํ๋ผ๋ฏธํฐ ๋ฐ๊ฟ์ ์งํํด๋ด
# 1.
Optimizers : Adam
Learning rate : 0.001
Loss : mean_absolute_error
epochs = 10
batch_size = 2
-----------------------------------
Loss : 0.0172
Acc : 0.8596
val_loss : 0.0179
val_acc : 0.8716
Test_loss : 0.018
Test_acc : 0.9121
# 2.
Optimizers : Adam
Learning rate : 0.01
Loss : mean_absolute_error
epochs = 15
batch_size = 20
-----------------------------------
Loss : 0.0248
Acc : 0.8551
val_loss : 0.0222
val_acc : 0.9167
Test_loss : 0.0213
Test_acc : 0.9220
# 1๊ณผ 2 ํ๋ผ๋ฏธํฐ ๋น๊ต
์ฒ์ ๋ชจ๋ธ๊บผ!
loss: 0.0053 - acc: 0.8702 - val_loss: 0.0060 - val_acc: 0.8673
cnn ๋ง๊ณ
ใทใ ๋ฅธ๊ฑธ๋ก๋
๋์ ํด๋ณด์ฅ
'๐ฉโ๐ป ์ปดํจํฐ ๊ตฌ์กฐ > Kaggle' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Kaggle] Credit Card Anomaly Detection (0) | 2022.11.02 |
---|---|
[Kaggle]Pneumonia/Normal Classification(CNN) (0) | 2022.03.20 |
[Kaggle] CNN Architectures (0) | 2022.02.04 |
[Kaggle] HeartAttack ์์ธก (0) | 2022.01.31 |
[Kaggle] Chest X-Ray ํ์ ์ด๋ฏธ์ง ๋ถ๋ฅํ๊ธฐ (0) | 2022.01.29 |