-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.py
More file actions
297 lines (239 loc) · 10.4 KB
/
util.py
File metadata and controls
297 lines (239 loc) · 10.4 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import torch
import yaml
import os
import copy
from collections import OrderedDict
def get_config(config):
with open(config, 'r') as stream:
return yaml.load(stream)
def get_params(model, lr, num_tasks, task_conditional, tasks):
if hasattr(model, 'module'):
model = model.module
# Set learning rate for common branches according to the number of update per iteration
if task_conditional:
multi_backprop_lr = lr / num_tasks
else:
multi_backprop_lr = copy.deepcopy(lr)
# Define appropriate lr for each parameter
params_list = []
for name, params in model.named_parameters():
if params.requires_grad:
if any(task in name for task in tasks):
params_list.append({'params': params, 'lr': lr})
else:
params_list.append({'params': params, 'lr': multi_backprop_lr})
return params_list
def activate_gpus(config):
"""Identify which GPU to activate
Args:
config: Configuration dictionary with project hyperparameters
Returns:
dict: Required information for GPU/CPU training
"""
if torch.cuda.is_available() and config['gpu_id'] > -1:
use_gpu = True
gpu_id = config['gpu_id']
else:
use_gpu = False
gpu_id = []
device = torch.device("cuda:" + str(gpu_id) if use_gpu else "cpu")
return {'device': device, 'gpu_id': gpu_id, 'use_gpu': use_gpu}
def mdl_to_device(mdl, gpu_info):
"""Send model to device (GPU/CPU)
Args:
mdl (object): Model
gpu_info(dict): Dictionary with required GPU information
Returns:
mdl (object): Model sent to device
"""
mdl.to(gpu_info['device'])
return mdl
def get_best_model(dirname, key):
"""Get best model
Args:
dirname (str): Directory name
key(str): Name of the model
Returns:
Name of best model
"""
if os.path.exists(dirname) is False:
return None
file_name = key + '_best.pth'
return os.path.join(dirname, file_name)
def get_last_model(dirname, key):
"""Get best model
Args:
dirname (str): Directory name
key(str): Name of the model
Returns:
Name of best model
"""
if os.path.exists(dirname) is False:
return None
file_name = key + '_last.pth'
return os.path.join(dirname, file_name)
def tensor_to_device(tensor, gpu_info):
"""Send tensor to device (GPU/CPU)
Args:
tensor (tensor): Any tensor
gpu_info(dict): Dictionary with required GPU information
Returns:
tensor (tensor): The tensor sent to the device
"""
return tensor.to(gpu_info['device'])
def dict_to_device(sample, gpu_info):
"""Send dictionary of tensors to device (GPU/CPU)
Args:
sample (dict): Dictionary of tensors (image and targets)
gpu_info(dict): Dictionary with required GPU information
Returns:
tensor (tensor): The tensor sent to the device
"""
sample['image'] = tensor_to_device(sample['image'], gpu_info)
for key, target in sample['labels'].items():
sample['labels'][key] = tensor_to_device(sample['labels'][key], gpu_info)
return sample
def create_dir(directory):
"""Create directory if it does not exist
Args:
directory(str): Directory to create
"""
if not os.path.exists(directory):
print("Creating directory: {}".format(dir))
os.makedirs(directory)
def create_results_dir(results_dir, exp_name):
"""Create the required results directory if it does not exist
Args:
results_dir(str): Directory to create
exp_name(str): Name of the experiment to be used in the directory created
Returns:
exp_dir (str): Path of experiment directory
checkpoint_dir (str): Path of checkpoint directory
img_dir (str): Path of image directory
"""
create_dir(results_dir)
exp_dir = os.path.join(results_dir, exp_name)
create_dir(exp_dir)
checkpoint_dir = os.path.join(exp_dir, 'checkpoints')
create_dir(checkpoint_dir)
img_dir = os.path.join(exp_dir, 'images')
create_dir(img_dir)
return exp_dir, checkpoint_dir, img_dir
def create_pred_dir(results_dir, exp_name, config):
"""Create the required results directory if it does not exist
Args:
results_dir(str): Directory to create
exp_name(str): Name of the experiment to be used in the directory created
config: Configuration dictionary with project hyperparameters
Returns:
checkpoint_dir (str): Path of checkpoint directory
pred_dir (str): Path of experiment directory
"""
exp_dir = os.path.join(results_dir, exp_name)
checkpoint_dir = os.path.join(exp_dir, 'checkpoints')
pred_dir = os.path.join(exp_dir, 'predictions')
create_dir(pred_dir)
dataset = config['dataset']
tasks = [k for k, v in config['dataset']['tasks_weighting'].items()]
for task in tasks:
task_dir = os.path.join(pred_dir, task)
create_dir(task_dir)
return checkpoint_dir, pred_dir
def write_loss(iteration, writer, model_statistics):
"""Create the required results directory if it does not exist
Args:
iteration (int): Current iteration
writer (object): Writer to log performance
model_statistics (dict): Statistics to log
"""
for key, value in model_statistics.items():
writer.add_scalar(key, value, iteration + 1)
def write_param(iteration, writer, model):
"""Create the required results directory if it does not exist
Args:
iteration (int): Current iteration
writer (object): Writer to log performance
model: Pytorch model
"""
for name, param in model.named_parameters():
writer.add_histogram(name + '_value', param.clone().cpu().data.numpy(), iteration + 1)
def write_grad(iteration, writer, model):
"""Create the required results directory if it does not exist
Args:
iteration (int): Current iteration
writer (object): Writer to log performance
model: Pytorch model
"""
for name, param in model.named_parameters():
if param.grad is not None:
writer.add_histogram(name + '_grad', param.grad.clone().cpu().data.numpy(), iteration + 1)
def dataset_model_info(name):
""""""
# Define the information for the datasets manually
# Name of task, and information of the tasks including output dim and if normalization is required
PascalContextMT = OrderedDict([('edge', {'out_dim': 1, 'normalize': False}),
('human_parts', {'out_dim': 7, 'normalize': False}),
('semseg', {'out_dim': 21, 'normalize': False}),
('normals', {'out_dim': 3, 'normalize': True}),
('sal', {'out_dim': 1, 'normalize': False}),
])
NYUDMT = OrderedDict([('edge', {'out_dim': 1, 'normalize': False}),
('semseg', {'out_dim': 41, 'normalize': False}),
('normals', {'out_dim': 3, 'normalize': True}),
('depth', {'out_dim': 1, 'normalize': False}),
])
Datasets = {'PascalContextMT': PascalContextMT,
'NYUDMT': NYUDMT}
if name in Datasets:
return Datasets[name]
else:
raise ValueError('Dataset {} not supported'.format(name))
def save_img(samples, outputs, tasks_weighting, pred_decoder, save_dir):
img_name = samples['meta']['image'][0]
# Cut image borders
orig_img_dim = (samples['meta']['im_size'][0][0].detach().cpu().numpy(),
samples['meta']['im_size'][1][0].detach().cpu().numpy())
current_img_dim = tuple(outputs[list(tasks_weighting.keys())[0]].size()[-2:])
delta_height = current_img_dim[0] - orig_img_dim[0]
delta_width = current_img_dim[1] - orig_img_dim[1]
height_location = [delta_height // 2, (delta_height // 2) + orig_img_dim[0]]
width_location = [delta_width // 2, (delta_width // 2) + orig_img_dim[1]]
assert height_location[1] - height_location[0] == orig_img_dim[0]
assert width_location[1] - width_location[0] == orig_img_dim[1]
# Save predictions for the different tasks
for ind, (task, _) in enumerate(tasks_weighting.items()):
if task in {'sal', 'edge'}:
pred = torch.sigmoid(outputs[task].squeeze()).detach().cpu().numpy()
pred_decoder.save_pred(save_dir, img_name, task,
pred[height_location[0]:height_location[1], width_location[0]:width_location[1]])
elif task in {'human_parts', 'semseg'}:
pred = torch.argmax(outputs[task].squeeze(), dim=0).detach().cpu().numpy()
pred_decoder.save_pred(save_dir, img_name, task,
pred[height_location[0]:height_location[1], width_location[0]:width_location[1]])
elif task in {'normals'}:
pred = outputs[task].squeeze().detach().cpu().numpy()
pred_decoder.save_pred(save_dir, img_name, task,
pred[:, height_location[0]:height_location[1], width_location[0]:width_location[1]])
elif task in {'depth'}:
pred = outputs[task].squeeze().detach().cpu().numpy()
pred_decoder.save_pred(save_dir, img_name, task,
pred[height_location[0]:height_location[1], width_location[0]:width_location[1]])
else:
raise ValueError('Image saving task {} is not supported'.format(task))
def dic_for_img_vis(samples, outputs, tasks_weighting):
plot_dictionary = OrderedDict()
plot_dictionary['image'] = samples['image'].squeeze().cpu().numpy()
for ind, (task, _) in enumerate(tasks_weighting.items()):
if task in {'sal', 'edge'}:
pred = torch.sigmoid(outputs[task].squeeze()).detach().cpu().numpy()
elif task in {'human_parts', 'semseg'}:
pred = torch.argmax(outputs[task].squeeze(), dim=0).detach().cpu().numpy()
elif task in {'normals'}:
pred = outputs[task].squeeze().detach().cpu().numpy()
else:
raise ValueError('Task {} for input decoding is not supported'.format(task))
label = samples['labels'][task].squeeze().cpu().numpy()
plot_dictionary[task] = {'pred': pred,
'gt': label,
}
return plot_dictionary