-
Notifications
You must be signed in to change notification settings - Fork 65
/
test.py
130 lines (99 loc) · 3.65 KB
/
test.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 sys
import os
import argparse
import time
import cjson
import threading
sys.path.append(os.path.abspath("caffe-fm/python"))
sys.path.append(os.path.abspath("python_layers"))
sys.path.append(os.getcwd())
import caffe
from IPython import embed
import numpy as np
import setproctitle
import cv2
from pycocotools.cocoeval import COCOeval
from alchemy.utils.image import resize_blob, visualize_masks
from alchemy.utils.timer import Timer
from alchemy.utils.mask import encode, crop, iou
from alchemy.utils.load_config import load_config
from alchemy.utils.progress_bar import printProgress
import config
import utils
from utils import gen_masks
from config import *
'''
python test.py gpu_id model [--debug=False] [--init_weights=*.caffemodel] [--useDet=False] \
[--test_num=5000] [--dataset=val2014] [--debug=False] [--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('--useCats', dest='useCats', type=str, default='False')
parser.add_argument('--debug', dest='debug', type=str, default='False')
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()
args.useCats = args.useCats == 'True'
args.debug = args.debug == 'True'
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)
timer = Timer()
config.ANNOTATION_TYPE = args.dataset
config.IMAGE_SET = "val2014"
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]
timer.tic()
results = []
for i in range(len(ds)):
spider.fetch()
img = spider.img_blob
image_id = int(ds[i].image_path[-10:-4])
# gen mask
ret = gen_masks(net, img, config,
dest_shape=(spider.origin_height, spider.origin_width),
image=args.debug)
if args.useCats:
ret_masks, ret_scores, ret_cats = ret
else:
ret_masks, ret_scores = ret
printProgress(i, len(ds), prefix='Progress: ', suffix='Complete', barLength=50)
for _ in range(len(ret_masks)):
cat = 1
if args.useCats:
cat = spider.dataset.getCatIds()[int(ret_cats[_].argmax())]
score = float(ret_cats[_].max())
else:
score = float(ret_scores[_])
objn = float(ret_scores[_])
results.append({
'image_id': image_id,
'category_id': cat,
'segmentation': encode(ret_masks[_]),
'score': score,
'objn': objn
})
with open('results/%s.json' % args.model, "wb") as f:
f.write(cjson.encode(results))