-
Notifications
You must be signed in to change notification settings - Fork 8
/
Utils.py
156 lines (126 loc) · 5.57 KB
/
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
import os
import imageio
import numpy as np
import torch
import torch.nn as nn
class mor_utils:
def __init__(self, device):
self.device = device
def printTensorList(self, data):
if isinstance(data, dict):
print('Dictionary Containing: ')
print('{')
for key, tensor in data.items():
print('\t', key, end='')
print(' with Tensor of Size: ', tensor.size())
print('}')
else:
print('List Containing: ')
print('[')
for tensor in data:
print('\tTensor of Size: ', tensor.size())
print(']')
def saveModels(self, model, optims, iterations, path):
if isinstance(model, nn.DataParallel):
checkpoint = {
'iters': iterations,
'model': model.module.state_dict(),
'optimizer': optims.state_dict()
}
else:
checkpoint = {
'iters': iterations,
'model': model.state_dict(),
'optimizer': optims.state_dict()
}
torch.save(checkpoint, path)
def loadModels(self, model, path, optims=None, Test=True):
checkpoint = torch.load(path)
model.load_state_dict(checkpoint['model'])
if not Test:
optims.load_state_dict(checkpoint['optimizer'])
return model, optims, checkpoint['iters']
def dumpOutputs(self, vis, preds, gts=None, num=13, iteration=0,
filename='Out_%d_%d.png', Train=True):
if Train:
"""Function to Collage the predictions with the outputs. Expects a single
set and not batches."""
pred_a = preds[0].cpu().detach().clone().numpy()
pred_a = (pred_a / pred_a.max()) * 255
pred_a = pred_a.transpose((1, 2, 0))
pred_a = pred_a.astype(np.uint8)
pred_s = preds[1].cpu().detach().clone().numpy()
pred_s[pred_s < 0] = 0
pred_s = (pred_s / pred_s.max()) * 255
pred_s = pred_s.transpose((1, 2, 0))
pred_s = pred_s.astype(np.uint8)
img = gts[0].cpu().detach().clone().numpy() * 255
img = img.astype(np.uint8)
img = img.transpose(1, 2, 0)
alb = gts[1].cpu().detach().clone().numpy() * 255
alb = alb.astype(np.uint8)
alb = alb.transpose(1, 2, 0)
shd = gts[2].cpu().detach().clone().numpy() * 255
shd = shd.astype(np.uint8)
shd = shd.transpose(1, 2, 0)
norm = preds[2].cpu().detach().clone().numpy() * 255
norm[norm < 0] = 0
norm = (norm / norm.max()) * 255
norm = norm.astype(np.uint8)
norm = norm.transpose(1, 2, 0)
row1 = np.concatenate((img, alb, shd), axis=1)
row2 = np.concatenate((norm, pred_a, pred_s), axis=1)
full = np.concatenate((row1, row2), axis=0)
imageio.imwrite(vis + '/' + filename % (num, iteration), full)
else:
pred_a = preds[0].cpu().detach().clone().numpy()
pred_a = (pred_a / pred_a.max()) * 255
pred_a = pred_a.transpose((1, 2, 0))
pred_a = pred_a.astype(np.uint8)
pred_s = preds[1].cpu().detach().clone().numpy()
pred_s[pred_s < 0] = 0
pred_s = (pred_s / pred_s.max()) * 255
pred_s = pred_s.transpose((1, 2, 0))
pred_s = pred_s.astype(np.uint8)
imageio.imwrite((vis + '/%s_pred_alb.png') % filename, pred_a)
imageio.imwrite((vis + '/%s_pred_shd.png') % filename, pred_s)
def dumpOutputs3(self, vis, preds, gts=None, num=13, iteration=0,
filename='Out_%d_%d.png', Train=True):
if Train:
"""Function to Collage the predictions with the outputs. Expects a single
set and not batches."""
pred_a = preds[0].cpu().detach().clone().numpy()
pred_a = (pred_a / pred_a.max()) * 255
pred_a = pred_a.transpose((1, 2, 0))
pred_a = pred_a.astype(np.uint8)
pred_s = preds[1].cpu().detach().clone().numpy()
pred_s[pred_s < 0] = 0
pred_s = (pred_s / pred_s.max()) * 255
pred_s = pred_s.transpose((1, 2, 0))
pred_s = pred_s.astype(np.uint8)
img = gts[0].cpu().detach().clone().numpy() * 255
img = img.astype(np.uint8)
img = img.transpose(1, 2, 0)
alb = gts[1].cpu().detach().clone().numpy() * 255
alb = alb.astype(np.uint8)
alb = alb.transpose(1, 2, 0)
shd = gts[2].cpu().detach().clone().numpy() * 255
shd = shd.astype(np.uint8)
shd = shd.transpose(1, 2, 0)
norm = preds[2].cpu().detach().clone().numpy() * 255
norm[norm < 0] = 0
norm = (norm / norm.max()) * 255
norm = norm.astype(np.uint8)
norm = norm.transpose(1, 2, 0)
row1 = np.concatenate((img, alb, shd), axis=1)
row2 = np.concatenate((norm, pred_a, pred_s), axis=1)
full = np.concatenate((row1, row2), axis=0)
imageio.imwrite(vis + '/' + filename % (num, iteration), full)
else:
for k, ele in preds.items():
pred = ele.cpu().detach().clone().numpy()
pred[pred < 0] = 0
pred = (pred / pred.max()) * 255
pred = pred.transpose((1, 2, 0))
pred = pred.astype(np.uint8)
imageio.imwrite((vis + '/%s_%s.png') % (filename, k), pred)