File size: 1,339 Bytes
c7e6ba9
aa2d9c4
c7e6ba9
 
aa2d9c4
c7e6ba9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa2d9c4
0b2d163
c7e6ba9
0b2d163
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
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
import torch
import gradio as gr
import numpy as np
import torch.nn.functional as F

from Allweather.util import load_img
from basicsr.models.archs.histoformer_arch import Histoformer

model_restoration = Histoformer()
checkpoint = torch.load("Allweather/pretrained_models/net_g_real.pth")
model_restoration.load_state_dict(checkpoint['params'])
model_restoration.cuda()
model_restoration = nn.DataParallel(model_restoration)

def preprocess(file_, factor = 8):
    img = np.float32(load_img(file_))/255.
    img = torch.from_numpy(img).permute(2,0,1)
    input_ = img.unsqueeze(0).cuda()

    # Padding in case images are not multiples of 8
    h,w = input_.shape[2], input_.shape[3]
    H,W = ((h+factor)//factor)*factor, ((w+factor)//factor)*factor
    padh = H-h if h%factor!=0 else 0
    padw = W-w if w%factor!=0 else 0
    input_ = F.pad(input_, (0,padw,0,padh), 'reflect')
    return input_

def predict(input_img):
    prediction = model_restoration(preprocess(input_img))
    return input_img, prediction 

gradio_app = gr.Interface(
    predict,
    inputs=gr.Image(label="Upload images with adverse weather degradations", sources=['upload', 'webcam'], type="pil"),
    outputs=gr.Image(label="Processed Image"),
    title="Image Restoration for All-in-one Adverse Weather",
)

if __name__ == "__main__":
    gradio_app.launch()