Skip to content

Commit 1356b09

Browse files
committed
Support Single Image Inference .
1 parent 08a0c45 commit 1356b09

File tree

8 files changed

+42
-9
lines changed

8 files changed

+42
-9
lines changed

basicsr/demo.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# from basicsr.data import create_dataloader, create_dataset
1010
from basicsr.models import create_model
1111
from basicsr.train import parse_options
12-
from basicsr.utils import FileClient, imfrombytes, img2tensor, padding
12+
from basicsr.utils import FileClient, imfrombytes, img2tensor, padding, tensor2img, imwrite
1313

1414
# from basicsr.utils import (get_env_info, get_root_logger, get_time_str,
1515
# make_exp_dirs)
@@ -37,10 +37,24 @@ def main():
3737

3838

3939
## 2. run inference
40+
opt['dist'] = False
4041
model = create_model(opt)
41-
model.single_image_inference(img, output_path)
4242

43-
print('inference {} .. finished.'.format(img_path))
43+
model.feed_data(data={'lq': img.unsqueeze(dim=0)})
44+
45+
if model.opt['val'].get('grids', False):
46+
model.grids()
47+
48+
model.test()
49+
50+
if model.opt['val'].get('grids', False):
51+
model.grids_inverse()
52+
53+
visuals = model.get_current_visuals()
54+
sr_img = tensor2img([visuals['result']])
55+
imwrite(sr_img, output_path)
56+
57+
print(f'inference {img_path} .. finished. saved to {output_path}')
4458

4559
if __name__ == '__main__':
4660
main()

basicsr/train.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def parse_options(is_train=True):
3535
default='none',
3636
help='job launcher')
3737
parser.add_argument('--local_rank', type=int, default=0)
38+
39+
parser.add_argument('--input_path', type=str, required=False, help='The path to the input image. For single image inference only.')
40+
parser.add_argument('--output_path', type=str, required=False, help='The path to the output image. For single image inference only.')
41+
3842
args = parser.parse_args()
3943
opt = parse(args.opt, is_train=is_train)
4044

@@ -59,6 +63,12 @@ def parse_options(is_train=True):
5963
opt['manual_seed'] = seed
6064
set_random_seed(seed + opt['rank'])
6165

66+
if args.input_path is not None and args.output_path is not None:
67+
opt['img_path'] = {
68+
'input_img': args.input_path,
69+
'output_img': args.output_path
70+
}
71+
6272
return opt
6373

6474

demo/blurry.png

1.03 MB
Loading

demo/deblur_img.png

1.2 MB
Loading

demo/denoise_img.png

61.2 KB
Loading

demo/noisy.png

167 KB
Loading
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
### Pretrained NAFNet Models
22
---
33

4-
* [NAFNet-SIDD-width64](https://drive.google.com/file/d/14Fht1QQJ2gMlk4N1ERCRuElg8JfjrWWR/view?usp=sharing)
5-
* [NAFNet-GoPro-width64](https://drive.google.com/file/d/1S0PVRbyTakYY9a82kujgZLbMihfNBLfC/view?usp=sharing)
4+
please refer to https://github.com/megvii-research/NAFNet/#results-and-pre-trained-models, and download the pretrained models into ./experiments/pretrained_models

readme.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,18 @@ python setup.py develop --no_cuda_ext
3535
### Quick Start
3636
* Image Denoise Colab Demo: [<a href="https://colab.research.google.com/drive/1dkO5AyktmBoWwxBwoKFUurIDn0m4qDXT?usp=sharing"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="google colab logo"></a>](https://colab.research.google.com/drive/1dkO5AyktmBoWwxBwoKFUurIDn0m4qDXT?usp=sharing)
3737
* Image Deblur Colab Demo: [<a href="https://colab.research.google.com/drive/1yR2ClVuMefisH12d_srXMhHnHwwA1YmU?usp=sharing"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="google colab logo"></a>](https://colab.research.google.com/drive/1yR2ClVuMefisH12d_srXMhHnHwwA1YmU?usp=sharing)
38-
39-
38+
* Single Image Inference Demo:
39+
* Image Denoise:
40+
```
41+
python basicsr/demo.py -opt options/test/SIDD/NAFNet-width64.yml --input_path ./demo/noisy.png --output_path ./demo/denoise_img.png
42+
```
43+
* Image Deblur:
44+
```
45+
python basicsr/demo.py -opt options/test/GoPro/NAFNet-width64.yml --input_path ./demo/blurry.png --output_path ./demo/deblur_img.png
46+
```
47+
* ```--input_path```: the path of the degraded image
48+
* ```--output_path```: the path to save the predicted image
49+
* [pretrained models](https://github.com/megvii-research/NAFNet/#results-and-pre-trained-models) should be downloaded.
4050
4151
### Results and Pre-trained Models
4252
@@ -78,8 +88,8 @@ If you have any questions, please contact [email protected] or chuxiaojie@m
7888
7989
<details>
8090
<summary>statistics</summary>
81-
91+
8292
![visitors](https://visitor-badge.glitch.me/badge?page_id=megvii-research/NAFNet)
83-
93+
8494
</details>
8595

0 commit comments

Comments
 (0)