def test_net_base(sess, net, imdb, roidb, weights_filename, visualize=False, mode='all'): np.random.seed(cfg.RNG_SEED) """Test a Fast R-CNN network on an image database.""" num_images = len(roidb) output_dir = get_output_dir(imdb, weights_filename) output_dir_image = os.path.join(output_dir, 'images') if visualize and not os.path.exists(output_dir_image): os.makedirs(output_dir_image) all_scores = [[] for _ in range(num_images)] # timers _t = {'im_detect': Timer(), 'evaluate': Timer()} if mode == 'all': eval_modes = ['pred_cls', 'sg_cls', 'sg_det'] else: eval_modes = [mode] print('EVAL MODES = ') print(eval_modes) evaluators = {} for m in eval_modes: evaluators[m] = SceneGraphEvaluator(imdb, mode=m) for i in range(num_images): _t['score'].tic() all_scores[i], blobs = im_detect(sess, imdb, net, [roidb[i]]) _t['score'].toc() print('score: {:d}/{:d} {:.3f}s' \ .format(i + 1, num_images, _t['score'].average_time)) if visualize and i % 10 == 0: basename = os.path.basename(imdb.image_path_at(i)).split('.')[0] im_vis, wrong = draw_predicted_boxes_test(blobs['data'], all_scores[i], blobs['gt_boxes']) if wrong: out_image = os.path.join(output_dir_image, basename + '.jpg') print(out_image) cv2.imwrite(out_image, im_vis) res_file = os.path.join(output_dir, 'results.pkl') with open(res_file, 'wb') as f: pickle.dump(all_scores, f, pickle.HIGHEST_PROTOCOL) print('Evaluating detections') mcls_sc, mcls_ac, mcls_ap, mins_sc, mins_ac, mins_ap = imdb.evaluate(all_scores, output_dir) eval_file = os.path.join(output_dir, 'results.txt') with open(eval_file, 'w') as f: f.write('{:.3f} {:.3f} {:.3f} {:.3f}'.format(mins_ap, mins_ac, mcls_ap, mcls_ac))
def test_net(net_name, weight_name, imdb, mode, max_per_image=100): sess = tf.Session() # set up testing mode rois = tf.placeholder(dtype=tf.float32, shape=[None, 5], name='rois') rel_rois = tf.placeholder(dtype=tf.float32, shape=[None, 5], name='rel_rois') ims = tf.placeholder(dtype=tf.float32, shape=[None, None, None, 3], name='ims') relations = tf.placeholder(dtype=tf.int32, shape=[None, 2], name='relations') inputs = {'rois': rois, 'rel_rois': rel_rois, 'ims': ims, 'relations': relations, 'num_roi': tf.placeholder(dtype=tf.int32, shape=[]), 'num_rel': tf.placeholder(dtype=tf.int32, shape=[]), 'num_classes': imdb.num_classes, 'num_predicates': imdb.num_predicates, 'rel_mask_inds': tf.placeholder(dtype=tf.int32, shape=[None]), 'rel_segment_inds': tf.placeholder(dtype=tf.int32, shape=[None]), 'rel_pair_mask_inds': tf.placeholder(dtype=tf.int32, shape=[None, 2]), 'rel_pair_segment_inds': tf.placeholder(dtype=tf.int32, shape=[None]), 'n_iter': cfg.TEST.INFERENCE_ITER} net = get_network(net_name)(inputs) net.setup() print ('Loading model weights from {:s}').format(weight_name) saver = tf.train.Saver() saver.restore(sess, weight_name) roidb = imdb.roidb if cfg.TEST.USE_RPN_DB: imdb.add_rpn_rois(roidb, make_copy=False) prepare_roidb(roidb) num_images = len(imdb.image_index) # timers _t = {'im_detect' : Timer(), 'evaluate' : Timer()} if mode == 'all': eval_modes = ['pred_cls', 'sg_cls', 'sg_det'] else: eval_modes = [mode] multi_iter = [net.n_iter - 1] if net.iterable else [0] print('Graph Inference Iteration ='), print(multi_iter) print('EVAL MODES ='), print(eval_modes) # initialize evaluator for each task evaluators = {} for m in eval_modes: evaluators[m] = {} for it in multi_iter: evaluators[m][it] = SceneGraphEvaluator(imdb, mode=m) for im_i in xrange(num_images): im = imdb.im_getter(im_i) for mode in eval_modes: bbox_reg = True if mode == 'pred_cls' or mode == 'sg_cls': # use ground truth object locations bbox_reg = False box_proposals = gt_rois(roidb[im_i]) else: # use RPN-proposed object locations box_proposals, roi_scores = non_gt_rois(roidb[im_i]) roi_scores = np.expand_dims(roi_scores, axis=1) nms_keep = cpu_nms(np.hstack((box_proposals, roi_scores)).astype(np.float32), cfg.TEST.PROPOSAL_NMS) nms_keep = np.array(nms_keep) num_proposal = min(cfg.TEST.NUM_PROPOSALS, nms_keep.shape[0]) keep = nms_keep[:num_proposal] box_proposals = box_proposals[keep, :] if box_proposals.size == 0 or box_proposals.shape[0] < 2: # continue if no graph continue _t['im_detect'].tic() out_dict = im_detect(sess, net, inputs, im, box_proposals, bbox_reg, multi_iter) _t['im_detect'].toc() _t['evaluate'].tic() for iter_n in multi_iter: sg_entry = out_dict[iter_n] evaluators[mode][iter_n].evaluate_scene_graph_entry(sg_entry, im_i, iou_thresh=0.5) _t['evaluate'].toc() print 'im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(im_i + 1, num_images, _t['im_detect'].average_time, _t['evaluate'].average_time) # print out evaluation results 打印结果 for mode in eval_modes: for iter_n in multi_iter: evaluators[mode][iter_n].print_stats()
def eval_net(score_file, imdb, mode, write_rel_f, max_per_image=100): dic = json.load(open("/ssd_disk/gay/scenegraph/scannet-SGG-dicts.json")) preds_ok = np.zeros((len(dic['predicate_to_idx']) + 1, 1)) preds_nok = np.zeros((len(dic['predicate_to_idx']) + 1, 1)) sg_entries = pickle.load(open(score_file)) # set up testing mode roidb = imdb.roidb if cfg.TEST.USE_RPN_DB: imdb.add_rpn_rois(roidb, make_copy=False) prepare_roidb(roidb) num_images = len(imdb.image_index) # timers _t = {'im_detect': Timer(), 'evaluate': Timer()} if mode == 'all': eval_modes = ['pred_cls', 'sg_cls', 'sg_det'] else: eval_modes = [mode] multi_iter = [1] #FIXME,. maybe try 2 print('Graph Inference Iteration ='), print(multi_iter) print('EVAL MODES ='), print(eval_modes) # initialize evaluator for each task evaluators = {} for m in eval_modes: evaluators[m] = {} for it in multi_iter: evaluators[m][it] = SceneGraphEvaluator(imdb, mode=m) sg_i = -1 preds = np.zeros((0, 1)) f = open(write_rel_f, 'w') """ #pickle.dump(roidb,open('gt.pc','wb')) my_roidb = [] for i in range(0,num_images): gt_en = {} gt_en['gt_classes'] = roidb[i]['gt_classes'] gt_en['boxes'] = roidb[i]['boxes'] gt_en['gt_relations'] = roidb[i]['gt_relations'] my_roidb.append(gt_en) pickle.dump(my_roidb,open('gt.pc','wb')) import sys; sys.exit() """ for im_i in xrange(0, num_images, 1): #xrange(num_images): im_i_roidb_full = imdb._image_index[im_i] seq_name = imdb.im2seq[im_i_roidb_full] im_path = imdb.impaths[imdb.roidb_idx_to_imdbidx[im_i_roidb_full]] fname = im_path.split('/')[-1].replace('.color.jpg', '') first_box_idx = imdb.im_to_first_box[im_i] im = imdb.im_getter(im_i) sg_i += 1 sg_entry = sg_entries[sg_i] scores_rel = sg_entry['relations'] gt_rel = roidb[im_i]['gt_relations'] gt_rela = np.hstack([np.ones((gt_rel.shape[0], 1)) * im_i, gt_rel]) pred_rel_vec = np.zeros((gt_rel.shape[0], 1)) for i in range(gt_rel.shape[0]): o1 = gt_rela[i, 1] o2 = gt_rela[i, 2] o1_oid = imdb.roidb_to_scannet_oid[first_box_idx + o1][0] # to check later o2_oid = imdb.roidb_to_scannet_oid[first_box_idx + o2][0] predicate = gt_rela[i, 3] if scores_rel[int(o1), int(o2), int(predicate)] > scores_rel[int(o2), int(o1), int(predicate)]: pred_rel_vec[i] = 1 preds_ok[int(predicate)] += 1 f.write(' '.join( (seq_name, fname, str(o1_oid), str(o2_oid), imdb.info['idx_to_predicate'][str(int(predicate))], '1\n'))) else: preds_nok[int(predicate)] += 1 pred_rel_vec[i] = 0 f.write(' '.join( (seq_name, fname, str(o1_oid), str(o2_oid), imdb.info['idx_to_predicate'][str(int(predicate))], '0\n'))) preds = np.vstack([preds, pred_rel_vec]) for mode in eval_modes: bbox_reg = True if mode == 'pred_cls' or mode == 'sg_cls': # use ground truth object locations bbox_reg = False box_proposals = gt_rois(roidb[im_i]) else: # use RPN-proposed object locations box_proposals, roi_scores = non_gt_rois(roidb[im_i]) roi_scores = np.expand_dims(roi_scores, axis=1) nms_keep = cpu_nms( np.hstack((box_proposals, roi_scores)).astype(np.float32), cfg.TEST.PROPOSAL_NMS) nms_keep = np.array(nms_keep) num_proposal = min(cfg.TEST.NUM_PROPOSALS, nms_keep.shape[0]) keep = nms_keep[:num_proposal] box_proposals = box_proposals[keep, :] if box_proposals.size == 0 or box_proposals.shape[0] < 2: # continue if no graph continue _t['im_detect'].tic() quadric_rois = np.vstack( [roidb[im_i]['quadric_rois'], roidb[im_i]['quadric_rois']] ) # this duplication, I don't know why, but the boxes are duplicated, so I did the same, maybe it is to evaluate different aspect quadric_rois = np.hstack( [np.zeros((quadric_rois.shape[0], 1)), quadric_rois] ) #this is because in the training phase, the image number is appended #out_dict = im_detect(sess, net, inputs, im, box_proposals, bbox_reg, multi_iter, quadric_rois) _t['im_detect'].toc() _t['evaluate'].tic() for iter_n in multi_iter: evaluators[mode][iter_n].evaluate_scene_graph_entry( sg_entry, im_i, iou_thresh=0.5) _t['evaluate'].toc() print 'im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(im_i + 1, num_images, _t['im_detect'].average_time, _t['evaluate'].average_time) # print out evaluation results for mode in eval_modes: for iter_n in multi_iter: evaluators[mode][iter_n].print_stats() print 'direct accuracy of the predicates', np.mean(preds) print preds_ok print preds_nok print preds_ok / (preds_ok + preds_nok) f.close()