def eval(gt, det): gt_bbox_num = 0 det_bbox_num = 0 tp = 0 miss_img = [] for img in gt.keys(): gt_bboxes = gt[img] img_tp = 0 gt_bbox_num += len(gt_bboxes) if img not in det: continue det_bbox_num += len(det[img]) for det_bbox in det[img]: if (utils.iou_calc1(gt_bboxes, det_bbox[:4]) >= 0.5).any(): img_tp += 1 tp += img_tp if(img_tp != len(det[img]) or img_tp != len(gt[img])): miss_img.append(img) recall = tp / gt_bbox_num precision = tp / det_bbox_num print('gt_bbox_num = %d' % gt_bbox_num) print('det_bbox_num = %d' % det_bbox_num) print('tp = %d' % tp) print('F1 = %f' % (2. / (1. / recall + 1. / precision))) return sorted(miss_img)
def dropObjectsInIgr(self, gt, det): det = np.array(det) idxFr = gt[:, 4] != 0 idxIgr = gt[:, 4] == 0 igrRegion = gt[idxIgr, 0:4] igrDet = np.ones(len(det), dtype=np.bool) for i in range(len(igrRegion)): iou = iou_calc1(igrRegion[i], det[:, :4]) igrDet[iou > 0.5] = False return gt[idxFr][:, [0, 1, 2, 3, 5]], det[igrDet]
def make_chip(self, sample, imgset): # get image and mask informations image = cv2.imread(sample['image']) height, width = sample['height'], sample['width'] img_id = osp.splitext(osp.basename(sample['image']))[0] mask_path = osp.join(self.segmentation_dir, '{}.hdf5'.format(img_id)) with h5py.File(mask_path, 'r') as hf: mask = np.array(hf['label']) mask_h, mask_w = mask.shape[:2] # make chip region_box, contours = utils.generate_box_from_mask(mask) region_box = utils.generate_crop_region(region_box, mask, (mask_w, mask_h), (width, height), self.gbm, args.aim) region_box = utils.resize_box(region_box, (mask_w, mask_h), (width, height)) # make tiling if args.tiling and imgset != "val": tiling = utils.add_tiling((width, height)) for pattern in tiling: if utils.iou_calc1(pattern, region_box).max() < 0.85: region_box = np.vstack((region_box, tiling)) if args.show: utils.show_image(image[..., ::-1], np.array(region_box)) # get box and class gt_bboxes, gt_cls = sample['bboxes'], sample['cls'] # generate chip annotations and writer chip image chip_gt_list, chip_label_list, neglect_list = self.generate_region_gt( region_box, gt_bboxes, gt_cls) chip_loc = self.write_chip_and_anno(image, img_id, region_box, chip_gt_list, chip_label_list, neglect_list, imgset) return chip_loc
def augPaster(self, chip_img, loc, bbox, labels): # 当前chip中目标尺度和中心点, 寻找hardlabel obj_scales = bbox[:, 2:4] - bbox[:, :2] obj_center = (bbox[:, 2:4] + bbox[:, :2]) / 2 # hardsIn = [hard for hard in self.hard_cls if hard in labels] # 抠出道路掩码并且创建粘贴点候选序列 road_chip = self.roadMask[loc[1]:loc[3], loc[0]:loc[2]].copy() # utils.show_image(road_chip) cand_chip = cv2.resize(road_chip, (0, 0), fx=hpy["fx"], fy=hpy["fy"], interpolation=cv2.INTER_NEAREST) cand_chip = cv2.erode(cand_chip, self.kernel) # utils.show_image(cand_chip) cand_points = np.argwhere(cand_chip[..., 0] >= 200) * ( road_chip.shape[0] / cand_chip.shape[0]) np.random.shuffle(cand_points) # 粘贴 acount = 0 # 当前粘贴的次数 for time, point in enumerate(cand_points): if time > 500: # 如果找了500次仍然没有找到合适的位置, 放弃 break adj_obj_idx = np.argmin(np.power(point - obj_center, 2).sum(1)) adj_scale = obj_scales[adj_obj_idx] adj_label = labels[adj_obj_idx] adj_rank = self.scale_rank[adj_label] # if acount < len(hardsIn): # pasting_cls = hardsIn[acount] # elif adj_label in self.aid_cls: if adj_label in self.aid_cls: pasting_cls = np.random.choice(self.aid_cls[adj_label]) else: continue scale_map = self.scale_rank[pasting_cls] / adj_rank scale = np.array(adj_scale * scale_map) # 寻找粘贴对象 paster_cls_pool = np.array(self.paster_pool[pasting_cls]) paster_shape = np.array(paster_cls_pool[:, [4, 5, 6]], dtype=np.float) mask1 = paster_shape[:, scale.argmax()] > paster_shape[:, scale.argmin()] mask2 = paster_shape[:, scale.argmax()] >= 0.75 * scale.max() mask3 = paster_shape[:, scale.argmax()] <= 2.5 * scale.max() # mask4 = paster_shape[:, 2] >= 0.5 * (scale[0] / scale[1]) # mask5 = paster_shape[:, 2] <= 1.5 * (scale[0] / scale[1]) mask = mask1 & mask2 & mask3 paster_cls_pool = paster_cls_pool[mask] if len(paster_cls_pool) == 0: continue paster_idx = np.random.choice(np.arange(len(paster_cls_pool))) paster_info = paster_cls_pool[paster_idx] paster_path = osp.join(self.maskPools_dir, "_".join(paster_info)) paster = cv2.imread(paster_path) # 获取粘贴位置, 判断位置是否合法 paster_box = self.getPastingLocal(chip_img, paster, scale, point) paster_area = np.product(paster_box[2:] - paster_box[:2]) paster_road_area = road_chip[paster_box[1]:paster_box[3], paster_box[0]:paster_box[2]].sum() if utils.iou_calc1(paster_box, bbox).max( ) > hpy["obt"] or paster_road_area < paster_area * hpy["ort"]: continue # 调整明亮度, 粘贴 paster = utils.adjustLumin(chip_img, paster, float(paster_info[-2]), hpy['alpha']) chip_img = self.pasting(chip_img, paster, paster_box) # utils.show_image(chip_img, np.array([paster_box])) # 添加尺度和中心信息、加入chip标签 obj_center = np.vstack( (obj_center, paster_box[2:4] + paster_box[:2] / 2)) obj_scales = np.vstack( (obj_scales, paster_box[2:4] - paster_box[:2])) labels = np.hstack((labels, int(pasting_cls))) bbox = np.vstack((bbox, paster_box)) # 计数 self.paster_num[int(pasting_cls)] += 1 acount += 1 if acount >= hpy["pasting_maximum"]: break return chip_img, bbox, labels