def post_process(opt, dets, meta): dets = dets.detach().cpu().numpy() dets = dets.reshape(1, -1, dets.shape[2]) dets = ctdet_post_process(dets.copy(), [meta['c']], [meta['s']], meta['out_height'], meta['out_width'], opt.num_classes) for j in range(1, opt.num_classes + 1): dets[0][j] = np.array(dets[0][j], dtype=np.float32).reshape(-1, 5) return dets[0]
def save_result1(self, outputs, batch, results): # 这里的outputs包含model输出的所有特征图(3个特征图) # 对每一个output特征图都计算dets_out,然后将3个dets_out整合起来,根据score来排序,然后用NMS或者其他方法来过滤!!! dets_list = [] dets_score_list = [] for idx in range(len(outputs)): output = outputs[idx] reg = output['reg'] if self.opt.reg_offset else None dets = ctdet_decode(output['hm'], output['wh'], reg=reg, cat_spec_wh=self.opt.cat_spec_wh, K=self.opt.K) dets = dets.detach().cpu().numpy().reshape(1, -1, dets.shape[2]) # print(type(dets), dets.shape) dets_list.append(dets) dets_300 = np.concatenate( (dets_list[0], dets_list[1], dets_list[2]), axis=1) # 将3个dets:array[1,100,6]连接起来成为dets_300:array[1,300,6] dets_300_sort = dets_300.copy() array_for_sort = dets_300.reshape( -1, dets.shape[2])[:, :5] # 将dets_300的最后一列改变成score,方便下面排序 sort_idx = np.lexsort( -array_for_sort.T) # sort_idx是dets_300按照score从大到小排序的索引 # print(array_for_sort.shape, sort_idx) for key, item in enumerate(sort_idx): dets_300_sort[0][key] = dets_300[0][ item] # 将排序后的numpy数组保存到 dets_300_sort # print(dets_300_sort.shape, dets_300_sort[0][0][4],dets_300_sort[0][101][4]) # dets_300_sort = dets_300_sort[:,:100,:] # 检测结果只取前100?? dets = dets_300_sort # 这个命名只是为了不改下面的两个语句中的变量名 dets_outs = ctdet_post_process(dets.copy(), batch['meta']['c'].cpu().numpy(), batch['meta']['s'].cpu().numpy(), output['hm'].shape[2], output['hm'].shape[3], output['hm'].shape[1]) for j in range(1, self.opt.num_classes + 1): # 给数组reshape一下,要不然不符合NMS需要输入2维数组的输入要求 dets_outs[0][j] = np.array(dets_outs[0][j], dtype=np.float32).reshape(-1, 5) # print(type(dets_outs),len(dets_outs[0])) results_nms = {} for j in range(1, self.opt.num_classes + 1): results_nms[j] = np.concatenate( [dets_out[j] for dets_out in dets_outs], axis=0).astype(np.float32) # print(j, results_nms[j]) soft_nms(results_nms[j], Nt=0.5, method=2) # print(111, type(dets_outs[0]), dets_outs[0].keys()) # print(222, type(results_nms), results_nms.keys()) # results_nms是dets_outs[0]经过NMS之后得到的结果 # results[batch['meta']['img_id'].cpu().numpy()[0]] = dets_outs[0] results[batch['meta']['img_id'].cpu().numpy()[0]] = results_nms
def save_result(self, output, batch, results): reg = output['reg'] if self.opt.reg_offset else None dets = ctdet_decode( output['hm'], output['wh'], reg=reg, cat_spec_wh=self.opt.cat_spec_wh, K=self.opt.K) dets = dets.detach().cpu().numpy().reshape(1, -1, dets.shape[2]) dets_out = ctdet_post_process( dets.copy(), batch['meta']['c'].cpu().numpy(), batch['meta']['s'].cpu().numpy(), output['hm'].shape[2], output['hm'].shape[3], output['hm'].shape[1]) results[batch['meta']['img_id'].cpu().numpy()[0]] = dets_out[0]
def save_result(self, output, batch, results): output = output[-1] reg = output['reg'] if self.opt.reg_offset else None dets = ctdet_decode(output['hm'], output['wh'], reg=reg, cat_spec_wh=self.opt.cat_spec_wh, K=self.opt.K) # print(type(dets), dets.size()) #<class 'torch.Tensor'>, [1, 100, 6] ,其中的6代表了 [tlx, tly, brx, bry, scores, clses], 按照score的高低排序 dets = dets.detach().cpu().numpy().reshape(1, -1, dets.shape[2]) # dets = dets[:,:30,:] # print(type(dets), dets.shape) #<class 'numpy.ndarray'>, (1, 100, 6) dets_out = ctdet_post_process(dets.copy(), batch['meta']['c'].cpu().numpy(), batch['meta']['s'].cpu().numpy(), output['hm'].shape[2], output['hm'].shape[3], output['hm'].shape[1]) # print(len(dets_out), dets_out) # len(dets_out) = 1 ; dets_out = [{1: [...], 2: [], 3: [[130.917, 491.190, 285.635, 504.33, 0.797]], 4: [...],...}, 20: [...]] results[batch['meta']['img_id'].cpu().numpy()[0]] = dets_out[0]
def save_result(self, output, batch, results): # 能不能尽量不改这里面的小函数,只改变针对函数的输入????? # print(output['hm'].size(), output['wh'].size(), output['reg'].size()) # [1, 20, 128, 128] [1, 2, 128, 128] [1, 2, 128, 128] reg = output[ 'reg'] if self.opt.reg_offset else None # self.opt.reg_offset = True dets = ctdet_decode(output['hm'], output['wh'], reg=reg, cat_spec_wh=self.opt.cat_spec_wh, K=self.opt.K) # print(dets.size()) #[1, 100, 6] ,其中的6代表了 [tlx, tly, brx, bry, scores, clses], 按照score的高低排序 dets = dets.detach().cpu().numpy().reshape( 1, -1, dets.shape[2]) # detach()用来切断梯度的传播 # print(dets.shape) #(1, 100, 6) dets_out = ctdet_post_process(dets.copy(), batch['meta']['c'].cpu().numpy(), batch['meta']['s'].cpu().numpy(), output['hm'].shape[2], output['hm'].shape[3], output['hm'].shape[1]) # print(len(dets_out), dets_out) # len(dets_out) = 1 ; dets_out = [{1: [...], 2: [], 3: [[130.917, 491.190, 285.635, 504.33, 0.797]], 4: [...],...}, 20: [...]] results[batch['meta']['img_id'].cpu().numpy()[0]] = dets_out[0]