def model_fusion(outpath,outfile): resultsa1,resultsa2,resultsa3,resultsa4=results_resolve(model_path="/root/data/gvision/my_merge/finalsubmission/fafafinal/det_results.json",weight=1) resultsb1,resultsb2,resultsb3,resultsb4=results_resolve(model_path="/root/data/gvision/my_merge/finalsubmission/final2/all.json",weight=0.6) annopath="/root/data/gvision/dataset/raw_data/image_annos/person_bbox_test.json" # for i in zip(list(resulta1,resulta2,resulta3,resulta4),list(resultb1,resultb2,resultb3,resultb4)): results1=resultsa1+resultsb1 results2=resultsa2+resultsb2 results3=resultsa3+resultsb3 results4=resultsa4+resultsb4 print('Loading split annotation json file: {}'.format(annopath)) with open(annopath, 'r') as load_f: srcanno = json.load(load_f) indexedresults=indexResults(results1,anno=srcanno) mergedresults = defaultdict(list) for (filename, objlist) in indexedresults.items(): # print("filename",filename) # print("srcfile, paras",srcfile, paras ) srcfile = filename.replace('_IMG', '/IMG')#02_Xili_Crossroad_IMG_02_01___0.5__0__0.jpg srcimageid = srcanno[srcfile]['image id'] for objdict in objlist: mergedresults[srcimageid].append([objdict['bbox'][0],objdict['bbox'][1],objdict['bbox'][2],objdict['bbox'][3],objdict['score'], objdict['category_id']]) for (imageid, objlist) in mergedresults.items(): print(imageid,objlist) # masxlist=[i[2]*i[3] for i in objlist] # max_wh=np.max(masxlist) # objlist=[[i[0],i[1],i[2],i[3],i[4]*0.05+i[3]*i[2]*0.95/max_wh,i[5],i[6]] for i in objlist ] keep = py_cpu_nms(np.array(objlist),0.5) outdets = [] for index in keep: outdets.append(objlist[index]) mergedresults[imageid] = outdets savelist = [] def say2(iss): imageid, objlist=iss[0],iss[1] # print(imageid, objlist) templist=[] for obj in objlist:#obj [22528, 1270, 24576, 1, 1.0, 4] # print(obj) templist.append({ "image_id": imageid, "category_id": obj[5], "bbox": obj[:4], # "bbox": tlbr2tlwh(obj[:4]), "score": obj[4] }) return templist executor = ThreadPoolExecutor(max_workers=80) func_var = [[file_name,dict_value] for file_name,dict_value in mergedresults.items()] print("fusion bbox into self'image start ") pbar2= tqdm(total=len(mergedresults), ncols=50) for temp in executor.map(say2,func_var): savelist+=temp pbar2.update(1) pbar2.close() with open(os.path.join(outpath, outfile), 'w') as f: dict_str = json.dumps(savelist, indent=2) f.write(dict_str) print(f"save ***results*** json :{os.path.join(outpath, outfile)}")
def detect(self, img): ''' Run face detection on a single image :param img: the image as torch tensor 3 x H x W :return: list of bounding boxes of the detected faces ''' # check if gray-scale if len(np.shape(img)) != 3: img = np.reshape(img, (np.shape(img)[0], np.shape(img)[1], 1)) img = np.concatenate((img, img, img), axis=2) # print(img.shape) result_boxes = [] for scale in self.scale_list: if (scale * min([img.shape[0], img.shape[1]])) < 20: break ratio = 1 / scale resized_image = rescale(img, scale, mode='constant', preserve_range=True) resized_image = np.rollaxis(resized_image, 2).copy() resized_image = resized_image / 255 resized_image = torch.autograd.Variable( torch.from_numpy(resized_image).view( -1, *resized_image.shape)).float() output = self.net(resized_image) # output size is 1 X 2 X H X W heatmap = output[ 0, 1, :, :] # take the probability of detecting Face class ( 1 X H X W ) preds = heatmap > 0.5 # 1 is we predict a face, 0 o/w H, W = preds.size() bboxes = [] for h in range(H): for w in range(W): if preds.data[h, w] == 1: score = heatmap.data[h, w] xmin = min(int(2 * w * ratio), img.shape[1] - int(12 * ratio)) xmax = xmin + int(12 * ratio) ymin = min(int(2 * h * ratio), img.shape[0] - int(12 * ratio)) ymax = ymin + int(12 * ratio) # croped_img = img[ymin: ymax, xmin: xmax] # print(croped_img.shape) bboxes.append([xmin, ymin, xmax, ymax, score]) # run NMS per scale # print('\tbefore NMS in scale {}: {}'.format(scale, len(bboxes))) if len(bboxes): bboxes = py_cpu_nms(np.array(bboxes), self.nms_threshold) # print('\tafter NMS in scale {}: {}'.format(scale, len(bboxes))) result_boxes += bboxes # print(result_boxes) return result_boxes
def merge_pkls(input_pkls_dir, outfile, nms_iou_thr, nms_score_thr, max_per_img): pkl_names_list = os.listdir(input_pkls_dir) num_pkls = len(pkl_names_list) if pkl_names_list == []: raise ValueError('Invalid input dir: can`t be NULL!') pkls_list = [mmcv.load(input_pkls_dir + '/' + pkl_name) for pkl_name in pkl_names_list] all_imgs_det = [] num_imgs = len(pkls_list[0]) num_class = 20 for i in range(num_imgs): img_dets_merged = [] img_dets_input = [pkl[i] for pkl in pkls_list] for ic in range(num_class): # [ [arr1, arr2, ..., arr20] * num_pkls ] cls_dets_input = np.zeros([0, 5]) for iscale in range(num_pkls): single_img_cls_dets_input = img_dets_input[iscale][ic] # stack dets of class(ic) from each pkls. if single_img_cls_dets_input.shape[0] > 0: cls_dets_input = np.row_stack([cls_dets_input, single_img_cls_dets_input]) if cls_dets_input.shape[0] > 1: # nms_keep_inds = py_cpu_softnms(cls_dets_input, method=2, iou_thr=nms_iou_thr, scr_thr=nms_score_thr, sigma=0.5) nms_keep_inds = py_cpu_nms(cls_dets_input, thresh=nms_iou_thr) cls_dets_merged = cls_dets_input[nms_keep_inds, :] else: cls_dets_merged = cls_dets_input # suppress low score bboxes. if cls_dets_merged.shape[0] > 1: scr_keep_inds = (np.where(cls_dets_merged[:, -1] > nms_score_thr))[0] cls_dets_merged = cls_dets_merged[scr_keep_inds, :] # if scr_keep_inds is NULL, return [] @shape(0, 5) img_dets_merged.append(cls_dets_merged) scores = np.hstack([img_dets_merged[j][:, -1] for j in range(num_class)]) if len(scores) > max_per_img: kth = len(scores) - max_per_img thresh = np.partition(scores, kth)[kth] for j in range(num_class): keep_inds = (img_dets_merged[j][:, -1] >= thresh) img_dets_merged[j] = img_dets_merged[j][keep_inds] all_imgs_det.append(img_dets_merged) mmcv.dump(all_imgs_det, outfile)
def get_detections(rpn, net, dataset, dataloader, nms_threshold=0.3, display=500): proposal_generator = ProposalGenerator() softmax_fast_rcnn = nn.Softmax().cuda() softmax_rpn = nn.Softmax2d().cuda() start_time = time.time() detection_results = { classname: {} for classname in CLASSES if classname != "__background__" } for i, (indices, image_arr, _, _, image_info) in enumerate(dataloader): image_name = dataset.get_image_name_from_index(indices[0]) bboxes = generate(rpn, softmax_rpn, proposal_generator, image_arr, image_info[0, :]) bboxes = bboxes * image_info[0, -1] # scale bboxes proposals = np.hstack((np.zeros(len(bboxes)).reshape(-1, 1), bboxes)) proposals = autograd.Variable(torch.Tensor(proposals).cuda()) image_arr = autograd.Variable(image_arr.cuda()) cls_scores, bbox_deltas = net(image_arr, proposals) cls_preds = softmax_fast_rcnn(cls_scores) cls_preds = cls_preds.cpu().data.numpy() bbox_preds = generate_proposals(bboxes, bbox_deltas.cpu().data.numpy()) bbox_preds = clip_boxes(bbox_preds, image_info[0, 0], image_info[0, 1]) for classname in detection_results: class_idx = CLASS_TO_INDEX[classname] idx = np.where(cls_preds[:, class_idx] > 0.05)[0] scores = cls_preds[idx, class_idx] bboxes = bbox_preds[idx, (class_idx * 4):(class_idx * 4 + 4)] detections = np.hstack( (bboxes, scores.reshape(-1, 1))).astype(np.float32, copy=False) keep_idx = py_cpu_nms(detections, nms_threshold) detections = detections[keep_idx, :] detection_results[classname][image_name] = detections if i % display == (display - 1): total_time = time.time() - start_time ave_time = total_time / display print("[+] Processed %d. Average time=%.3f/image" % (i, ave_time)) start_time = time.time() return detection_results
def test_model(self): saver = tf.train.Saver() _rois_coord = self.net.rois_coord[:, 1:5] #rois_coord = self.net.rois_coord _pred_box = self.net.bbox_pred _pred_score = self.net.cls_prob #_pred_box_score_arg = tf.argmax(_pred_score, axis=1) dect_total_result = [[[] for i in range(self.val_data.num_gtlabels)] for j in range(self.net.num_classes)] with tf.Session() as sess: saver.restore(sess, self.ckpt_filename) for i in range(self.val_data.num_gtlabels): print(i, ' image test compeleted') train_data = self.val_data.get() image_height = np.array(train_data['image'].shape[1]) image_width = np.array(train_data['image'].shape[2]) feed_dict = {self.net.image: train_data['image'], self.net.image_width: image_width,\ self.net.image_height: image_height} rois_coord, pred_box, pred_score= sess.run([_rois_coord, _pred_box, _pred_score],\ feed_dict=feed_dict) for k in range(1, self.net.num_classes): #pre_class_arg = np.where(pred_score[:,k]>=0)[0] cls_pred_box_target = pred_box[:, k * 4:(k + 1) * 4] cls_pred_box_target = cls_pred_box_target * np.array( cfg.bbox_nor_stdv) + np.array(cfg.bbox_nor_mean) cls_pred_box_coord = self.coord_transform_inv( rois_coord, cls_pred_box_target.astype(np.float32)) cls_pred_box_coord = cls_pred_box_coord / train_data[ 'scale'] + 1.0 cls_pred_score = pred_score[:, k] #print(cls_pred_box_coord.shape, cls_pred_score.shape) cls_pred_score = cls_pred_score[:, np.newaxis] cls_pred_target = np.concatenate( (cls_pred_box_coord, cls_pred_score), axis=1) keep = py_cpu_nms(cls_pred_target, cfg.test_nms_thresh) cls_pred_target = cls_pred_target[keep, :] dect_total_result[k][i] = cls_pred_target for j in range(1, self.net.num_classes): keep = np.where(dect_total_result[j][i][:, -1] >= 0.5)[0] dect_total_result[j][i] = dect_total_result[j][i][ keep, :] # mean_ap = self.map_compute(dect_total_result) print('the mean_ap of pascal_voc 2007 is', mean_ap)
def forward(self, bbox_deltas, scores, image_info): scores = scores.view([scores.size(0), 18, -1, scores.size(3)]) scores = scores.data.numpy() scores = scores[:, 9:, :, :] # take all foreground scores bbox_deltas = bbox_deltas.data.numpy() height = scores.shape[2] width = scores.shape[3] anchors = generate_all_anchors(width, height, self._STRIDE, self._BASE_ANCHORS) im_h = image_info[0] im_w = image_info[1] scale_const = image_info[2] scores = scores.transpose([0, 2, 3, 1]).reshape([-1, 1]) bbox_deltas = bbox_deltas.transpose([0, 2, 3, 1]).reshape([-1, 4]) proposals = generate_proposals(anchors, bbox_deltas) proposals = clip_boxes(proposals, im_w, im_h) threshold = 16. * scale_const keep = filter_boxes(proposals, threshold) proposals = proposals[keep, :] scores = scores[keep] order = scores.ravel().argsort()[::-1] order = order[:12000] proposals = proposals[order, :] scores = scores[order] keep = py_cpu_nms(np.hstack((proposals, scores)), 0.7) keep = keep[:2000] proposals = proposals[keep, :] scores = scores[keep] batch_idx = np.zeros((proposals.shape[0], 1), dtype=np.float32) blob = np.hstack((batch_idx, proposals.astype(np.float32, copy=False))) return autograd.Variable(torch.Tensor(blob)), \ autograd.Variable(torch.Tensor(scores))
def test_ssd(self, image_paths): if isinstance(image_paths, str): image_paths = [image_paths] with tf.Session() as sess: sess.run(tf.compat.v1.global_variables_initializer()) ckpt = tf.train.get_checkpoint_state(cfg.MODEL_PATH) if ckpt and ckpt.model_checkpoint_path: # 如果保存过模型,则在保存的模型的基础上继续训练 self.net.saver.restore(sess, ckpt.model_checkpoint_path) print('Model Reload Successfully!') for path in image_paths: value = self.pre_process(path) image = value['image'] - self.pixel_means feed_dict = {self.net.x: image} pred_loc, pred_cls, layer_anchors = sess.run( [self.pred_loc, self.pred_cls, self.net.anchors], feed_dict ) pos_loc, pos_cls, pos_anchors, pos_scores = self.decode_output( pred_loc, pred_cls, layer_anchors) pos_boxes = decode_targets(pos_anchors, pos_loc, image.shape) pos_scores = np.expand_dims(pos_scores, axis=-1) keep_index = py_cpu_nms(np.hstack([pos_boxes, pos_scores])) self.draw_result( value['image'], pos_boxes[keep_index], pos_cls[keep_index], value['scale'] )
def detect(self, img): ''' Run face detection on a single image :param img: the image as torch tensor 3 x H x W :return: list of bounding boxes of the detected faces ''' # check if gray-scale if len(np.shape(img)) != 3: img = np.reshape(img, (np.shape(img)[0], np.shape(img)[1], 1)) img = np.concatenate((img, img, img), axis=2) # get detection boxes from 12-detector bbox_from_12 = self.simple_detector.detect(img) result = [] for box in bbox_from_12: xmin, ymin, xmax, ymax, _ = list(map(int, box)) window = img[ymin:ymax, xmin:xmax, :] window = resize( window, (24, 24, 3), preserve_range=True, mode='constant') / 255 window = torch.from_numpy(np.rollaxis(window, 2)).clone() assert (window.size()) == torch.rand(3, 24, 24).size() window = torch.autograd.Variable(window.view(1, 3, 24, 24)).float() output = self.net(window) # if the new detector agrees with the simple one, keep the bbox # print(output) val, pred = torch.max(output, dim=1) if float(pred.data[0]): result.append([xmin, ymin, xmax, ymax, val.data[0]]) after_24 = len(result) # run global NMS if len(result): result = py_cpu_nms(np.array(result), self.nms_threshold) print(' 12-nms-{}, 24-{}, nms-{}'.format(len(bbox_from_12), after_24, len(result))) return result
def test_model(self): saver = tf.train.Saver() _rois_coord = self.net.rois_coord[:, 1:5] _pred_box = self.net.bbox_pred _pred_score = self.net.cls_prob _pred_box_score_arg = tf.argmax(_pred_score, axis=1) dect_total_result = [[[] for i in range(cfg.img_save_num)] for j in range(self.net.num_classes)] test_data = [[] for i in range(cfg.img_save_num)] with tf.Session() as sess: saver.restore(sess, self.ckpt_filename) for i in range(cfg.img_save_num): print(i, ' image test compeleted') train_data = self.val_data.get( ) #if you want to change the test image, you can using cv2.imread() here to read your own image data test_data[i] = train_data image = cv2.imread(test_data[i]['imname']) image_height = np.array(train_data['image'].shape[1]) image_width = np.array(train_data['image'].shape[2]) feed_dict = {self.net.image: train_data['image'], self.net.image_width: image_width,\ self.net.image_height: image_height} rois_coord, pred_box, pred_box_score_arg, pred_score= sess.run([_rois_coord, _pred_box, _pred_box_score_arg, _pred_score],\ feed_dict=feed_dict) pred_box_score_arg = pred_box_score_arg.astype(np.int32) num_pred = pred_box_score_arg.shape[0] pred_box_gather = np.empty([num_pred, 4], dtype=np.float32) pred_score_gather = np.empty(num_pred) for j in range(num_pred): pred_box_gather[j, :] = pred_box[j, 4 * pred_box_score_arg[j]:4 * (pred_box_score_arg[j] + 1)] pred_score_gather[j] = pred_score[j, pred_box_score_arg[j]] pred_box_gather = pred_box_gather * np.array( cfg.bbox_nor_stdv) + np.array(cfg.bbox_nor_mean) pre_box_coord = self.coord_transform_inv( rois_coord, pred_box_gather.astype(np.float32)) pre_box_coord = pre_box_coord for k in range(1, self.net.num_classes): pre_class_arg = np.where(pred_box_score_arg == k)[0] cls_pred_box_coord = pre_box_coord[pre_class_arg, :] cls_pred_score = pred_score_gather[pre_class_arg] #print(cls_pred_box_coord.shape, cls_pred_score.shape) cls_pred_score = cls_pred_score[:, np.newaxis] cls_pred_target = np.concatenate( (cls_pred_box_coord, cls_pred_score), axis=1) keep = py_cpu_nms(cls_pred_target, cfg.test_nms_thresh) cls_pred_target = cls_pred_target[keep, :] for h in range(len(cls_pred_target)): if cls_pred_target[h][4] > 0.5: x1 = int(cls_pred_target[h][0]) y1 = int(cls_pred_target[h][1]) x2 = int(cls_pred_target[h][2]) y2 = int(cls_pred_target[h][3]) cv2.rectangle(image, (x1, y1), (x2, y2), ((k % 19) * 15, (k % 3) * 100 + 40, (k % 6) * 50 + 35), 2) cv2.rectangle(image, (x1, y1 - 20), (x2, y1), (125, 125, 125), -1) lineType = cv2.LINE_AA if cv2.__version__ > '3' else cv2.CV_AA cv2.putText(image, cfg.CLASSES[k] + ' : %.2f' % cls_pred_target[h][4], \ (x1 + 5, y1 - 7), cv2.FONT_HERSHEY_SIMPLEX, 0.5, \ (0, 0, 0), 1, lineType) if not os.path.exists(self.image_output_dir): os.mkdir(self.image_output_dir) im_save_path = os.path.join( self.image_output_dir, test_data[i]['imname'].split('/')[-1]) cv2.imwrite(im_save_path, image) cv2.imshow('Image', image) cv2.waitKey(0) for k in range(cfg.img_save_num): imname = test_data[k]['imname'] im_scale = test_data[k]['scale'] image = cv2.imread(imname) im = cv2.resize(image, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) im = self.draw_result(im, dect_total_result, k) if not os.path.exists(self.image_output_dir): os.mkdir(self.image_output_dir) im_save_path = os.path.join(self.image_output_dir, imname.split('/')[-1]) cv2.imwrite(im_save_path, im) cv2.imshow('Image', im) cv2.waitKey(0)
def mergeResults(self,outfile,merge_input_mode="xywh",is_nms=True,nms_thresh=0.9,nms_name="nms"): """ :param is_nms: do non-maximum suppression on after merge :param nms_thresh: non-maximum suppression IoU threshold :return: """ print('Loading source annotation json file: {}'.format(self.srcannopath)) with open(self.srcannopath, 'r') as load_f: srcanno = json.load(load_f) mergedresults = defaultdict(list) # random.seed(0) # print("len",len(self.results)) keep_input=[] for (filename, objlist) in self.results.items(): # for (filename, objlist) in random.sample(self.results.items(),2): # srcfile, scale, left, up = filename.split('___') # srcfile =srcfile.replace('_IMG', '/IMG')+".jpg" # up =up[:-4] # # print(filename, objlist) # # srcimageid =srcfile[-2:] # # print(srcfile) # srcimageid = srcanno[srcfile]['image id'] # print("srcimageid",srcimageid) # print(srcfile, scale, left, up ) srcfile, paras = filename.split('___')#srcfile, paras 15_Nanshani_Park_IMG_15_04 0.5__4224__6144.jpg # print("srcfile, paras",srcfile, paras ) srcfile = srcfile.replace('_IMG', '/IMG') + self.imgext#02_Xili_Crossroad_IMG_02_01___0.5__0__0.jpg srcimageid = srcanno[srcfile]['image id'] scale, left, up = paras.replace(self.imgext, '').split('__')#scale, left, up 0.5 4224 6144 print(srcfile, scale, left, up ) print(f"before objlist {len(objlist)}") if not iskeep_dets: for objdict in objlist: mergedresults[srcimageid].append([*recttransfer(objdict['bbox'], float(scale), int(left), int(up),merge_input_mode),objdict['score'], objdict['category_id'],objdict['image_id']]) if iskeep_dets: keep_input=keep_input+[[srcimageid,int(left), int(up),float(scale),i['bbox'][0],i['bbox'][1],i['bbox'][2],i['bbox'][3],i['score'],i['image_id'],i['category_id']] for i in objlist] # keep_input.append([[srcimageid,int(left), int(up),float(scale),i['bbox'][0],i['bbox'][1],i['bbox'][2],i['bbox'][3],i['score'],i['image_id'],i['category_id']] for i in objlist]) if iskeep_dets: print(f"before keep {len(keep_input)}") if 391<=srcimageid<=420:#14otcUP_boundary keep_dets,_keep_values=self.keep_dets(np.array(keep_input),UP_boundary[0],PANDA_TEST_SIZE[0]) if 421<=srcimageid<=450:#15 nanshangongyuan keep_dets,_keep_values=self.keep_dets(np.array(keep_input),UP_boundary[1],PANDA_TEST_SIZE[1]) if 451<=srcimageid<=465:#16xiaoxue----------01 keep_dets,_keep_values=self.keep_dets(np.array(keep_input),UP_boundary[2],PANDA_TEST_SIZE[2]) if 466<=srcimageid<=480:#16xiaoxue--------02 keep_dets,_keep_values=self.keep_dets(np.array(keep_input),UP_boundary[3],PANDA_TEST_SIZE[2]) if 481<=srcimageid<=510:#17zhongguan keep_dets,_keep_values=self.keep_dets(np.array(keep_input),UP_boundary[4],PANDA_TEST_SIZE[3]) if 511<=srcimageid<=540:#18xilin-------01 keep_dets,_keep_values=self.keep_dets(np.array(keep_input),UP_boundary[5],PANDA_TEST_SIZE[4]) if 541<=srcimageid<=555:#18xilin----------02 keep_dets,_keep_values=self.keep_dets(np.array(keep_input),UP_boundary[6],PANDA_TEST_SIZE[5]) print(f"after keep {len(keep_dets)}") for objdict,keep_value in zip(keep_dets,_keep_values): left, up,scale=keep_value[0],keep_value[1],keep_value[2] # print("objdict",objdict)#{'image_id': 7110, 'bbox': [47.7, 866.2, 198.8, 442.8], 'score': 0.83231, 'category_id': 1} mergedresults[srcimageid].append([*recttransfer(objdict['bbox'], float(scale), int(left), int(up),merge_input_mode), objdict['score'], objdict['category_id'],objdict['image_id']]) img_size = {} for anno in srcanno: # print(srcanno[anno]['image id']) img_size[srcanno[anno]['image id']] = srcanno[anno]['image size'] if is_nms: if nms_name=="nms": for (imageid, objlist) in mergedresults.items(): masxlist=[i[2]*i[3] for i in objlist] max_wh=np.max(masxlist) objlist=[[i[0],i[1],i[2],i[3],i[4]*0.05+i[3]*i[2]*0.95/max_wh,i[5],i[6]] for i in objlist ] keep = py_cpu_nms(np.array(objlist), nms_thresh) outdets = [] for index in keep: outdets.append(objlist[index]) mergedresults[imageid] = outdets if nms_name=="setnms": for (imageid, objlist) in mergedresults.items(): print("input nms element",objlist[0])#[829, 5939, 923, 6000, 0.24672751128673553, 1, 149] print(f"before setnms {nms_thresh} ",len(objlist)) keep=np.array(objlist)[set_cpu_nms(np.array(objlist), nms_thresh)].tolist() # print("keep",keep,"\n",len(keep),type(keep)) print(f"{imageid} after setnms_{nms_thresh} ",len(keep)) mergedresults[imageid] = keep if nms_name=="emnms": for (imageid, objlist) in mergedresults.items(): size_anno = img_size[imageid] boxes = [[obj[0] / size_anno['width'], obj[1] / size_anno['height'], obj[2] / size_anno['width'], obj[3] / size_anno['height']] for obj in objlist] scores = [obj[4] for obj in objlist] labels = [obj[5] for obj in objlist] boxes, scores, labels = nms([boxes], [scores], [labels]) boxes[:, [0, 2]] *= size_anno['width'] boxes[:, [1, 3]] *= size_anno['height'] outdets = [x[0] + [x[1], x[2]] for x in zip(boxes.tolist(), scores.tolist(), labels.tolist())] mergedresults[imageid] = outdets if nms_name=="softnms": for (imageid, objlist) in mergedresults.items(): print(f"{imageid} before softnms_{nms_thresh} ",len(objlist)) # print("ssss",len(objlist[0])) # print("ssss",objlist[0]) masxlist=[i[2]*i[3] for i in objlist] max_wh=np.max(masxlist) objlist=[[i[0],i[1],i[2],i[3],i[4]*0.05+i[3]*i[2]*0.95/max_wh,i[5],i[6]] for i in objlist ] # tempmax=np.max(np.array(objlist)[:, 4]) # print("max",tempmax)#208909381.05317593 # objlist=[[i[0],i[1],i[2],i[3],i[4]/(tempmax+0.00001),i[5],i[6]] for i in objlist ] # print(objlist) newdets,keep=soft_nms(np.array(objlist),iou_thr=nms_thresh, method='linear',sigma=0.5, min_score=1e-3)#'gaussian''linear', # keep =py_cpu_softnms(np.array(objlist),thresh=nms_thresh, Nt=0.02, sigma=0.5, method=1) # print(keep) outdets = [] for index in keep: outdets.append(objlist[index]) print(f"{imageid} after softnms_{nms_thresh} ",len(keep)) mergedresults[imageid] = outdets savelist = [] def say2(iss): imageid, objlist=iss[0],iss[1] # print(imageid, objlist) templist=[] for obj in objlist:#obj [22528, 1270, 24576, 1, 1.0, 4] # print(obj) templist.append({ "image_id": imageid, "category_id": obj[5], "bbox": tlbr2tlwh(obj[:4]), "score": obj[4] }) if test: print(f"fliter berfore len {len(templist)}") if isfliter: if 391<=imageid<=420:#14otc templist=fliter(templist,fliterscore["14_OCT"],AnotPath="/root/data/gvision/dataset/xml/14_OCT_Habour.xml", segma_woh=3,segma_area=3,up_bound=4000,down_bound=None,down_fs=0.95,yichang=0) if 421<=imageid<=450:#15 nanshangongyuan templist=fliter(templist,fliterscore["15_nanshan"],AnotPath="/root/data/gvision/dataset/xml/15_Nanshani_Park.xml", segma_woh=3,segma_area=2,up_bound=1500,down_bound=7000,down_fs=None,yichang=0) if 451<=imageid<=465:#16xiaoxue----------01 templist=fliter(templist,fliterscore["1601_shool"],AnotPath="/root/data/gvision/dataset/xml/IMG_16_01_head.xml", segma_woh=3,segma_area=3,up_bound=0,down_bound=None,down_fs=None,yichang=0) if 466<=imageid<=480:#16xiaoxue--------02 templist=fliter(templist,fliterscore["1602_shool"],AnotPath="/root/data/gvision/dataset/xml/IMG_16_25_02_.xml", segma_woh=3,segma_area=3,up_bound=0,down_bound=None,down_fs=None,yichang=0) if 481<=imageid<=510:#17zhongguan templist=fliter(templist,fliterscore["17_newzhongguan"],AnotPath="/root/data/gvision/dataset/xml/17_New_Zhongguan.xml", segma_woh=3,segma_area=3,up_bound=6000,down_bound=7000,down_fs=None,yichang=0) if 511<=imageid<=540:#18xilin-------01 templist=fliter(templist,fliterscore["1801_xilin"],AnotPath="/root/data/gvision/dataset/xml/IMG_18_01_01.xml", segma_woh=3,segma_area=3,up_bound=4000,down_bound=None,down_fs=None,yichang=0) if 541<=imageid<=555:#18xilin----------02 templist=fliter(templist,fliterscore["1802_xilin"],AnotPath="/root/data/gvision/dataset/xml/IMG_18_02.xml", segma_woh=3,segma_area=3,up_bound=4000,down_bound=None,down_fs=None,yichang=0) if isdel_inter: templist=del_inter(templist) if test: print(f"del_inter after len {len(templist)}") return templist executor = ThreadPoolExecutor(max_workers=80) func_var = [[file_name,dict_value] for file_name,dict_value in mergedresults.items()] print("merge bbox into self'image start ") pbar2= tqdm(total=len(mergedresults), ncols=50) for temp in executor.map(say2,func_var): savelist+=temp pbar2.update(1) pbar2.close() with open(os.path.join(self.outpath, outfile), 'w', encoding=self.code) as f: dict_str = json.dumps(savelist, indent=2) f.write(dict_str) print(f"save ***results*** json :{os.path.join(self.outpath, outfile)}")
def wnms(results,outpath,outfile,iouthresh,savejson=1,nmsname="nms"): indexedresults=indexResults(results) mergedresults = defaultdict(list) for (imageid, objlist) in indexedresults.items(): for objdict in objlist: mergedresults[imageid].append([objdict['bbox'][0],objdict['bbox'][1],objdict['bbox'][2],objdict['bbox'][3],objdict['score'], objdict['category_id']]) objlist=mergedresults[imageid] # masxlist=[i[2]*i[3] for i in objlist] # max_wh=np.max(masxlist) # objlist=[[i[0],i[1],i[2],i[3],i[4]*0.05+i[3]*i[2]*0.95/max_wh,i[5],i[6]] for i in objlist ] if nmsname=="softnms": newdets,keep=soft_nms(np.array(objlist),iou_thr=iouthresh, method='gaussian',sigma=0.5, min_score=1e-3)#'gaussian''linear', # keep =py_cpu_softnms(np.array(objlist),thresh=nms_thresh, Nt=0.02, sigma=0.5, method=1) outdets = [] for index in keep: outdets.append(objlist[index]) mergedresults[imageid] = outdets elif nmsname=="nms": keep = py_cpu_nms(np.array(objlist),iouthresh) outdets = [] for index in keep: outdets.append(objlist[index]) mergedresults[imageid] = outdets elif nmsname=="setnms": keep=np.array(objlist)[set_cpu_nms(np.array(objlist), nms_thresh)].tolist() mergedresults[imageid] = keep elif nmsname==False: print("no nms") else: raise ValueError('nmsname must is softnms or nms') savelist = [] def say2(iss): imageid, objlist=iss[0],iss[1] templist=[] for obj in objlist:#obj [22528, 1270, 24576, 1, 1.0, 4] templist.append({ "image_id": imageid, "category_id": obj[5], "bbox": obj[:4], # "bbox": tlbr2tlwh(obj[:4]), "score": obj[4] }) if isfliter: if 391<=imageid<=420:#14otc templist=fliter(templist,fliterscore["14_OCT"],AnotPath="/root/data/gvision/dataset/xml/14_OCT_Habour.xml", segma_woh=3,segma_area=3,up_bound=4000,down_bound=None,down_fs=0.95,yichang=0) if 421<=imageid<=450:#15 nanshangongyuan templist=fliter(templist,fliterscore["15_nanshan"],AnotPath="/root/data/gvision/dataset/xml/15_Nanshani_Park.xml", segma_woh=3,segma_area=2,up_bound=1500,down_bound=7000,down_fs=None,yichang=0) if 451<=imageid<=465:#16xiaoxue----------01 templist=fliter(templist,fliterscore["1601_shool"],AnotPath="/root/data/gvision/dataset/xml/IMG_16_01_01.xml", segma_woh=3,segma_area=3,up_bound=0,down_bound=None,down_fs=None,yichang=0) if 466<=imageid<=480:#16xiaoxue--------02 templist=fliter(templist,fliterscore["1602_shool"],AnotPath="/root/data/gvision/dataset/xml/IMG_16_25_02_.xml", segma_woh=3,segma_area=3,up_bound=0,down_bound=None,down_fs=None,yichang=0) if 481<=imageid<=510:#17zhongguan templist=fliter(templist,fliterscore["17_newzhongguan"],AnotPath="/root/data/gvision/dataset/xml/17_New_Zhongguan.xml", segma_woh=3,segma_area=3,up_bound=6000,down_bound=7000,down_fs=None,yichang=0) if 511<=imageid<=540:#18xilin-------01 templist=fliter(templist,fliterscore["1801_xilin"],AnotPath="/root/data/gvision/dataset/xml/IMG_18_01_01.xml", segma_woh=3,segma_area=3,up_bound=4000,down_bound=None,down_fs=None,yichang=0) if 541<=imageid<=555:#18xilin----------02 templist=fliter(templist,fliterscore["1802_xilin"],AnotPath="/root/data/gvision/dataset/xml/IMG_18_02.xml", segma_woh=3,segma_area=3,up_bound=4000,down_bound=None,down_fs=None,yichang=0) if test: print(f"del_inter after len {len(templist)}") return templist executor = ThreadPoolExecutor(max_workers=80) func_var = [[file_name,dict_value] for file_name,dict_value in mergedresults.items()] print("fusion bbox into self'image start ") pbar2= tqdm(total=len(mergedresults), ncols=50) for temp in executor.map(say2,func_var): # print(temp) savelist+=temp pbar2.update(1) pbar2.close() # assert len(savelist)==0,f"error{savelist} error" if savejson: assert isinstance(savelist[0], dict),f"the results must is not {savelist[0]}" # if not isinstance(savelist[0], dict): # raise f"the results must is not {savelist[0]}" # print(savelist[0]['category_id']) outfile=outfile[:-5].replace("all",f"{savelist[1]['category_id']}")+".json" with open(os.path.join(outpath, outfile), 'w') as f: dict_str = json.dumps(savelist, indent=2) f.write(dict_str) print(f"save ***{len(savelist)} results*** json :{os.path.join(outpath, outfile)}") return savelist
def wnms(results, outpath, outfile, iouthresh, savejson=1, nmsname="nms"): indexedresults = indexResults(results) mergedresults = defaultdict(list) for (imageid, objlist) in indexedresults.items(): for objdict in objlist: mergedresults[imageid].append([ objdict['bbox'][0], objdict['bbox'][1], objdict['bbox'][2], objdict['bbox'][3], objdict['score'], objdict['category_id'], objdict["number"] ]) objlist = mergedresults[imageid] # masxlist=[i[2]*i[3] for i in objlist] # max_wh=np.max(masxlist) # objlist=[[i[0],i[1],i[2],i[3],i[4]*0.05+i[3]*i[2]*0.95/max_wh,i[5],i[6]] for i in objlist ] keep = py_cpu_nms(np.array(objlist), 1) outdets = [] for index in keep: outdets.append(objlist[index]) mergedresults[imageid] = outdets if nmsname == "softnms": keep = py_cpu_nms(np.array(objlist), 1) newdets, keep = soft_nms(np.array(objlist), iou_thr=iouthresh, method='linear', sigma=0.5, min_score=1e-3) #'gaussian''linear', # keep =py_cpu_softnms(np.array(objlist),thresh=nms_thresh, Nt=0.02, sigma=0.5, method=1) outdets = [] for index in keep: outdets.append(objlist[index]) mergedresults[imageid] = outdets elif nmsname == "setnms": print(objlist[0]) print(len(objlist[0])) keep = np.array(objlist)[set_cpu_nms(np.array(objlist), iouthresh)].tolist() mergedresults[imageid] = keep elif nmsname == "nms": keep = py_cpu_nms(np.array(objlist), 1) outdets = [] for index in keep: outdets.append(objlist[index]) mergedresults[imageid] = outdets elif nmsname == False: print("no nms") else: raise ValueError('nmsname must is softnms or nms') savelist = [] def say2(iss): imageid, objlist = iss[0], iss[1] templist = [] for obj in objlist: #obj [22528, 1270, 24576, 1, 1.0, 4] templist.append({ "image_id": imageid, "category_id": int(obj[5]), "bbox": obj[:4], # "bbox": tlbr2tlwh(obj[:4]), "score": obj[4] }) templist = fliter(imageid, objlist[1][5], templist) return templist executor = ThreadPoolExecutor(max_workers=80) func_var = [[file_name, dict_value] for file_name, dict_value in mergedresults.items()] print("fusion bbox into self'image start ") pbar2 = tqdm(total=len(mergedresults), ncols=50) for temp in executor.map(say2, func_var): # print(temp) savelist += temp pbar2.update(1) pbar2.close() # assert len(savelist)==0,f"error{savelist} error" if savejson: assert isinstance(savelist[0], dict), f"the results must is not {savelist[0]}" # if not isinstance(savelist[0], dict): # raise f"the results must is not {savelist[0]}" # print(savelist[0]['category_id']) outfile = outfile[:-5].replace( "all", f"{savelist[1]['category_id']}") + ".json" with open(os.path.join(outpath, outfile), 'w') as f: dict_str = json.dumps(savelist, indent=2) f.write(dict_str) print( f"save ***{len(savelist)} results*** json :{os.path.join(outpath, outfile)}" ) return savelist
orig_img = io.imread(filename) nms_img = orig_img.copy() fig, (ax1, ax2) = plt.subplots(1, 2, num='Non-maximum Suppression') for bbox in dets: start_x, start_y, end_x, end_y, _ = bbox rect = mpatches.Rectangle((start_x, start_y), end_x - start_x + 1, end_y - start_y + 1, fill=False, edgecolor='red', linewidth=2) ax1.add_patch(rect) # Non-maximum Suppression picks = dets[nms.py_cpu_nms(dets, 0.3)] for bbox in picks: start_x, start_y, end_x, end_y, _ = bbox rect = mpatches.Rectangle((start_x, start_y), end_x - start_x + 1, end_y - start_y + 1, fill=False, edgecolor='green', linewidth=2) ax2.add_patch(rect) ax1.set_title('Before NMS') ax1.set_axis_off() ax1.imshow(orig_img, cmap='gray') ax2.set_title('After NMS')
def model_fusion(outpath, outfile): resultsa1, resultsa2, resultsa3, resultsa4 = results_resolve( model_path= "/root/data/gvision/my_merge/finalsubmission/fafafinal/det_results.json", weight=1) resultsb1, resultsb2, resultsb3, resultsb4 = results_resolve( model_path= "/root/data/gvision/my_merge/finalsubmission/final2/all.json", weight=0.6) results1 = resultsa1 + resultsb1 results2 = resultsa2 + resultsb2 results3 = resultsa3 + resultsb3 results4 = resultsa4 + resultsb4 # resultsa1,resultsa2,resultsa3,resultsa4=results_resolve(model_path="/root/data/gvision/my_merge/finalsubmission/fafafinal/det_results.json",weight=[1,1,1,1]) # resultsb1,resultsb2,resultsb3,resultsb4=results_resolve(model_path="/root/data/gvision/my_merge/finalsubmission/final2/all.json",weight=[0.3,0.3,0.3,0.3]) # resultsc1,resultsc2,resultsc3,resultsc4=results_resolve(model_path="/root/data/gvision/my_merge/finalsubmission/final2/all.json",weight=[0.3,0.3,0.3,0.3]) # results1=resultsa1+resultsb1+resultsc1 # results2=resultsa2+resultsb2+resultsc2 # results3=resultsa3+resultsb3+resultsc3 # results4=resultsa4+resultsb4+resultsc4 indexedresults = indexResults(results1) mergedresults = defaultdict(list) for (imageid, objlist) in indexedresults.items(): for objdict in objlist: mergedresults[imageid].append([ objdict['bbox'][0], objdict['bbox'][1], objdict['bbox'][2], objdict['bbox'][3], objdict['score'], objdict['category_id'] ]) objlist = mergedresults[imageid] # masxlist=[i[2]*i[3] for i in objlist] # max_wh=np.max(masxlist) # objlist=[[i[0],i[1],i[2],i[3],i[4]*0.05+i[3]*i[2]*0.95/max_wh,i[5],i[6]] for i in objlist ] keep = py_cpu_nms(np.array(objlist), 0.5) outdets = [] for index in keep: outdets.append(objlist[index]) mergedresults[imageid] = outdets savelist = [] def say2(iss): imageid, objlist = iss[0], iss[1] templist = [] for obj in objlist: #obj [22528, 1270, 24576, 1, 1.0, 4] templist.append({ "image_id": imageid, "category_id": obj[5], "bbox": obj[:4], # "bbox": tlbr2tlwh(obj[:4]), "score": obj[4] }) return templist executor = ThreadPoolExecutor(max_workers=80) func_var = [[file_name, dict_value] for file_name, dict_value in mergedresults.items()] print("fusion bbox into self'image start ") pbar2 = tqdm(total=len(mergedresults), ncols=50) for temp in executor.map(say2, func_var): savelist += temp pbar2.update(1) pbar2.close() with open(os.path.join(outpath, outfile), 'w') as f: dict_str = json.dumps(savelist, indent=2) f.write(dict_str) print(f"save ***results*** json :{os.path.join(outpath, outfile)}")
def test_model(self): saver = tf.train.Saver() _rois_coord = self.net.rois_coord[:, 1:5] #rois_coord = self.net.rois_coord _pred_box = self.net.bbox_pred _pred_score = self.net.cls_prob _pred_box_score_arg = tf.argmax(_pred_score, axis=1) dect_total_result = [[[] for i in range(cfg.img_save_num)] for j in range(self.net.num_classes)] test_data = [[] for i in range(cfg.img_save_num)] with tf.Session() as sess: saver.restore(sess, self.ckpt_filename) for i in range(cfg.img_save_num): print(i, ' image test compeleted') train_data = self.val_data.get( ) #if you want to change the test imgae, you can using cv2.imread() here to read your own image data test_data[i] = train_data image_height = np.array(train_data['image'].shape[1]) image_width = np.array(train_data['image'].shape[2]) feed_dict = {self.net.image: train_data['image'], self.net.image_width: image_width,\ self.net.image_height: image_height} rois_coord, pred_box, pred_box_score_arg, pred_score= sess.run([_rois_coord, _pred_box, _pred_box_score_arg, _pred_score],\ feed_dict=feed_dict) pred_box_score_arg = pred_box_score_arg.astype(np.int32) num_pred = pred_box_score_arg.shape[0] pred_box_gather = np.empty([num_pred, 4], dtype=np.float32) pred_score_gather = np.empty(num_pred) for j in range(num_pred): pred_box_gather[j, :] = pred_box[j, 4 * pred_box_score_arg[j]:4 * (pred_box_score_arg[j] + 1)] pred_score_gather[j] = pred_score[j, pred_box_score_arg[j]] pred_box_gather = pred_box_gather * np.array( cfg.bbox_nor_stdv) + np.array(cfg.bbox_nor_mean) pre_box_coord = self.coord_transform_inv( rois_coord, pred_box_gather.astype(np.float32)) pre_box_coord = pre_box_coord for k in range(1, self.net.num_classes): pre_class_arg = np.where(pred_box_score_arg == k)[0] cls_pred_box_coord = pre_box_coord[pre_class_arg, :] cls_pred_score = pred_score_gather[pre_class_arg] #print(cls_pred_box_coord.shape, cls_pred_score.shape) cls_pred_score = cls_pred_score[:, np.newaxis] cls_pred_target = np.concatenate( (cls_pred_box_coord, cls_pred_score), axis=1) keep = py_cpu_nms(cls_pred_target, cfg.test_nms_thresh) cls_pred_target = cls_pred_target[keep, :] dect_total_result[k][i] = cls_pred_target for k in range(cfg.img_save_num): imname = test_data[k]['imname'] im_scale = test_data[k]['scale'] image = cv2.imread(imname) im = cv2.resize(image, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) im = self.draw_result(im, dect_total_result, k) if not os.path.exists(self.image_output_dir): os.mkdir(self.image_output_dir) im_save_path = os.path.join(self.image_output_dir, '{:d}'.format(k) + '.jpg') cv2.imwrite(im_save_path, im) cv2.imshow('Image', im) cv2.waitKey(0)