-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.py
52 lines (43 loc) · 1.24 KB
/
helpers.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
import os
import json
import numpy as np
def silent_print(string):
pass
def makedir(path):
'''
if path does not exist in the file system, create it
'''
if not os.path.exists(path):
os.makedirs(path)
def print_and_write(str, file):
print(str)
file.write(str + '\n')
def find_high_activation_crop(activation_map, percentile=95):
threshold = np.percentile(activation_map, percentile)
mask = np.ones(activation_map.shape)
mask[activation_map < threshold] = 0
lower_y, upper_y, lower_x, upper_x = 0, 0, 0, 0
for i in range(mask.shape[0]):
if np.amax(mask[i]) > 0.5:
lower_y = i
break
for i in reversed(range(mask.shape[0])):
if np.amax(mask[i]) > 0.5:
upper_y = i
break
for j in range(mask.shape[1]):
if np.amax(mask[:,j]) > 0.5:
lower_x = j
break
for j in reversed(range(mask.shape[1])):
if np.amax(mask[:,j]) > 0.5:
upper_x = j
break
return lower_y, upper_y+1, lower_x, upper_x+1
def json_load(filepath):
ret_dict = dict()
dict_ = json.load(open(filepath, 'r'))
for k, v in dict_.items():
ret_dict[int(k)] = v
del dict_
return ret_dict