def _get_voc_results_file_template(self): # VOCdevkit/results/VOC2007/Main/<comp_id>_det_test_aeroplane.txt filename = self._get_comp_id() + '_det_' + self._image_set + '_{:s}.txt' path = os.path.join( get_output_dir(self, None), filename) return path
def test_net(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.05): np.random.seed(cfg.RNG_SEED) """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)] output_dir = get_output_dir(imdb, weights_filename) # timers _t = {'im_detect': Timer(), 'misc': Timer()} for i in xrange(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(sess, net, im) _t['im_detect'].toc() _t['misc'].tic() # skip j = 0, because it's the background class for j in xrange(1, imdb.num_classes): 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, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] all_boxes[j][i] = cls_dets # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack( [all_boxes[j][i][:, -1] for j in xrange(1, imdb.num_classes)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in xrange(1, imdb.num_classes): keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] _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) det_file = os.path.join(output_dir, 'detections.pkl') with open(det_file, 'wb') as f: cPickle.dump(all_boxes, f, cPickle.HIGHEST_PROTOCOL) print('Evaluating detections') imdb.evaluate_detections(all_boxes, output_dir)
def _find_draw_folder(self, mode, draw_folder): if (draw_folder is None): draw_folder = mode out_dir = os.path.join(get_output_dir(self, mode=mode), '{}_drawn'.format(draw_folder)) if not os.path.exists(out_dir): os.makedirs(out_dir) return out_dir
def delete_eval_draw_folder(self, im_folder, mode): datapath = os.path.join(get_output_dir(self, mode), '{}_drawn'.format(im_folder)) #datapath = os.path.join(cfg.DATA_DIR, self._name ,im_folder,'{}_drawn'.format(mode)) if (os.path.isdir(datapath)): print('deleting files in dir {}'.format(datapath)) shutil.rmtree(datapath) os.makedirs(datapath)
def test_net(net, imdb, weights_filename, max_per_image=100, thresh=0.): np.random.seed(cfg.RNG_SEED) """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 range(num_images)] for _ in range(imdb.num_classes)] output_dir = get_output_dir(imdb, weights_filename) # timers _t = {'im_detect': Timer(), 'misc': Timer()} for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(net, im) _t['im_detect'].toc() _t['misc'].tic() # skip j = 0, because it's the background class for j in range(1, imdb.num_classes): 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( torch.from_numpy(cls_boxes), torch.from_numpy(cls_scores), cfg.TEST.NMS).numpy() if cls_dets.size > 0 else [] cls_dets = cls_dets[keep, :] all_boxes[j][i] = cls_dets # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack( [all_boxes[j][i][:, -1] for j in range(1, imdb.num_classes)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in range(1, imdb.num_classes): keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] _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())) det_file = os.path.join(output_dir, 'detections.pkl') with open(det_file, 'wb') as f: pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL) print('Evaluating detections') imdb.evaluate_detections(all_boxes, output_dir)
def extract_net(sess, net, imdb, weights_filename, roidb, max_per_image=100, thresh=0.05): #change from test_net, and this function is used to extract features from conv5 of vgg16 np.random.seed(cfg.RNG_SEED) """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 range(num_images)] for _ in range(imdb.num_classes)] output_dir = get_output_dir(imdb, weights_filename) print("output_dir is:", output_dir) print("\n") # timers _t = {'im_detect': Timer(), 'misc': Timer()} data_input = [] #N_save is the number of processed images which will be saved into one file N_save = 100 for i in range(1000): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() feature_maps = im_extract(sess, net, im) fea = np.squeeze(feature_maps) data_temp = {} data_temp['img_path'] = imdb.image_path_at(i) data_temp['box'] = roidb[i]['boxes'] data_temp['img_shape'] = np.shape(im) data_temp['fea'] = fea data_input.append(data_temp) if (i + 1) % N_save == 0: file_name_of_input = '/home/yangxu/project/rd/input/input' + format( int((i + 1) / N_save), '03') + '.npz' np.savez(file_name_of_input, data_input=data_input) data_input = [] _t['im_detect'].toc() _t['misc'].tic() print('im_extract: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(i + 1, num_images, _t['im_detect'].average_time, _t['misc'].average_time)) print( "i is: {0}, the path of image is {1}, shape of the image is {2}, the shape of the feature map is{3}\n" .format(i, imdb.image_path_at(i), np.shape(im), np.shape(fea)))
def prepare_params(): # output directory where the models are saved output_dir = get_output_dir(imdb, args.tag, args.package_name) logger.info('Output will be saved to `{:s}`'.format(output_dir)) # tensorboard directory where the summaries are saved during training tb_dir = get_output_tb_dir(imdb, args.tag) logger.info('TensorFlow summaries will be saved to `{:s}`'.format(tb_dir)) return output_dir, tb_dir
def test_eval(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.05): np.random.seed(cfg.RNG_SEED) """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 range(num_images)] for _ in range(imdb.num_classes)] output_dir = get_output_dir(imdb, weights_filename) # timers _t = {'im_detect': Timer(), 'misc': Timer()} for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(sess, net, im) _t['im_detect'].toc() _t['misc'].tic() imc = im.copy() # skip j = 0, because it's the background class for j in range(1, imdb.num_classes): 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, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] all_boxes[j][i] = cls_dets high_score_index = np.argmax(cls_scores) cls_box = cls_boxes[high_score_index] cv2.rectangle(imc, (cls_box[0], cls_box[1]), (cls_box[2], cls_box[3]), color=(0, 255, 0)) cv2.putText(imc, imdb._classes[j], (int(cls_box[0]), int((cls_box[1] + cls_box[3]) / 2)), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 255, 0), 1, cv2.LINE_AA) cv2.imshow('boxs', cv2.resize(imc, (0, 0), fx=2, fy=2)) cv2.waitKey(0)
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_vertical(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.05): count = 0 np.random.seed(cfg.RNG_SEED) """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 range(num_images)] for _ in range(imdb.num_classes)] output_dir = get_output_dir(imdb, weights_filename) # timers _t = {'im_detect' : Timer(), 'misc' : Timer()} im = None for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) print (imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(sess, net, im) _t['im_detect'].toc() _t['misc'].tic() # skip j = 0, because it's the background class for j, cls in enumerate(CLASSES[1:]): j += 1 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, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] all_boxes[j][i] = cls_dets vertical_points = vis_detections(im, cls, cls_dets, thresh=0.9) #res_image, flag = vc.spine_contour(im, vertical_points, imdb.image_path_at(i)) #res_image = Draw_bone(res_image,vertical_points) #cv2.imwrite("test-" + str(count) + ".jpg", im) cv2.imshow('result', im) cv2.waitKey(1000) count = count + 1 '''
def test_net(sess, net, imdb, weights_filename, thresh=0.05): np.random.seed(cfg.RNG_SEED) """Test a SSH 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 range(num_images)] output_dir = get_output_dir(imdb, weights_filename) # timers _t = {'im_detect': Timer(), 'misc': Timer()} for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(sess, net, im) _t['im_detect'].toc() _t['misc'].tic() inds = np.where(scores[:, 0] > thresh)[0] scores = scores[inds, 0] boxes = boxes[inds, :] dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False) keep = nms(dets, cfg.TEST.NMS) dets = dets[keep, :] all_boxes[i] = det _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)) det_file = os.path.join(output_dir, 'detections.pkl') with open(det_file, 'wb') as f: pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL) print('Evaluating detections') imdb.evaluate_detections(all_boxes, output_dir)
def test_net_rpn_eval(sess, net, imdb, weights_filename, rpn_eval_nms=1., max_per_image=300, nms_thresh=0., ovthresh=[0.5, 0.6, 0.7, 0.8, 0.9]): np.random.seed(cfg.RNG_SEED) """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 range(num_images)] for _ in range(2)] # only bg/fg in rpn output_dir = get_output_dir(imdb, weights_filename) # timers _t = {'im_detect': Timer(), 'misc': Timer()} det_file = os.path.join( output_dir, 'detections_rpn_{}_{}_nms_v2.pkl'.format(str(rpn_eval_nms), str(max_per_image))) print('det result save to file: {}'.format(det_file)) if not os.path.exists(det_file): for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect_rpn(sess, net, im) _t['im_detect'].toc() _t['misc'].tic() # bboxes are all class-agnostic inds = np.where(scores > nms_thresh)[0] cls_scores = scores[inds] cls_boxes = boxes cls_dets = np.hstack((cls_boxes, cls_scores)) \ .astype(np.float32, copy=False) keep = nms(cls_dets, rpn_eval_nms) cls_dets = cls_dets[keep, :] if 0 < max_per_image < cls_dets.shape[0]: image_thresh = np.sort(cls_dets[:, -1])[-max_per_image] keep = np.where(cls_dets[:, -1] >= image_thresh)[0] cls_dets = cls_dets[keep, :] all_boxes[1][i] = cls_dets _t['misc'].toc() if (i + 1) % 100 == 0: print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(i + 1, num_images, _t['im_detect'].average_time, _t['misc'].average_time)) # vis_detections_one_img(im, 'temp.jpg', cls_dets[:15]) with open(det_file, 'wb') as f: pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL) else: with open(det_file, 'rb') as f: all_boxes = pickle.load(f) for t in ovthresh: print('Evaluating detections with ovthersh: {:.2f}'.format(t)) imdb.evaluate_detections_rpn_recall(all_boxes, output_dir, t)
print('Set proposal method: {:s}'.format(cfg.TRAIN.PROPOSAL_METHOD)) roidb = get_training_roidb(imdb) return roidb roidbs = [get_roidb(s) for s in imdb_names.split('+')] roidb = roidbs[0] if len(roidbs) > 1: for r in roidbs[1:]: roidb.extend(r) tmp = get_imdb(imdb_names.split('+')[1]) imdb = datasets.imdb.imdb(imdb_names, tmp.classes) else: imdb = get_imdb(imdb_names) return imdb, roidb save_path = '/home/vador/Documents/project/AI/drl-rpn-tf-video/output-weights/voc_2007_train/' # Cannot use weights_path because it leads to dimensions mismatch weigths_path = '/home/vador/Documents/project/AI/drl-rpn-tf-video/pretrained-data/data3D/vgg16_drl_rpn_iter_1.ckpt' imdb_name = 'voc_2007_train' imdbval_name = 'voc_2007_trainval' imdb, roidb = combined_roidb(imdb_name) print('{:d} roidb entries'.format(len(roidb))) output_dir = get_output_dir(imdb, None, save_path) _, valroidb = combined_roidb(imdbval_name) train_net(net, imdb, roidb, valroidb, output_dir, pretrained_model=weigths_path, max_iters=1)
def test_net(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.65): np.random.seed(cfg.RNG_SEED) """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 range(num_images)] for _ in range(imdb.num_classes)] output_dir = get_output_dir(imdb, weights_filename) # timers _t = {'im_detect' : Timer(), 'misc' : Timer()} for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(sess, net, im) _t['im_detect'].toc() _t['misc'].tic() # skip j = 0, because it's the background class for j in range(1, imdb.num_classes): 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, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] all_boxes[j][i] = cls_dets # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack([all_boxes[j][i][:, -1] for j in range(1, imdb.num_classes)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in range(1, imdb.num_classes): keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] #fow img show #print(imdb.image_path_at(i)) #for k in range(len(all_boxes[1][i])): # cv2.rectangle(im,(all_boxes[1][i][k,0],all_boxes[1][i][k,1]),(all_boxes[1][i][k,2],all_boxes[1][i][k,3]),(0,0,255),2) #cv2.imwrite(os.path.join(output_dir,str(i)+'.jpg'),im) #for output txt if i % 100 == 0: print('{}/10000 done'.format(i)) result_file = os.path.join(output_dir,imdb._image_index[i]+'.txt') fout = open(result_file,'w') for k in range(len(all_boxes[1][i])): x1 = all_boxes[1][i][k,0] y1 = all_boxes[1][i][k,1] x2 = all_boxes[1][i][k,0] y2 = all_boxes[1][i][k,3] x3 = all_boxes[1][i][k,2] y3 = all_boxes[1][i][k,3] x4 = all_boxes[1][i][k,2] y4 = all_boxes[1][i][k,1] fout.write(str(x1)+','+str(y1)+','+str(x2)+','+str(y2)+','+str(x3)+','+str(y3)+','+str(x4)+','+str(y4)+'\n') fout.close() det_file = os.path.join(output_dir, 'detections.pkl') with open(det_file, 'wb') as f: pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)
def test_net(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.00): """Test a drl-RPN network on an image database.""" # Set numpy's random seed np.random.seed(cfg.RNG_SEED) nbr_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 range(nbr_images)] for _ in range(cfg.NBR_CLASSES)] output_dir = get_output_dir(imdb, weights_filename) # timers _t = {'im_detect': Timer(), 'misc': Timer()} _t_drl_rpn = { 'init': Timer(), 'fulltraj': Timer(), 'upd-obs-vol': Timer(), 'upd-seq': Timer(), 'upd-rl': Timer(), 'action-rl': Timer(), 'coll-traj': Timer() } avg_traj = 0.0 avg_frac = 0.0 # Create StatCollector (tracks various drl-RPN test statistics) stat_strings = ['#fix/img', 'exploration'] #sc = StatCollector(nbr_images, stat_strings, False) # Try getting gt-info if available try: gt_roidb = imdb.gt_roidb() print(gt_roidb[0]) except: gt_roidb = None # Visualize search trajectories? do_visualize = cfg.DRL_RPN_TEST.DO_VISUALIZE # Can be convenient to run from some other image, especially if visualizing, # but having nbr_ims_eval = nbr_images and start_idx = 0 --> regular testing! nbr_ims_eval = nbr_images start_idx = 0 end_idx = 4 #start_idx + nbr_ims_eval # Test drl-RPN on the test images for i in range(start_idx, end_idx): # Need to know image index if performing visualizations if do_visualize: im_idx = i else: im_idx = None # Try extracting gt-info for diagnostics (possible for voc 2007) if gt_roidb is None: nbr_gts = None else: nbr_gts = gt_roidb[i]['boxes'].shape[0] # Detect! im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes, _t_drl_rpn, stats = im_detect(sess, net, im, _t_drl_rpn, im_idx, nbr_gts) _t['im_detect'].toc() # Update and print some stats # sc.update(0, stats) # sc.print_stats(False) _t['misc'].tic() # skip j = 0, because it's the background class for j in range(1, cfg.NBR_CLASSES): 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])) keep = nms(cls_dets, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] all_boxes[j][i] = cls_dets # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack( [all_boxes[j][i][:, -1] for j in range(1, cfg.NBR_CLASSES)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in range(1, cfg.NBR_CLASSES): keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] _t['misc'].toc() print('\nim_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(i + 1, nbr_images, _t['im_detect'].average_time, _t['misc'].average_time)) #print_timings(_t_drl_rpn) # uncomment for some timing details! det_file = os.path.join(output_dir, 'detections.pkl') with open(det_file, 'wb') as f: pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL) print('Evaluating detections') print('Disabled for now') # imdb.evaluate_detections(all_boxes, output_dir, start_idx, end_idx)
return roidb roidbs = [get_roidb(s) for s in imdb_names.split('+')] roidb = roidbs[0] if len(roidbs) > 1: for r in roidbs[1:]: roidb.extend(r) tmp = get_imdb(imdb_names.split('+')[1]) imdb = datasets.imdb.imdb(imdb_names, tmp.classes) else: imdb = get_imdb(imdb_names) return imdb, roidb if __name__ == "__main__": print "it's here" imdb = get_imdb('voc_2007_trainval') print('Loaded dataset `{:s}` for training'.format(imdb.name)) # imdb = get_imdb('mio_tcd_loc_train') # print('Loaded dataset `{:s}` for training'.format(imdb.name)) cfg.TRAIN.PROPOSAL_METHOD = 'gt' # TODO: may need to look more closely at roi_data_layer.roidb imdb, roidb = combined_roidb('deep_fashion_general_train') print('{:d} roidb entries'.format(len(roidb))) # output directory where the models are saved output_dir = get_output_dir(imdb, None) print('Output will be saved to `{:s}`'.format(output_dir))
def dpp_test_net(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.2, vis=False): """Test a Fast R-CNN network on an image database.""" num_images = len(imdb.image_index) thresh = cfg.TEST.SCORE_THRESH print("===> SCORE_THRESHOLD is: ", thresh) # 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)] output_dir = get_output_dir(imdb, weights_filename) # timers _t = {'im_detect': Timer(), 'misc': Timer()} if not cfg.TEST.HAS_RPN: roidb = imdb.roidb im_dets_pair = {} sim_classes = pickle.load(open(cfg.TRAIN.similarity_path, "r")) ff = {} for j in xrange(1, imdb.num_classes): cls_file = os.path.join(output_dir, '%s.txt' % imdb.classes[j]) ff[j] = open(cls_file, 'a') for i in xrange(num_images): # filter out any ground truth boxes if cfg.TEST.HAS_RPN: box_proposals = None else: # The roidb may contain ground-truth rois (for example, if the roidb # comes from the training or val split). We only want to evaluate # detection on the *non*-ground-truth rois. We select those the rois # that have the gt_classes field set to 0, which means there's no # ground truth. box_proposals = roidb[i]['boxes'][roidb[i]['gt_classes'] == 0] im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(sess, net, im) _t['im_detect'].toc() _t['misc'].tic() # skip j = 0, because it's the background class im_dets_pair[i] = {} im_dets_pair[i]['im'] = im im_dets_pair[i]['lbl'] = imdb.classes score_thresh = thresh epsilon = 0.01 DPP_ = DPP(epsilon=0.02) keep = DPP_.dpp_MAP(im_dets_pair[i], scores, boxes, sim_classes, score_thresh, epsilon, max_per_image, close_thr=0.00001) if len(keep['box_id']) > 0: for j in xrange(1, imdb.num_classes): inds = np.where(keep['box_cls'] == j)[0] box_ids = keep['box_id'][inds] for per_cls in box_ids: ff[j].write( "%s %g %d %d %d %d\n" % (imdb.image_path_at(i), scores[per_cls, j], boxes[per_cls, 4 * j], boxes[per_cls, 4 * j + 1], boxes[per_cls, 4 * j + 2], boxes[per_cls, 4 * j + 3])) cls_dets = np.hstack((boxes[box_ids, 4 * j:(j + 1) * 4], scores[box_ids, j][:, np.newaxis])) \ .astype(np.float32, copy=False) all_boxes[j][i] = cls_dets im_dets_pair[i][j] = {} im_dets_pair[i][j]['dets'] = cls_dets # if vis: # vis_detections(im, imdb.classes[j], cls_dets, score_thresh) else: for j in xrange(1, imdb.num_classes): all_boxes[j][i] = np.array([]) _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)) for j in xrange(1, imdb.num_classes): ff[j].close() det_file = os.path.join(output_dir, 'detections_dpp.pkl') with open(det_file, 'wb') as f: cPickle.dump(all_boxes, f, cPickle.HIGHEST_PROTOCOL) print('Evaluating detections') imdb.evaluate_detections(all_boxes, output_dir)
# train set imdb, roidb = combined_roidb(args.imdb_name) # Set class names in config file based on IMDB class_names = imdb.classes cfg_from_list(['CLASS_NAMES', [class_names]]) if args.alpha: cfg_from_list(['LRP_HAI.ALPHA', True]) # Update config to match start of training detector cfg_from_list(['LRP_HAI_TRAIN.DET_START', args.det_start]) # output directory where the models are saved output_dir = get_output_dir(imdb, args.tag, args.save_path) logger = setup_logger("LRP-HAI", save_dir=args.save_path, filename="log_train.txt") logger.info('Called with args:') logger.info(args) logger.info('Using attention alpha:') logger.info(cfg.LRP_HAI.ALPHA) logger.info('Using config:\n{}'.format(pprint.pformat(cfg))) logger.info('{:d} roidb entries'.format(len(roidb))) logger.info('Output will be saved to `{:s}`'.format(output_dir)) # also add the validation set, but with no flipping images orgflip = cfg.TRAIN.USE_FLIPPED cfg.TRAIN.USE_FLIPPED = False
def test_net_train(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.): """Test an FROG network on an image database, and generate pseudo ground truths for training faster rcnn.""" np.random.seed(cfg.RNG_SEED) 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 range(num_images)] for _ in range(imdb.num_classes)] output_dir = get_output_dir(imdb, weights_filename) # timers _t = {'im_detect': Timer(), 'misc': Timer()} images_real = np.zeros((num_images, ), dtype=object) gt = np.zeros((num_images, ), dtype=object) roidb = imdb.roidb scores_all = [] boxes_all = [] for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(sess, net, im) _t['im_detect'].toc() scores_all.append(scores) boxes_all.append(boxes) _t['misc'].tic() # skip j = 0, because it's the background class for j in range(1, imdb.num_classes): 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, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] all_boxes[j][i] = cls_dets # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack( [all_boxes[j][i][:, -1] for j in range(1, imdb.num_classes)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in range(1, imdb.num_classes): keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] gt_tmp = { 'aeroplane': np.empty((0, 4), dtype=np.float32), 'bicycle': np.empty((0, 4), dtype=np.float32), 'bird': np.empty((0, 4), dtype=np.float32), 'boat': np.empty((0, 4), dtype=np.float32), 'bottle': np.empty((0, 4), dtype=np.float32), 'bus': np.empty((0, 4), dtype=np.float32), 'car': np.empty((0, 4), dtype=np.float32), 'cat': np.empty((0, 4), dtype=np.float32), 'chair': np.empty((0, 4), dtype=np.float32), 'cow': np.empty((0, 4), dtype=np.float32), 'diningtable': np.empty((0, 4), dtype=np.float32), 'dog': np.empty((0, 4), dtype=np.float32), 'horse': np.empty((0, 4), dtype=np.float32), 'motorbike': np.empty((0, 4), dtype=np.float32), 'person': np.empty((0, 4), dtype=np.float32), 'pottedplant': np.empty((0, 4), dtype=np.float32), 'sheep': np.empty((0, 4), dtype=np.float32), 'sofa': np.empty((0, 4), dtype=np.float32), 'train': np.empty((0, 4), dtype=np.float32), 'tvmonitor': np.empty((0, 4), dtype=np.float32) } # print(222, roidb[i].keys()) tmp_idx = np.where(roidb[i]['label'][0][:imdb.num_classes])[0] for j in xrange(len(tmp_idx)): idx_real = np.argmax(scores[:, tmp_idx[j]]) gt_tmp[imdb.classes[tmp_idx[j]]] = np.array([ boxes[idx_real, tmp_idx[j] * 4 + 1], boxes[idx_real, tmp_idx[j] * 4], boxes[idx_real, tmp_idx[j] * 4 + 3], boxes[idx_real, tmp_idx[j] * 4 + 2] ], dtype=np.float32) gt_tmp[imdb.classes[tmp_idx[j]]] += 1 gt[i] = {'gt': gt_tmp} images_real[i] = imdb.image_index[i] _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)) # save gt model_save_gt = {'images': images_real, 'gt': gt} sio.savemat('{}_pseudo_gt.mat'.format(imdb.name), model_save_gt) det_file = os.path.join(output_dir, 'detections.pkl') with open(det_file, 'wb') as f: pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL) print('Evaluating detections') imdb.evaluate_detections(all_boxes, output_dir)
def cadc_eval(detpath, db, frameset, classname, cachedir, mode, ovthresh=0.5, eval_type='2d', d_levels=0): #Min overlap is 0.7 for cars, 0.5 for ped/bike """rec, prec, ap = cadc_eval(detpath, annopath, framesetfile, classname, [ovthresh]) Top level function that does the PASCAL VOC evaluation. detpath: Path to detections detpath.format(classname) should produce the detection results file. annopath: Path to annotations annopath.format(framename) should be the xml annotations file. framesetfile: Text file containing the list of frames, one frame per line. classname: Category name (duh) cachedir: Directory for caching the annotations [ovthresh]: Overlap threshold (default = 0.5) [use_07_metric]: Whether to use VOC07's 11 point AP computation (default False) """ #Misc hardcoded variables idx = 0 ovthresh_dc = 0.5 # assumes detections are in detpath.format(classname) # assumes annotations are in annopath.format(framename) # assumes framesetfile is a text file with each line an frame name # cachedir caches the annotations in a pickle file frame_path = get_frame_path(db, mode, eval_type) class_recs = load_recs(frameset, frame_path, db, mode, classname) # read dets detfile = detpath.format(classname) print('Opening det file: ' + detfile) #sys.exit('donezo') with open(detfile, 'r') as f: lines = f.readlines() #Extract detection file into array splitlines = [x.strip().split(' ') for x in lines] #Many entries have the same idx & token frame_idx = [x[0] for x in splitlines ] #TODO: I dont like how this is along many frames frame_tokens = [x[1] for x in splitlines] confidence = np.array([float(x[2]) for x in splitlines]) #All detections for specific class bbox_elem = cfg[cfg.NET_TYPE.upper()].NUM_BBOX_ELEM BB = np.array([[float(z) for z in x[3:3 + bbox_elem]] for x in splitlines]) #det_cnt = np.zeros((cfg.CADC.MAX_FRAME)) _, uncertainties = eval_utils.extract_uncertainties(bbox_elem, splitlines) #Repeated for X detections along every frame presented idx = len(frame_idx) #DEPRECATED ---- 3 types, easy medium hard tp = np.zeros((BB.shape[0], d_levels)) fp = np.zeros((BB.shape[0], d_levels)) fn = np.zeros((BB.shape[0])) #tp_frame = np.zeros(cfg.CADC.MAX_FRAME) #fp_frame = np.zeros(cfg.CADC.MAX_FRAME) #npos_frame = np.zeros(cfg.CADC.MAX_FRAME) npos = np.zeros((len(class_recs), d_levels)) #Count number of total labels in all frames count_npos(class_recs, npos) det_results = [] frame_uncertainties = [] #Check if there are any dets at all if BB.shape[0] > 0: # sort by confidence (highest first) sorted_ind = np.argsort(-confidence) sorted_scores = np.sort(-confidence) idx_sorted = [int(frame_idx[x]) for x in sorted_ind] frame_tokens_sorted = [frame_tokens[x] for x in sorted_ind] #print(frame_ids) # go down dets and mark true positives and false positives #Zip together sorted_ind with frame tokens sorted. #sorted_ind -> Needed to know which detection we are selecting next #frame_tokens_sorted -> Needed to know which set of GT's are for the same frame as the det idx = 0 print('num dets {}'.format(len(sorted_ind))) for det_idx, token in zip(sorted_ind, frame_tokens_sorted): det_confidence = confidence[det_idx] #R is a subset of detections for a specific class #print('doing det for frame {}'.format(frame_idx[d])) #Need to find associated GT frame ID alongside its detection id 'd' #Only one such frame, why appending? R = None skip_iter = True R = eval_utils.find_rec(class_recs, token) if (R is None): continue #Deprecated #R = class_recs[frame_ids[d]] bb = BB[det_idx, :].astype(float) var = {} #Variance extraction, collect on a per scene basis for key, val in uncertainties.items(): #uc_avg[key][int(R['idx'])] += val[det_idx, :] var[key] = val[det_idx, :] #det_cnt[int(R['idx'])] += 1 #Variance extraction, collect on a per scene basis ovmax = -np.inf #Multiple possible bounding boxes, perhaps for multi car detection BBGT = R['boxes'].astype(float) BBGT_dc = R['boxes_dc'].astype(float) #Preload all GT boxes and count number of true positive GT's #Not sure why we're setting ignore to false here if it were true #for i, BBGT_elem in enumerate(BBGT): # BBGT_height = BBGT_elem[3] - BBGT_elem[1] ovmax_dc = 0 if BBGT_dc.size > 0 and cfg.TEST.IGNORE_DC: overlaps_dc = eval_utils.iou(BBGT_dc, bb, eval_type) ovmax_dc = np.max(overlaps_dc) #Compute IoU if BBGT.size > 0: overlaps = eval_utils.iou(BBGT, bb, eval_type) ovmax = np.max(overlaps) #Index of max overlap between a BBGT and BB jmax = np.argmax(overlaps) else: jmax = 0 # Minimum IoU Threshold for a true positive if ovmax > ovthresh and ovmax_dc < ovthresh_dc: #if ovmax > ovthresh: #ignore if not contained within easy, medium, hard if not R['ignore'][jmax]: if not R['hit'][jmax]: if (R['difficulty'][jmax] <= 2 and tp.shape[1] >= 3): tp[idx, 2] += 1 if (R['difficulty'][jmax] <= 1 and tp.shape[1] >= 2): tp[idx, 1] += 1 if (R['difficulty'][jmax] <= 0): tp[idx, 0] += 1 #tp_frame[int(R['idx'])] += 1 R['hit'][jmax] = True det_results.append( write_det(R, det_confidence, ovmax, bb, var, jmax, det_fp=False)) else: #If it already exists, cant double classify on same spot. if (R['difficulty'][jmax] <= 2 and fp.shape[1] >= 3): fp[idx, 2] += 1 if (R['difficulty'][jmax] <= 1 and fp.shape[1] >= 2): fp[idx, 1] += 1 if (R['difficulty'][jmax] <= 0): fp[idx, 0] += 1 #fp_frame[int(R['idx'])] += 1 det_results.append( write_det(R, det_confidence, ovmax, bb, var, det_fp=True)) #If your IoU is less than required, its simply a false positive. elif (BBGT.size > 0 and ovmax_dc < ovthresh_dc): #elif(BBGT.size > 0) fp[idx, 0] += 1 if (fp.shape[1] >= 2): fp[idx, 1] += 1 if (fp.shape[1] >= 3): fp[idx, 2] += 1 #fp_frame[int(R['idx'])] += 1 det_results.append( write_det(R, det_confidence, ovmax, bb, var, det_fp=True)) idx = idx + 1 else: print('cadc eval, no GT boxes detected') #for i in np.arange(cfg.cadc.MAX_FRAME): # frame_dets = np.sum(det_cnt[i]) # frame_uc = eval_utils.write_frame_uncertainty(uc_avg,frame_dets,i) # if(frame_uc != '' and cfg.DEBUG.PRINT_SCENE_RESULT): # print(frame_uc) # frame_uncertainties.append(frame_uc) #if(cfg.DEBUG.TEST_FRAME_PRINT): # eval_utils.display_frame_counts(tp_frame,fp_frame,npos_frame) out_dir = get_output_dir(db, mode='test') out_file = '{}_detection_results.txt'.format(classname) eval_utils.save_detection_results(det_results, out_dir, out_file) #if(len(frame_uncertainties) != 0): # uc_out_file = '{}_frame_uncertainty_results.txt'.format(classname) # eval_utils.save_detection_results(frame_uncertainties, out_dir, uc_out_file) map = mrec = mprec = np.zeros((d_levels, )) prec = 0 rec = 0 fp_sum = np.cumsum(fp, axis=0) tp_sum = np.cumsum(tp, axis=0) #fn = 1-fp #fn_sum = np.cumsum(fn, axis=0) npos_sum = np.sum(npos, axis=0) #print('Difficulty Level: {:d}, fp sum: {:f}, tp sum: {:f} npos: {:d}'.format(i, fp_sum[i], tp_sum[i], npos[i])) #recall #Per frame per class AP for i in range(0, d_levels): npos_sum_d = npos_sum[i] #Override to avoid NaN if (npos_sum_d == 0): npos_sum_d = np.sum([1]) rec = tp_sum[:, i] / npos_sum_d.astype(float) prec = tp_sum[:, i] / np.maximum(tp_sum[:, i] + fp_sum[:, i], np.finfo(np.float64).eps) # avoid divide by zero in case the first detection matches a difficult # ground truth precision rec, prec = zip(*sorted(zip(rec, prec))) mprec[i] = np.average(prec) mrec[i] = np.average(rec) map[i] = eval_utils.ap(rec, prec) return mrec, mprec, map
if args.cfg_file is not None: cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) print('Using config:') pprint.pprint(cfg) np.random.seed(cfg.RNG_SEED) # train set imdb, roidb = combined_roidb(args.imdb_name) print('{:d} roidb entries'.format(len(roidb))) # output directory where the models are saved output_dir = get_output_dir(imdb, store_name) print('Output will be saved to `{:s}`'.format(output_dir)) # tensorboard directory where the summaries are saved during training tb_dir = get_output_tb_dir(imdb, args.tag) print('TensorFlow summaries will be saved to `{:s}`'.format(tb_dir)) # also add the validation set, but with no flipping images orgflip = cfg.TRAIN.USE_FLIPPED cfg.TRAIN.USE_FLIPPED = False _, valroidb = combined_roidb(args.imdbval_name) print('{:d} validation roidb entries'.format(len(valroidb))) cfg.TRAIN.USE_FLIPPED = orgflip # load network if args.net == 'vgg16':
def test_net_with_sample(sess, net, imdb, weights_filename, sample_images, max_per_image=100, thresh=0.05, sample_names_dict=None, use_saved_detections=False): np.random.seed(cfg.RNG_SEED) """Test a Fast R-CNN network on an image database.""" num_images = len(sample_images) # 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 range(num_images)] for _ in range(imdb.num_classes)] output_dir = get_output_dir(imdb, weights_filename) det_file = os.path.join(output_dir, 'detections.pkl') # timers _t = {'im_detect': Timer(), 'misc': Timer()} if use_saved_detections: with open(det_file, 'rb') as f: all_boxes = pickle.load(f) else: for i in range(num_images): im = cv2.imread(imdb.image_path_from_index(sample_images[i])) _t['im_detect'].tic() scores, boxes = im_detect(sess, net, im) _t['im_detect'].toc() _t['misc'].tic() # skip j = 0, because it's the background class for j in range(1, imdb.num_classes): 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, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] all_boxes[j][i] = cls_dets # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack([ all_boxes[j][i][:, -1] for j in range(1, imdb.num_classes) ]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in range(1, imdb.num_classes): keep = np.where( all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] _t['misc'].toc() if i % 100 == 0: print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(i + 1, num_images, _t['im_detect'].diff, _t['misc'].diff)) with open(det_file, 'wb') as f: pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL) print('Evaluating detections') ret_dict = {} if sample_names_dict: for num_imgs, sample_images in sample_names_dict.items(): boxes_subset = [l[:num_imgs] for l in all_boxes] ret_dict[num_imgs] = imdb.evaluate_detections( boxes_subset, output_dir, sample_images) else: ret_dict[num_imgs] = imdb.evaluate_detections(all_boxes, output_dir, sample_images) return ret_dict
def test_net(net, imdb, roidb, weights_filename, max_per_image=100, thresh=0.): np.random.seed(cfg.RNG_SEED) """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 range(num_images)] for _ in range(imdb.num_classes)] output_dir = get_output_dir(imdb, weights_filename) # ------------------------------------------------------- ap_meter = AveragePrecisionMeter(difficult_examples=True) ap_meter.reset() # timers _t = {'im_detect': Timer(), 'misc': Timer()} for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes, det_cls_prob, target = im_detect(net, im, roidb[i]) _t['im_detect'].toc() _t['misc'].tic() output = np.reshape(det_cls_prob[:], (1, -1)) target = np.reshape(target[:], (1, -1)) ap_meter.add(output, target) # skip j = 0, because it's the background class for j in range(0, imdb.num_classes): 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(torch.from_numpy(cls_dets), cfg.TEST.NMS).numpy() if cls_dets.size > 0 else [] # cls_dets = cls_dets[keep, :] # all_boxes[j][i] = cls_dets keep = nms(torch.from_numpy(cls_dets), cfg.TEST.NMS) if cls_dets.size > 0 else [] all_boxes[j][i] = keep[0].numpy() # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack([all_boxes[j][i][:, -1] for j in range(1, imdb.num_classes)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in range(1, imdb.num_classes): keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] _t['misc'].toc() if i % 100 == 0: print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s'.format(i + 1, num_images, _t['im_detect'].average_time(), _t['misc'].average_time())) ap = ap_meter.value().numpy() print('the classification AP is ') for index, cls in enumerate(imdb._classes): if cls == '__background__': continue print(('AP for {} = {:.4f}'.format(cls, ap[index]))) print('__________________') map = 100 * ap.mean() print('the mAP is {:.4f}'.format(map)) det_file = os.path.join(output_dir, 'detections.pkl') with open(det_file, 'wb') as f: pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL) print('Evaluating detections') imdb.evaluate_detections(all_boxes, output_dir)
def test_net(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.05): np.random.seed(cfg.RNG_SEED) """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 range(num_images)] for _ in range(imdb.num_classes)] output_dir = get_output_dir(imdb, weights_filename) # timers _t = {'im_detect' : Timer(), 'misc' : Timer()} for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, polys = im_detect(sess, net, im) _t['im_detect'].toc() _t['misc'].tic() boxes = np.zeros((polys.shape[0], 8), dtype=polys.dtype) boxes[:, 0] = np.min(polys[:, 0:8:2], axis=1) boxes[:, 1] = np.min(polys[:, 1:8:2], axis=1) boxes[:, 2] = np.max(polys[:, 0:8:2], axis=1) boxes[:, 3] = np.max(polys[:, 1:8:2], axis=1) boxes[:, 4] = np.min(polys[:, 8::2], axis=1) boxes[:, 5] = np.min(polys[:, 9::2], axis=1) boxes[:, 6] = np.max(polys[:, 8::2], axis=1) boxes[:, 7] = np.max(polys[:, 9::2], axis=1) # skip j = 0, because it's the background class # print(boxes.shape) # print(polys.shape) for j in range(1, imdb.num_classes): inds = np.where(scores[:, j] > thresh)[0] cls_scores = scores[inds, j] cls_boxes = boxes[inds, j*4:(j+1)*4] cls_polys = polys[inds, j*8:(j+1)*8] cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) cls_dets_poly = cls_polys.astype(np.float32, copy=False) keep = nms(cls_dets, cfg.TEST.NMS) # cls_dets = cls_dets[keep, :] cls_dets = cls_boxes[keep, :] cls_dets_poly = cls_dets_poly[keep, :] cls_scores = cls_scores[:, np.newaxis] cls_scores = cls_scores[keep, :] cls_dets = np.hstack((cls_dets, cls_dets_poly, cls_scores)) # print(cls_dets) all_boxes[j][i] = cls_dets # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack([all_boxes[j][i][:, -1] for j in range(1, imdb.num_classes)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in range(1, imdb.num_classes): keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] _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)) det_file = os.path.join(output_dir, 'detections.pkl') with open(det_file, 'wb') as f: pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL) print('Evaluating detections') imdb.evaluate_detections(all_boxes, output_dir, det_file)
def test_net_MC(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.05, vis=False): """Test a Fast R-CNN network on an image database.""" num_images = len(imdb.image_index) thresh = cfg.TEST.SCORE_THRESH # all detections are collected into: # all_boxes[cls][image] = N x 5 array of detections in # (x1, y1, x2, y2, score) print("score threshold:", thresh) all_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(imdb.num_classes)] output_dir = get_output_dir(imdb, weights_filename) # timers _t = {'im_detect': Timer(), 'misc': Timer()} if not cfg.TEST.HAS_RPN: roidb = imdb.roidb for i in xrange(num_images): # filter out any ground truth boxes if cfg.TEST.HAS_RPN: box_proposals = None else: # The roidb may contain ground-truth rois (for example, if the roidb # comes from the training or val split). We only want to evaluate # detection on the *non*-ground-truth rois. We select those the rois # that have the gt_classes field set to 0, which means there's no # ground truth. box_proposals = roidb[i]['boxes'][roidb[i]['gt_classes'] == 0] im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(sess, net, im) _t['im_detect'].toc() _t['misc'].tic() # skip j = 0, because it's the background class cls_dets_all = np.array(()) box_inds = [] for j in xrange(1, imdb.num_classes): 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, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] box_inds.extend(inds[keep]) cls_lbl = j * np.ones((cls_dets.shape[0], 1)) cls_dets = np.hstack((cls_dets, cls_lbl)) if j == 1: cls_dets_all = cls_dets else: cls_dets_all = np.vstack((cls_dets_all, cls_dets)) \ .astype(np.float32, copy=False) box_inds = np.array(box_inds) keep_MC = nms(cls_dets_all[:, :-1], cfg.TEST.MC_NMS) cls_dets_all = cls_dets_all[keep_MC, :] for j in xrange(1, imdb.num_classes): keeps_j = np.where(cls_dets_all[:, -1] == j)[0] cls_dets_class_j = cls_dets_all[keeps_j, :-1] all_boxes[j][i] = cls_dets_class_j # if vis: # vis_detections(im, imdb.classes[j], cls_dets_class_j, cfg.TEST.SCORE_THRESH) # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack( [all_boxes[j][i][:, -1] for j in xrange(1, imdb.num_classes)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in xrange(1, imdb.num_classes): keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] _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)) det_file = os.path.join(output_dir, 'detections.pkl') with open(det_file, 'wb') as f: cPickle.dump(all_boxes, f, cPickle.HIGHEST_PROTOCOL) print('Evaluating detections') imdb.evaluate_detections(all_boxes, output_dir)
cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) print('Using config:') pprint.pprint(cfg) np.random.seed(cfg.RNG_SEED) # train set print(args.imdb_name) imdb, roidb = combined_roidb(args.imdb_name) print('{:d} roidb entries'.format(len(roidb))) # output directory where the models are saved output_dir = get_output_dir(imdb, args.tag) print('Output will be saved to `{:s}`'.format(output_dir)) # tensorboard directory where the summaries are saved during training tb_dir = get_output_tb_dir(imdb, args.tag) print('TensorFlow summaries will be saved to `{:s}`'.format(tb_dir)) # also add the validation set, but with no flipping images orgflip = cfg.TRAIN.USE_FLIPPED cfg.TRAIN.USE_FLIPPED = False _, valroidb = combined_roidb(args.imdbval_name) print('{:d} validation roidb entries'.format(len(valroidb))) cfg.TRAIN.USE_FLIPPED = orgflip # load network if args.net == 'vgg16':
def test_net(sess, net, imdb, weights_filename, max_per_image=100, thresh=0): np.random.seed(cfg.RNG_SEED) """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 range(num_images)] for _ in range(imdb.num_classes)] all_f1 = np.zeros((imdb.num_images, imdb.num_classes), np.float) all_auc = np.zeros((imdb.num_images, imdb.num_classes), np.float) all_auc_new = np.zeros((imdb.num_images, imdb.num_classes), np.float) counters = [] output_dir = get_output_dir(imdb, weights_filename) if os.path.isfile(os.path.join(output_dir, 'detections.pkl')): all_boxes = pickle.load(open(os.path.join(output_dir, 'detections.pkl'), 'r')) else: # timers if cfg.USE_MASK is True: _t = {'im_detect': Timer(), 'mask': Timer()} for i in range(num_images): print(imdb.image_path_at(i)) im = cv2.imread(imdb.image_path_at(i)) mask_gt = cv2.imread(imdb.mask_path_at(i)) mask_gt = cv2.cvtColor(mask_gt, cv2.COLOR_BGR2GRAY) ret, mask_gt = cv2.threshold(mask_gt, 127, 255, cv2.THRESH_BINARY) mask_gt = (mask_gt / 255.0).astype(np.float32) _t['im_detect'].tic() scores, boxes, feat, s, maskcls_inds, mask_boxes, mask_scores, mask_pred, _ ,_= im_detect(sess, net, im) _t['im_detect'].toc() _t['mask'].tic() # skip j = 0, because it's the background class # for j in range(1, imdb.num_classes): # 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, cfg.TEST.NMS) # cls_dets = cls_dets[keep, :] # all_boxes[j][i] = cls_dets batch_ind = np.where(mask_scores > 0.)[0] cls=maskcls_inds[np.argmax(mask_scores)].astype(int) mask_boxes=mask_boxes.astype(int) if batch_ind.shape[0] == 0: f1 = 1e-10 auc_score = 1e-10 else: mask_out = np.zeros(im.shape[:2],dtype=np.float) for ind in batch_ind: height = mask_boxes[ind, 3] - mask_boxes[ind, 1] width = mask_boxes[ind, 2] - mask_boxes[ind, 0] if width <= 0 or height <= 0: continue else: mask_box_pre = cv2.resize(mask_pred[ind, :, :, :], (width, height)) mask_pre = np.zeros(im.shape[:2],dtype=np.float) bbox1 = mask_boxes[ind, :] mask_pre[bbox1[1]:bbox1[3], bbox1[0]:bbox1[2]] = mask_box_pre mask_out = np.where(mask_out >= mask_pre, mask_out, mask_pre) precision, recall,auc_score = cal_precision_recall_mae(mask_out, mask_gt) f1 = cal_fmeasure(precision, recall) f1=np.max(np.array(f1)) print('F1 score per image:',f1) print('AUV score per image:', auc_score) all_f1[i, cls] = f1 all_auc[i, cls] = auc_score _t['mask'].toc() print('Im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(i + 1, num_images, _t['im_detect'].average_time, _t['mask'].average_time)) class_f1 = np.zeros(imdb.num_classes) class_auc = np.zeros(imdb.num_classes) for j in range(1, imdb.num_classes): cls_f1 = all_f1[:, j] f1_ind = np.where(cls_f1 > 0.)[0] cls_f1 = cls_f1[f1_ind] class_f1[j] = np.average(cls_f1) cls_auc = all_auc[:, j] auc_ind = np.where(cls_auc > 0)[0] cls_auc = cls_auc[auc_ind] class_auc[j] = np.average(cls_auc) # det_file = os.path.join(output_dir, 'detections_{:f}.pkl'.format(10)) # with open(det_file, 'wb') as f: # pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL) else: _t = {'im_detect': Timer(), 'compute': Timer()} for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes, _, _ = im_detect(sess, net, im) _t['im_detect'].toc() _t['compute'].tic() # skip j = 0, because it's the background class for j in range(1, imdb.num_classes): 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, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] all_boxes[j][i] = cls_dets # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack([all_boxes[j][i][:, -1] for j in range(1, imdb.num_classes)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in range(1, imdb.num_classes): keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] _t['compute'].toc() print('Im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(i + 1, num_images, _t['im_detect'].average_time, _t['compute'].average_time)) det_file = os.path.join(output_dir, 'detections_{:f}.pkl'.format(10)) with open(det_file, 'wb') as f: pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL) imdb.evaluate_detections(all_boxes, output_dir) if cfg.USE_MASK is True: print('~~~~~~~~~~~~~~~~~~~~~~~~~~') print('Test Results:') print('Average F1 Score: %.3f' %np.average(class_f1[1:])) print('Average AUC Score: %.3f' %np.average(class_auc[1:])) print('~~~~~~~~~~~~~~~~~~~~~~~~~~') print('\n') print('============================================================') print('Constrained R-CNN') print('A general image manipulation detection model.') print('Licensed under The MIT License [see LICENSE for details]') print('Written by Huizhou Li') print('============================================================')
if args.cfg_file is not None: cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) print('Using config:') pprint.pprint(cfg) np.random.seed(cfg.RNG_SEED) # train set imdb, roidb = combined_roidb(args.imdb_name) print('{:d} roidb entries'.format(len(roidb))) # output directory where the models are saved output_dir = get_output_dir(imdb, args.tag) print('Output will be saved to `{:s}`'.format(output_dir)) # tensorboard directory where the summaries are saved during training tb_dir = get_output_tb_dir(imdb, args.tag) print('TensorFlow summaries will be saved to `{:s}`'.format(tb_dir)) # also add the validation set, but with no flipping images orgflip = cfg.TRAIN.USE_FLIPPED cfg.TRAIN.USE_FLIPPED = False _, valroidb = combined_roidb(args.imdbval_name) print('{:d} validation roidb entries'.format(len(valroidb))) cfg.TRAIN.USE_FLIPPED = orgflip # load network if args.net == 'vgg16':
print('Called with args:') print(args) if args.cfg_file is not None: cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) print('Using config:') pprint.pprint(cfg) np.random.seed(cfg.RNG_SEED) # Choose net if args.net == 'adgm': net = Adgm if args.net == 'vae': net = Vae # output directory where the models are saved output_dir = get_output_dir(args.dataset, args.tag) print('Output will be saved to `{:s}`'.format(output_dir)) # tensorboard directory where the summaries are saved during training tb_dir = get_output_tb_dir(args.dataset, args.tag) print('TensorFlow summaries will be saved to `{:s}`'.format(tb_dir)) train_net(net, Mnist, output_dir, tb_dir, max_iters=args.max_iters)
def test_idn(sess, net, imdb, weights_filename, max_per_image=100, thresh=-4.0, sim_thresh=0.7): np.random.seed(cfg.RNG_SEED) """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 range(num_images)] for _ in range(imdb.num_classes)] output_dir = get_output_dir(imdb, weights_filename) # timers _t = {'im_detect': Timer(), 'misc': Timer()} for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() blobs_out = im_detect_idn(sess, net, im) _t['im_detect'].toc() if blobs_out['num_patch'][0] > 0: im_info = blobs_out['im_info'] pred_boxes = blobs_out['input_boxes'][:, 1:] / im_info[2] pred_clss = blobs_out['input_clss'].astype(np.int32) pred_scores = blobs_out['dpp_quality'] input_index = range(len(pred_boxes)) ara = (pred_boxes[:, 3] - pred_boxes[:, 1]) * (pred_boxes[:, 2] - pred_boxes[:, 0]) # Remove too small patches if len(np.where(ara <= 100)[0]) > 0: [input_index.remove(jj) for jj in np.where(ara <= 100)[0]] pred_boxes = pred_boxes[input_index] pred_scores = pred_scores[input_index] pred_clss = pred_clss[input_index] pIOU = blobs_out['pIOU'] pIOU[np.where(pIOU < 0.4)] = 0 idn_sim_feat = blobs_out['idn_sim_feat'] idn_sim_feat = preprocessing.normalize(idn_sim_feat, norm='l2') _t['misc'].tic() # skip j = 0, because it's the background class for j in range(1, imdb.num_classes): inds = np.where(np.squeeze(pred_clss) == j)[0] cls_scores = pred_scores[inds] cls_boxes = pred_boxes[inds] cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) S_sim = np.matmul(idn_sim_feat[inds], idn_sim_feat[inds].T) keep = [] if len(S_sim) > 0: keep = dpp_infer(S_sim, pred_scores[inds], pIOU[inds, :][:, inds], sim_thresh, thresh) cls_dets = cls_dets[keep, :] all_boxes[j][i] = cls_dets # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack([ all_boxes[j][i][:, -1] for j in range(1, imdb.num_classes) ]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in range(1, imdb.num_classes): keep = np.where( all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] _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)) det_file = os.path.join(output_dir, 'detections.pkl') with open(det_file, 'wb') as f: pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL) print('Evaluating detections') imdb.evaluate_detections(all_boxes, output_dir)
limiter=0) elif (cfg.NET_TYPE == 'lidar'): db, roidb = combined_lidb_roidb('train', args.db_name, cfg.TRAIN.DRAW_ROIDB_GEN, None, limiter=0) _, val_roidb = combined_lidb_roidb('val', args.db_name, cfg.TRAIN.DRAW_ROIDB_GEN, db, limiter=0) print('{:d} roidb entries'.format(len(roidb))) print('{:d} val roidb entries'.format(len(val_roidb))) # output directory where the models are saved output_dir = get_output_dir(db, weights_filename=args.tag) print('Output will be saved to `{:s}`'.format(output_dir)) # tensorboard directory where the summaries are saved during training tb_dir = get_output_tb_dir(db, weights_filename=args.tag) print('TensorFlow summaries will be saved to `{:s}`'.format(tb_dir)) # also add the validation set, but with no flipping images #orgflip = cfg.TRAIN.USE_FLIPPED #cfg.TRAIN.USE_FLIPPED = False print('db: {}'.format(args.db_name)) #cfg.TRAIN.USE_FLIPPED = orgflipqua # load network if (cfg.NET_TYPE == 'image'): if args.net == 'vgg16':
def test_net1(sess, net, imdb, weights_filename): np.random.seed(cfg.RNG_SEED) """Test a Fast R-CNN network on an image database.""" num_images = len(imdb.image_index) output_dir = get_output_dir(imdb, weights_filename) # timers _t = {'im_detect': Timer(), 'misc': Timer()} # 加载GT annopath = os.path.join(imdb._devkit_path, 'VOC' + imdb._year, "Annotations", '{:s}.xml') cachedir = os.path.join(imdb._devkit_path, 'annotations_cache') if not os.path.isdir(cachedir): os.mkdir(cachedir) imagesetfile = os.path.join(imdb._devkit_path, 'VOC' + imdb._year, 'ImageSets', 'Main', imdb._image_set + '.txt') cachefile = os.path.join(cachedir, 'test_annots.pkl') # read list of images with open(imagesetfile, 'r') as f: lines = f.readlines() imagenames = [x.strip() for x in lines] if not os.path.isfile(cachefile): # load annotations recs = {} for i, imagename in enumerate(imagenames): print('image——name', imagename) recs[imagename] = parse_rec(annopath.format(imagename)) # if i % 100 == 0: print('Reading annotation for {:d}/{:d}'.format( i + 1, len(imagenames))) # save print('Saving cached annotations to {:s}'.format(cachefile)) with open(cachefile, 'wb') as f: pickle.dump(recs, f) else: # load with open(cachefile, 'rb') as f: try: recs = pickle.load(f) except: recs = pickle.load(f, encoding='bytes') # 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 range(num_images)] for _ in range(imdb.num_classes)] for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(sess, net, im) _t['im_detect'].toc() print(scores[1]) #print(len(boxes)) print('im_detect: {:d}/{:d} {:.3f}s' \ .format(i + 1, num_images, _t['im_detect'].average_time)) # skip j = 0, because it's the background class for j in range(1, imdb.num_classes): cls_scores = scores[:, j] cls_boxes = boxes[:, j * 4:(j + 1) * 4] cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) all_boxes[j][i] = cls_dets # validate over thresholds print('Evaluating detections') _t['misc'].tic() inter_thrs = np.arange(0.7, 0, -0.1) prob_thrs = np.arange(0, 0.7, 0.1) best_f_score = 0 best_tdr = None best_fdr = None best_inter_thr = None best_prob_thr = None # put inference in nms proper format for prob_thr in prob_thrs: probs = [] for i in range(num_images): prob_tmp = [[]] for j in range(1, imdb.num_classes): img_preds = all_boxes[j][i] pick = (img_preds[:, -1] > prob_thr) prob_tmp.append(img_preds[pick]) probs.append(prob_tmp) for inter_thr in inter_thrs: total_pores = 0 total_dets = 0 true_dets = 0 dets = [] for i, imagename in enumerate(imagenames): det_tmp = [[]] for j, cls in enumerate(imdb.classes): if cls == '__background__': continue img_preds = probs[i][j] keep = nms(img_preds, cfg.TEST.NMS) img_dets = img_preds[keep] img_det_ctr = trans_ctr(img_dets[:, :-1]) det_tmp.append(img_det_ctr) total_dets += len(img_det_ctr) R = [obj for obj in recs[imagename] if obj['name'] == cls] bbox = np.array([x['bbox'] for x in R]) bbox_ctr = trans_ctr(bbox) total_pores += len(bbox_ctr) print('AAAAA', len(img_det_ctr), prob_thr) print('BBBBB', len(bbox_ctr), inter_thr) true_dets += len( find_correspondences(img_det_ctr, bbox_ctr)) dets.append(det_tmp) # compute tdr, fdr and f score eps = 1e-5 tdr = true_dets / (total_pores + eps) fdr = (total_dets - true_dets) / (total_dets + eps) f_score = 2 * (tdr * (1 - fdr)) / (tdr + (1 - fdr)) # update best parameters if f_score > best_f_score: best_f_score = f_score best_tdr = tdr best_fdr = fdr best_inter_thr = inter_thr best_prob_thr = prob_thr _t['misc'].toc() print(('best F_SCORE = {:.4f}\n best TDR ={:.4f} \n best FDR = {:.4f}\n ' 'best NMS thr = {:2f}\n best CONF thr = {:2f}'.format( best_f_score, best_tdr, best_fdr, best_inter_thr, best_prob_thr))) print('{:.3f}s'.format(_t['misc'].average_time))
def _test_net(net, imdb, weights_filename, max_per_image=100, thresh=0.): vis = False print("Numeber of classes=:", imdb.num_classes) np.random.seed(cfg.RNG_SEED) """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 range(num_images)] for _ in range(imdb.num_classes)] ## original_all_boxes = [[[] for _ in range(num_images)] for _ in range(imdb.num_classes)] ## output_dir = get_output_dir(imdb, weights_filename) # timers _t = {'im_detect': Timer(), 'misc': Timer()} # extract gt objects for this class class_recs = {} npos = 0 for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) print("img path=:", imdb.image_path_at(i)) img_name = imdb.image_path_at(i).split('/')[-1] folder_name = imdb.image_path_at(i).split('/')[-2] _t['im_detect'].tic() scores, boxes = im_detect(net, im) _t['im_detect'].toc() _t['misc'].tic() #print("############################") #print("score shape, box shape",scores.shape, boxes.shape) result = get_bbox_for_max_score(im, scores, boxes, img_name) basepath = "data/temp/foggytrain2" filepath = os.path.join(basepath, folder_name) if (not os.path.exists(filepath)): os.makedirs(filepath) json_name = img_name.split(".png")[0] + ".json" json_file = os.path.join(filepath, json_name) #print("object type",type(result)) with open(json_file, 'w') as fp: json.dump(str(result), fp) print("file written at path:", json_file) # break # # skip j = 0, because it's the background class # for j in range(1, imdb.num_classes): # 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(torch.from_numpy(cls_dets), cfg.TEST.NMS).cpu().numpy() if cls_dets.size > 0 else [] # # ## # # original_all_boxes[j][i] = cls_dets # # ## # cls_dets = cls_dets[keep, :] # all_boxes[j][i] = cls_dets # ## # obj_scores = net.roi_scores.cpu().data.numpy() # inds = np.where(obj_scores[:] > thresh)[0] # cls_scores = obj_scores[inds] # cls_boxes = boxes[inds, 4:8] # cls_dets = np.hstack((cls_boxes, obj_scores[:])) \ # .astype(np.float32, copy=False) # original_all_boxes[j][i] = cls_dets # # Limit to max_per_image detections *over all classes* # if max_per_image > 0: # image_scores = np.hstack([all_boxes[j][i][:, -1] # for j in range(1, imdb.num_classes)]) # if len(image_scores) > max_per_image: # image_thresh = np.sort(image_scores)[-max_per_image] # for j in range(1, imdb.num_classes): # keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0] # all_boxes[j][i] = all_boxes[j][i][keep, :] # _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())) # det_file = os.path.join(output_dir, 'detections.pkl') # with open(det_file, 'wb') as f: # pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL) # print('Evaluating detections') # imdb.evaluate_detections(all_boxes, output_dir)
def test_net_damage(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.05): count = 0 np.random.seed(cfg.RNG_SEED) """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 range(num_images)] for _ in range(imdb.num_classes)] output_dir = get_output_dir(imdb, weights_filename) # timers _t = {'im_detect': Timer(), 'misc': Timer()} im = None for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) print(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(sess, net, im) _t['im_detect'].toc() _t['misc'].tic() # skip j = 0, because it's the background class for j, cls in enumerate(CLASSES[1:]): j += 1 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, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] all_boxes[j][i] = cls_dets vis_detections(im, cls, cls_dets, thresh=0.9) im_name = imdb.image_path_at(i).split('/')[-1] cv2.imwrite( "/Users/anekisei/Documents/tf-faster-rcnn/tools/save_damage/" + im_name, im) cv2.imshow('result', im) count = count + 1 if cv2.waitKey(1) & 0xff == 27: break # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack( [all_boxes[j][i][:, -1] for j in range(1, imdb.num_classes)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in range(1, imdb.num_classes): keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] _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)) #for cls_ind, cls in enumerate(CLASSES[1:]): #vis_detections(im, class_name, dets, thresh=0.5) det_file = os.path.join(output_dir, 'detections.pkl') with open(det_file, 'wb') as f: pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL) print('Evaluating detections') imdb.evaluate_detections(all_boxes, output_dir)