-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpyt_utils.py
217 lines (176 loc) · 7.09 KB
/
pyt_utils.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# encoding: utf-8
import os
import sys
import time
import argparse
from collections import OrderedDict, defaultdict
import torch
import torch.utils.model_zoo as model_zoo
import torch.distributed as dist
from .logger import get_logger
logger = get_logger()
# colour map
label_colours = [(0,0,0)
# 0=background
,(128,0,0),(0,128,0),(128,128,0),(0,0,128),(128,0,128)
# 1=aeroplane, 2=bicycle, 3=bird, 4=boat, 5=bottle
,(0,128,128),(128,128,128),(64,0,0),(192,0,0),(64,128,0)
# 6=bus, 7=car, 8=cat, 9=chair, 10=cow
,(192,128,0),(64,0,128),(192,0,128),(64,128,128),(192,128,128)
# 11=diningtable, 12=dog, 13=horse, 14=motorbike, 15=person
,(0,64,0),(128,64,0),(0,192,0),(128,192,0),(0,64,128)]
# 16=potted plant, 17=sheep, 18=sofa, 19=train, 20=tv/monitor
def reduce_tensor(tensor, dst=0, op=dist.ReduceOp.SUM, world_size=1):
tensor = tensor.clone()
dist.reduce(tensor, dst, op)
if dist.get_rank() == dst:
tensor.div_(world_size)
return tensor
def all_reduce_tensor(tensor, op=dist.ReduceOp.SUM, world_size=1, norm=True):
tensor = tensor.clone()
dist.all_reduce(tensor, op)
if norm:
tensor.div_(world_size)
return tensor
def load_model(model, model_file, is_restore=False):
t_start = time.time()
if isinstance(model_file, str):
device = torch.device('cpu')
state_dict = torch.load(model_file, map_location=device)
if 'model' in state_dict.keys():
state_dict = state_dict['model']
else:
state_dict = model_file
t_ioend = time.time()
if is_restore:
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = 'module.' + k
new_state_dict[name] = v
state_dict = new_state_dict
model.load_state_dict(state_dict, strict=False)
ckpt_keys = set(state_dict.keys())
own_keys = set(model.state_dict().keys())
missing_keys = own_keys - ckpt_keys
unexpected_keys = ckpt_keys - own_keys
if len(missing_keys) > 0:
logger.warning('Missing key(s) in state_dict: {}'.format(
', '.join('{}'.format(k) for k in missing_keys)))
if len(unexpected_keys) > 0:
logger.warning('Unexpected key(s) in state_dict: {}'.format(
', '.join('{}'.format(k) for k in unexpected_keys)))
del state_dict
t_end = time.time()
logger.info(
"Load model, Time usage:\n\tIO: {}, initialize parameters: {}".format(
t_ioend - t_start, t_end - t_ioend))
return model
def parse_devices(input_devices):
if input_devices.endswith('*'):
devices = list(range(torch.cuda.device_count()))
return devices
devices = []
for d in input_devices.split(','):
if '-' in d:
start_device, end_device = d.split('-')[0], d.split('-')[1]
assert start_device != ''
assert end_device != ''
start_device, end_device = int(start_device), int(end_device)
assert start_device < end_device
assert end_device < torch.cuda.device_count()
for sd in range(start_device, end_device + 1):
devices.append(sd)
else:
device = int(d)
assert device < torch.cuda.device_count()
devices.append(device)
logger.info('using devices {}'.format(
', '.join([str(d) for d in devices])))
return devices
def extant_file(x):
"""
'Type' for argparse - checks that file exists but does not open.
"""
if not os.path.exists(x):
# Argparse uses the ArgumentTypeError to give a rejection message like:
# error: argument input: x does not exist
raise argparse.ArgumentTypeError("{0} does not exist".format(x))
return x
def link_file(src, target):
if os.path.isdir(target) or os.path.isfile(target):
os.remove(target)
os.system('ln -s {} {}'.format(src, target))
def ensure_dir(path):
if not os.path.isdir(path):
os.makedirs(path)
def _dbg_interactive(var, value):
from IPython import embed
embed()
def decode_labels(mask, num_images=1, num_classes=21):
"""Decode batch of segmentation masks.
Args:
mask: result of inference after taking argmax.
num_images: number of images to decode from the batch.
num_classes: number of classes to predict (including background).
Returns:
A batch with num_images RGB images of the same size as the input.
"""
mask = mask.data.cpu().numpy()
n, h, w = mask.shape
assert(n >= num_images), 'Batch size %d should be greater or equal than number of images to save %d.' % (n, num_images)
outputs = np.zeros((num_images, h, w, 3), dtype=np.uint8)
for i in range(num_images):
img = Image.new('RGB', (len(mask[i, 0]), len(mask[i])))
pixels = img.load()
for j_, j in enumerate(mask[i, :, :]):
for k_, k in enumerate(j):
if k < num_classes:
pixels[k_,j_] = label_colours[k]
outputs[i] = np.array(img)
return outputs
def decode_predictions(preds, num_images=1, num_classes=21):
"""Decode batch of segmentation masks.
Args:
mask: result of inference after taking argmax.
num_images: number of images to decode from the batch.
num_classes: number of classes to predict (including background).
Returns:
A batch with num_images RGB images of the same size as the input.
"""
if isinstance(preds, list):
preds_list = []
for pred in preds:
preds_list.append(pred[-1].data.cpu().numpy())
preds = np.concatenate(preds_list, axis=0)
else:
preds = preds.data.cpu().numpy()
preds = np.argmax(preds, axis=1)
n, h, w = preds.shape
assert(n >= num_images), 'Batch size %d should be greater or equal than number of images to save %d.' % (n, num_images)
outputs = np.zeros((num_images, h, w, 3), dtype=np.uint8)
for i in range(num_images):
img = Image.new('RGB', (len(preds[i, 0]), len(preds[i])))
pixels = img.load()
for j_, j in enumerate(preds[i, :, :]):
for k_, k in enumerate(j):
if k < num_classes:
pixels[k_,j_] = label_colours[k]
outputs[i] = np.array(img)
return outputs
def inv_preprocess(imgs, num_images, img_mean):
"""Inverse preprocessing of the batch of images.
Add the mean vector and convert from BGR to RGB.
Args:
imgs: batch of input images.
num_images: number of images to apply the inverse transformations on.
img_mean: vector of mean colour values.
Returns:
The batch of the size num_images with the same spatial dimensions as the input.
"""
imgs = imgs.data.cpu().numpy()
n, c, h, w = imgs.shape
assert(n >= num_images), 'Batch size %d should be greater or equal than number of images to save %d.' % (n, num_images)
outputs = np.zeros((num_images, h, w, c), dtype=np.uint8)
for i in range(num_images):
outputs[i] = (np.transpose(imgs[i], (1,2,0)) + img_mean).astype(np.uint8)
return outputs