52Hz commited on
Commit
d95213b
·
1 Parent(s): 3314f9f

Delete demo.py

Browse files
Files changed (1) hide show
  1. demo.py +0 -89
demo.py DELETED
@@ -1,89 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- import torch.nn.functional as F
4
- import torchvision.transforms.functional as TF
5
- from PIL import Image
6
- import os
7
- import utils
8
- from skimage import img_as_ubyte
9
- from collections import OrderedDict
10
- from natsort import natsorted
11
- from glob import glob
12
- import cv2
13
- import argparse
14
- from model.CMFNet import CMFNet
15
-
16
- model = CMFNet()
17
-
18
- parser = argparse.ArgumentParser(description='Demo Image Restoration')
19
- parser.add_argument('--input_dir', default='./demo_samples/deraindrop', type=str, help='Input images folder')
20
- parser.add_argument('--result_dir', default='./demo_results', type=str, help='Directory for results')
21
- parser.add_argument('--weights', default='./pretrained_model/deraindrop_DeRainDrop_CMFNet.pth', type=str, help='Path to weights')
22
-
23
- args = parser.parse_args()
24
-
25
- def save_img(filepath, img):
26
- cv2.imwrite(filepath, cv2.cvtColor(img, cv2.COLOR_RGB2BGR))
27
-
28
- def load_checkpoint(model, weights):
29
- checkpoint = torch.load(weights)
30
- try:
31
- model.load_state_dict(checkpoint["state_dict"])
32
- except:
33
- state_dict = checkpoint["state_dict"]
34
- new_state_dict = OrderedDict()
35
- for k, v in state_dict.items():
36
- name = k[7:] # remove `module.`
37
- new_state_dict[name] = v
38
- model.load_state_dict(new_state_dict)
39
-
40
- inp_dir = args.input_dir
41
- out_dir = args.result_dir
42
-
43
- os.makedirs(out_dir, exist_ok=True)
44
-
45
- files = natsorted(glob(os.path.join(inp_dir, '*.jpg'))
46
- + glob(os.path.join(inp_dir, '*.JPG'))
47
- + glob(os.path.join(inp_dir, '*.png'))
48
- + glob(os.path.join(inp_dir, '*.PNG')))
49
-
50
- if len(files) == 0:
51
- raise Exception(f"No files found at {inp_dir}")
52
-
53
- # Load corresponding model architecture and weights
54
- model = CMFNet()
55
- model.cuda()
56
-
57
- load_checkpoint(model, args.weights)
58
- model.eval()
59
-
60
- img_multiple_of = 8
61
- print('restoring images......')
62
- for file_ in files:
63
- img = Image.open(file_).convert('RGB')
64
- input_ = TF.to_tensor(img).unsqueeze(0).cuda()
65
-
66
- # Pad the input if not_multiple_of 8
67
- h, w = input_.shape[2], input_.shape[3]
68
- H, W = ((h + img_multiple_of) // img_multiple_of) * img_multiple_of, (
69
- (w + img_multiple_of) // img_multiple_of) * img_multiple_of
70
- padh = H - h if h % img_multiple_of != 0 else 0
71
- padw = W - w if w % img_multiple_of != 0 else 0
72
- input_ = F.pad(input_, (0, padw, 0, padh), 'reflect')
73
-
74
- with torch.no_grad():
75
- restored = model(input_)
76
- restored = restored[4]
77
- restored = torch.clamp(restored, 0, 1)
78
-
79
- # Un-pad the output
80
- restored = restored[:, :, :h, :w]
81
-
82
- restored = restored.permute(0, 2, 3, 1).cpu().detach().numpy()
83
- restored = img_as_ubyte(restored[0])
84
-
85
- f = os.path.splitext(os.path.split(file_)[-1])[0]
86
- save_img((os.path.join(out_dir, f + '.png')), restored)
87
-
88
- print(f"Files saved at {out_dir}")
89
- print('finish !')