-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_ours.py
More file actions
401 lines (314 loc) · 11.6 KB
/
utils_ours.py
File metadata and controls
401 lines (314 loc) · 11.6 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import matplotlib.pyplot as plt
import numpy as np
import argparse
import torch
import cv2
def normalize_image(pic):
# print('type is ',type(pic))
if pic.min() == 0 and pic.max()==0:
return(pic)
else:
npic = (pic - pic.min()) / (pic.max() - pic.min())
return npic
def show(image, title='.'):
# display an image along with title
# handles PIL format,and numpy arrays
if isinstance(image, torch.Tensor) and len(image.size()) == 3:
# image = image.numpy()
print(image.shape)
image = image.permute(1, 2, 0)
# image = normalize_image(image)
f, ax = plt.subplots(figsize=(10, 10))
ax.imshow(image)
ax.set_title(title, fontsize=30)
plt.show()
def overlay(img,mask,orig,title='.'):
masked = np.ma.masked_where(mask == 0, mask)
img_masked = np.ma.masked_where(img == 0, img)
img = img + 1
img[img > 1] = .9
f, ax = plt.subplots(figsize=(10, 10))
# pic = orig
pic = orig[:,1,:,:]
pic = np.transpose(pic,(1,2,0))
pic = normalize_image(pic)
ax.imshow(pic)
ax.imshow(img_masked,'autumn',interpolation='none', alpha=0.5)
ax.imshow(masked, 'jet', interpolation='none', alpha=0.5)
ax.set_title(title, fontsize=30)
plt.show()
def side(img,mask,orig,title='.'):
masked = np.ma.masked_where(mask == 0, mask)
img_masked = np.ma.masked_where(img == 0, img)
img = img + 1
img[img > 1] = .9
f, ax = plt.subplots(figsize=(10, 10))
# pic = orig
pic = orig[:,0,:,:]
pic = np.transpose(pic,(1,2,0))
pic = normalize_image(pic)
ax.imshow(pic)
ax.imshow(img_masked,'autumn',interpolation='none', alpha=0.5)
ax.set_title(title, fontsize=30)
plt.show()
def byside(img,mask,orig,title='.'):
masked = np.ma.masked_where(mask == 0, mask)
img_masked = np.ma.masked_where(img == 0, img)
img = img + 1
img[img > 1] = .9
f, ax = plt.subplots(figsize=(10, 10))
# pic = orig
pic = orig[:,0,:,:]
pic = np.transpose(pic,(1,2,0))
pic = normalize_image(pic)
ax.imshow(pic)
# ax.imshow(img_masked,'autumn',interpolation='none', alpha=0.5)
ax.imshow(masked, 'jet', interpolation='none', alpha=0.5)
ax.set_title(title, fontsize=30)
plt.show()
def overlay2(mask,orig,title='.'):
masked = np.ma.masked_where(mask == 0, mask)
# histogram(masked)
# img_masked = np.ma.masked_where(img == 0, img)
# img = img + 1
# img[img > 1] = .9
f, ax = plt.subplots(figsize=(10, 10))
pic = orig
# pic = orig[:,1,:,:]
# pic = np.transpose(pic,(1,2,0))
# pic = normalize_image(pic)
ax.imshow(pic)
# ax.imshow(img_masked,'autumn',interpolation='none', alpha=0.5)
ax.imshow(masked, 'autumn', interpolation='none', alpha=0.5)
ax.set_title(title, fontsize=30)
plt.show()
def oldIOU(gt,img,orig):
#takes ground truth, gt, and and ouput image ,img, and calculates IOU
# make sure they are in numpy
#test to see if they are binary - reject or fix if not
for i in range(0,10):
intersection = gt[i] + img[i]
intersection[intersection < 2] = 0
intersection[intersection > 0] = 1
intersection_sum = intersection.sum()
union = gt[i] + img[i]
union[union > 1] = 1
union_sum = union.sum()
IOU = intersection_sum/union_sum
# overlay(gt[i],img[i],orig[i],IOU)
return IOU
def IOU(gt,img):
#takes ground truth, gt, and and ouput image ,img, and calculates IOU
# make sure they are in numpy
#test to see if they are binary - reject or fix if not
intersection = gt + img
intersection[intersection < 2] = 0
intersection[intersection > 0] = 1
intersection_sum = intersection.sum()
union = gt + img
union[union > 1] = 1
union_sum = union.sum()
if union_sum > 0:
IOU = intersection_sum/union_sum
else:
# print('union sum not positive ',union_sum)
IOU = torch.Tensor([0])
return IOU
def IOU2(gt,img):
#takes ground truth, gt, and and ouput image ,img, and calculates IOU
# make sure they are in numpy
#test to see if they are binary - reject or fix if not
intersection = gt + img
intersection[intersection < 2] = 0
intersection[intersection > 0] = 1
intersection_sum = intersection.sum()
union = gt + img
union[union > 1] = 1
union_sum = union.sum()
if gt.sum() > 0:
IOU = intersection_sum/union_sum
else:
# print('union sum not positive ',union_sum)
IOU = float('NaN')
return IOU
def basic_overlay(img,mask,title='.'):
masked = np.ma.masked_where(mask == 0, mask)
img_masked = np.ma.masked_where(img == 0, img)
img = img + 1
img[img > 1] = .9
f, ax = plt.subplots(figsize=(10, 10))
ax.imshow(img_masked,'autumn',interpolation='none', alpha=0.5)
ax.imshow(masked, 'jet', interpolation='none', alpha=0.5)
ax.set_title(title, fontsize=30)
plt.show()
def testIOU():
a = np.zeros((10,10))
a[3:6,3:6]= 1
b = np.zeros((10, 10))
b[3:6,3:6]= 1
iou = IOU(a,b)
basic_overlay(a,b,iou)
a = np.zeros((10,10))
a[3:6,3:6]= 1
b = np.zeros((10, 10))
b[7:9,7:9]= 1
iou = IOU(a,b)
basic_overlay(a,b,iou)
a = np.zeros((10,10))
a[3:6,3:6]= 1
b = np.zeros((10, 10))
b[5:8,3:6]= 1
iou = IOU(a,b)
basic_overlay(a,b,iou)
a = np.zeros((10,10))
a[3:7,3:7]= 1
b = np.zeros((10, 10))
b[4:6,4:6]= 1
iou = IOU(a,b)
basic_overlay(a,b,iou)
def histogram(arr):
if isinstance(arr, torch.Tensor):
arr = arr.cpu()
arr = arr.data.numpy()
num_bins = 200
arr = arr.ravel()
n, bins, patches = plt.hist(arr, num_bins, facecolor='blue', alpha=0.5)
plt.show()
def measure_pixelwise_var_v2(pred, flip_pred, frames_cnt=5, use_sig_output=False):
"""cyclic variance
varv3 - cyclic version
"""
count = 0
batch_variance = np.zeros((pred.shape[0], 1, 8, 224, 224))
# remove the redundant frames 1dt n last of flipped map
temp_batch_var = np.zeros((pred.shape[0], 1, 14, 224, 224))
# use sigmoid output not logits
# probability scores
if use_sig_output==True:
pred = torch.sigmoid(pred)
flip_pred = torch.sigmoid(flip_pred)
for zz in range(0, pred.shape[0]):
clip = pred[zz][0]
flip_clip = flip_pred[zz][0]
cyclic_clip = torch.cat([clip, flip_clip[1:7]], axis=0).cpu().detach().numpy()
# calculated variance over 14 frames
clip_variance = np.zeros_like(temp_batch_var[0][0])
for temp_cnt in range(temp_batch_var.shape[2]):
# 3 frames
if frames_cnt==3:
if temp_cnt+1>(temp_batch_var.shape[2] - 1):
temp_var = np.take(cyclic_clip, indices=[temp_cnt-1, temp_cnt, 0], axis=0)
else:
temp_var = np.take(cyclic_clip, indices=[temp_cnt-1, temp_cnt, temp_cnt+1], axis=0)
# 5 frames
if frames_cnt==5:
if temp_cnt+1>(temp_batch_var.shape[2] - 1):
temp_var = np.take(cyclic_clip, indices=[temp_cnt-2, temp_cnt-1, temp_cnt, 0, 1], axis=0)
elif temp_cnt+2>(temp_batch_var.shape[2] - 1):
temp_var = np.take(cyclic_clip, indices=[temp_cnt-2, temp_cnt-1, temp_cnt, temp_cnt+1, 0], axis=0)
else:
temp_var = np.take(cyclic_clip, indices=[temp_cnt-2, temp_cnt-1, temp_cnt, temp_cnt+1, temp_cnt+2], axis=0)
temp_var = np.var(temp_var, axis=0)
clip_variance[temp_cnt] = temp_var
# overlap
for add_half in range(8):
if add_half==0 or add_half==7:
clip_variance[add_half] = 2* clip_variance[add_half]
else:
clip_variance[add_half] = clip_variance[add_half] + clip_variance[14-add_half]
# normalize
clip_variance = clip_variance[:8]
clip_variance -= clip_variance.min()
clip_variance /= (clip_variance.max() - clip_variance.min() + 1e-7)
clip_variance = np.expand_dims(clip_variance, axis=0)
batch_variance[zz] = clip_variance
batch_variance = torch.from_numpy(batch_variance)
return batch_variance
def dft_HighPass(pred, radius = 32):
batch_video = pred.cpu().detach().numpy()
batch_filtered = np.zeros((batch_video.shape[0], 1, 8, 224, 224))
mask = np.zeros_like(batch_video[0][0][0])
cy = mask.shape[0] // 2
cx = mask.shape[1] // 2
cv2.circle(mask, (cx,cy), radius, (255,255,255), -1)[0]
mask = 255 - mask
mask = cv2.GaussianBlur(mask, (19,19), 0)
for zz in range(0, batch_video.shape[0]):
clip = batch_video[zz][0]
for xx in range(clip.shape[0]):
img = clip[xx]
dft = np.fft.fft2(img, axes=(0,1))
dft_shift = np.fft.fftshift(dft)
dft_shift_masked = np.multiply(dft_shift,mask)
back_ishift_masked = np.fft.ifftshift(dft_shift_masked)
img_filtered = np.fft.ifft2(back_ishift_masked, axes=(0,1))
img_filtered = np.abs(img_filtered).clip(0,1)
batch_filtered[zz][0][xx] = img_filtered
batch_filtered = torch.from_numpy(batch_filtered)
return batch_filtered
def exp_rampup(rampup_length):
"""Exponential rampup from https://arxiv.org/abs/1610.02242"""
def warpper(epoch):
if epoch < rampup_length:
epoch = np.clip(epoch, 0.0, rampup_length)
phase = 1.0 - epoch / rampup_length
return float(np.exp(-5.0 * phase * phase))
else:
return 1.0
return warpper
def weighted_mse_loss(input, target, weight):
return (weight * (input - target) ** 2).mean()
def weighted_mae_loss(input, target, weight):
return (weight * (input - target)).mean()
# def mAP(gt,img):
# #takes ground truth, gt, and and ouput image ,img, and calculates IOU
# # make sure they are in numpy
# #test to see if they are binary - reject or fix if not
#
#
# intersection = gt + img
#
# intersection[intersection < 2] = 0
# intersection[intersection > 0] = 1
# intersection_sum = intersection.sum()
#
# gt_sum = gt.sum()
#
# if gt_sum > 0:
# overlap = intersection_sum/gt_sum
# else:
# # print('union sum not positive ',union_sum)
# overlap = float('NaN')
#
# return overlap
if __name__ == "__main__":
import torchvision.models as models
from tensorboardX import SummaryWriter
from torch.autograd import Variable
import torch
resnet18 = models.resnet18(False)
writer = SummaryWriter()
for name, param in resnet18.named_parameters():
writer.add_histogram(name, param.clone().cpu().data.numpy(), 0)
writer.export_scalars_to_json("./all_scalars.json")
dummy_img = Variable(torch.rand(32, 3, 64, 64))
res = resnet18(dummy_img)
writer.add_graph(resnet18, res)
writer.close()
# parser = argparse.ArgumentParser()
# parser.add_argument('--awesome', default='yes')
# args = parser.parse_args()
# print('bloop')
# print(args.awesome)
# def write_csv(self):
# with open('master.csv','w') as csv_file:
# writer = csv.writer(csv_file)
# for a in self.master:
# writer.writerow([a.get('path'),a.get('actor'),a.get('view'),a.get('label'),a.get('frame')])
#
# def read_csv(self):
# self.master = []
# with open('master.csv', 'r') as csv_file:
# reader = csv.reader(csv_file)
# for row in reader:
# self.master.append({'path': row[0], 'actor': row[1], 'view': row[2], 'label': row[3], 'frame': row[4]})