-
Notifications
You must be signed in to change notification settings - Fork 2
/
cross_validate_crf.py
216 lines (154 loc) · 5.57 KB
/
cross_validate_crf.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
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
from __future__ import division
import pydensecrf.densecrf as dcrf
from pydensecrf.utils import unary_from_labels, create_pairwise_bilateral, create_pairwise_gaussian, unary_from_softmax
import numpy as np
import matplotlib.pyplot as plt
import sys
import scipy.io as sio
from skimage import color
from skimage.io import imread, imsave
from os import listdir, makedirs
from os.path import isfile, join, isdir
import argparse
import sys
import os
import subprocess
import shutil
# from apply_crf import *
# Change settings here
# Uncomment Run1 or Run 2 ... for different resolutions of the grid-search
IMAGE_DATA = '/data/arunirc/Research/dense-crf-data/training_subset/'
SEG_DATA = '/data/arunirc/Research/dense-crf-data/our-modifiedObjPrior/FBMS/Trainingset'
OUT_DIR = '/data/arunirc/Research/dense-crf-data/cross-val-crf-modifiedObjPrior/'
MODE = 'run' # 'run' or 'eval' or 'pick'
METRIC = 'pr' # 'iou' or 'pr'
# Run 1
# bilateral (colorspace)
# RUN_NUM = 1
# range_W=[3, 5, 10]
# range_XY_STD=[40, 50, 60, 70, 80, 90, 100]
# range_RGB_STD=[3, 5, 7, 9, 10]
# # Run 2
# # bilateral (colorspace)
RUN_NUM = 2
range_W = [10, 15, 20]
range_XY_STD = [10, 20, 30, 40]
range_RGB_STD = [1, 2, 3, 4, 5, 6]
# range_W=[5]
# range_XY_STD=[40]
# range_RGB_STD=[3]
# gaussian (positional)
POS_W = 3
POS_X_STD = 3
MAX_ITER = 5
def grid_runner():
'''
Run CRF segmentations using a grid-search over CRF settings
'''
if not os.path.isdir(OUT_DIR):
os.makedirs(OUT_DIR)
for w in range_W:
Bi_W=w
for x in range_XY_STD:
Bi_XY_STD=x
for r in range_RGB_STD:
Bi_R_STD = r
out_dir_name = join( OUT_DIR, 'w-'+str(w) + '_x-'+str(x) + '_r-'+str(r) )
# # if already computed in a prior run -- skip
if os.path.isdir(out_dir_name):
print 'Skipping %s. Already exists.' % out_dir_name
continue
cmd = 'python apply_crf.py ' \
+ '-i ' + IMAGE_DATA + ' ' \
+ '-s ' + SEG_DATA + ' ' \
+ '-o ' + out_dir_name + ' ' \
+ '-d ' + 'fbms ' \
+ '-cgw ' + str(POS_W) + ' ' \
+ '-cgx ' + str(POS_X_STD) + ' ' \
+ '-cbw ' + str(w) + ' ' \
+ '-cbx ' + str(x) + ' ' \
+ '-cbc ' + str(r) + ' ' \
+ '-mi ' + str(MAX_ITER) + ' -z &'
print cmd
subprocess.call(cmd, shell=True)
print 'done'
def grid_evaluater():
'''
Calculate an evaluation metric over all pre-computed segmentation results
(run after `grid_runner` )
'''
print 'Running evaluations'
GT_DATA = IMAGE_DATA
RAW_SEG_DATA = SEG_DATA
if not os.path.isdir(OUT_DIR):
os.makedirs(OUT_DIR)
for w in range_W:
Bi_W=w
for x in range_XY_STD:
Bi_XY_STD=x
for r in range_RGB_STD:
Bi_R_STD = r
out_dir_name = join( OUT_DIR, 'w-'+str(w) + '_x-'+str(x) + '_r-'+str(r) )
CRF_SEG_DATA = out_dir_name
cmd = 'python eval_segmentation.py ' \
+ '-g ' + GT_DATA + ' ' \
+ '-c ' + CRF_SEG_DATA + ' ' \
+ '-r ' + RAW_SEG_DATA + ' ' \
+ '-o ' + out_dir_name + ' &'
print cmd
subprocess.call(cmd, shell=True)
print 'Done'
def grid_picker():
'''
Pick the best settings from grid search evaluation results
(run after `grid_evaluater`)
'''
print 'Pick best settings'
GT_DATA = IMAGE_DATA
RAW_SEG_DATA = SEG_DATA
if not os.path.isdir(OUT_DIR):
os.makedirs(OUT_DIR)
best_w = 0
best_x = 0
best_r = 0
best_val = 0
grid_val = np.zeros
for w in range_W:
Bi_W = w
for x in range_XY_STD:
Bi_XY_STD = x
for r in range_RGB_STD:
Bi_R_STD = r
out_dir_name = join( OUT_DIR, 'w-'+str(w) + '_x-'+str(x) + '_r-'+str(r) )
CRF_SEG_DATA = out_dir_name
# select the evaluation metric
if METRIC == 'iou':
iou_crf = np.loadtxt( join(out_dir_name,'result_iou_fg_crf.txt'), delimiter=',' )
# print '%d %d %d ' % (w, x, r)
val = np.mean(iou_crf)
# print val
elif METRIC == 'pr':
prf_crf = np.loadtxt( join(out_dir_name,'result_pr_fg_crf.txt'), delimiter=',' )
val = prf_crf[2]
if val > best_val:
best_val = val
best_w = w
best_x = x
best_r = r
if METRIC == 'iou':
print 'Best IOU: %f' % best_val
print 'Settings: w=%f, x=%f, r=%f' % (best_w, best_x, best_r)
np.savetxt(join(OUT_DIR,'crf_best_iou_' + str(RUN_NUM) +'.txt'), \
[best_val, best_w, best_x, best_r], delimiter=',')
elif METRIC == 'pr':
print 'Best f-measure: %f' % best_val
print 'Settings: w=%f, x=%f, r=%f' % (best_w, best_x, best_r)
np.savetxt(join(OUT_DIR,'crf_best_pr_' + str(RUN_NUM) +'.txt'), \
[best_val, best_w, best_x, best_r], delimiter=',')
if __name__ == '__main__':
if MODE == 'run':
grid_runner()
elif MODE == 'eval':
grid_evaluater()
elif MODE == 'pick':
grid_picker()