-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_patches.py
130 lines (90 loc) · 4.48 KB
/
generate_patches.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
import mahotas as mh
import numpy as np
import os
import time
import mlproof as mlp
PATCH_PATH = os.path.expanduser('~/patches/cylinder2/')
def generate_patches(start_slice, end_slice):
patch_index = 0
all_patches_count = 0
all_error_patches = []
all_correct_patches = []
for z in range(start_slice, end_slice):
t0 = time.time()
print 'working on slice', z
input_image, input_prob, input_mask, input_gold, input_rhoana = mlp.Util.read_section(os.path.expanduser('~/data/cylinderNEW/'),z)
error_patches, patches = mlp.Patch.patchify_maxoverlap(input_image, input_prob, input_mask, input_rhoana, input_gold, sample_rate=1)
print 'Generated', len(error_patches), 'split error patches and', len(patches), ' correct patches in', time.time()-t0, 'seconds.'
all_error_patches += error_patches
all_correct_patches += patches
NO_PATCHES = len(all_error_patches) + len(all_correct_patches)
print 'We have a total of',NO_PATCHES,'patches.'
print 'Errors:',len(all_error_patches)
print 'Correct:',len(all_correct_patches)
PATCH_BYTES = 75*75
P_SIZE = (NO_PATCHES, 1, 75,75) # rather than raveled right now
p_image = np.zeros(P_SIZE, dtype=np.float32)
p_prob = np.zeros(P_SIZE, dtype=np.float32)
p_binary = np.zeros(P_SIZE, dtype=np.bool)
p_merged_array = np.zeros(P_SIZE, dtype=np.bool)
# p_dyn_obj = np.zeros((NO_PATCHES, PATCH_BYTES),dtype=np.bool)
# p_dyn_bnd = np.zeros((NO_PATCHES, PATCH_BYTES),dtype=np.bool)
p_border_overlap = np.zeros(P_SIZE, dtype=np.bool)
p_larger_border_overlap = np.zeros(P_SIZE, dtype=np.bool)
p_target = np.zeros(NO_PATCHES)
i = 0
for p in all_error_patches:
p_image[i] = (p['image'].astype(np.float32)/255.)#.ravel()
p_prob[i] = (p['prob'].astype(np.float32)/255.)#.ravel()
p_binary[i] = p['binary']#.ravel()
p_merged_array[i] = p['merged_array']#.ravel()
# p_dyn_obj[i] = p['dyn_obj'].ravel()
# p_dyn_bnd[i] = p['dyn_bnd'].ravel()
p_border_overlap[i] = p['border_overlap']#.ravel()
p_larger_border_overlap[i] = p['larger_border_overlap']#.ravel()
p_target[i] = 1 # <--- important
i += 1
for p in all_correct_patches:
p_image[i] = (p['image'].astype(np.float32)/255.)#.ravel()
p_prob[i] = (p['prob'].astype(np.float32)/255.)#.ravel()
p_binary[i] = p['binary']#.ravel()
p_merged_array[i] = p['merged_array']#.ravel()
# p_dyn_obj[i] = p['dyn_obj'].ravel()
# p_dyn_bnd[i] = p['dyn_bnd'].ravel()
p_border_overlap[i] = p['border_overlap']#.ravel()
p_larger_border_overlap[i] = p['larger_border_overlap']#.ravel()
p_target[i] = 0 # <--- important
i+=1
# return p_image, p_prob, p_binary, p_merged_array, p_dyn_obj, p_dyn_bnd, p_border_overlap, p_larger_border_overlap, p_target
return p_image, p_prob, p_binary, p_merged_array, p_border_overlap, p_larger_border_overlap, p_target
def shuffle_in_unison_inplace(a, b, c, d, e, f, g):
assert len(a) == len(b)
assert len(a) == len(g)
p = np.random.permutation(len(a))
return a[p], b[p], c[p], d[p], e[p], f[p], g[p]
def run(start_slice, end_slice, filename):
if not os.path.exists(PATCH_PATH):
os.makedirs(PATCH_PATH)
p = generate_patches(start_slice, end_slice)
shuffled = shuffle_in_unison_inplace(p[0],
p[1],
p[2],
p[3],
p[4],
p[5],
p[6]
)
print 'saving..'
np.savez(PATCH_PATH+filename+'.npz', image=shuffled[0],
prob=shuffled[1],
binary=shuffled[2],
merged_array=shuffled[3],
# dyn_obj=shuffled[4],
# dyn_bnd=shuffled[5],
border_overlap=shuffled[4],
larger_border_overlap=shuffled[5])
np.savez(PATCH_PATH+filename+'_targets.npz', targets=shuffled[6])
print 'Done!'
# return correct, split, shuffled
run(0,250, 'train')
run(250,300, 'test')