def process(sess, net, im, only_car=True, vis=False): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image # im_file = os.path.join(cfg.DATA_DIR, 'mcdc/anc105_j03_ldl_u8794_01_8_3', image_name) # im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.5 NMS_THRESH = 0.3 for cls_ind, cls in enumerate(CLASSES[1:]): if only_car: if cls == 'car': cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] if vis: vis_detections(im, cls, dets, thresh=CONF_THRESH) else: print('*' * 30 + 'car' + '*' * 30) #pass dets = detections_thresh(im, cls, dets, thresh=CONF_THRESH) dets = find_middle_car_use_iou(dets) # dets = find_middle_car_use_area(dets) # print(dets) dets = dets.reshape(1, 5) # print(dets) #vis_detections(im, cls, dets, thresh=CONF_THRESH) else: cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] if vis: vis_detections(im, cls, dets, thresh=CONF_THRESH) else: pass dets = detections_thresh(im, cls, dets, thresh=CONF_THRESH) return dets
def run(net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(INPUT_DIR, image_name) print(im_file) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time(), boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.6 NMS_THRESH = 0.3 result = {} cls_all_boxes = np.array([[0,0,0,0]]) cls_all_scores = np.array([]) cls_all_cls = [] ind_nms_result = {} for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(torch.from_numpy(dets), NMS_THRESH) dets = dets[keep.numpy(), :] ind_nms_result[cls] = dets cls_all_boxes = np.vstack((cls_all_boxes,cls_boxes)).astype(np.float32) cls_all_scores = np.append(cls_all_scores,cls_scores) cls_all_cls.extend([cls]*len(cls_scores)) output_ind_nms_result(image_name, im, ind_nms_result,CONF_THRESH) cls_all_boxes = cls_all_boxes[1:] dets = np.hstack((cls_all_boxes, cls_all_scores[:, np.newaxis])).astype(np.float32) keep = nms(torch.from_numpy(dets), NMS_THRESH).numpy() dets = dets[keep, :] cls_all_cls = np.array(cls_all_cls) cls_all_cls = cls_all_cls[keep] for cls_ind, cls in enumerate(CLASSES[1:]): result[cls] = dets[cls_all_cls==cls,:] output_result(image_name, im, result,CONF_THRESH)
def demo3(sess, net, im): scores, boxes = im_detect(sess, net, im) CONF_THRESH = 0.5 NMS_THRESH = 0.3 result = None classes = [] for cls_ind, cls in enumerate(CLASSES[1:]): #print('a') cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) # print(cls) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] boxess = get_box(dets, CONF_THRESH) if boxess is not None: classes.append(cls) if result is None: result = boxess else: result = np.vstack((result, boxess)) return result, classes
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image #im_file = os.path.join(folder, image_name) #im_file = "/data_raid5/weijun/android_data/PNGImages/" + image_name #im_file = "data/VOCdevkit/android_data/PNGImages/" + image_name im = cv2.imread(image_name) im = im[:, :, (2, 1, 0)] #fig, ax = plt.subplots(figsize=(12, 12)) #ax.imshow(im, aspect='equal') # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() #print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] #vis_detections(im, cls, dets, thresh=CONF_THRESH) #vis_detections(fig, ax, cls, dets, thresh=CONF_THRESH) create_clippings(image_name, cls, dets, thresh=CONF_THRESH)
def demo(sess, net, image): """Detect object classes in an image using pre-computed object proposals.""" # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, image) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) fig, ax = plt.subplots(figsize=(12, 12)) # Visualize detections for each class CONF_THRESH = 0.1 NMS_THRESH = 0.3 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(ax, image, cls, dets, thresh=CONF_THRESH) plt.axis('off') plt.tight_layout() plt.draw()
def process_img(net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'face', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds scores, boxes = im_detect(net, im) # Visualize detections for each class CONF_THRESH = 0.7 NMS_THRESH = 0.3 cls_ind = 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] # 300 x 5 (x1,y1,x2,y2,scores) dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) # NMS keep = nms(torch.from_numpy(dets), NMS_THRESH) dets = dets[keep.numpy(), :] crop_imgs = extract_faces(im, dets, thresh=CONF_THRESH) return crop_imgs
def demo(sess, net, color_image, depth_colormap): """Detect object classes in an image using pre-computed object proposals.""" # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, color_image) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.7 NMS_THRESH = 0.3 dets_col = [] cls_col = [] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] dets_col.append(dets) cls_col.append(cls) vis_detections(color_image, depth_colormap, cls_col, dets_col, thresh=CONF_THRESH) depth_col, bbox_col = calc_histogram(depth_image, cls_col, dets_col, thresh=CONF_THRESH) print("box depth:", depth_col[0], "sucker depth:", depth_col[1]) print("box bbox:", bbox_col[0], "sucker bbox", bbox_col[1])
def demo(sess, net, image_name): """Detect pedestrians in an image using pre-computed model.""" # Load the demo image im1_file = os.path.join(cfg.DATA_DIR, 'demo', image_name + '_visible.png') im1 = cv2.imread(im1_file) im2_file = os.path.join(cfg.DATA_DIR, 'demo', image_name + '_lwir.png') im2 = cv2.imread(im2_file) im = [im1, im2] # Detect all object classes and regress object bounds timer = Timer() timer.tic() boxes, scores = im_detect_demo(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.5 NMS_THRESH = 0.3 dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(im, dets, thresh=CONF_THRESH)
def demo(sess, net, image_name, CONF_THRESH): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'demo', 'Images', image_name) im = cv2.imread(im_file, cv2.IMREAD_UNCHANGED) scene_name = image_name[:10] # 'scene_0021' image_index = image_name[11:15] # '0003' # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() NMS_THRESH = 0.3 image_grasps = [] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] add_predicted_grasps(cls, dets, image_grasps, thresh=CONF_THRESH) image_grasps = np.array(image_grasps) np.save( os.path.join('..', 'predicted_rectangle_grasp', scene_name, graspnet_config.CAMERA_NAME, image_index), image_grasps)
def img_test(net,image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im) timer.toc() # Visualize detections for each class CONF_THRESH = 0.9 NMS_THRESH = 0.3 box_out = [] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(torch.from_numpy(dets), NMS_THRESH) dets = dets[keep.numpy(), :] inds = np.where(dets[:, -1] >= CONF_THRESH)[0] dets = dets[inds].astype(int)[:,:4] box_out.append(dets) return box_out
def postproc_dets(self, scores, boxes): thresh = 0.1 max_per_image = 100 # Visualize detections for each class dets_all_cls = [[] for _ in range(self.class_num)] # skip j = 0, because it's the background class for j in range(1, self.class_num): 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, :] dets_all_cls[j] = cls_dets # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack( [dets_all_cls[j][:, -1] for j in range(1, self.class_num)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in range(1, self.class_num): keep = np.where(dets_all_cls[j][:, -1] >= image_thresh)[0] dets_all_cls[j] = dets_all_cls[j][keep, :] return dets_all_cls
def demo(sess, net, im): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image # im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) # im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print(('Detection took {:.3f}s for ' '{:d} object proposals').format(timer.total_time, boxes.shape[0])) frameRate = 1.0 / timer.total_time print("fps: " + str(frameRate)) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(im, cls, dets, thresh=CONF_THRESH) cv2.putText(im, '{:s} {:.2f}'.format("FPS:", frameRate), (1750, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255))
def im_Detect_Highscore(sess, net, image, CONF_THRESH=0.8, NMS_THRESH=0.3): """Detect object classes in an image using pre-computed object proposals.""" im = image # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) print('okokzzqtestgithub') # get a list of all high score classbox # if the classbox's score > CONF_THRESH, than this classbox will add the imageAllClass imageAllClass = [] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] rightDect = vis_detections(cls, dets, thresh=CONF_THRESH) if rightDect is None: pass else: for iti in rightDect: imageAllClass.append(iti) return imageAllClass
def extract(sess, net, image_path): im = cv2.imread(image_path) scores, boxes, feature_maps = im_detect(sess, net, im) CONF_THRESH = 0.8 NMS_THRESH = 0.3 results = [] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background # take only the top predictions per image cls_scores = scores[:, cls_ind] score_thresh = max(np.sort(cls_scores)[::-1][cfg.TEST.top_scores_image], cfg.TEST.min_score_thresh) cls_scores_indices = cls_scores > score_thresh cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = cls_scores[cls_scores_indices] cls_boxes = cls_boxes[cls_scores_indices] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] feature_maps_nms = feature_maps[keep, :] results.append((dets, feature_maps_nms)) # vis_detections(im, cls, dets, thresh=CONF_THRESH) detected_objects = build_det_dict(results) return detected_objects
def predict_image(sess, net, im, CLASSES): # Detect all object classes and regress object bounds scores, boxes = _detect_image(sess, net, im) CONF_THRESH = 0.8 NMS_THRESH = 0.3 result_data = [] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] box, classname, flag, score = _get_thresh_label(cls, dets, thresh=CONF_THRESH) if flag == -1: continue vgg_166 = True if vgg_166: for i in range(len(classname)): result_data.append("{},{:.3f},{},{},{},{}".format( classname[i], score[i], int(box[i, 0]), int(box[i, 1]), int(box[i, 2]), int(box[i, 3]))) else: for i in range(len(classname)): if classname[i] in CLASSES[1:7]: result_data.append("{},{:.3f},{},{},{},{}".format( classname[i], score[i], int(box[i, 0]), int(box[i, 1]), int(box[i, 2]), int(box[i, 3]))) return result_data
def _generate_rois(rpn_cls_prob, rpn_bbox_pred, anchors, num_anchors): scores = rpn_cls_prob[:, :, :, num_anchors:] scores = scores.reshape((-1, 1)) rpn_bbox_pred = rpn_bbox_pred.reshape((-1, 4)) proposal = bbox_target_to_region(rpn_bbox_pred, anchors) proposal = clip_box(proposal, 600, 1000) # before nms orders = np.argsort(scores.ravel())[::-1] before_nms_indexs = orders[:BEFORE_NMS_TOP] scores = scores[before_nms_indexs] proposal = proposal[before_nms_indexs] proposal = proposal.astype(np.float32, copy=False) scores = scores.astype(np.float32, copy=False) print(proposal.shape) print(scores.shape) # after nms keep = nms(np.hstack((proposal, scores)), 0.7) proposal = proposal[keep[:AFTER_NMS_TOP], :] scores = scores[keep[:AFTER_NMS_TOP], :] indexs = np.zeros((proposal.shape[0], 1), dtype=np.float32) proposal = np.hstack((indexs, proposal.astype(np.float32, copy=False))) return proposal, scores
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 im = im[:, :, (2, 1, 0)] fig, ax = plt.subplots(figsize=(12, 12)) ax.imshow(im, aspect='equal') for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] #print(cls,"\t",cls_ind) vis_detections(im, cls, dets, ax, thresh=CONF_THRESH) plt.axis('off') plt.tight_layout() plt.draw()
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(image_name, im, cls, dets, thresh=CONF_THRESH)
def basademo(net, image_name, dataname, exptname): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image #im_file = os.path.join('/om/user/mcusi/nnInit/pytorch-faster-rcnn/data/bASA/JPEGImages', image_name) im_file = '/om/user/mcusi/nnInit/pytorch-faster-rcnn/data/' + dataname + '/demos/' + image_name + '.jpg' print(im_file) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time(), boxes.shape[0])) # Visualize detections for each class CONF_THRESH = [1, 0.4, 0.6] NMS_THRESH = [1, 0.5, 0.3] elements = [] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(torch.from_numpy(dets), NMS_THRESH[cls_ind]) dets = dets[keep.numpy(), :] fn = '/om2/user/mcusi/bayesianASA/cogsci2018/fig/' + dataname + image_name + cls + exptname + '.png' vis_detections(im, cls, dets, fn, thresh=CONF_THRESH[cls_ind])
def demo(sess, net, image_name, direction='Temp'): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, direction, image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s '.format(timer.total_time)) # Visualize detections for each class CONF_THRESH = 0.3 NMS_THRESH = 0.6 #numaximum surpression a = [0, 0, 0, 0] i = 0 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] # num_vehicle = num_vehicle + num_detections(im, cls, dets, thresh=CONF_THRESH) a[i] = a[i] + num_detections(im, cls, dets, thresh=CONF_THRESH) return a
def run_on_fddb(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im = cv2.imread(image_name) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.5 NMS_THRESH = 0.3 inds = np.where(scores[:, 0] > CONF_THRESH)[0] scores = scores[inds, 0] boxes = boxes[inds, :] dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] return dets
def apply_nms(all_boxes, thresh): """Apply non-maximum suppression to all predicted boxes output by the test_net method. """ num_classes = len(all_boxes) num_images = len(all_boxes[0]) nms_boxes = [[[] for _ in range(num_images)] for _ in range(num_classes)] for cls_ind in range(num_classes): for im_ind in range(num_images): dets = all_boxes[cls_ind][im_ind] if dets == []: continue x1 = dets[:, 0] y1 = dets[:, 1] x2 = dets[:, 2] y2 = dets[:, 3] scores = dets[:, 4] inds = np.where((x2 > x1) & (y2 > y1))[0] dets = dets[inds,:] if dets == []: continue keep = nms(dets, thresh) if len(keep) == 0: continue nms_boxes[cls_ind][im_ind] = dets[keep, :].copy() return nms_boxes
def demo(sess, net, im): """Detect object classes in an image using pre-computed object proposals.""" # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.7 NMS_THRESH = 0.3 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background if cls != 'person' and cls != 'plasticbag' and cls != 'bottle': continue cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(im, cls, dets, thresh=CONF_THRESH) # Display the image with bouding boxes cv2.imshow('', im)
def detect(self, image): scores, boxes, rboxes, quadboxes = im_detect(self.sess, self.net, image) # print('Detection {:d} object proposals'.format( boxes.shape[0])) # skip the backgound, only keep the text boxes inds = np.where(scores[:, 1] > self.conf_thresh)[0] txt_scores = scores[inds, 1] txt_boxes = boxes[inds, 4:8] txt_rboxes = rboxes[inds, 5:10] txt_quadboxes = quadboxes[inds, 8:16] txt_dets = np.hstack( (txt_boxes, txt_scores[:, np.newaxis])).astype(np.float32, copy=False) keep = nms(txt_dets, self.nms_thresh) txt_dets = txt_dets[keep, :] txt_rboxes = txt_rboxes[keep, :] txt_quadboxes = txt_quadboxes[keep, :] txt_dets = np.hstack((txt_dets, txt_rboxes, txt_quadboxes)) # return txt_dets regions = [] for txt_det in txt_dets: overlap, idx = iou(np.asarray(regions), txt_det) if overlap < self.iou_thresh: regions.append(txt_det) return regions
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, 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() 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 demo(net, im): """Detect object classes in an image using pre-computed object proposals.""" # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im) timer.toc() #print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time(), boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 nms_keep_indices = [None] * len(CLASSES) for cls_ind, cls in enumerate(CLASSES[:]): cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(torch.from_numpy(dets), NMS_THRESH) dets = dets[keep.numpy(), :] nms_keep_indices[cls_ind] = keep.numpy().tolist() return scores, boxes, nms_keep_indices
def apply_nms(all_boxes, thresh): """Apply non-maximum suppression to all predicted boxes output by the test_net method. """ num_classes = len(all_boxes) num_images = len(all_boxes[0]) nms_boxes = [[[] for _ in range(num_images)] for _ in range(num_classes)] for cls_ind in range(num_classes): for im_ind in range(num_images): dets = all_boxes[cls_ind][im_ind] if dets == []: continue x1 = dets[:, 0] y1 = dets[:, 1] x2 = dets[:, 2] y2 = dets[:, 3] inds = np.where((x2 > x1) & (y2 > y1))[0] dets = dets[inds, :] if dets == []: continue keep = nms(dets, thresh) if len(keep) == 0: continue nms_boxes[cls_ind][im_ind] = dets[keep, :].copy() return nms_boxes
def all_objects(scores, boxes, image_name, thresh=0.8, nms_tresh=0.3): """All detected objects on photo(with threshold)""" end = os.path.splitext(image_name)[-1] print(image_name, end) with open(os.path.join(RESULT_PATH, image_name[:-len(end)] + '.txt'), 'w') as f: f.write(end + '\n') for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, nms_tresh) dets = dets[keep, :] inds = np.where(dets[:, -1] >= thresh)[0] if len(inds) == 0: continue for i in inds: bbox = dets[i, :4] score = dets[i, -1] list_to_file = [cls, score, bbox[0], bbox[1], bbox[2], bbox[3]] for ele in list_to_file: f.write(str(ele) + ' ') f.write('\n')
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(im, cls, dets, thresh=CONF_THRESH)
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image once_time = 0 im = cv2.imread(img_path) # print('>>>>>>>', im.shape[0], im.shape[1]) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() once_time = timer.total_time print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.85 NMS_THRESH = 0.3 inds = np.where(scores[:, 0] > CONF_THRESH)[0] scores = scores[inds, 0] boxes = boxes[inds, :] dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] print('>>>>>num_faces:', dets.shape[0]) cv2_vis(im, CLASSES[1], dets) return once_time
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 output_path = os.path.join(cfg.DATA_DIR,'test_output') for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] name = image_name.split('.')[0] + '.txt' with open(os.path.join(output_path,name),'a') as f: for item in dets: f.write(str(item[0]) + '\t' + str(item[1]) + '\t' + str(item[2])+ '\t' + str(item[3]) + '\t' +str(item[4]) + '\n')
def demo(sess, net, image_name): # 参数:net 测试时使用的网络结构;image_name:图片名称 """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() # from model.test import im_detect # def im_detect(sess, net, im): return scores, pred_boxes scores, boxes = im_detect(sess, net, im) # scores为<class 'numpy.ndarray'>,存放了300个object proposals的得分 # boxes为这300个框 timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 # score 阈值,最后画出候选框时需要,>thresh才会被画出 NMS_THRESH = 0.3 # 非极大值抑制的阈值,剔除重复候选框 for cls_ind, cls in enumerate(CLASSES[1:]): # CLASSES为元组,其中0是背景,故从1开始,但此时的ind0对应第一个标签 cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] # 300个框,4个坐标 cls_scores = scores[:, cls_ind] # 300个分数 # 将分数和框合并到一起成为一个新的(300, 5)矩阵, np.hstack():在水平方向上平铺 dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) # 进行非极大值抑制,得到抑制后的 dets dets = dets[keep, :] # keep 为留下的索引 vis_detections(im, cls, dets, thresh=CONF_THRESH) # 从索引1开始到~每个 cls 存在的 dets 进行显示
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" """使用训练好的模型进行目标检测与识别""" # Load the demo image 载入图片 im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds # 检测所有类别并回归目标边界 timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) #目标检测 timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class # 检测结构可视化 CONF_THRESH = 0.8 #类别确认阈值 NMS_THRESH = 0.3 #区域检测阈值 for cls_ind, cls in enumerate(CLASSES[1:]): #字典枚举 cls_ind += 1 # because we skipped background 因为跳过了背景类别 cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] #获取包围框 cls_scores = scores[:, cls_ind] #获取对应的分值 dets = np.hstack((cls_boxes, #将包围框与对应的分值整合 cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) #包围框筛选 dets = dets[keep, :] vis_detections(im, cls, dets, thresh=CONF_THRESH)
def getImageLabelNum(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im = cv2.imread(image_name) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 countNum=0 labelList=[] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] # vis_detections(im, cls, dets, thresh=CONF_THRESH) count=len(np.where(dets[:, -1] >= CONF_THRESH)[0]) if count!=0: countNum += count labelList.append(cls) return countNum,labelList
def demo(net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time(), boxes.shape[0])) # Visualize detections for each class thresh = 0.8 # CONF_THRESH NMS_THRESH = 0.3 im = im[:, :, (2, 1, 0)] fig, ax = plt.subplots(figsize=(12, 12)) ax.imshow(im, aspect='equal') cntr = -1 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(torch.from_numpy(dets), NMS_THRESH) dets = dets[keep.numpy(), :] inds = np.where(dets[:, -1] >= thresh)[0] if len(inds) == 0: continue else: cntr += 1 for i in inds: bbox = dets[i, :4] score = dets[i, -1] ax.add_patch( plt.Rectangle((bbox[0], bbox[1]), bbox[2] - bbox[0], bbox[3] - bbox[1], fill=False, edgecolor=COLORS[cntr % len(COLORS)], linewidth=3.5) ) ax.text(bbox[0], bbox[1] - 2, '{:s} {:.3f}'.format(cls, score), bbox=dict(facecolor='blue', alpha=0.5), fontsize=14, color='white') ax.set_title('All detections with threshold >= {:.1f}'.format(thresh), fontsize=14) plt.axis('off') plt.tight_layout() plt.savefig('demo_' + image_name) print('Saved to `{}`'.format(os.path.join(os.getcwd(), 'demo_' + image_name)))
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)] 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, :] _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 im_detect(self, sess, net, image): """ :param sess: Tensor flow session :param net: ConvNet :param im: A PIL Image :return: The scores and BB in a tuple with shapes (300x21), (300x84) """ blobs, im_scales = self._get_blobs(image) assert len(im_scales) == 1, "Only single-image batch implemented" im_blob = blobs['data'] blobs['im_info'] = np.array([im_blob.shape[1], im_blob.shape[2], im_scales[0]], dtype=np.float32) # # Here is where the magic starts # _, scores, bbox_pred, rois = net.test_image(sess, blobs['data'], blobs['im_info']) boxes = rois[:, 1:5] / im_scales[0] scores = np.reshape(scores, [scores.shape[0], -1]) bbox_pred = np.reshape(bbox_pred, [bbox_pred.shape[0], -1]) if cfg.TEST.BBOX_REG: # Apply bounding-box regression deltas box_deltas = bbox_pred pred_boxes = bbox_transform_inv(boxes, box_deltas) pred_boxes = FasterRCNN._clip_boxes(pred_boxes, np.array(image).shape) else: # Simply repeat the boxes, once for each class pred_boxes = np.tile(boxes, (1, scores.shape[1])) tmp = [] for j, cls in enumerate(self.imdb.classes): if j == 0: continue inds = np.where(scores[:, j] > self.thresh)[0] cls_scores = scores[inds, j] cls_boxes = pred_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, :] cls_dets = np.insert(cls_dets, 5, j, axis=1) threshold_filter = np.where(cls_dets[:, 4] >= cfg.TEST.NMS)[0] tmp.append(cls_dets[threshold_filter, :]) return np.vstack(tmp)
def pre_process(text_proposals, scores): keep_inds = np.where(scores > TextLineCfg.TEXT_PROPOSALS_MIN_SCORE)[0] text_proposals, scores = text_proposals[keep_inds], scores[keep_inds] # 按得分排序 sorted_indices = np.argsort(scores.ravel())[::-1] text_proposals, scores = text_proposals[sorted_indices], scores[sorted_indices] # 对proposal做nms keep_inds = nms(np.hstack((text_proposals, scores)), TextLineCfg.TEXT_PROPOSALS_NMS_THRESH) text_proposals, scores = text_proposals[keep_inds], scores[keep_inds] return text_proposals, scores
def fusion_nms(boxes): labels_counter = np.zeros(21) for bb in boxes: labels_counter[int(bb[5])] += 1 j = np.argmax(labels_counter) keep = nms(boxes, cfg.TEST.NMS) boxes = np.insert(boxes[keep, :-1], 4, max(boxes[keep, 4]), axis=1) boxes = np.insert(boxes, 5, j, axis=1) inds = np.where(boxes[:, -1] >= cfg.TEST.NMS)[0] return boxes[inds, :]
def proposal_layer(rpn_cls_prob, rpn_bbox_pred, im_info, cfg_key, _feat_stride, anchors, num_anchors): """A simplified version compared to fast/er RCNN For details please see the technical report """ if type(cfg_key) == bytes: cfg_key = cfg_key.decode('utf-8') pre_nms_topN = cfg[cfg_key].RPN_PRE_NMS_TOP_N post_nms_topN = cfg[cfg_key].RPN_POST_NMS_TOP_N nms_thresh = cfg[cfg_key].RPN_NMS_THRESH im_info = im_info[0] # Get the scores and bounding boxes scores = rpn_cls_prob[:, :, :, num_anchors:] rpn_bbox_pred = rpn_bbox_pred.reshape((-1, 4)) scores = scores.reshape((-1, 1)) proposals = bbox_transform_inv(anchors, rpn_bbox_pred) proposals = clip_boxes(proposals, im_info[:2]) # Pick the top region proposals order = scores.ravel().argsort()[::-1] if pre_nms_topN > 0: order = order[:pre_nms_topN] proposals = proposals[order, :] scores = scores[order] # Non-maximal suppression keep = nms(np.hstack((proposals, scores)), nms_thresh) # Pick th top region proposals after NMS if post_nms_topN > 0: keep = keep[:post_nms_topN] proposals = proposals[keep, :] scores = scores[keep] # Only support single image as input batch_inds = np.zeros((proposals.shape[0], 1), dtype=np.float32) blob = np.hstack((batch_inds, proposals.astype(np.float32, copy=False))) return blob, scores
def proposal_layer(rpn_cls_prob, rpn_bbox_pred, im_info, cfg_key, _feat_stride, anchors, num_anchors): """A simplified version compared to fast/er RCNN For details please see the technical report """ if type(cfg_key) == bytes: cfg_key = cfg_key.decode('utf-8') pre_nms_topN = cfg[cfg_key].RPN_PRE_NMS_TOP_N post_nms_topN = cfg[cfg_key].RPN_POST_NMS_TOP_N nms_thresh = cfg[cfg_key].RPN_NMS_THRESH # Get the scores and bounding boxes scores = rpn_cls_prob[:, :, :, num_anchors:] rpn_bbox_pred = rpn_bbox_pred.view((-1, 4)) scores = scores.contiguous().view(-1, 1) proposals = bbox_transform_inv(anchors, rpn_bbox_pred) proposals = clip_boxes(proposals, im_info[:2]) # Pick the top region proposals scores, order = scores.view(-1).sort(descending=True) if pre_nms_topN > 0: order = order[:pre_nms_topN] scores = scores[:pre_nms_topN].view(-1, 1) proposals = proposals[order.data, :] # Non-maximal suppression keep = nms(torch.cat((proposals, scores), 1).data, nms_thresh) # Pick th top region proposals after NMS if post_nms_topN > 0: keep = keep[:post_nms_topN] proposals = proposals[keep, :] scores = scores[keep,] # Only support single image as input batch_inds = Variable(proposals.data.new(proposals.size(0), 1).zero_()) blob = torch.cat((batch_inds, proposals), 1) return blob, scores
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)