-
Notifications
You must be signed in to change notification settings - Fork 17
/
testAttentionMask.py
103 lines (83 loc) · 3.01 KB
/
testAttentionMask.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
'''
Modified version of the original code from Hu et al.
@author Hu et al.
@author Christian Wilms
@date 11/15/18
'''
import sys
import os
import argparse
import time
import cjson
sys.path.append(os.path.abspath("caffe/python"))
sys.path.append(os.path.abspath("python_layers"))
sys.path.append(os.getcwd())
import caffe
import setproctitle
from alchemy.utils.mask import encode
from alchemy.utils.load_config import load_config
from alchemy.utils.progress_bar import printProgress
import config
import utils
from config import *
from utils import gen_masks_new
'''
python test.py gpu_id model [--init_weights=*.caffemodel] [--dataset=val2014] \
[--end=5000]
'''
def parse_args():
parser = argparse.ArgumentParser('train net')
parser.add_argument('gpu_id', type=int)
parser.add_argument('model', type=str)
parser.add_argument('--init_weights', dest='init_weights', type=str,
default=None)
parser.add_argument('--dataset', dest='dataset', type=str,
default='val2014')
parser.add_argument('--end', dest='end', type=int, default=5000)
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
caffe.set_mode_gpu()
caffe.set_device(int(args.gpu_id))
setproctitle.setproctitle(args.model)
net = caffe.Net(
'models/' + args.model + ".test.prototxt",
'params/' + args.init_weights,
caffe.TEST)
# surgeries
interp_layers = [layer for layer in net.params.keys() if 'up' in layer]
utils.interp(net, interp_layers)
if os.path.exists("configs/%s.json" % args.model):
load_config("configs/%s.json" % args.model)
else:
print "Specified config does not exists, use the default config..."
time.sleep(2)
config.ANNOTATION_TYPE = args.dataset
config.IMAGE_SET = args.dataset
from spiders.coco_ssm_spider import COCOSSMDemoSpider
spider = COCOSSMDemoSpider()
spider.dataset.sort(key=lambda item: int(item.image_path[-10:-4]))
ds = spider.dataset[:args.end]
results = []
for i in range(len(ds)):
spider.fetch()
img = spider.img_blob
image_id = int(ds[i].image_path[-10:-4])
ret = gen_masks_new(net, img, config,
dest_shape=(spider.origin_height, spider.origin_width))
ret_masks, ret_scores = ret
printProgress(i, len(ds), prefix='Progress: ', suffix='Complete', barLength=50)
for _ in range(len(ret_masks)):
score = float(ret_scores[_])
objn = float(ret_scores[_])
results.append({
'image_id': image_id,
'category_id': 1, #as we are doing class-agnostic proposal
#generation, cat_id is irrelevant
'segmentation': encode(ret_masks[_]),
'score': score,
'objn': objn
})
with open('results/%s.json' % args.model, "wb") as f:
f.write(cjson.encode(results))