-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinference.py
94 lines (77 loc) · 2.74 KB
/
inference.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import argparse
import logging
import os
import numpy as np
import torch
from skimage import io
from configs import CFG
from datas import build_dataset, build_transform
from models import build_model
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('config',
type=str,
help='config file')
parser.add_argument('checkpoint',
type=str,
help='checkpoint file')
parser.add_argument('input',
type=str,
help='input image file')
parser.add_argument('--output',
type=str,
default='output.tif',
help='output segmentation map file')
parser.add_argument('--device',
type=str,
default='cuda:0',
help='device for inferring')
parser.add_argument('--no-show',
action='store_true',
help='whether not to show output segmentation map')
parser.add_argument('--no-save',
action='store_true',
help='whether not to save output segmentation map')
args = parser.parse_args()
return args
def main():
# parse command line arguments
args = parse_args()
# merge config with config file
CFG.merge_from_file(args.config)
# build transform
transform = build_transform('test')
# build dataset
test_dataset = build_dataset('test')
NUM_CHANNELS = test_dataset.num_channels
NUM_CLASSES = test_dataset.num_classes
# build model
model = build_model(NUM_CHANNELS, NUM_CLASSES)
model.to(args.device)
# load checkpoint
if not os.path.isfile(args.checkpoint):
raise RuntimeError('checkpoint {} not found'.format(args.checkpoint))
checkpoint = torch.load(args.checkpoint)
model.load_state_dict(checkpoint['model']['state_dict'])
best_miou = checkpoint['metric']['mIoU']
logging.info('load checkpoint {} with mIoU={:.4f}'.format(args.checkpoint, best_miou))
# infer
model.eval() # set model to evaluation mode
x = io.imread(args.input) # read image
x = transform(image=x)['image'] # preprocess image
x = x.unsqueeze(0) # sample to batch
x = x.to(args.device)
y = model(x)
pred = y.argmax(axis=1)
pred = pred.data.cpu().numpy()
pred = pred.squeeze(axis=0)
pred = pred.astype(np.uint8)
for label in test_dataset.labels:
pred[pred == label] = test_dataset.label2pixel(label)
if not args.no_show:
io.imshow(pred)
io.show()
if not args.no_save:
io.imsave(args.output, pred)
if __name__ == '__main__':
main()