๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[Kaggle]Super Image Resolution_๊ณ ํ™”์งˆ ์ด๋ฏธ์ง€ ๋งŒ๋“ค๊ธฐ ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป ์ปดํ“จํ„ฐ ๊ตฌ์กฐ/Kaggle

[Kaggle]Super Image Resolution_๊ณ ํ™”์งˆ ์ด๋ฏธ์ง€ ๋งŒ๋“ค๊ธฐ

์ง•์ง•์•ŒํŒŒ์นด 2022. 2. 7. 18:57
728x90
๋ฐ˜์‘ํ˜•

220207 ์ž‘์„ฑ

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” Kaggle ์„ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค>

https://www.kaggle.com/theblackmamba31/low-resolution-images-to-high-resolution

 

Low resolution images to high resolution

Explore and run machine learning code with Kaggle Notebooks | Using data from multiple data sources

www.kaggle.com

http://dmqm.korea.ac.kr/activity/seminar/318

 

๊ณ ๋ ค๋Œ€ํ•™๊ต DMQA ์—ฐ๊ตฌ์‹ค

๊ณ ๋ ค๋Œ€ํ•™๊ต ์‚ฐ์—…๊ฒฝ์˜๊ณตํ•™๋ถ€ ๋ฐ์ดํ„ฐ๋งˆ์ด๋‹ ๋ฐ ํ’ˆ์งˆ์• ๋„๋ฆฌํ‹ฑ์Šค ์—ฐ๊ตฌ์‹ค

dmqa.korea.ac.kr

 

 

 

 

 

 

 

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

  1. HR ์ด๋ฏธ์ง€๋ฅผ ์ˆ˜์ง‘
  2. Downsampling ํ•„ํ„ฐ๋ฅผ ์ ์šฉํ•˜์—ฌ LR ์ด๋ฏธ์ง€๋ฅผ ์–ป๊ธฐ
  3. 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 ๋ง๊ณ 
ใ„ทใ…๋ฅธ๊ฑธ๋กœ๋„
๋„์ „ํ•ด๋ณด์žฅ
728x90
๋ฐ˜์‘ํ˜•
Comments