-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
41 lines (36 loc) · 918 Bytes
/
util.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
import numpy as np
import random
import torch
from six.moves import cPickle as pickle
def flip_ordering(o):
return np.array([i for _, i in sorted(zip(o, list(range(o.size))))])
def flip_orderings(O):
return [flip_ordering(o) for o in O]
def make_occlusion_mask(case='avg'):
mask = np.zeros((28,28))
a = random.randint(0,27)
b = random.randint(0,27)
if case == 'botright':
b = 27
elif case == 'topleft':
a = 0
i1 = min(a,b)
i2 = max(a,b)+1
a = random.randint(0,27)
b = random.randint(0,27)
if case == 'botright':
b = 27
elif case == 'topleft':
a = 0
j1 = min(a,b)
j2 = max(a,b)+1
mask[i1:i2, j1:j2] = 1
# print('Occlusion: ', (i1, i2, j1, j2))
return torch.Tensor(mask.flatten())
def save_dict(di_, filename_):
with open(filename_, 'wb') as f:
pickle.dump(di_, f)
def load_dict(filename_):
with open(filename_, 'rb') as f:
ret_di = pickle.load(f)
return ret_di