-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathensemble_infer.py
146 lines (121 loc) · 5.36 KB
/
ensemble_infer.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from __future__ import division
import os
import torch
from torchvision import transforms
import argparse
import json
import torch.distributed as dist
from sys import platform as sys_pf
if sys_pf == 'darwin':
import matplotlib
matplotlib.use("MacOSX")
from utils import is_image_file, pil_loader, process_images, normalize_tensor
from HRNet import HRNet
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def setup(rank, world_size):
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '29501'
# initialize the process group
dist.init_process_group("nccl", rank=rank, world_size=world_size)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Inference on images for corrosion detection")
parser.add_argument('--models', nargs='+', help='List of paths to directory with model file and hypes.json [required]')
parser.add_argument('--image', type=str, help='Path to an image to run inference on')
parser.add_argument('--gt', type=str, help='Optional path to ground truth file, will return confusion matrix.')
parser.add_argument('--target', type=int, default=1, help='Optional target class, default to 1')
parser.add_argument('--out_res', nargs='+', type=int, default=None, help='Optional output resolution')
parser.add_argument('--thresh', type=float, default=None, help='Optional threshold')
args = parser.parse_args()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if dist.is_available():
setup(0, 1)
models = args.models
#TODO pass the model savefiles as args
modelos = ['fold0_epoch110.pt',
'fold1_epoch90.pt',
'fold2_epoch90.pt',
'fold3_epoch110.pt',
'fold4_epoch90.pt',
'fold5_epoch90.pt',
'fold6_epoch90.pt',
'fold7_epoch90.pt',
'fold8_epoch90.pt', ]
print('image file is ', args.image)
if is_image_file(args.image):
image_orig = pil_loader(args.image)
else:
RuntimeError('image provided is not a supported image format')
detector = []
out = []
seg = []
var = []
for model in models:
hypesfile = os.path.join(model, 'hypes.json')
with open(hypesfile, 'r') as f:
hypes = json.load(f)
image_shape = hypes['arch']['image_shape']
num_classes = hypes['arch']['num_classes']
class_colors = hypes['data']['class_colours']
class_labels = hypes['data']['class_labels']
overlay_colours = hypes['data']['overlay_colours']
label_colours = hypes['data']['overlay_colours']
batch_size = 1
batch_shape = [batch_size] + image_shape
weights_factor = hypes['data']['class_weights']
if args.out_res is not None:
if len(args.out_res) == 1:
input_res = [args.out_res[0], args.out_res[0]]
elif len(args.out_res) > 2:
print('out res must be length 2')
exit()
else:
input_res = args.out_res
else:
input_res = hypes['arch']['image_shape'][1:3]
input_transforms = transforms.Compose(
[transforms.Resize(input_res, 0),
transforms.ToTensor(),
transforms.Normalize(hypes['data']['pop_mean'], hypes['data']['pop_std0'])
]
)
channels = hypes['solver']['channels']
image = input_transforms(image_orig)
image = image.unsqueeze(dim=0).to(device)
for modelo in modelos:
modelfile = os.path.join(model, modelo)
seg_model = HRNet(config=hypes)
pretrained_dict = torch.load(modelfile, map_location=device)
if 'state_dict' in pretrained_dict:
pretrained_dict = pretrained_dict['state_dict']
prefix = "module."
keys = sorted(pretrained_dict.keys())
for key in keys:
if key.startswith(prefix):
newkey = key[len(prefix):]
pretrained_dict[newkey] = pretrained_dict.pop(key)
# also strip the prefix in metadata if any.
if "_metadata" in pretrained_dict:
metadata = pretrained_dict["_metadata"]
for key in list(metadata.keys()):
if len(key) == 0:
continue
newkey = key[len(prefix):]
metadata[newkey] = metadata.pop(key)
seg_model.load_state_dict(pretrained_dict)
seg_model.to(device)
with torch.no_grad():
outDict = seg_model(image)
out.append(outDict['out'].squeeze().detach())
var.append(outDict['logVar'].squeeze().detach())
out = torch.stack(out)
var = torch.stack(var)
varmax = var.max()
varmin = var.min()
out = normalize_tensor(out)
var = normalize_tensor(var) * (varmax - varmin)
savename = os.path.join(os.getcwd(), 'figures',
str(hypes['arch']['config']), str(args.thresh),
str('multimodel_' + os.path.splitext(os.path.basename(args.image))[0]))
os.makedirs(os.path.dirname(savename), mode=0o755, exist_ok=True)
fscore = process_images(hypes, savename, image_orig, out, var, args.gt, input_res, threshold=args.thresh,
printout=True)