def vr_make_meta_gt_visual_phrase(): m = h5py.File('data/sg_vrd_meta.h5','r',driver='core') h5f = h5py.File('data/sg_vrd_vp_meta.h5') triplets = {} cnt = 0 zl.tick() for k in m['gt/train'].keys(): if cnt %1000==0: print cnt,zl.tock() zl.tick() cnt+=1 gt_boxes = [] gt_labels = [] sub_boxes = m['gt/train/%s/sub_boxes'%k][...] obj_boxes = m['gt/train/%s/obj_boxes'%k][...] rlp_labels = m['gt/train/%s/rlp_labels'%k][...] for i in xrange(rlp_labels.shape[0]): sub_box = sub_boxes[i] obj_box = obj_boxes[i] rlp_label = rlp_labels[i] joint_box = [min(sub_box[0],obj_box[0]), min(sub_box[1],obj_box[1]),max(sub_box[2],obj_box[2]),max(sub_box[3],obj_box[3])] s_lbl = zl.idx2name_cls(m,rlp_label[0]) o_lbl = zl.idx2name_cls(m,rlp_label[2]) p_lbl = zl.idx2name_pre(m,rlp_label[1]) spo = '%s_%s_%s'%(s_lbl,p_lbl,o_lbl) lbl = zl.name2idx_tri(h5f,spo) gt_boxes.append(joint_box) gt_labels.append(lbl) h5f.create_dataset('gt/train/%s/labels'%k,data = np.array(gt_labels).astype(np.int16)) h5f.create_dataset('gt/train/%s/boxes'%k,data = np.array(gt_boxes).astype(np.int16))
def get_triplet_stats(split): m = h5py.File('/media/zawlin/ssd/Dropbox/proj/sg_vrd_meta.h5') triplets = {} triplets_idx = {} for imid in m['gt/%s/' % split].keys(): rlp_labels = m['gt/%s/%s/rlp_labels' % (split, imid)][...] sub_boxes = m['gt/%s/%s/sub_boxes' % (split, imid)][...] obj_boxes = m['gt/%s/%s/obj_boxes' % (split, imid)][...] for i in xrange(rlp_labels.shape[0]): p_idx = rlp_labels[i][1] s_idx = rlp_labels[i][0] o_idx = rlp_labels[i][2] p_str = zl.idx2name_pre(m, p_idx) s_str = zl.idx2name_cls(m, s_idx) o_str = zl.idx2name_cls(m, o_idx) sb = sub_boxes[i] ob = obj_boxes[i] #triplet = str(sub_cls_idx)+'_'+str(pre_idx)+'_'+str(obj_cls_idx) triplet_idx = str(s_idx) + '_' + str(p_idx) + '_' + str(o_idx) triplet = s_str + '_' + p_str + '_' + o_str if triplet not in triplets: triplets[triplet] = 0 if triplet_idx not in triplets_idx: triplets_idx[triplet_idx] = 0 triplets[triplet] += 1 triplets_idx[triplet_idx] += 1 triplets_sorted = sorted(triplets.items(), key=operator.itemgetter(1), reverse=True) triplets_idx_sorted = sorted(triplets_idx.items(), key=operator.itemgetter(1), reverse=True) return triplets, triplets_sorted
def visualize_gt_data(): m = h5py.File('/media/zawlin/ssd/Dropbox/proj/sg_vrd_meta.h5') split = 'test' triplets = {} root = 'data/sg_vrd_2016/Data/sg_%s_images/' % split for imid in m['gt/%s/' % split].keys(): im_path = root + imid + '.jpg' im = cv2.imread(im_path) rlp_labels = m['gt/%s/%s/rlp_labels' % (split, imid)][...] sub_boxes = m['gt/%s/%s/sub_boxes' % (split, imid)][...] obj_boxes = m['gt/%s/%s/obj_boxes' % (split, imid)][...] for i in xrange(rlp_labels.shape[0]): p_idx = rlp_labels[i][1] s_idx = rlp_labels[i][0] o_idx = rlp_labels[i][2] p_str = zl.idx2name_pre(m, p_idx) s_str = zl.idx2name_cls(m, s_idx) o_str = zl.idx2name_cls(m, o_idx) sb = sub_boxes[i] ob = obj_boxes[i] cv2.rectangle(im, (sb[0], sb[1]), (sb[2], sb[3]), (0, 200, 0), 2) cv2.rectangle(im, (ob[0], ob[1]), (ob[2], ob[3]), (0, 0, 200), 2) #triplet = str(sub_cls_idx)+'_'+str(pre_idx)+'_'+str(obj_cls_idx) triplet = s_str + '_' + p_str + '_' + o_str print triplet if triplet not in triplets: triplets[triplet] = 0 triplets[triplet] += 1 cv2.imshow('im', im) c = cv2.waitKey(0) & 0xFF if c == 27: exit(0)
def vg_make_meta_visual_phrase(): m = h5py.File('data/vg1_2_meta.h5', 'r', driver='core') h5f = h5py.File('data/vg1_2_vp_meta.h5') triplets = {} cnt = 0 zl.tick() for k in m['gt/train'].keys(): if cnt % 1000 == 0: print cnt, zl.tock() zl.tick() cnt += 1 # sub_boxes = m['gt/train/%s/sub_boxes'%k][...] # obj_boxes = m['gt/train/%s/obj_boxes'%k][...] rlp_labels = m['gt/train/%s/rlp_labels' % k][...] for i in xrange(rlp_labels.shape[0]): # sub_box = sub_boxes[i] # obj_box = obj_boxes[i] rlp_label = rlp_labels[i] # joint_bbox = [min(sub_bbox[0],obj_bbox[0]), min(sub_bbox[1],obj_bbox[1]),max(sub_bbox[2],obj_bbox[2]),max(sub_bbox[3],obj_bbox[3])] s_lbl = zl.idx2name_cls(m, rlp_label[0]) o_lbl = zl.idx2name_cls(m, rlp_label[2]) p_lbl = zl.idx2name_pre(m, rlp_label[1]) spo = '%s_%s_%s' % (s_lbl, p_lbl, o_lbl) # spo = '%d_%d_%d'%(rlp_label[0],rlp_label[1],rlp_label[2]) if spo not in triplets: triplets[spo] = 0 triplets[spo] += 1 zl.save('output/pkl/triplets_train_vp.pkl', triplets) triplets_sorted = zl.sort_dict_by_val(triplets) triplets_ok = [] for k, v in triplets_sorted: triplets_ok.append(k) print k, v triplets_ok = sorted(triplets_ok) triplets_ok = ['__background__'] + triplets_ok for i in xrange(len(triplets_ok)): h5f['meta/tri/idx2name/%d' % i] = triplets_ok[i] h5f['meta/tri/name2idx/%s' % triplets_ok[i]] = i print len(triplets_ok)
def detect(im, net, thresh, imid, m): rets = [] box_proposals = None score_raw, scores, fc7, boxes = im_detect(net, im, box_proposals) boxes_tosort = [] for j in xrange(1, 201): inds = np.where(scores[:, j] > thresh)[0] cls_scores = scores[inds, j] cls_boxes = boxes[inds, j * 4:(j + 1) * 4] cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) keep = nms(cls_dets, .5, force_cpu=True) # nms threshold cls_dets = cls_dets[keep, :] boxes_tosort.append(cls_dets) for j in xrange(len(boxes_tosort)): cls_dets = boxes_tosort[j] for di in xrange(cls_dets.shape[0]): di = cls_dets[di] score = di[-1] cls_idx = j + 1 #cls_name = str(m['meta/cls/idx2name/' + str(cls_idx)][...]) cls_name = zl.idx2name_cls(m, cls_idx) if score > 1: score = 1 ret = {} ret['cls_name'] = cls_name ret['x1'] = di[0] ret['y1'] = di[1] ret['x2'] = di[2] ret['y2'] = di[3] ret['score'] = score rets.append(ret) # cv2.putText(im, cls_name, (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1) # cv2.rectangle(im, (di[0], di[1]), (di[2], di[3]), (255, 0, 0), 2) return rets
def run_retrieval_vp_v2(): # h5_path = 'output/sg_vrd_2016_result.classeme.hdf5' h5_path = 'output/precalc/vg1_2_vp2016_test_nms2_.4.hdf5' m = h5py.File('/home/zawlin/Dropbox/proj/vg1_2_meta.h5', 'r', 'core') m_vp = h5py.File('/home/zawlin/Dropbox/proj/vg1_2_vp_meta.h5', 'r', 'core') cache_path = 'output/cache/%s.pkl' % h5_path.split('/')[-1] data_root = '/home/zawlin/data/data_vrd/vg_1.2/' rlp_map = [] for i in xrange(1, len(m_vp['meta/tri/idx2name'].keys())): tri = str(m_vp['meta/tri/idx2name'][str(i)][...]) s_lbl = tri.split('_')[0] p_lbl = tri.split('_')[1] o_lbl = tri.split('_')[2] rlp_label = [ zl.name2idx_cls(m, s_lbl), zl.name2idx_pre(m, p_lbl), zl.name2idx_cls(m, o_lbl) ] rlp_map.append(rlp_label) rlp_map = np.array(rlp_map) if os.path.exists(cache_path): print 'load from cache' h5f = zl.load(cache_path) else: h5_in = h5py.File(h5_path, 'r') h5f = {} print 'preloading data' for i in h5_in: h5f[i] = {} h5f[i]['labels'] = h5_in[i]['labels'][...] h5f[i]['confs'] = h5_in[i]['confs'][...] h5f[i]['boxes'] = h5_in[i]['boxes'][...] zl.save(cache_path, h5f) print 'preloading data done' retr_meta = zl.load('output/pkl/vg_retr_meta.pkl') thresh = 0.0 images = {} imids = h5f.keys() results = {} cnt = 0 r_acc_100 = 0 r_acc_50 = 0 tp_total = 0 gt_total = 0 median = [] total = 0 retr_meta = zl.load('output/pkl/vg_retr_meta.pkl') for k in xrange(len(retr_meta['rlp_labels'])): total += retr_meta['counts'][k] if k > 1000: print total / 1000. exit(0) break continue cnt += 1 rlp_label = retr_meta['rlp_labels'][k] gt_files = retr_meta['files'][k] # print gt_files # exit(0) # for f in gt_files: # impath= zl.imid2path(m,f) # print impath # im= cv2.imread(data_root+impath) # cv2.imshow('im',im) # cv2.waitKey(0) results = {} zl.tick() ranks = [] for imid in imids: labels = h5f[imid]['labels'] - 1 rlp_confs = h5f[imid]['confs'] rlp_labels = rlp_map[labels] if rlp_labels.shape[0] == 0: results[imid] = 0.0 continue indexor = np.arange(rlp_labels.shape[0]) ind = indexor[np.all(rlp_labels == rlp_label, axis=1)] if ind.shape[0] == 0: results[imid] = 0.0 continue confs = rlp_confs[ind] results[imid] = np.average(confs) results_sorted = zl.sort_dict_by_val(results) total_gt = len(gt_files) + 0.0 gt_total += total_gt + 0.0 tp_50 = 0. tp_100 = 0. found = False s_lbl = zl.idx2name_cls(m, rlp_label[0]) p_lbl = zl.idx2name_pre(m, rlp_label[1]) o_lbl = zl.idx2name_cls(m, rlp_label[2]) lbl_str = '%s_%s_%s' % (s_lbl, p_lbl, o_lbl) delay = 0 for i in xrange(len(results_sorted)): imid, v = results_sorted[i] impath = zl.imid2path(m, imid) if found and i >= 5: break # print gt_files cor = imid in gt_files if cor: if not found: found = True median.append(i) if i < 5: tp_100 += 1 tp_total += 1 if i < 50: tp_50 += 1 # if True: # cor_or_not = str(cor) # if cor :delay=0 # if delay ==0: # im = cv2.imread(data_root+impath) # cv2.putText(im, cor_or_not, (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1) # cv2.putText(im, lbl_str, (50, 80), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1) # cv2.putText(im, str(i), (50, 100), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1) # cv2.imshow('im',im) # c = cv2.waitKey(delay)&0xFF # if c ==27: # exit(0) # if c == ord('s'): # delay = 1-delay # if c == ord('c'): # delay = 1 r_50 = tp_50 / 5 #total_gt r_100 = tp_100 / 5 #total_gt r_acc_50 += r_50 r_acc_100 += r_100 med = np.median(median) print '%d %f %f %f %f %d %f' % (cnt, r_50, r_100, r_acc_50 / cnt, r_acc_100 / cnt, med, zl.tock())
def run(): prototxt = 'models/sg_vrd/rel_iccv/test_iccv_gt.prototxt' obj_detector_model='data/models/vrd_rfcn/vrd_resnet50_rfcn_iter_70000.caffemodel' relation_model='output/sg_vrd_rfcn/psroi_context_tri_sum_cached_iter_75500.caffemodel' caffe.set_mode_gpu() caffe.set_device(0) net = caffe.Net(prototxt, caffe.TEST) net.copy_from(obj_detector_model) net.copy_from(relation_model) m = h5py.File('/home/zawlin/Dropbox/proj/sg_vrd_meta.h5', 'r') cnt = 0 root_img = '/home/zawlin/Dropbox/iccv17_hw/_results_from_zl/img/' root_cls = '/home/zawlin/Dropbox/iccv17_hw/_results_from_zl/cls/' import glog for k in m['gt/test'].keys(): if not os.path.exists(root_img+k): os.makedirs(root_img+k) cnt += 1 glog.info(cnt) if cnt >80: break rlp_labels = m['gt/test/%s/rlp_labels'%k][...] sub_boxes = m['gt/test/%s/sub_boxes'%k][...].astype(np.float) obj_boxes = m['gt/test/%s/obj_boxes'%k][...].astype(np.float) if sub_boxes.shape[0]>0: zeros = np.zeros((sub_boxes.shape[0],1), dtype=np.float) # first index is always zero since we do one image by one image sub_boxes = np.concatenate((zeros, sub_boxes),axis=1) obj_boxes = np.concatenate((zeros, obj_boxes),axis=1) im_path = C.get_sg_vrd_path_test(k) im = cv2.imread(im_path) for i in xrange(sub_boxes.shape[0]): # sb = sub_boxes[i][1:].astype(np.int) # ob = obj_boxes[i][1:].astype(np.int) rlp = rlp_labels[i] rel = zl.idx2name_cls(m,rlp[0])+' '+zl.idx2name_pre(m,rlp[1])+' '+zl.idx2name_cls(m,rlp[2]) x1,y1,x2,y2 = union(sub_boxes[i][1:],obj_boxes[i][1:]) cv2.rectangle(im,(x1,y1),(x2,y2),(255,0,0),2) cv2.putText(im,rel,(x1,y1),cv2.FONT_HERSHEY_SIMPLEX,1.0,(0,0,255),2) # cv2.rectangle(im,(sb[0],sb[1]),(sb[2],sb[3]),(255,0,0),2) # cv2.rectangle(im,(ob[0],ob[1]),(ob[2],ob[3]),(255,0,0),2) cv2.imshow('im',im) cv2.imwrite(root_img+k+'/_.orig.jpg',im) im_detect(net,im_path,sub_boxes,obj_boxes) rfcn_sub_rel = net.blobs['rfcn_sub_rel'].data[0] rfcn_obj_rel = net.blobs['rfcn_obj_rel'].data[0] rfcn_union_rel = net.blobs['rfcn_union_rel'].data[0] for pi in xrange(70): index = pi head, last = index * 9, (index + 1)*9 feat_sub = rfcn_sub_rel[head:last] feat_obj= rfcn_obj_rel[head:last] feat_union= rfcn_union_rel[head:last] im_vis_sub = get_vis(feat_sub) im_vis_obj = get_vis(feat_obj) im_vis_union = get_vis(feat_union) pre = zl.idx2name_pre(m,pi) cv2.imwrite(root_img+k+'/%s_sub.jpg'%pre,im_vis_sub) cv2.imwrite(root_img+k+'/%s_obj.jpg'%pre,im_vis_obj) cv2.imwrite(root_img+k+'/%s_union.jpg'%pre,im_vis_union) if not os.path.exists(root_cls+pre): os.makedirs(root_cls+pre) cv2.imwrite(root_cls+pre+'/%s_sub.jpg'%k,im_vis_sub) cv2.imwrite(root_cls+pre+'/%s_obj.jpg'%k,im_vis_obj) cv2.imwrite(root_cls+pre+'/%s_union.jpg'%k,im_vis_union) #cv2.imshow(pre+'sub',im_vis_sub) #cv2.imshow(pre+'obj',im_vis_obj) #cv2.imshow(pre+'union',im_vis_union) #if cv2.waitKey(0)==27: # exit(0) else: #todo #print nothing pass cv2.waitKey(0)
def run_gen_recall_vg(): def run_gen_recall(): cfg_from_file('experiments/cfgs/rfcn_end2end.yml') caffe.set_mode_gpu() caffe.set_device(0) h5f = h5py.File('output/vr_object_detections_rfcn.hdf5') m = h5py.File('/home/zawlin/Dropbox/proj/sg_vrd_meta.h5', 'r', 'core') net = caffe.Net( 'models/sg_vrd/resnet50/test.prototxt', #'output/rfcn_end2end/sg_vrd_2016_train/vrd_resnet50_rfcn_iter_500.caffemodel', 'output/rfcn_end2end/sg_vrd_2016_train/vrd_resnet50_rfcn_iter_80000.caffemodel', #'data/models/vrd_rfcn/vrd_resnet50_rfcn_iter_70000.caffemodel', caffe.TEST) # net.name = os.path.splitext(os.path.basename(args.caffemodel))[0] net.name = 'sgvrd' imdb = get_imdb('sg_vrd_2016_test') imdb.competition_mode(0) """Test a Fast R-CNN network on an image database.""" num_images = len(imdb.image_index) # all detections are collected into: # all_boxes[cls][image] = N x 5 array of detections in # (x1, y1, x2, y2, score) all_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(imdb.num_classes)] root = 'data/sg_vrd_2016/Data/sg_test_images/' _t = {'im_detect': Timer(), 'misc': Timer()} cnt = 0 thresh = 0.001 img_set_file = 'data/sg_vrd_2016/ImageSets/test.txt' imlist = { line.strip().split(' ')[1]: line.strip().split(' ')[0] for line in open(img_set_file) } mat_pred_label = [] mat_pred_conf = [] mat_pred_bb = [] mat_gt_label = [] mat_gt_bb = [] for i in xrange(1000): imid = str(m['db/testidx/' + str(i)][...]) im_path = root + imid + '.jpg' cnt += 1 im = cv2.imread(im_path) cv2.imshow('im', im) if im == None: print im_path _t['im_detect'].tic() scores, boxes, boxes_rpn = im_detect(net, im, None) _t['im_detect'].toc() # scores = score_raw _t['misc'].tic() # h5f.create_dataset(imid + '/scores',dtype='float16', data=scores.astype(np.float16)) # h5f.create_dataset(imid + '/boxes',dtype='short', data=boxes.astype(np.short)) # scores=np.apply_along_axis(softmax,1,scores) # scores[:,16]+=icr boxes_tosort = [] for j in xrange(1, 101): inds = np.where(scores[:, j] > 0.00001)[0] cls_scores = scores[inds, j] cls_boxes = boxes[inds, 4:8] #cls_boxes = boxes[inds, j * 4:(j + 1) * 4] # cls_boxes = boxes[inds] cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) keep = nms(cls_dets, .2, force_cpu=True) # nms threshold # keep = nms_fast(cls_dets,.3) cls_dets = cls_dets[keep, :] boxes_tosort.append(cls_dets) mat_pred_label_i = [] mat_pred_conf_i = [] mat_pred_bb_i = [] for j in xrange(len(boxes_tosort)): cls_dets = boxes_tosort[j] for di in xrange(cls_dets.shape[0]): # print 'here' di = cls_dets[di] score = di[-1] cls_idx = j + 1 cls_name = zl.idx2name_cls(m, cls_idx) #cls_name = str(m['meta/cls/idx2name/' + str(cls_idx)][...]) if score > 1: score = 1 if score < thresh: continue cv2.rectangle(im, (di[0], di[1]), (di[2], di[3]), (255, 0, 0), 2) x, y = int(di[0]), int(di[1]) if x < 10: x = 15 if y < 10: y = 15 mat_pred_label_i.append(cls_idx) mat_pred_conf_i.append(score) mat_pred_bb_i.append([di[0], di[1], di[2], di[3]]) cv2.putText(im, cls_name, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2) res_line = '%s %d %f %d %d %d %d' % ( imid, cls_idx, score, di[0], di[1], di[2], di[3]) #output.write(res_line+'\n') mat_pred_label.append(mat_pred_label_i) mat_pred_conf.append(mat_pred_conf_i) mat_pred_bb.append(mat_pred_bb_i) obj_boxes = m['gt/test/%s/obj_boxes' % imid][...] sub_boxes = m['gt/test/%s/sub_boxes' % imid][...] rlp_labels = m['gt/test/%s/rlp_labels' % imid][...] mat_gt_label_i = [] mat_gt_bb_i = [] mat_gt_i = [] for gti in xrange(obj_boxes.shape[0]): mat_gt_i.append([ rlp_labels[gti, 0], sub_boxes[gti, 0], sub_boxes[gti, 1], sub_boxes[gti, 2], sub_boxes[gti, 3] ]) mat_gt_i.append([ rlp_labels[gti, 2], obj_boxes[gti, 0], obj_boxes[gti, 1], obj_boxes[gti, 2], obj_boxes[gti, 3] ]) if len(mat_gt_i) > 0: mat_gt_i = np.array(mat_gt_i) mat_gt_i = zl.unique_arr(mat_gt_i) for gti in xrange(mat_gt_i.shape[0]): mat_gt_bb_i.append(mat_gt_i[gti, 1:]) mat_gt_label_i.append(mat_gt_i[gti, 0]) mat_gt_label.append(mat_gt_label_i) mat_gt_bb.append(mat_gt_bb_i) #matlab_gt.append(matlab_gt_i) #now get gt # cv2.imshow('im',im) # if cv2.waitKey(0) == 27: # exit(0) _t['misc'].toc() print 'im_detect: {:d} {:.3f}s {:.3f}s' \ .format(cnt, _t['im_detect'].average_time, _t['misc'].average_time) sio.savemat( 'output/sg_vrd_objs.mat', { 'pred_bb': mat_pred_bb, 'pred_conf': mat_pred_conf, 'pred_label': mat_pred_label, 'gt_bb': mat_gt_bb, 'gt_label': mat_gt_label })
def run_gen_result(): cfg_from_file('experiments/cfgs/rfcn_end2end.yml') caffe.set_mode_gpu() caffe.set_device(0) h5f = h5py.File('output/precalc/sg_vrd_objs.hdf5') m = h5py.File('data/sg_vrd_meta.h5', 'r', 'core') net = caffe.Net( 'models/sg_vrd/resnet50/test.prototxt', 'data/models/vrd_rfcn/vrd_resnet50_rfcn_iter_64000.caffemodel', caffe.TEST) net.name = 'sgvrd' root = 'data/sg_vrd_2016/Data/sg_test_images/' _t = {'im_detect': Timer(), 'misc': Timer()} cnt = 0 thresh = 0.01 for imid in m['gt/test/'].keys(): im_path = root + imid + '.jpg' cnt += 1 im = cv2.imread(im_path) cv2.imshow('im', im) if im == None: print im_path _t['im_detect'].tic() scores, boxes, boxes_rpn = im_detect(net, im, None) _t['im_detect'].toc() # scores = score_raw _t['misc'].tic() # h5f.create_dataset(imid + '/scores',dtype='float16', data=scores.astype(np.float16)) # h5f.create_dataset(imid + '/boxes',dtype='short', data=boxes.astype(np.short)) # scores=np.apply_along_axis(softmax,1,scores) # scores[:,16]+=icr boxes_tosort = [] for j in xrange(1, 101): inds = np.where(scores[:, j] > 0.00001)[0] cls_scores = scores[inds, j] cls_boxes = boxes[inds, 4:8] #cls_boxes = boxes[inds, j * 4:(j + 1) * 4] # cls_boxes = boxes[inds] cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) keep = nms(cls_dets, .2, force_cpu=True) # nms threshold # keep = nms_fast(cls_dets,.3) cls_dets = cls_dets[keep, :] boxes_tosort.append(cls_dets) h5_tosave = [] for j in xrange(len(boxes_tosort)): cls_dets = boxes_tosort[j] for di in xrange(cls_dets.shape[0]): # print 'here' di = cls_dets[di] score = di[-1] cls_idx = j + 1 cls_name = zl.idx2name_cls(m, cls_idx) #cls_name = str(m['meta/cls/idx2name/' + str(cls_idx)][...]) if score > 1: score = 1 if score < thresh: continue dilist = di.tolist() dilist.append(cls_idx) h5_tosave.append(dilist) cv2.rectangle(im, (di[0], di[1]), (di[2], di[3]), (255, 0, 0), 2) x, y = int(di[0]), int(di[1]) if x < 10: x = 15 if y < 10: y = 15 cv2.putText(im, cls_name, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2) res_line = '%s %d %f %d %d %d %d' % ( imid, cls_idx, score, di[0], di[1], di[2], di[3]) #output.write(res_line+'\n') h5f['test/%s/boxes' % imid] = np.array(h5_tosave).astype(np.float16) #cv2.imshow('im',im) #if cv2.waitKey(0) == 27: # exit(0) _t['misc'].toc() print 'im_detect: {:d} {:.3f}s {:.3f}s' \ .format(cnt, _t['im_detect'].average_time, _t['misc'].average_time)
def run_test_object_detection_eval(): cfg_from_file('experiments/cfgs/rfcn_end2end.yml') caffe.set_mode_gpu() caffe.set_device(0) h5f = h5py.File('output/vr_object_detections_rfcn.hdf5') m = h5py.File('data/sg_vrd_meta.h5', 'r', 'core') net = caffe.Net( 'models/sg_vrd/resnet50/test.prototxt', 'data/models/vrd_rfcn/vrd_resnet50_rfcn_iter_64000.caffemodel', caffe.TEST) # net.name = os.path.splitext(os.path.basename(args.caffemodel))[0] net.name = 'sgvrd' imdb = get_imdb('sg_vrd_2016_test') imdb.competition_mode(0) """Test a Fast R-CNN network on an image database.""" num_images = len(imdb.image_index) # all detections are collected into: # all_boxes[cls][image] = N x 5 array of detections in # (x1, y1, x2, y2, score) all_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(imdb.num_classes)] root = 'data/sg_vrd_2016/Data/sg_test_images/' _t = {'im_detect': Timer(), 'misc': Timer()} cnt = 0 thresh = .1 img_set_file = 'data/sg_vrd_2016/ImageSets/test.txt' imlist = { line.strip().split(' ')[1]: line.strip().split(' ')[0] for line in open(img_set_file) } output = open('output/vr_object_detections.txt', 'w') for imid in imlist.keys(): im_path = root + imlist[imid] + '.jpg' cnt += 1 im = cv2.imread(im_path) cv2.imshow('im', im) if im == None: print im_path _t['im_detect'].tic() scores, boxes, boxes_rpn = im_detect(net, im, None) _t['im_detect'].toc() # scores = score_raw _t['misc'].tic() # h5f.create_dataset(imid + '/scores',dtype='float16', data=scores.astype(np.float16)) # h5f.create_dataset(imid + '/boxes',dtype='short', data=boxes.astype(np.short)) # scores=np.apply_along_axis(softmax,1,scores) # scores[:,16]+=icr boxes_tosort = [] for j in xrange(1, 101): inds = np.where(scores[:, j] > 0.001)[0] cls_scores = scores[inds, j] cls_boxes = boxes[inds, 4:8] #cls_boxes = boxes[inds, j * 4:(j + 1) * 4] # cls_boxes = boxes[inds] cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) keep = nms(cls_dets, .4, force_cpu=True) # nms threshold # keep = nms_fast(cls_dets,.3) cls_dets = cls_dets[keep, :] boxes_tosort.append(cls_dets) for j in xrange(len(boxes_tosort)): cls_dets = boxes_tosort[j] for di in xrange(cls_dets.shape[0]): # print 'here' di = cls_dets[di] score = di[-1] cls_idx = j + 1 cls_name = zl.idx2name_cls(m, cls_idx) #cls_name = str(m['meta/cls/idx2name/' + str(cls_idx)][...]) if score > 1: score = 1 if score < thresh: continue cv2.rectangle(im, (di[0], di[1]), (di[2], di[3]), (255, 0, 0), 2) x, y = int(di[0]), int(di[1]) if x < 10: x = 15 if y < 10: y = 15 #cv2.putText(im,cls_name,(x,y),cv2.FONT_HERSHEY_SIMPLEX,1.0,(0,0,255),2) res_line = '%s %d %f %d %d %d %d' % ( imid, cls_idx, score, di[0], di[1], di[2], di[3]) output.write(res_line + '\n') #cv2.imshow('im',im) #if cv2.waitKey(0) == 27: # exit(0) _t['misc'].toc() print 'im_detect: {:d} {:.3f}s {:.3f}s' \ .format(cnt, _t['im_detect'].average_time, _t['misc'].average_time)
def gen_recall(): cfg_from_file('experiments/cfgs/rfcn_end2end_iccv_eb.yml') #cfg_from_file('experiments/cfgs/rfcn_end2end_iccv_eb.yml') imdb, roidb = combined_roidb('sg_vrd_2016_test') m = h5py.File('/home/zawlin/Dropbox/proj/sg_vrd_meta.h5', 'r', 'core') import cv2 h5path = 'data/sg_vrd_2016/EB/eb.h5' h5f = h5py.File(h5path,driver='core') h5_rois = {} for i in h5f['test/']: data=h5f['test/%s'%i][...].astype(np.float32) idx = np.argsort(data[:,-1],axis=0) data_sorted = data[idx][::-1] data_sorted_idx = np.where((data_sorted[:,2]-data_sorted[:,0]>20) & (data_sorted[:,3]-data_sorted[:,1]>20)) data_sorted = data_sorted[data_sorted_idx] #print data_sorted h5_rois[i] = data_sorted[:4000,:4] #cfg.TEST.HAS_RPN=False net =None #prototxt = 'models/pascal_voc/ResNet-50/rfcn_end2end/test_iccv_eb_sigmoid.prototxt' prototxt = 'models/sg_vrd/wsd/test_eb_wsddn_s.prototxt' #model = 'data/rfcn_models/resnet50_rfcn_iter_1200.caffemodel' #model = 'output/rfcn_end2end/voc_0712_train/resnet50_rfcn_iter_16000.caffemodel' #model = 'output/rfcn_end2end/voc_0712_train/eb_wsddn_s_iter_5000.caffemodel' model = 'output/rfcn_end2end/sg_vrd_2016_train/eb_wsddn_s_iter_11000.caffemodel' #model = 'data/rfcn_models/resnet50_rfcn_final.caffemodel' #model = 'output/rfcn_end2end/voc_0712_train/resnet50_rfcn_eb_sigx_iter_100000.caffemodel' #model = 'data/rfcn_models_iccv/eb_resnet50_rfcn_iter_600.caffemodel' caffe.set_mode_gpu() caffe.set_device(0) net = caffe.Net(prototxt, model, caffe.TEST) #prototxt = 'models/pascal_voc/ResNet-50/rfcn_end2end/test_iccv_rpn.prototxt' #model = 'data/rfcn_models_iccv/eb_resnet50_rfcn_iter_800.caffemodel' #model = 'output/rfcn_end2end/voc_0712_train/resnet50_rfcn_iter_1600.caffemodel' #model = 'data/rfcn_models_iccv/eb_resnet50_rfcn_iter_800.caffemodel' #net2 = caffe.Net(prototxt, model, caffe.TEST) #net.params['conv_new_1_zl'][0].data[...] = net2.params['conv_new_1_zl'][0].data[...] #net.params['conv_new_1_zl'][1].data[...] = net2.params['conv_new_1_zl'][1].data[...] #net2 = None net.name = 'resnet50_rfcn_iter_1200' num_images = len(imdb.image_index) #num_images = 100 #del imdb.image_index[num_images:] all_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(imdb.num_classes)] output_dir = get_output_dir(imdb, net) zl.tic() # timers _t = {'im_detect' : Timer(), 'misc' : Timer()} max_per_image =20 thresh = 0.00001 cv2.namedWindow('im',0) cnt = 0 mat_pred_label = [] mat_pred_conf = [] mat_pred_bb = [] mat_gt_label = [] mat_gt_bb = [] for i in xrange(num_images): cnt+=1 # filter out any ground truth boxes im_path = imdb.image_path_at(i) im_name = im_path.split('/')[-1] imid = im_name[:-4] eb_roi = h5_rois[im_name] im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect_iccv(net, im, eb_roi) _t['im_detect'].toc() _t['misc'].tic() boxes_tosort = [] for j in xrange(1, 101): inds = np.where(scores[:, j-1] > 0.00001)[0] cls_scores = scores[inds, j-1] cls_boxes = boxes[inds, 1:] #cls_boxes = boxes[inds, j * 4:(j + 1) * 4] # cls_boxes = boxes[inds] cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) keep = nms(cls_dets, .7, force_cpu=True) # nms threshold # keep = nms_fast(cls_dets,.3) cls_dets = cls_dets[keep, :] boxes_tosort.append(cls_dets) mat_pred_label_i = [] mat_pred_conf_i = [] mat_pred_bb_i = [] for j in xrange(len(boxes_tosort)): cls_dets = boxes_tosort[j] idx = np.argsort(cls_dets[:,-1],axis=0)[::-1] cls_dets = cls_dets[idx] if cls_dets.shape[0]>max_per_image: cls_dets = cls_dets[:max_per_image,:] for di in xrange(cls_dets.shape[0]): # print 'here' di = cls_dets[di] score = di[-1] cls_idx = j + 1 cls_name = zl.idx2name_cls(m,cls_idx) #cls_name = str(m['meta/cls/idx2name/' + str(cls_idx)][...]) if score > 1: score = 1 if score < thresh: continue cv2.rectangle(im,(di[0],di[1]),(di[2],di[3]),(255,0,0),2) x, y = int(di[0]), int(di[1]) if x < 10: x = 15 if y < 10: y = 15 mat_pred_label_i.append(cls_idx) mat_pred_conf_i.append(score) mat_pred_bb_i.append([di[0],di[1],di[2],di[3]]) cv2.putText(im,cls_name,(x,y),cv2.FONT_HERSHEY_SIMPLEX,1.0,(0,0,255),2) res_line = '%s %d %f %d %d %d %d'%(imid,cls_idx,score,di[0],di[1],di[2],di[3]) mat_pred_label.append(mat_pred_label_i) mat_pred_conf.append(mat_pred_conf_i) mat_pred_bb.append(mat_pred_bb_i) obj_boxes = m['gt/test/%s/obj_boxes'%imid][...] sub_boxes = m['gt/test/%s/sub_boxes'%imid][...] rlp_labels = m['gt/test/%s/rlp_labels'%imid][...] mat_gt_label_i = [] mat_gt_bb_i = [] mat_gt_i = [] for gti in xrange(obj_boxes.shape[0]): mat_gt_i.append([rlp_labels[gti,0],sub_boxes[gti,0],sub_boxes[gti,1],sub_boxes[gti,2],sub_boxes[gti,3]]) mat_gt_i.append([rlp_labels[gti,2],obj_boxes[gti,0],obj_boxes[gti,1],obj_boxes[gti,2],obj_boxes[gti,3]]) if len(mat_gt_i)>0: mat_gt_i = np.array(mat_gt_i) mat_gt_i=zl.unique_arr(mat_gt_i) for gti in xrange(mat_gt_i.shape[0]): mat_gt_bb_i.append(mat_gt_i[gti,1:]) mat_gt_label_i.append(mat_gt_i[gti,0]) mat_gt_label.append(mat_gt_label_i) mat_gt_bb.append(mat_gt_bb_i) #matlab_gt.append(matlab_gt_i) #now get gt cv2.imshow('im',im) if cv2.waitKey(0) == 27: exit(0) _t['misc'].toc() print 'im_detect: {:d} {:.3f}s {:.3f}s' \ .format(cnt, _t['im_detect'].average_time, _t['misc'].average_time) sio.savemat('output/sg_vrd_objs.mat', {'pred_bb': mat_pred_bb, 'pred_conf':mat_pred_conf, 'pred_label':mat_pred_label, 'gt_bb':mat_gt_bb, 'gt_label':mat_gt_label })
def run_retrieval_zeroshot(): # h5_path = 'output/sg_vrd_2016_result_all_19500.hdf5' # h5_path = 'output/sg_vrd_2016_result_diff_all_5000.hdf5' # h5_path= 'output/results/lu_method_results.hdf5' # h5_path = 'output/sg_vrd_2016_result.hdf5.dd' # h5_path = 'output/results/lu_method_results_max.hdf5' h5_path = 'output/results/lu_visual_method_results.hdf5' data_root = 'data/sg_vrd_2016/Data/sg_test_images/' m = h5py.File('/home/zawlin/Dropbox/proj/sg_vrd_meta.h5', 'r', 'core') zeroshots = m['meta/zeroshots'][...] gt_cache_path = 'output/cache/sg_vrd_gt_cache.pkl' gt_h5f = {} np.random.seed(76) if os.path.exists(gt_cache_path): print 'load gt from cache' gt_h5f = zl.load(gt_cache_path) else: print 'cacheing gt' for k in m['gt/test']: gt_h5f[k] = {} sub_boxes = m['gt/test'][k]['sub_boxes'][...] rlp_labels = m['gt/test'][k]['rlp_labels'][...] obj_boxes = m['gt/test'][k]['obj_boxes'][...] gt_h5f[k]['sub_boxes'] = sub_boxes gt_h5f[k]['obj_boxes'] = obj_boxes gt_h5f[k]['rlp_labels'] = rlp_labels print 'caching gt done' zl.save(gt_cache_path, gt_h5f) cache_path = 'output/cache/%s.pkl' % h5_path.split('/')[-1] # h5f = h5py.File('output/sg_vrd_2016_result.classeme.hdf5') if os.path.exists(cache_path): print 'load from cache' h5f = zl.load(cache_path) else: h5_in = h5py.File(h5_path, 'r') h5f = {} print 'preloading data' for i in h5_in: h5f[i] = {} h5f[i]['rlp_labels'] = h5_in[i]['rlp_labels'][...] h5f[i]['rlp_confs'] = h5_in[i]['rlp_confs'][...] h5f[i]['sub_boxes'] = h5_in[i]['sub_boxes'][...] h5f[i]['obj_boxes'] = h5_in[i]['obj_boxes'][...] zl.save(cache_path, h5f) print 'preloading data done' #h5f = h5py.file('output/results/lu_method_results.hdf5') retr_meta = zl.load('output/pkl/vr_retr_meta.pkl') thresh = 0.0 images = {} imids = h5f.keys() results = {} cnt = 0 r_acc_100 = 0 r_acc_50 = 0 tp_total = 0 gt_total = 0 median = [] for k in xrange(len(retr_meta['rlp_labels'])): # if k>1000: # break rlp_label = retr_meta['rlp_labels'][k] if not np.any(np.all(zeroshots == rlp_label, axis=1)): continue gt_files = retr_meta['files'][k] cnt += 1 results = {} zl.tic() for imid in imids: rlp_labels = h5f[imid]['rlp_labels'] rlp_confs = h5f[imid]['rlp_confs'] if rlp_confs.shape[0] == 0: results[imid] = 0.0 continue indexor = np.arange(rlp_labels.shape[0]) ind = indexor[np.all(rlp_labels == rlp_label, axis=1)] if ind.shape[0] == 0: results[imid] = 0.0 continue confs = rlp_confs[ind] results[imid] = np.random.uniform(-1, 1) #np.average(confs) results_sorted = zl.sort_dict_by_val(results) total_gt = len(gt_files) + 0.0 gt_total += total_gt + 0.0 tp_50 = 0. tp_100 = 0. found = False delay = 0 s_lbl = zl.idx2name_cls(m, rlp_label[0]) p_lbl = zl.idx2name_pre(m, rlp_label[1]) o_lbl = zl.idx2name_cls(m, rlp_label[2]) lbl_str = '%s_%s_%s' % (s_lbl, p_lbl, o_lbl) r_at_k = 5 for i in xrange(len(results_sorted)): imid, v = results_sorted[i] if found and i >= r_at_k: break cor = imid in gt_files if cor: if not found: found = True median.append(i) if i < r_at_k: tp_100 += 1 tp_total += 1 if i < 50: tp_50 += 1 # if True: # cor_or_not = str(cor) # if cor :delay=0 # if delay ==0: # im = cv2.imread(data_root+imid+'.jpg') # cv2.putText(im, cor_or_not, (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1) # cv2.putText(im, lbl_str, (50, 80), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1) # cv2.putText(im, str(i), (50, 100), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1) # cv2.imshow('im',im) # c = cv2.waitKey(delay)&0xFF # if c ==27: # exit(0) # if c == ord('s'): # delay = 1-delay # if c == ord('c'): # delay = 1 r_50 = tp_50 / r_at_k #total_gt r_100 = tp_100 / r_at_k #total_gt r_acc_50 += r_50 r_acc_100 += r_100 med = np.median(median) print '%d %f %f %f %f %d %f' % (cnt, r_50, r_100, r_acc_50 / cnt, r_acc_100 / cnt, med, zl.toc())
def gen_meta_for_retrieval(): out_pkl = 'output/pkl/vr_retr_meta.pkl' m = h5py.File('/home/zawlin/Dropbox/proj/sg_vrd_meta.h5', 'r') data_root = 'data/sg_vrd_2016/Data/sg_test_images/' rlp_labels = [] files = [] counts = [] sub_boxes = [] obj_boxes = [] for k in m['gt/test']: gt_rlp_labels = m['gt/test'][k]['rlp_labels'][...] gt_sub_boxes = m['gt/test'][k]['sub_boxes'][...] gt_obj_boxes = m['gt/test'][k]['obj_boxes'][...] for i in xrange(gt_rlp_labels.shape[0]): gt_rlp_label = gt_rlp_labels[i] gt_sub_box = gt_sub_boxes[i] gt_obj_box = gt_obj_boxes[i] if len(rlp_labels) == 0: rlp_labels.append(gt_rlp_label.tolist()) files.append([k]) sub_boxes.append([gt_sub_box.tolist()]) obj_boxes.append([gt_obj_box.tolist()]) counts.append(1) continue bInd = np.all(gt_rlp_label == rlp_labels, axis=1) ind = np.arange(len(rlp_labels))[bInd] if len(ind) == 0: rlp_labels.append(gt_rlp_label.tolist()) files.append([k]) counts.append(1) sub_boxes.append([gt_sub_box.tolist()]) obj_boxes.append([gt_obj_box.tolist()]) else: files[ind].append(k) counts[ind] = counts[ind] + 1 sub_boxes[ind].append(gt_sub_box.tolist()) obj_boxes[ind].append(gt_obj_box.tolist()) # rlp_labels.append(gt_rlp_label.tolist()) # files.append([k]) # counts.append(1) rlp_labels = np.array(rlp_labels) files = np.array(files) counts = np.array(counts) sub_boxes = np.array(sub_boxes) obj_boxes = np.array(obj_boxes) ind = np.argsort(counts)[::-1] counts = counts[ind] files = files[ind] rlp_labels = rlp_labels[ind] sub_boxes = sub_boxes[ind] obj_boxes = obj_boxes[ind] print sub_boxes[:4] for i in xrange(20): rlp_label = rlp_labels[i] print files[i] s_lbl = zl.idx2name_cls(m, rlp_label[0]) p_lbl = zl.idx2name_pre(m, rlp_label[1]) o_lbl = zl.idx2name_cls(m, rlp_label[2]) print s_lbl, p_lbl, o_lbl for j in xrange(len(files[i])): s_box = sub_boxes[i][j] o_box = obj_boxes[i][j] fpath = files[i][j] impath = data_root + fpath + '.jpg' im = cv2.imread(impath) cv2.rectangle(im, (s_box[0], s_box[1]), (s_box[2], s_box[3]), (0, 255, 0), 1) cv2.rectangle(im, (o_box[0], o_box[1]), (o_box[2], o_box[3]), (255, 0, 0), 1) cv2.imshow('im', im) cv2.waitKey(0) retr_meta = { 'counts': counts, 'files': files, 'rlp_labels': rlp_labels, 'sub_boxes': sub_boxes, 'obj_boxes': obj_boxes } zl.save(out_pkl, retr_meta)
def gen_recall(): cfg_from_file('experiments/cfgs/rfcn_end2end_iccv_eb.yml') #cfg_from_file('experiments/cfgs/rfcn_end2end_iccv_eb.yml') m = h5py.File('/home/zawlin/Dropbox/proj/sg_vrd_meta.h5', 'r', 'core') h5objpath= 'output/precalc/sg_vrd_obj_rerank_wsd.h5' h5f_wsd = h5py.File(h5objpath,'a') h5path = 'data/sg_vrd_2016/EB/eb.h5' h5f = h5py.File(h5path,driver='core') h5_rois = {} for i in h5f['test/']: data=h5f['test/%s'%i][...].astype(np.float32) idx = np.argsort(data[:,-1],axis=0) data_sorted = data[idx][::-1] #data_sorted_idx = np.where((data_sorted[:,2]-data_sorted[:,0]>20) & (data_sorted[:,3]-data_sorted[:,1]>20)) #data_sorted = data_sorted[data_sorted_idx] #print data_sorted h5_rois[i[:-4]] = data_sorted[:4000,:4] #cfg.TEST.HAS_RPN=False net =None #prototxt = 'models/pascal_voc/ResNet-50/rfcn_end2end/test_iccv_eb_sigmoid.prototxt' prototxt = 'models/sg_vrd/wsd/test_eb_wsddn_s.prototxt' #model = 'data/rfcn_models/resnet50_rfcn_iter_1200.caffemodel' root = 'data/sg_vrd_2016/Data/sg_test_images/' #model = 'output/rfcn_end2end/voc_0712_train/resnet50_rfcn_iter_16000.caffemodel' #model = 'output/rfcn_end2end/voc_0712_train/eb_wsddn_s_iter_5000.caffemodel' model = 'output/rfcn_end2end/sg_vrd_2016_train/eb_wsddn_s_iter_11000.caffemodel' #model = 'data/rfcn_models/resnet50_rfcn_final.caffemodel' #model = 'output/rfcn_end2end/voc_0712_train/resnet50_rfcn_eb_sigx_iter_100000.caffemodel' #model = 'data/rfcn_models_iccv/eb_resnet50_rfcn_iter_600.caffemodel' caffe.set_mode_gpu() caffe.set_device(0) net = caffe.Net(prototxt, model, caffe.TEST) db_net = caffe.Net('data/models/dbox/test.prototxt','data/models/dbox/fast-dbox-slidwindow-multiscale.caffemodel',caffe.TEST) net.name = 'resnet50_rfcn_iter_1200' zl.tic() # timers _t = {'im_detect' : Timer(), 'misc' : Timer()} max_per_image =5 thresh = 0.00001 cv2.namedWindow('im',0) cnt = 0 for k in m['gt/test'].keys(): im_path = root+k+'.jpg' im = cv2.imread(im_path) imid = k cnt+=1 # filter out any ground truth boxes eb_roi = h5_rois[imid] db_scores = dbox.im_obj_detect(db_net,im,eb_roi) idx = np.argsort(db_scores[:,-1],axis=0)[::-1] eb_roi = eb_roi[idx[:100]] _t['im_detect'].tic() scores, boxes = im_detect_iccv(net, im, eb_roi) _t['im_detect'].toc() _t['misc'].tic() boxes_tosort = [] h5_tosave = [] for j in xrange(1, 101): inds = np.where(scores[:, j-1] > 0.00001)[0] cls_scores = scores[inds, j-1] cls_boxes = boxes[inds, 1:] cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) cls_idx = np.zeros((cls_dets.shape[0],1))+j h5box = np.hstack((cls_boxes,cls_scores[:,np.newaxis],cls_idx)) if h5box.shape[0]>0: h5_tosave.extend(h5box.tolist()) keep = nms(cls_dets, .2, force_cpu=True) # nms threshold cls_dets = cls_dets[keep, :] boxes_tosort.append(cls_dets) h5_tosave = np.array(h5_tosave,dtype=np.float16) if h5_tosave.shape[0]>max_per_image: idx = np.argsort(h5_tosave[:,4],axis=0)[::-1] h5_tosave = h5_tosave[idx[:max_per_image]] for i in xrange(h5_tosave.shape[0]) : di=h5_tosave[i] x, y = int(di[0]), int(di[1]) if x < 10: x = 15 if y < 10: y = 15 score = di[-2] if score > 1: score = 1 if score < thresh: continue cls_idx = int(di[-1]) cls_name = zl.idx2name_cls(m,cls_idx) cv2.rectangle(im,(di[0],di[1]),(di[2],di[3]),(255,0,0),2) cv2.putText(im,cls_name,(x,y),cv2.FONT_HERSHEY_SIMPLEX,1.0,(0,0,255),2) # 5 for vrd # 10 for vg #h5f_wsd.create_dataset('test/'+imid+'/boxes',dtype='float16', data=h5_tosave) cv2.imshow('im',im) if cv2.waitKey(0) == 27: exit(0) _t['misc'].toc() print 'im_detect: {:d} {:.3f}s {:.3f}s' \ .format(cnt, _t['im_detect'].average_time, _t['misc'].average_time)