forked from rbgirshick/py-faster-rcnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run2stage.py
369 lines (322 loc) · 13.9 KB
/
run2stage.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import sys
import os
def add_path(path):
#if path not in sys.path:
sys.path.insert(0, path)
this_dir = '/home/ubuntu/py-faster-rcnn/' #osp.dirname(__file__)
caffe_path = os.path.join(this_dir, 'caffe-fast-rcnn', 'python')
add_path(caffe_path)
lib_path = os.path.join(this_dir, 'lib')
add_path(lib_path)
from utils.timer import Timer
import cv2
import xml.etree.ElementTree as ET
from sys import argv
import argparse
import matplotlib
matplotlib.use('Agg')
import caffe
from fast_rcnn.config import cfg, cfg_from_file, cfg_from_list
from fast_rcnn.test import im_detect, apply_nms
import pprint
import numpy as np
import heapq
import cPickle
from PIL import Image
import skimage
import uuid
import time
'''
runs images through 2 stage model, saves label matrices
python run2stage.py ([--images image_list.txt]) [--cfg1 experiments/cfgs/faster_rcnn_end2end.yml] [--prototxt1 xxx.prototxt] [--model1 xxx.caffemodel] [--prototxt2 deploy.prototxt] [--model2 xxx.caffemodel]
'''
def parse_args():
"""
Parse input arguments
"""
parser = argparse.ArgumentParser(description='Test a Fast R-CNN network')
parser.add_argument('--gpu', dest='gpu_id', help='GPU id to use',
default=0, type=int)
parser.add_argument('--prototxt1', dest='prototxt1',
help='prototxt file defining the network',
default='/home/ubuntu/py-faster-rcnn/models/VGG_CNN_M_1024/faster_rcnn_end2end/test.prototxt', type=str)
parser.add_argument('--prototxt2', dest='prototxt2',
help='prototxt file defining the network',
default='/home/ubuntu/py-faster-rcnn/caffe-fast-rcnn/models/bvlc_reference_caffenet/deploy.prototxt', type=str)
parser.add_argument('--model1', dest='caffemodel1',
help='model to test',
default='/home/ubuntu/py-faster-rcnn/output/faster_rcnn_end2end/train/vgg_cnn_m_1024_faster_rcnn_lr0.01_iter_100000.caffemodel', type=str)
parser.add_argument('--model2', dest='caffemodel2',
help='model to test',
default='/home/ubuntu/py-faster-rcnn/caffe-fast-rcnn/models/bvlc_reference_caffenet/caffenet_train_iter_40000.caffemodel', type=str)
parser.add_argument('--cfg1', dest='cfg_file1',
help='optional config file', default='/home/ubuntu/py-faster-rcnn/experiments/cfgs/faster_rcnn_end2end.yml', type=str)
parser.add_argument('--wait', dest='wait',
help='wait until net file exists',
default=True, type=bool)
parser.add_argument('--images', dest='images',
help='file with names of files to test',
default='/home/ubuntu/try1/data/ImageSets/test.txt', type=str)
parser.add_argument('--set', dest='set_cfgs',
help='set config keys', default=None,
nargs=argparse.REMAINDER)
parser.add_argument('--classes1', dest='classes1',
help='list of class names for stage 1', default=['__background__', 'rbc', 'other'])
parser.add_argument('--classes2', dest='classes2',
help='list of class names', default=['__background__', 'rbc', 'tro', 'sch', 'ring', 'gam', 'leu'],
type=list)
parser.add_argument('--output', dest='output_dir',
help='output directory',default='/home/ubuntu/svg', type=str)
parser.add_argument('--mean2', dest='mean2',
help='mean pixels, stage2', default=[189.97, 133.83, 149.26 ], type=list)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
return args
def StageOne(file_, prototxt, model, classes, THRESHOLD=1.0/3, num_images = 1, output_dir = '/home/ubuntu/py-faster-rcnn/output' ):
'''
run one image through object detector to classify each cell as background, rbc, or other
Return: all boxes with score above THRESHOLD
'''
net = caffe.Net(prototxt, model, caffe.TEST)
net.name = os.path.splitext(os.path.basename(model))[0]
_t = {'im_detect' : Timer(), 'misc' : Timer()}
# top_scores will hold one minheap of scores per class (used to enforce
# the max_per_set constraint)
num_classes = len(classes)
top_scores = [[] for _ in xrange(num_classes)]
# all detections are collected into:
# all_boxes[cls] = N x 5 array of detections in
# (x1, y1, x2, y2, score)
all_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(num_classes)]
for i in xrange(num_images):
# filter out any ground truth boxes
if cfg.TEST.HAS_RPN:
box_proposals = None
else:
raise Exception("HAS_RPN is False")
im = cv2.imread(file_)
_t['im_detect'].tic()
scores, boxes = im_detect(net, im, box_proposals)
_t['im_detect'].toc()
_t['misc'].tic()
for j in xrange(1, num_classes):
inds = np.where(scores[:, j] > THRESHOLD)[0]
cls_scores = scores[inds, j]
cls_boxes = boxes[inds, j*4:(j+1)*4]
top_inds = np.argsort(-cls_scores)
cls_scores = cls_scores[top_inds]
cls_boxes = cls_boxes[top_inds, :]
# push new scores onto the minheap
for val in cls_scores:
heapq.heappush(top_scores[j], val)
# if we've collected more than the max number of detection,
# then pop items off the minheap and update the class threshold
#if len(top_scores[j]) > max_per_set:
# while len(top_scores[j]) > max_per_set:
# heapq.heappop(top_scores[j])
# thresh[j] = top_scores[j][0]
all_boxes[j][i] = \
np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \
.astype(np.float32, copy=False)
_t['misc'].toc()
#print 'im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \
# .format(i + 1, num_images, _t['im_detect'].average_time,
# _t['misc'].average_time)
#only keep boxes with scores above the threshold
for j in xrange(1, num_classes):
for i in xrange(num_images):
inds = np.where(all_boxes[j][i][:, -1] > THRESHOLD)[0]
all_boxes[j][i] = all_boxes[j][i][inds, :]
det_file = os.path.join(output_dir, 'detections.pkl')
with open(det_file, 'wb') as f:
cPickle.dump(all_boxes, f, cPickle.HIGHEST_PROTOCOL)
#Apply NMS to all detections
nms_dets = apply_nms(all_boxes, cfg.TEST.NMS)
with open(det_file, 'wb') as f:
cPickle.dump(nms_dets, f, cPickle.HIGHEST_PROTOCOL)
return nms_dets
def StageTwo(file_path, prototxt, model, detections, classes, mean):
'''
run detections from one image through image classifier
Return: all detections
'''
net = caffe.Net(prototxt, model, caffe.TEST)
net.name = os.path.splitext(os.path.basename(model))
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_mean('data', np.array(mean))
transformer.set_transpose('data', (2,0,1))
transformer.set_raw_scale('data', 255.0)
transformer.set_channel_swap('data', (2,1,0))
probs = np.zeros((len(detections), len(classes)))
full_im = Image.open(file_path)
#full_im = caffe.io.load_image(file_path)
full_im = full_im.copy() #caffe.io.load_image()
for det_index, det in enumerate(detections):
img = full_im.crop((int(det[0]), int(det[1]), int(det[2]), int(det[3])))
img.save('/home/ubuntu/stage2.jpg')
img = caffe.io.load_image('/home/ubuntu/stage2.jpg')
net.blobs['data'].reshape(1, 3, 227,227)
net.blobs['data'].data[...] = transformer.preprocess('data', np.array(img))#np.array(img))
output = net.forward()
probs[det_index] = output['prob']
return probs
def WriteXml(root, box, cls, attributes_text, index):
'''
write to Element Tree
'''
object_ = ET.Element('object')
root.append(object_)
name_ = ET.SubElement(object_, 'name')
name_.text = cls
deleted_ = ET.SubElement(object_, 'deleted')
deleted_.text = "0"
verified_ = ET.SubElement(object_, 'verified')
verified_.text = "0"
occluded_ = ET.SubElement(object_, 'occluded')
occluded_.text = 'no'
attributes_ = ET.SubElement(object_, 'attributes')
attributes_.text = attributes_text
parts_ = ET.SubElement(object_, 'parts')
hasparts_ = ET.SubElement(parts_, 'hasparts')
ispartof_ = ET.SubElement(parts_, 'ispartof')
date_ = ET.SubElement(object_, 'date')
date_.text = str(0)
id_ = ET.SubElement(object_, 'id')
id_.text = str(index)
type_ = ET.SubElement(object_, 'type')
type_.text = 'bounding_box'
polygon_ = ET.SubElement(object_, 'polygon')
username_ = ET.SubElement(polygon_, 'username')
username_.text = 'anonymous'
for i in range(4):
pt_ = ET.SubElement(polygon_, 'pt')
x_ = ET.SubElement(pt_, 'x')
x_.text = str(box[((i + i%2)%4)])
y_ = ET.SubElement(pt_, 'y')
y_.text = str(box[(i + (i+1)%2)])
def CreateXml(LabelMe_path, file_, stage1_dets, stage2_probs, classes):
'''
create LabelMe xml file from detection coordinates
'''
LabelMe_annotation_dir = os.path.join(LabelMe_path, 'Annotations')
file_ = file_.split('Images/')[1]
image_dir = file_.split('/')[0]
#make LabelMe xml annotation file
LabelMe_file = os.path.join(LabelMe_annotation_dir, file_+'.xml')
#clear existing annotations
tree = ET.parse(LabelMe_file)
root = tree.getroot()
root.find('folder').text = image_dir
for obj in root.findall('object'):
root.remove(obj)
#get detection coordinates
rbc_dets = stage1_dets[1][0]
other_dets = stage1_dets[2][0]
#for each set of coordinates, create object instance
for index, box in enumerate(rbc_dets):
box = rbc_dets[index][:4]
attributes = str(rbc_dets[index][-1])
writeXML(root, box, classes[1], attributes, index)
for index_other, box in enumerate(other_dets):
index += 1
box = other_dets[index_other][:4]
attributes = str(other_dets[index_other][-1])
writeXML(root, box, classes[np.argmax(stage2_probs[index_other])], attributes, index)
tree.write(LabelMe_file)
os.chmod(LabelMe_file, 0o777)
def WriteRect(box, cls, score):
'''
write rect to Element Tree
'''
rect_ = ET.Element('rect')
rect_.set('description', cls)
rect_.set('score', str(score))
rect_.set('x', str(int(box[0])))
rect_.set('y', str(int(box[1])))
rect_.set('width', str(int(box[2]-box[0])))
rect_.set('height', str(int(box[3]-box[1])))
rect_.set('fill', 'none')
rect_.set('stroke-width', '10')
rect_.set('stroke', 'black')
return rect_
def WriteImage(file_, output):
'''
write image
'''
image_ = ET.Element('image')
file_, ext = os.path.splitext(file_)
image_dir, file_ = os.path.relpath(file_, output).split('/', 1)
image_.set('xlink:href', os.path.join(image_dir, file_+ext))#str(uuid.uuid3(uuid.NAMESPACE_DNS, file_))+ext))
return image_
def CreateSvg(output_dir, file_, detections, probs, classes):
'''
create svg using original image, detections, probability distributions and save to output
'''
output = os.path.join(output_dir, os.path.basename(file_.rsplit(".",1)[0]))
try:
#clear existing annotations
tree = ET.parse(output+'.svg')
root = tree.getroot()
for obj in root.findall('rect'):
root.remove(obj)
except:
root = ET.Element('svg')
tree = ET.ElementTree(root)
root.set('xmlns', "http://www.w3.org/2000/svg")
root.set('xmlns:xlink', "http://www.w3.org/1999/xlink")
#get detection coordinates
rbc_dets = detections[1][0]
other_dets = detections[2][0]
image_ = WriteImage(file_, output_dir)
root.append(image_)
#for each set of coordinates, create object instance
for index, box in enumerate(rbc_dets):
box = rbc_dets[index]
attributes = str(rbc_dets[index][-1])
root.append(WriteRect(box[:4], classes[1], box[4]))
for index_other, box in enumerate(other_dets):
index += 1
box = other_dets[index_other]
attributes = str(other_dets[index_other][-1])
root.append(WriteRect(box[:4], classes[np.argmax(probs[index_other])], box[4]))
tree.write(output+'.svg')
os.chmod(output+'.svg', 0o777)
return output
def get_files(ImageSet_test):
test_files = []
with open(ImageSet_test) as f:
for file_ in f.readlines():
test_files.append(file_.strip())
return test_files
def get_dimensions(file_):
with Image.open(file_) as im:
return im.size
if __name__ == '__main__':
args = parse_args()
print('Called with args:')
print(args)
if args.cfg_file1 is not None:
cfg_from_file(args.cfg_file1)
if args.set_cfgs is not None:
cfg_from_list(args.set_cfgs)
cfg.GPU_ID = args.gpu_id
while not os.path.exists(args.caffemodel1) and args.wait:
print('Waiting for {} to exist...'.format(args.caffemodel1))
time.sleep(10)
caffe.set_mode_gpu()
caffe.set_device(args.gpu_id)
#get test image filenames
imageSet_test = args.images
test_files = get_files(imageSet_test)#['/home/ubuntu/try1/data/Images/g8_t1_up/g6010001.jpg']
classes1 = args.classes1
classes2 = args.classes2
#for each image in the list, run through stage 1, then run through stage 2, then convert results and create xml file
for file_index, file_ in enumerate(test_files):
dimensions = get_dimensions(file_)
cfg_from_list(['TEST.SCALES', str([min(dimensions)]), 'TEST.MAX_SIZE', str(max(dimensions))])
pprint.pprint(cfg)
nms_dets = StageOne(file_, args.prototxt1, args.caffemodel1, classes1, THRESHOLD=1.0/len(classes1))
stage2_probs = StageTwo(file_, args.prototxt2, args.caffemodel2, nms_dets[classes1.index('other')][0], classes2)
CreateSvg(args.output_dir, file_, nms_dets, stage2_probs, classes2, args.mean2)