-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpredict.py
68 lines (55 loc) · 2.49 KB
/
predict.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# encoding:utf-8
"""Run prediction on one or more images."""
import argparse
import os
import time
import torch
from PIL import Image
import utils as ut
def main():
"""Script entry point."""
parser = argparse.ArgumentParser(
description='Run prediction on one or more images.')
parser.add_argument('image', nargs='+', help='Image file(s) to process.')
# Supported models:
# - ens: ensemble of 5 models at different resolutions, with histogram
# equalization for the first 2 (default)
# - ens_he: ensemble of 5 models, with histogram equalization for all
# - single: single model full resolution model, with histogram equalization
parser.add_argument('--model', default='ens',
choices=('ens', 'ens_he', 'single'),
help='Model to use.')
parser.add_argument('--scale', default=1.0, type=float,
help='Scale factor for the image.')
parser.add_argument('--raw', action='store_true', help='return raw '
'predictions (before post-processing).')
parser.add_argument('--out', default='predictions/',
help='Output directory.')
args = parser.parse_args()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
equalized = args.model == 'ens_he'
models = ut.load_ensemble(equalized=equalized, device=device)
for image_file in args.image:
if not os.path.isfile(image_file):
print(f'File {image_file} not found.')
continue
print(f'Processing {image_file}...', end='', flush=True)
image = ut.load_image(image_file, scale=args.scale)
start = time.time()
pred = ut.predict_with_ensemble(models, image, equalized=equalized,
use_ensemble=args.model != 'single')
print("time: {:.3f} s".format(time.time()-start))
if not args.raw:
print(" cleaning...", end="", flush=True)
start = time.time()
pred = ut.clean_predictions(pred)
print("time: {:.3f} s".format(time.time()-start))
composite = ut.create_overlay(image, pred)
# save results as PNGs
name, _ = os.path.splitext(os.path.basename(image_file))
os.makedirs(args.out, exist_ok=True)
name = os.path.join(args.out, name)
Image.fromarray(pred).save(f"{name}_pred.png")
Image.fromarray(composite).save(f"{name}_overlay.png")
if __name__ == '__main__':
main()