python 网站开发流程图,百度搜索推广技巧,长沙网站建设大全,网站建设对企业品牌价值提升的影响CenterNet模型推理部分解析
CenterNet官方代码环境部署 CenterNet作为2019年CVPR推出的论文#xff0c;论文中给出了官方代码所在的github仓库地址。https://github.com/xingyizhou/CenterNet。 整个代码的代码量并不是特别大#xff0c;但整个项目的难点在于使用了老版本的…CenterNet模型推理部分解析
CenterNet官方代码环境部署 CenterNet作为2019年CVPR推出的论文论文中给出了官方代码所在的github仓库地址。https://github.com/xingyizhou/CenterNet。 整个代码的代码量并不是特别大但整个项目的难点在于使用了老版本的pytorch1.0与一些依赖的资源库从而导致了整个项目在启动和加载时或产生很多的错误。
GPU版本安装问题 在官方代码的Readme文件夹的下面给出了安装步骤的文件。首先文件中说明了当时使用的时python3.6版本。不建议使用python3.6版本 自己使用的conda激活虚拟环境后使用的是3.8版本。 对出错的部分没有进行截图因此只是简单的进行一定的陈述。
错误一使用pip来安装CUDA的pytorch版本我在安装的时候发现如果使用pip来安装torch会安装的是torchcu这种版本。而使用conda安装的话会安装一些CUDA相关的依赖 而在安装dcnv2的过程中需要CUDA相关库的支持。所以pytorch要使用conda来进行安装。 最好之间安装Gpu的版本 gpu.c相关文件也需要编译而且容易报错。 DCNV2文件对torch版本的不支持自己当时使用git切换conda的虚拟环境安装.make.sh文件时候报错需要使用最新的DCNV2库卸载之后重新安装替代。 踩坑的地方在启动setup.py文件之后可能会产生两个错误第一个是因为缺少c的编译环境缺少支持库的错误。第二个是说c库的版本过高不支持 网上查的原因是因为CUDA更新比较慢而vistual Studio的更新过快。我自己将2022的版本降为2019年的启动成功。 一定要下载nvcc完全对应的版本我自己的是12.1但当时安装习惯安装的是11.8nvcc会报错 总结我个人使用的是pytorch2.3.0的版本来进行安装的如果安装过程中出现错误可以联系博主简单交流。
模型推理部分代码
检测部分的启动参数示例 ctdet –demo …/images/17790319373_bd19b24cfc_k.jpg –load_model …/models/ctdet_coco_dla_2x.pth 启动部分会产生一个模型下载失败的错误将模型自己手动的下载到C盘的cache-torch…文件下面即可以启动模型成功。
自己断点调试报错的一部分主要是网络错误作用是加载预训练的一部分权重数据。 和Yolo一样也是可以检测视频文件的。 断点调试分析 推理过程整体的执行流程图与代码 def demo(opt):os.environ[CUDA_VISIBLE_DEVICES] opt.gpus_str #指定GPU对哪些环境变量可用opt.debug max(opt.debug, 1) # 设置debug属性便于进行调试Detector detector_factory[opt.task]detector Detector(opt)if opt.demo webcam or \opt.demo[opt.demo.rfind(.) 1:].lower() in video_ext:cam cv2.VideoCapture(0 if opt.demo webcam else opt.demo)detector.pause Falsewhile True:_, img cam.read()cv2.imshow(input, img)ret detector.run(img)time_str for stat in time_stats:time_str time_str {} {:.3f}s |.format(stat, ret[stat])print(time_str)if cv2.waitKey(1) 27:return # esc to quitelse:if os.path.isdir(opt.demo): # 如果参数中的待检测数据是文件夹image_names []ls os.listdir(opt.demo)for file_name in sorted(ls):ext file_name[file_name.rfind(.) 1:].lower()if ext in image_ext:image_names.append(os.path.join(opt.demo, file_name))else: # 普通文件image_names [opt.demo] #获取文件名for (image_name) in image_names:ret detector.run(image_name) # 通过检测网络执行推理(核心)time_str for stat in time_stats:time_str time_str {} {:.3f}s |.format(stat, ret[stat])print(time_str)核心推理函数的细化分析 run()函数整体的部分 def run(self, image_or_path_or_tensor, metaNone):load_time, pre_time, net_time, dec_time, post_time 0, 0, 0, 0, 0 #时间变量初始化merge_time, tot_time 0, 0debugger Debugger(datasetself.opt.dataset, ipynb(self.opt.debug3), # Debugger便于调试与错误诊断themeself.opt.debugger_theme)start_time time.time()pre_processed Falseif isinstance(image_or_path_or_tensor, np.ndarray):image image_or_path_or_tensorelif type(image_or_path_or_tensor) type (): image cv2.imread(image_or_path_or_tensor) # 通过opencv读取图片else:image image_or_path_or_tensor[image][0].numpy()pre_processed_images image_or_path_or_tensorpre_processed Trueloaded_time time.time()load_time (loaded_time - start_time)detections [] # 检测列表for scale in self.scales:scale_start_time time.time()if not pre_processed:images, meta self.pre_process(image, scale, meta) # 执行图片的预处理else:# import pdb; pdb.set_trace()images pre_processed_images[images][scale][0]meta pre_processed_images[meta][scale]meta {k: v.numpy()[0] for k, v in meta.items()}images images.to(self.opt.device) # 将image张量移动到GPU上torch.cuda.synchronize() # 确保CUDA操作完成pre_process_time time.time()pre_time pre_process_time - scale_start_timeoutput, dets, forward_time self.process(images, return_timeTrue)torch.cuda.synchronize()net_time forward_time - pre_process_time # 根据之前的函数计算出推理解码等一系列的时间decode_time time.time()dec_time decode_time - forward_timeif self.opt.debug 2:self.debug(debugger, images, dets, output, scale)dets self.post_process(dets, meta, scale) # 执行后处理的过程(得到80个类别的列表信息部分含有数据)torch.cuda.synchronize()post_process_time time.time()post_time post_process_time - decode_time # 计算后处理时间detections.append(dets)results self.merge_outputs(detections)torch.cuda.synchronize()end_time time.time()merge_time end_time - post_process_timetot_time end_time - start_timeif self.opt.debug 1:self.show_results(debugger, image, results) # 调用opencv函数显示结果return {results: results, tot: tot_time, load: load_time, # 放入所有的信息进行返回pre: pre_time, net: net_time, dec: dec_time,post: post_time, merge: merge_time}预处理函数的执行部分 def pre_process(self, image, scale, metaNone):height, width image.shape[0:2]new_height int(height * scale)new_width int(width * scale)if self.opt.fix_res: # 转为输入大小512 x 512inp_height, inp_width self.opt.input_h, self.opt.input_wc np.array([new_width / 2., new_height / 2.], dtypenp.float32)s max(height, width) * 1.0else:inp_height (new_height | self.opt.pad) 1inp_width (new_width | self.opt.pad) 1c np.array([new_width // 2, new_height // 2], dtypenp.float32)s np.array([inp_width, inp_height], dtypenp.float32)trans_input get_affine_transform(c, s, 0, [inp_width, inp_height])# 获取仿射变换矩阵resized_image cv2.resize(image, (new_width, new_height))# 读取图片裁剪inp_image cv2.warpAffine(resized_image, trans_input, (inp_width, inp_height),flagscv2.INTER_LINEAR) # 应用之前的仿射矩阵进行仿射变换inp_image ((inp_image / 255. - self.mean) / self.std).astype(np.float32) # 归一化处理images inp_image.transpose(2, 0, 1).reshape(1, 3, inp_height, inp_width)if self.opt.flip_test:images np.concatenate((images, images[:, :, :, ::-1]), axis0)images torch.from_numpy(images) # 转为张量的格式meta {c: c, s: s, out_height: inp_height // self.opt.down_ratio, # opt.down_ratio:下采样倍率out_width: inp_width // self.opt.down_ratio} # 储存c:中心点坐标 s:缩放因子return images, meta # 128x128(下采样4倍的特征图)解码扩增部分对应的代码信息 def ctdet_decode(heat, wh, regNone, cat_spec_whFalse, K100):batch, cat, height, width heat.size() # cat:表示类别的数目# heat torch.sigmoid(heat)# perform nms on heatmapsheat _nms(heat) # maxpooling(3x3)类似nms算法# scores得分最高的 K 个点的得分。# inds得分最高的 K 个点的索引。# clses得分最高的 K 个点对应的类别。# ys得分最高的 K 个点的 y 坐标。# xs得分最高的 K 个点的 x 坐标。scores, inds, clses, ys, xs _topk(heat, KK) # 在热力图 heat 上执行 Top-K 操作提取最高 K 个得分的点提取最高 K 个得分的点if reg is not None: # 位置微调的过程reg _transpose_and_gather_feat(reg, inds) # 将回归参数 reg 按照 indsTop-K 操作返回的索引重新排列以便收集与 Top-K 操作选择的热力图位置相对应的回归参数reg reg.view(batch, K, 2) # 收集到的回归参数 reg 调整为形状 [batch, K, 2] 的张量其中 batch 是批次大小K 是 Top-K 操作选择的点的数量2 表示每个点的两个回归参数通常是一个点的 x 和 y 方向的偏移量xs xs.view(batch, K, 1) reg[:, :, 0:1] # x坐标与回归偏移量的坐标相加ys ys.view(batch, K, 1) reg[:, :, 1:2]else:xs xs.view(batch, K, 1) 0.5ys ys.view(batch, K, 1) 0.5wh _transpose_and_gather_feat(wh, inds) # 特征图的维度从 [batch, num_anchors, height, width] 转换为 [batch, K, num_anchors]if cat_spec_wh: # 是否每个类别有特定的宽度和高度预测wh wh.view(batch, K, cat, 2) # wh 张量的形状调整为 [batch, K, num_classes, 2]clses_ind clses.view(batch, K, 1, 1).expand(batch, K, 1, 2).long() # 创建一个索引张量用于从 wh 中收集特定类别的宽度和高度wh wh.gather(2, clses_ind).view(batch, K, 2) # 得到结果值else:wh wh.view(batch, K, 2)clses clses.view(batch, K, 1).float()scores scores.view(batch, K, 1)bboxes torch.cat([xs - wh[..., 0:1] / 2, # torch.cat 函数将四个坐标点左上角和右下角拼接成一个检测框的坐标张量ys - wh[..., 1:2] / 2,xs wh[..., 0:1] / 2, # xs wh[..., 0:1] / 2 和 ys wh[..., 1:2] / 2计算右下角的坐标。ys wh[..., 1:2] / 2], dim2) # xs - wh[..., 0:1] / 2 和 ys - wh[..., 1:2] / 2计算左上角的坐标。detections torch.cat([bboxes, scores, clses], dim2)return detections # 连接之后返回检测框结果(论文中提到的扩增的过程)后处理函数的执行部分 def post_process(self, dets, meta, scale1):dets dets.detach().cpu().numpy() # 转为numpy格式进入cpu进行计算dets dets.reshape(1, -1, dets.shape[2])dets ctdet_post_process( # 尺度变换、坐标转换、非极大值抑制NMS 传入的meta[c]中心点 meta[s]缩放尺度dets.copy(), [meta[c]], [meta[s]],meta[out_height], meta[out_width], self.opt.num_classes)for j in range(1, self.num_classes 1): # 后处理转换类型dets[0][j] np.array(dets[0][j], dtypenp.float32).reshape(-1, 5)dets[0][j][:, :4] / scalereturn dets[0]剩余的部分函数不在进行说明了但模型推理代码的复现流程主要还是需要按照我自己绘制的流程图进行debug 文章转载自: http://www.morning.ydxg.cn.gov.cn.ydxg.cn http://www.morning.prsxj.cn.gov.cn.prsxj.cn http://www.morning.rntby.cn.gov.cn.rntby.cn http://www.morning.sfswj.cn.gov.cn.sfswj.cn http://www.morning.mzcsp.cn.gov.cn.mzcsp.cn http://www.morning.lhhdy.cn.gov.cn.lhhdy.cn http://www.morning.kjcll.cn.gov.cn.kjcll.cn http://www.morning.xnflx.cn.gov.cn.xnflx.cn http://www.morning.srmdr.cn.gov.cn.srmdr.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.tkcct.cn.gov.cn.tkcct.cn http://www.morning.gftnx.cn.gov.cn.gftnx.cn http://www.morning.bpxmw.cn.gov.cn.bpxmw.cn http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn http://www.morning.jxlnr.cn.gov.cn.jxlnr.cn http://www.morning.c7512.cn.gov.cn.c7512.cn http://www.morning.snygg.cn.gov.cn.snygg.cn http://www.morning.yggdq.cn.gov.cn.yggdq.cn http://www.morning.kstlm.cn.gov.cn.kstlm.cn http://www.morning.hslgq.cn.gov.cn.hslgq.cn http://www.morning.trhlb.cn.gov.cn.trhlb.cn http://www.morning.nclps.cn.gov.cn.nclps.cn http://www.morning.xqcst.cn.gov.cn.xqcst.cn http://www.morning.dpqqg.cn.gov.cn.dpqqg.cn http://www.morning.rbkgp.cn.gov.cn.rbkgp.cn http://www.morning.njhyk.cn.gov.cn.njhyk.cn http://www.morning.mdpcz.cn.gov.cn.mdpcz.cn http://www.morning.lsjtq.cn.gov.cn.lsjtq.cn http://www.morning.bsqbg.cn.gov.cn.bsqbg.cn http://www.morning.ydtdn.cn.gov.cn.ydtdn.cn http://www.morning.mlnby.cn.gov.cn.mlnby.cn http://www.morning.cbnlg.cn.gov.cn.cbnlg.cn http://www.morning.clndl.cn.gov.cn.clndl.cn http://www.morning.csnmd.cn.gov.cn.csnmd.cn http://www.morning.fkcjs.cn.gov.cn.fkcjs.cn http://www.morning.mlfgx.cn.gov.cn.mlfgx.cn http://www.morning.lmhwm.cn.gov.cn.lmhwm.cn http://www.morning.yhglt.cn.gov.cn.yhglt.cn http://www.morning.rxlck.cn.gov.cn.rxlck.cn http://www.morning.kqkmx.cn.gov.cn.kqkmx.cn http://www.morning.mlpmf.cn.gov.cn.mlpmf.cn http://www.morning.stbhn.cn.gov.cn.stbhn.cn http://www.morning.lfcfn.cn.gov.cn.lfcfn.cn http://www.morning.nqyzg.cn.gov.cn.nqyzg.cn http://www.morning.rlns.cn.gov.cn.rlns.cn http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.kmqms.cn.gov.cn.kmqms.cn http://www.morning.ynbyk.cn.gov.cn.ynbyk.cn http://www.morning.zqdhr.cn.gov.cn.zqdhr.cn http://www.morning.gtxrw.cn.gov.cn.gtxrw.cn http://www.morning.ktmbp.cn.gov.cn.ktmbp.cn http://www.morning.bnrff.cn.gov.cn.bnrff.cn http://www.morning.pmptm.cn.gov.cn.pmptm.cn http://www.morning.cwjsz.cn.gov.cn.cwjsz.cn http://www.morning.pngfx.cn.gov.cn.pngfx.cn http://www.morning.nyplp.cn.gov.cn.nyplp.cn http://www.morning.rkkpr.cn.gov.cn.rkkpr.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.rxkq.cn.gov.cn.rxkq.cn http://www.morning.nxfwf.cn.gov.cn.nxfwf.cn http://www.morning.tpps.cn.gov.cn.tpps.cn http://www.morning.qnsmk.cn.gov.cn.qnsmk.cn http://www.morning.fbbmg.cn.gov.cn.fbbmg.cn http://www.morning.chfxz.cn.gov.cn.chfxz.cn http://www.morning.rywn.cn.gov.cn.rywn.cn http://www.morning.plznfnh.cn.gov.cn.plznfnh.cn http://www.morning.brzlp.cn.gov.cn.brzlp.cn http://www.morning.tynqy.cn.gov.cn.tynqy.cn http://www.morning.pfgln.cn.gov.cn.pfgln.cn http://www.morning.lmfmd.cn.gov.cn.lmfmd.cn http://www.morning.ymjgx.cn.gov.cn.ymjgx.cn http://www.morning.gstmn.cn.gov.cn.gstmn.cn http://www.morning.thlzt.cn.gov.cn.thlzt.cn http://www.morning.yysqz.cn.gov.cn.yysqz.cn http://www.morning.bhjyh.cn.gov.cn.bhjyh.cn http://www.morning.mprky.cn.gov.cn.mprky.cn http://www.morning.pwrkl.cn.gov.cn.pwrkl.cn http://www.morning.xnnpy.cn.gov.cn.xnnpy.cn http://www.morning.tkrdg.cn.gov.cn.tkrdg.cn http://www.morning.wdxr.cn.gov.cn.wdxr.cn