Spaces:
Running
Running
VDSR added
#6
by namnguyen2103 - opened
- .gitattributes +1 -0
- app.py +241 -216
- models/VDSR/vdsr.ipynb +3 -0
- models/VDSR/vdsr.py +66 -0
- models/VDSR/vdsr_checkpoint.pth +3 -0
.gitattributes
CHANGED
|
@@ -37,3 +37,4 @@ models/HAT/hat-for-image-sr-2.ipynb filter=lfs diff=lfs merge=lfs -text
|
|
| 37 |
model/RCAN/rcan-for-image-sr.ipynb filter=lfs diff=lfs merge=lfs -text
|
| 38 |
models/RCAN/rcan-for-image-sr.ipynb filter=lfs diff=lfs merge=lfs -text
|
| 39 |
models/SRGAN/srgan-dl-prj.ipynb filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 37 |
model/RCAN/rcan-for-image-sr.ipynb filter=lfs diff=lfs merge=lfs -text
|
| 38 |
models/RCAN/rcan-for-image-sr.ipynb filter=lfs diff=lfs merge=lfs -text
|
| 39 |
models/SRGAN/srgan-dl-prj.ipynb filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
models/VDSR/vdsr.ipynb filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
|
@@ -1,216 +1,241 @@
|
|
| 1 |
-
import time
|
| 2 |
-
import streamlit as st
|
| 3 |
-
import subprocess
|
| 4 |
-
import numpy as np
|
| 5 |
-
from PIL import Image
|
| 6 |
-
from io import BytesIO
|
| 7 |
-
from models.HAT.hat import *
|
| 8 |
-
from models.RCAN.rcan import *
|
| 9 |
-
from models.SRGAN.srgan import *
|
| 10 |
-
from models.
|
| 11 |
-
from models.Interpolation.
|
| 12 |
-
from models.Interpolation.
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
st.
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
st.
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
st.session_state['
|
| 72 |
-
st.session_state['
|
| 73 |
-
st.session_state['
|
| 74 |
-
st.session_state['
|
| 75 |
-
st.session_state['
|
| 76 |
-
st.session_state['
|
| 77 |
-
st.session_state['
|
| 78 |
-
st.session_state['
|
| 79 |
-
st.session_state['
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
st.session_state['
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
st.session_state['
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
if st.
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
st.
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
st.
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
if st.
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import subprocess
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
from models.HAT.hat import *
|
| 8 |
+
from models.RCAN.rcan import *
|
| 9 |
+
from models.SRGAN.srgan import *
|
| 10 |
+
from models.VDSR.vdsr import *
|
| 11 |
+
from models.Interpolation.nearest_neighbor import NearestNeighbor_for_deployment
|
| 12 |
+
from models.Interpolation.bilinear import Bilinear_for_deployment
|
| 13 |
+
from models.Interpolation.bicubic import Bicubic_for_deployment
|
| 14 |
+
|
| 15 |
+
subprocess.call('pip install natsort', shell=True)
|
| 16 |
+
from models.SRFlow.srflow import *
|
| 17 |
+
|
| 18 |
+
# Initialize session state for enhanced images
|
| 19 |
+
if 'nearest_enhanced_image' not in st.session_state:
|
| 20 |
+
st.session_state['nearest_enhanced_image'] = None
|
| 21 |
+
if 'bilinear_enhanced_image' not in st.session_state:
|
| 22 |
+
st.session_state['bilinear_enhanced_image'] = None
|
| 23 |
+
if 'bicubic_enhanced_image' not in st.session_state:
|
| 24 |
+
st.session_state['bicubic_enhanced_image'] = None
|
| 25 |
+
if 'hat_enhanced_image' not in st.session_state:
|
| 26 |
+
st.session_state['hat_enhanced_image'] = None
|
| 27 |
+
if 'rcan_enhanced_image' not in st.session_state:
|
| 28 |
+
st.session_state['rcan_enhanced_image'] = None
|
| 29 |
+
if 'srgan_enhanced_image' not in st.session_state:
|
| 30 |
+
st.session_state['srgan_enhanced_image'] = None
|
| 31 |
+
if 'srflow_enhanced_image' not in st.session_state:
|
| 32 |
+
st.session_state['srflow_enhanced_image'] = None
|
| 33 |
+
if 'vdsr_enhanced_image' not in st.session_state:
|
| 34 |
+
st.session_state['vdsr_enhanced_image'] = None
|
| 35 |
+
|
| 36 |
+
# Initialize session state for button clicks
|
| 37 |
+
if 'nearest_clicked' not in st.session_state:
|
| 38 |
+
st.session_state['nearest_clicked'] = False
|
| 39 |
+
if 'bilinear_clicked' not in st.session_state:
|
| 40 |
+
st.session_state['bilinear_clicked'] = False
|
| 41 |
+
if 'bicubic_clicked' not in st.session_state:
|
| 42 |
+
st.session_state['bicubic_clicked'] = False
|
| 43 |
+
if 'hat_clicked' not in st.session_state:
|
| 44 |
+
st.session_state['hat_clicked'] = False
|
| 45 |
+
if 'rcan_clicked' not in st.session_state:
|
| 46 |
+
st.session_state['rcan_clicked'] = False
|
| 47 |
+
if 'srgan_clicked' not in st.session_state:
|
| 48 |
+
st.session_state['srgan_clicked'] = False
|
| 49 |
+
if 'srflow_clicked' not in st.session_state:
|
| 50 |
+
st.session_state['srflow_clicked'] = False
|
| 51 |
+
if 'vdsr_clicked' not in st.session_state:
|
| 52 |
+
st.session_state['vdsr_clicked'] = False
|
| 53 |
+
|
| 54 |
+
st.markdown("<h1 style='text-align: center'>Image Super Resolution</h1>", unsafe_allow_html=True)
|
| 55 |
+
|
| 56 |
+
# Sidebar for navigation
|
| 57 |
+
st.sidebar.title("Options")
|
| 58 |
+
app_mode = st.sidebar.selectbox("Choose the input source", ["Upload image", "Take a photo"])
|
| 59 |
+
|
| 60 |
+
# Depending on the choice, show the uploader widget or webcam capture
|
| 61 |
+
if app_mode == "Upload image":
|
| 62 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png"], on_change=lambda: reset_states())
|
| 63 |
+
if uploaded_file is not None:
|
| 64 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 65 |
+
elif app_mode == "Take a photo":
|
| 66 |
+
camera_input = st.camera_input("Take a picture", on_change=lambda: reset_states())
|
| 67 |
+
if camera_input is not None:
|
| 68 |
+
image = Image.open(camera_input).convert("RGB")
|
| 69 |
+
|
| 70 |
+
def reset_states():
|
| 71 |
+
st.session_state['hat_enhanced_image'] = None
|
| 72 |
+
st.session_state['rcan_enhanced_image'] = None
|
| 73 |
+
st.session_state['srgan_enhanced_image'] = None
|
| 74 |
+
st.session_state['srflow_enhanced_image'] = None
|
| 75 |
+
st.session_state['bicubic_enhanced_image'] = None
|
| 76 |
+
st.session_state['bilinear_enhanced_image'] = None
|
| 77 |
+
st.session_state['nearest_enhanced_image'] = None
|
| 78 |
+
st.session_state['vdsr_enhanced_image'] = None
|
| 79 |
+
st.session_state['hat_clicked'] = False
|
| 80 |
+
st.session_state['rcan_clicked'] = False
|
| 81 |
+
st.session_state['srgan_clicked'] = False
|
| 82 |
+
st.session_state['srflow_clicked'] = False
|
| 83 |
+
st.session_state['bicubic_clicked'] = False
|
| 84 |
+
st.session_state['bilinear_clicked'] = False
|
| 85 |
+
st.session_state['nearest_clicked'] = False
|
| 86 |
+
st.session_state['vdsr_clicked'] = False
|
| 87 |
+
|
| 88 |
+
def get_image_download_link(img, filename):
|
| 89 |
+
"""Generates a link allowing the PIL image to be downloaded"""
|
| 90 |
+
# Convert the PIL image to Bytes
|
| 91 |
+
buffered = BytesIO()
|
| 92 |
+
img.save(buffered, format="PNG")
|
| 93 |
+
return st.download_button(
|
| 94 |
+
label="Download Image",
|
| 95 |
+
data=buffered.getvalue(),
|
| 96 |
+
file_name=filename,
|
| 97 |
+
mime="image/png"
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
if 'image' in locals():
|
| 101 |
+
# st.image(image, caption='Uploaded Image', use_column_width=True)
|
| 102 |
+
st.write("")
|
| 103 |
+
# ------------------------ Nearest Neighbor ------------------------ #
|
| 104 |
+
if st.button('Enhance with Nearest Neighbor'):
|
| 105 |
+
with st.spinner('Processing using Nearest Neighbor...'):
|
| 106 |
+
enhanced_image = NearestNeighbor_for_deployment(image)
|
| 107 |
+
st.session_state['nearest_enhanced_image'] = enhanced_image
|
| 108 |
+
st.session_state['nearest_clicked'] = True
|
| 109 |
+
st.success('Done!')
|
| 110 |
+
if st.session_state['nearest_enhanced_image'] is not None:
|
| 111 |
+
col1, col2 = st.columns(2)
|
| 112 |
+
col1.header("Original")
|
| 113 |
+
col1.image(image, use_column_width=True)
|
| 114 |
+
col2.header("Enhanced")
|
| 115 |
+
col2.image(st.session_state['nearest_enhanced_image'], use_column_width=True)
|
| 116 |
+
with col2:
|
| 117 |
+
get_image_download_link(st.session_state['nearest_enhanced_image'], 'nearest_enhanced.jpg')
|
| 118 |
+
|
| 119 |
+
# ------------------------ Bilinear ------------------------ #
|
| 120 |
+
if st.button('Enhance with Bilinear'):
|
| 121 |
+
with st.spinner('Processing using Bilinear...'):
|
| 122 |
+
enhanced_image = Bilinear_for_deployment(image)
|
| 123 |
+
st.session_state['bilinear_enhanced_image'] = enhanced_image
|
| 124 |
+
st.session_state['bilinear_clicked'] = True
|
| 125 |
+
st.success('Done!')
|
| 126 |
+
if st.session_state['bilinear_enhanced_image'] is not None:
|
| 127 |
+
col1, col2 = st.columns(2)
|
| 128 |
+
col1.header("Original")
|
| 129 |
+
col1.image(image, use_column_width=True)
|
| 130 |
+
col2.header("Enhanced")
|
| 131 |
+
col2.image(st.session_state['bilinear_enhanced_image'], use_column_width=True)
|
| 132 |
+
with col2:
|
| 133 |
+
get_image_download_link(st.session_state['bilinear_enhanced_image'], 'bilinear_enhanced.jpg')
|
| 134 |
+
|
| 135 |
+
# ------------------------ Bicubic ------------------------ #
|
| 136 |
+
if st.button('Enhance with Bicubic'):
|
| 137 |
+
with st.spinner('Processing using Bicubic...'):
|
| 138 |
+
enhanced_image = Bicubic_for_deployment(image)
|
| 139 |
+
st.session_state['bicubic_enhanced_image'] = enhanced_image
|
| 140 |
+
st.session_state['bicubic_clicked'] = True
|
| 141 |
+
st.success('Done!')
|
| 142 |
+
if st.session_state['bicubic_enhanced_image'] is not None:
|
| 143 |
+
col1, col2 = st.columns(2)
|
| 144 |
+
col1.header("Original")
|
| 145 |
+
col1.image(image, use_column_width=True)
|
| 146 |
+
col2.header("Enhanced")
|
| 147 |
+
col2.image(st.session_state['bicubic_enhanced_image'], use_column_width=True)
|
| 148 |
+
with col2:
|
| 149 |
+
get_image_download_link(st.session_state['bicubic_enhanced_image'], 'bicubic_enhanced.jpg')
|
| 150 |
+
|
| 151 |
+
# ------------------------ HAT ------------------------ #
|
| 152 |
+
if st.button('Enhance with HAT'):
|
| 153 |
+
with st.spinner('Processing using HAT...'):
|
| 154 |
+
with st.spinner('Wait for it... the model is processing the image'):
|
| 155 |
+
enhanced_image = HAT_for_deployment(image)
|
| 156 |
+
st.session_state['hat_enhanced_image'] = enhanced_image
|
| 157 |
+
st.session_state['hat_clicked'] = True
|
| 158 |
+
st.success('Done!')
|
| 159 |
+
if st.session_state['hat_enhanced_image'] is not None:
|
| 160 |
+
col1, col2 = st.columns(2)
|
| 161 |
+
col1.header("Original")
|
| 162 |
+
col1.image(image, use_column_width=True)
|
| 163 |
+
col2.header("Enhanced")
|
| 164 |
+
col2.image(st.session_state['hat_enhanced_image'], use_column_width=True)
|
| 165 |
+
with col2:
|
| 166 |
+
get_image_download_link(st.session_state['hat_enhanced_image'], 'hat_enhanced.jpg')
|
| 167 |
+
|
| 168 |
+
# ------------------------ RCAN ------------------------ #
|
| 169 |
+
if st.button('Enhance with RCAN'):
|
| 170 |
+
with st.spinner('Processing using RCAN...'):
|
| 171 |
+
with st.spinner('Wait for it... the model is processing the image'):
|
| 172 |
+
rcan_model = RCAN()
|
| 173 |
+
device = torch.device('cpu') if not torch.cuda.is_available() else torch.device('cuda')
|
| 174 |
+
rcan_model.load_state_dict(torch.load('models/RCAN/rcan_checkpoint.pth', map_location=device))
|
| 175 |
+
enhanced_image = rcan_model.inference(image)
|
| 176 |
+
st.session_state['rcan_enhanced_image'] = enhanced_image
|
| 177 |
+
st.session_state['rcan_clicked'] = True
|
| 178 |
+
st.success('Done!')
|
| 179 |
+
if st.session_state['rcan_enhanced_image'] is not None:
|
| 180 |
+
col1, col2 = st.columns(2)
|
| 181 |
+
col1.header("Original")
|
| 182 |
+
col1.image(image, use_column_width=True)
|
| 183 |
+
col2.header("Enhanced")
|
| 184 |
+
col2.image(st.session_state['rcan_enhanced_image'], use_column_width=True)
|
| 185 |
+
with col2:
|
| 186 |
+
get_image_download_link(st.session_state['rcan_enhanced_image'], 'rcan_enhanced.jpg')
|
| 187 |
+
|
| 188 |
+
# --------------------------SRGAN-------------------------- #
|
| 189 |
+
if st.button('Enhance with SRGAN'):
|
| 190 |
+
with st.spinner('Processing using SRGAN...'):
|
| 191 |
+
with st.spinner('Wait for it... the model is processing the image'):
|
| 192 |
+
srgan_model = GeneratorResnet()
|
| 193 |
+
device = torch.device('cpu') if not torch.cuda.is_available() else torch.device('cuda')
|
| 194 |
+
srgan_model = torch.load('models/SRGAN/srgan_checkpoint.pth', map_location=device)
|
| 195 |
+
enhanced_image = srgan_model.inference(image)
|
| 196 |
+
st.session_state['srgan_enhanced_image'] = enhanced_image
|
| 197 |
+
st.session_state['srgan_clicked'] = True
|
| 198 |
+
st.success('Done!')
|
| 199 |
+
if st.session_state['srgan_enhanced_image'] is not None:
|
| 200 |
+
col1, col2 = st.columns(2)
|
| 201 |
+
col1.header("Original")
|
| 202 |
+
col1.image(image, use_column_width=True)
|
| 203 |
+
col2.header("Enhanced")
|
| 204 |
+
col2.image(st.session_state['srgan_enhanced_image'], use_column_width=True)
|
| 205 |
+
with col2:
|
| 206 |
+
get_image_download_link(st.session_state['srgan_enhanced_image'], 'srgan_enhanced.jpg')
|
| 207 |
+
|
| 208 |
+
# ------------------------ SRFlow ------------------------ #
|
| 209 |
+
if st.button('Enhance with SRFlow'):
|
| 210 |
+
with st.spinner('Processing using SRFlow...'):
|
| 211 |
+
with st.spinner('Wait for it... the model is processing the image'):
|
| 212 |
+
enhanced_image = return_SRFlow_result(image)
|
| 213 |
+
st.session_state['srflow_enhanced_image'] = enhanced_image
|
| 214 |
+
st.session_state['srflow_clicked'] = True
|
| 215 |
+
st.success('Done!')
|
| 216 |
+
if st.session_state['srflow_enhanced_image'] is not None:
|
| 217 |
+
col1, col2 = st.columns(2)
|
| 218 |
+
col1.header("Original")
|
| 219 |
+
col1.image(image, use_column_width=True)
|
| 220 |
+
col2.header("Enhanced")
|
| 221 |
+
col2.image(st.session_state['srflow_enhanced_image'], use_column_width=True)
|
| 222 |
+
with col2:
|
| 223 |
+
get_image_download_link(st.session_state['srflow_enhanced_image'], 'srflow_enhanced.jpg')
|
| 224 |
+
|
| 225 |
+
# ------------------------ VDSR ------------------------ #
|
| 226 |
+
if st.button('Enhance with VDSR'):
|
| 227 |
+
with st.spinner('Processing using VDSR...'):
|
| 228 |
+
# Load the VDSR model
|
| 229 |
+
vdsr_model = torch.load('models/VDSR/vdsr_checkpoint.pth', map_location=torch.device('cpu'))
|
| 230 |
+
enhanced_image = vdsr_model.inference(image)
|
| 231 |
+
st.session_state['vdsr_enhanced_image'] = enhanced_image
|
| 232 |
+
st.session_state['vdsr_clicked'] = True
|
| 233 |
+
st.success('Done!')
|
| 234 |
+
if st.session_state['vdsr_enhanced_image'] is not None:
|
| 235 |
+
col1, col2 = st.columns(2)
|
| 236 |
+
col1.header("Original")
|
| 237 |
+
col1.image(image, use_column_width=True)
|
| 238 |
+
col2.header("Enhanced")
|
| 239 |
+
col2.image(st.session_state['vdsr_enhanced_image'], use_column_width=True)
|
| 240 |
+
with col2:
|
| 241 |
+
get_image_download_link(st.session_state['vdsr_enhanced_image'], 'vdsr_enhanced.jpg')
|
models/VDSR/vdsr.ipynb
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:acef3257ccc3ca0d7071b66a57f326b921e5696b917fe75c773afc08d14debcb
|
| 3 |
+
size 11173590
|
models/VDSR/vdsr.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from torchvision.transforms import ToTensor
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
+
from math import sqrt
|
| 7 |
+
import torch.nn.functional as F
|
| 8 |
+
|
| 9 |
+
#define class Block contain conv and relu layer
|
| 10 |
+
class Block(nn.Module):
|
| 11 |
+
def __init__(self):
|
| 12 |
+
super(Block, self).__init__()
|
| 13 |
+
self.conv = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1, bias=False)
|
| 14 |
+
self.relu = nn.ReLU(inplace=True)
|
| 15 |
+
|
| 16 |
+
def forward(self, x):
|
| 17 |
+
return self.relu(self.conv(x))
|
| 18 |
+
|
| 19 |
+
class VDSR(nn.Module):
|
| 20 |
+
def __init__(self, in_channels=3, out_channels=3, num_blocks=18):
|
| 21 |
+
super(VDSR, self).__init__()
|
| 22 |
+
self.residual_layer = self.make_layer(Block, num_blocks)
|
| 23 |
+
self.input = nn.Conv2d(in_channels=in_channels, out_channels=64, kernel_size=3, stride=1, padding=1, bias=False)
|
| 24 |
+
self.output = nn.Conv2d(in_channels=64, out_channels=out_channels, kernel_size=3, stride=1, padding=1, bias=False)
|
| 25 |
+
self.relu = nn.ReLU(inplace=True)
|
| 26 |
+
|
| 27 |
+
for m in self.modules():
|
| 28 |
+
if isinstance(m, nn.Conv2d):
|
| 29 |
+
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
|
| 30 |
+
m.weight.data.normal_(0, sqrt(2. / n))
|
| 31 |
+
|
| 32 |
+
def make_layer(self, block, num_layers):
|
| 33 |
+
layers=[]
|
| 34 |
+
for _ in range(num_layers):
|
| 35 |
+
layers.append(block())
|
| 36 |
+
return nn.Sequential(*layers)
|
| 37 |
+
|
| 38 |
+
def forward(self, x):
|
| 39 |
+
residual = x
|
| 40 |
+
out = self.relu(self.input(x))
|
| 41 |
+
out = self.residual_layer(out)
|
| 42 |
+
out = self.output(out)
|
| 43 |
+
out = torch.add(residual, out)
|
| 44 |
+
return out
|
| 45 |
+
|
| 46 |
+
def inference(self, x):
|
| 47 |
+
"""
|
| 48 |
+
x is a PIL image
|
| 49 |
+
"""
|
| 50 |
+
self.eval()
|
| 51 |
+
with torch.no_grad():
|
| 52 |
+
x = ToTensor()(x).unsqueeze(0)
|
| 53 |
+
x = F.interpolate(x, scale_factor=4, mode='bicubic', align_corners=False).clamp(0, 1)
|
| 54 |
+
x = self.forward(x).clamp(0, 1)
|
| 55 |
+
x = Image.fromarray((x.squeeze(0).permute(1, 2, 0).detach().numpy() * 255).astype('uint8'))
|
| 56 |
+
return x
|
| 57 |
+
|
| 58 |
+
if __name__ == '__main__':
|
| 59 |
+
current_dir = os.path.dirname(os.path.realpath(__file__))
|
| 60 |
+
|
| 61 |
+
model = torch.load(current_dir + '/vdsr_checkpoint.pth', map_location=torch.device('cpu'))
|
| 62 |
+
model.eval()
|
| 63 |
+
with torch.no_grad():
|
| 64 |
+
input_image = Image.open('images/demo.png')
|
| 65 |
+
output_image = model.inference(input_image)
|
| 66 |
+
print(input_image.size, output_image.size)
|
models/VDSR/vdsr_checkpoint.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:839903f0373cbbd60ee00c4367436a718dd8689c8fda1d901471aa0f570e54be
|
| 3 |
+
size 2689946
|