赞
踩
欢迎正在学习或者想学的CV的同学进群一起讨论与学习,v:Rex1586662742,q群:468713665

int main(){
//cmake和make是不同的路径
if(!BaiscTools::build_model("/home/rex/Desktop/tensorrt_learning/trt_cpp/workspace/centernet",1)){
return -1;
}
demoInfer demo;
string demo_name = "centernet_gpu";
demo.do_infer(demo_name);
return 0;
}
def decode_bbox(pred_hms, pred_whs, pred_offsets, confidence, cuda): b, c, output_h, output_w = pred_hms.shape detects = [] for batch in range(b): heat_map = pred_hms[batch].permute(1, 2, 0).view([-1, c]) pred_wh = pred_whs[batch].permute(1, 2, 0).view([-1, 2]) pred_offset = pred_offsets[batch].permute(1, 2, 0).view([-1, 2]) yv, xv = torch.meshgrid(torch.arange(0, output_h), torch.arange(0, output_w)) xv, yv = xv.flatten().float(), yv.flatten().float() if cuda: xv = xv.cuda() yv = yv.cuda() class_conf, class_pred = torch.max(heat_map, dim = -1) mask = class_conf > confidence print("mask.shape: ",mask.shape) pred_wh_mask = pred_wh[mask] pred_offset_mask = pred_offset[mask] if len(pred_wh_mask) == 0: detects.append([]) continue xv_mask = torch.unsqueeze(xv[mask] + pred_offset_mask[..., 0], -1) yv_mask = torch.unsqueeze(yv[mask] + pred_offset_mask[..., 1], -1) half_w, half_h = pred_wh_mask[..., 0:1] / 2, pred_wh_mask[..., 1:2] / 2 bboxes = torch.cat([xv_mask - half_w, yv_mask - half_h, xv_mask + half_w, yv_mask + half_h], dim=1) bboxes[:, [0, 2]] /= output_w bboxes[:, [1, 3]] /= output_h detect = torch.cat([bboxes, torch.unsqueeze(class_conf[mask],-1), torch.unsqueeze(class_pred[mask],-1).float()], dim=-1) detects.append(detect) return detects def postprocess(prediction, need_nms, image_shape, input_shape, letterbox_image, nms_thres=0.4): output = [None for _ in range(len(prediction))] for i, image_pred in enumerate(prediction): detections = prediction[i] if len(detections) == 0: continue unique_labels = detections[:, -1].cpu().unique() if detections.is_cuda: unique_labels = unique_labels.cuda() detections = detections.cuda() for c in unique_labels: detections_class = detections[detections[:, -1] == c] if need_nms: keep = nms( detections_class[:, :4], detections_class[:, 4], nms_thres ) max_detections = detections_class[keep] else: max_detections = detections_class output[i] = max_detections if output[i] is None else torch.cat((output[i], max_detections)) if output[i] is not None: output[i] = output[i].cpu().numpy() box_xy, box_wh = (output[i][:, 0:2] + output[i][:, 2:4])/2, output[i][:, 2:4] - output[i][:, 0:2] output[i][:, :4] = centernet_correct_boxes(box_xy, box_wh, input_shape, image_shape, letterbox_image) return output
pred = torch.from_numpy(pred.reshape(batch_size, 128 * 128, 24))[0]
pred_hms = pred[:, 0:20]
pred_whs = pred[:, 20:22]
pred_xys = pred[:, 22:]
keep = pred_hms.max(-1).values > 0.3
hms = pred_hms[keep]
whs = pred_whs[keep]
xys = pred_xys[keep]
#labels为 1~128*128里面的位置索引
scores, labels = torch.max(hms, 1)
#idx表示 在128*128中满足条件的 点的索引
idx = torch.nonzero(keep == True).squeeze()
score = hms.argmax()

#热力点坐标加上偏移量
xys[:, 0] += idx % 128
xys[:, 1] += idx / 128
#缩放回原图尺寸,因为当前特征图大小为128*128 ,原图大小为 image_o_size[0]*image_o_size[0]
left = (xys[:, 0] - whs[:, 0] / 2) * image_o_size[1] / 128
top = (xys[:, 1] - whs[:, 1] / 2) * image_o_size[0] / 128
right = (xys[:, 0] + whs[:, 0] / 2) * image_o_size[1] / 128
bottom = (xys[:, 1] + whs[:, 1] / 2) * image_o_size[0] / 128
bboxs = torch.stack((left, top, right, bottom), dim=1)
#nms
nms_keep = nms(bboxs, scores, iou_threshold=0.5)
bboxs = bboxs[nms_keep]
vector<bbox> boxes; /预测结果指针 float* prob = output->cpu<float>(); //类别数量 int num = 20; float *start = prob; //总元素个数 128*128*24 int count = output->count(); for(int i=0;i<count;i+=24){ //现在有128*128个点 就有128*128行,每行24个,前20个为类别,后四个为 w,h,x,y start = prob+i; //得分最高的点的类别 int label = max_element(start,start+num) - start; //d得分 float confidence = start[label]; if(confidence<0.3) continue; float w = start[ num]; float h = start[num+1]; //热力点的坐标加上偏移量 float x = start[num+2] + (i/24)%128; float y = start[num+3] + (i/24)/128; //恢复到原图尺寸 float left = (x - w * 0.5) /128 * img_h; float top = (y - h * 0.5) /128 * img_w; float right = (x + w * 0.5) /128 * img_h; float bottom = (y + h * 0.5) /128 * img_w; boxes.emplace_back(left, top, right, bottom, confidence, (float)label); //接下来是nms
static __global__ void decode_kernel( float* predict,int im_h,int im_w,int num_bboxes, int num_classes, float confidence_threshold, float* invert_affine_matrix, float* parray, int max_objects, int NUM_BOX_ELEMENT ){ int position = blockDim.x * blockIdx.x + threadIdx.x; if (position >= num_bboxes) return; // 每隔24个位置跳到下一行 float* pitem = predict + (4 + num_classes) * position; //pitem 前20个位置表示概率,后四个位置表示whxy int label = 0; //第一个位置的socre float* class_confidence = pitem; float confidence = *class_confidence; //找到20个位置中最大的score和label for(int i = 1; i < num_classes; ++i, ++class_confidence){ if(*class_confidence > confidence){ confidence = *class_confidence; label = i; } } if(confidence < confidence_threshold) return; int index = atomicAdd(parray, 1); if(index >= max_objects) return; float* pwhxy = pitem; float width = *(pwhxy+20); float height = *(pwhxy+21); //偏移量,还需要加上 当前热力点的坐标 float cx = *(pwhxy+22) + position%128; float cy = *(pwhxy+23) + position/128; float left = (cx - width * 0.5f) * im_h / 128; float top = (cy - height * 0.5f) * im_w / 128; float right = (cx + width * 0.5f) * im_h / 128; float bottom = (cy + height * 0.5f) * im_w / 128; // 第一个位置用来计数 // left, top, right, bottom, confidence, class, keepflag float* pout_item = parray + 1 + index * NUM_BOX_ELEMENT; *pout_item++ = left; *pout_item++ = top; *pout_item++ = right; *pout_item++ = bottom; *pout_item++ = confidence; *pout_item++ = label; *pout_item++ = 1; // 1 = keep, 0 = ignore }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。