deep-hight-relolution-net.pytorch训练自己的关键点检测数据步骤

本文主要是介绍deep-hight-relolution-net.pytorch训练自己的关键点检测数据步骤,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  1. 数据集准备
  2. 标注转为coco格式
  3.  部署源码(https://github.com/HuangJunJie2017/UDP-Pose/tree/master/deep-high-resolution-net.pytorchttps://github.com/HuangJunJie2017/UDP-Pose/tree/master/deep-high-resolution-net.pytorch

  4. 修改代码适配自己的数据集
  5. 修改yaml文件
  6. 开始训练

1、labelme进行数据标注

2、分别是data数据组织形式 annotations和trainks文件里的存放形式,.json参考keypoints_coco格式注意area等计算正确

 3、部署时注意pytorch gpu版本的安装 conda安装很慢可以离线下载安装

torchvision-0.8.2+cu110-cp36-cp36m-linux_x86_64.whl

torch-1.7.1+cu110-cp36-cp36m-linux_x86_64.whl

cudnn-11.0-linux-x64-v8.0.2.39.tgz

系统我使用的是ubuntu20.04 cuda 11.0 cudnn注意nvidia显卡驱动等安装就绪

下载地址:https://download.pytorch.org/whl/cu110/torch_stable.html

4、源码修改涉及deep-hight-relolution-net.pytorch和pycocotools的修改

cocoeval.py 修改 523行self.kpt_oks_sigmas为自己数据集的,我的是10个点的标注所以长度为10, oks_sigmas的计算如下:

self.kpt_oks_sigmas = np.array([0.049122907658985714, 0.10621929450191409, 0.11158566108333368, 0.09565673656561206, 0.15358307377967345, 0.058919262650392636, 0.13980433515716498, 0.10966226101729055, 0.00814148481761285, 0.154930312361198])
import json
import os
import collections
import numpy as np
oks = []
points = collections.defaultdict(list)
file_path = r'datasets\hand_keypoints_10\annotations'
mean_label_points[label])**2) / area) ** 0.5
points_number = 10
for json_file in os.listdir(file_path):if not json_file.endswith('json'):continuewith open(os.path.join(file_path, json_file),'r',encoding='utf8')as fp:json_data = json.load(fp)# print('这是文件中的json数据:',json_data)print('这是读取到文件数据的数据类型:', type(json_data))shapes = json_data['annotations']for shape in shapes:for i in range(points_number):if shape['keypoints'][i*3: i * 3 + 3] != [0, 0, 0]:points[i].append(shape['keypoints'][i*3: i * 3 + 3])
print(points)
mean_label_points = collections.defaultdict(float)
for label, point in points.items():mean_label_points[label] = sum(np.array(point)) / len(point)
print(mean_label_points)
area = 400 * 400
sigmas_label_points = collections.defaultdict(float)
# sigmas_label_points[label] = 0
for label, point in points.items():sigmas_label_points[label] = 0for p in point:sigmas_label_points[label] += (sum((np.array(p) - mean_label_points[label])**2) / area) ** 0.5sigmas_label_points[label] = sigmas_label_points[label] / len(p)
print(sigmas_label_points.values())

pycocotools coco.py

   def loadRes(self, resFile):........# annsImgIds = [ann['image_id'] for ann in anns]annsImgIds = [ann['id'] for ann in res.dataset['images']]assert set(annsImgIds) == (set(annsImgIds) & set(self.getImgIds())), \'Results do not correspond to current coco set'elif 'keypoints' in anns[0]:res.dataset['categories'] = copy.deepcopy(self.dataset['categories'])for id, ann in enumerate(anns):s = ann['keypoints']x = s[0::3]y = s[1::3]x0,x1,y0,y1 = np.min(x), np.max(x), np.min(y), np.max(y)ann['area'] = (x1-x0)*(y1-y0)ann['id'] = id + 1ann['image_id'] = annsImgIds[id]ann['bbox'] = [x0,y0,x1-x0,y1-y0]print('DONE (t={:0.2f}s)'.format(time.time()- tic))res.dataset['annotations'] = annsres.createIndex()return res

 deep-hight-relolution-net.pytorch lib dataset coco.py

def _load_coco_keypoint_annotation_kernal(self, index):filename = im_ann['file_name']filename = os.path.join(self.root, 'trainks', filename)rec.append({# 'image': self.image_path_from_index(index),'image': filename,'center': center,'scale': scale,'joints_3d': joints_3d,'joints_3d_vis': joints_3d_vis,'filename': '','imgnum': 0,})

  deep-hight-relolution-net.pytorch lib dataset coco.py

init部分的修改:# load image file namesself.image_set_index = self._load_image_set_index()self.num_images = len(self.image_set_index)logger.info('=> num_images: {}'.format(self.num_images))self.num_joints = 10# self.flip_pairs = [[1, 2], [3, 4], [5, 6], [7, 8],#                    [0, 1], [2, 3], [4, 5], [8, 9]]self.flip_pairs = []self.parent_ids = Noneself.upper_body_ids = (0, 1, 2, 3, 4, 5, 6, 7, 8,9, 10)self.lower_body_ids = ()self.joints_weight = np.array([1., 1., 1., 1., 1., 1., 1.5, 1.5, 1.5,1.5],dtype=np.float32).reshape((self.num_joints, 1))
    def evaluate(self, cfg, preds, output_dir, all_boxes, img_path,*args, **kwargs):rank = cfg.RANKres_folder = os.path.join(output_dir, 'results')if not os.path.exists(res_folder):try:os.makedirs(res_folder)except Exception:logger.error('Fail to make {}'.format(res_folder))res_file = os.path.join(res_folder, 'keypoints_{}_results_{}.json'.format(self.image_set, rank))# person x (keypoints)_kpts = []for idx, kpt in enumerate(preds):_kpts.append({'keypoints': kpt,'center': all_boxes[idx][0:2],'scale': all_boxes[idx][2:4],'area': all_boxes[idx][4],'score': all_boxes[idx][5],'image': img_path[idx].split(".jpg")[0]})

根据我的数据特性不需要使用flip

FLIP_TEST FLIP: False 两个变量都设置为false 
NUM_JOINTS

experiments coco hrnet yaml修改为:

AUTO_RESUME: true
CUDNN:BENCHMARK: trueDETERMINISTIC: falseENABLED: true
DATA_DIR: ''
GPUS: (0,)
OUTPUT_DIR: 'output'
LOG_DIR: 'log'
WORKERS: 0
PRINT_FREQ: 100DATASET:COLOR_RGB: trueDATASET: 'mycoco'DATA_FORMAT: jpgFLIP: FalseNUM_JOINTS_HALF_BODY: 10PROB_HALF_BODY: 0.0ROOT: 'data/hand_keypoints_10/'ROT_FACTOR: 45SCALE_FACTOR: 0.35TEST_SET: 'valks'TRAIN_SET: 'trainks'
MODEL:INIT_WEIGHTS: trueNAME: pose_hrnetNUM_JOINTS: 10PRETRAINED: 'models/pytorch/imagenet/hrnet_w32-36af842e.pth'TARGET_TYPE: gaussianIMAGE_SIZE:- 192- 256HEATMAP_SIZE:- 48- 64SIGMA: 2EXTRA:PRETRAINED_LAYERS:- 'conv1'- 'bn1'- 'conv2'- 'bn2'- 'layer1'- 'transition1'- 'stage2'- 'transition2'- 'stage3'- 'transition3'- 'stage4'FINAL_CONV_KERNEL: 1STAGE2:NUM_MODULES: 1NUM_BRANCHES: 2BLOCK: BASICNUM_BLOCKS:- 4- 4NUM_CHANNELS:- 32- 64FUSE_METHOD: SUMSTAGE3:NUM_MODULES: 4NUM_BRANCHES: 3BLOCK: BASICNUM_BLOCKS:- 4- 4- 4NUM_CHANNELS:- 32- 64- 128FUSE_METHOD: SUMSTAGE4:NUM_MODULES: 3NUM_BRANCHES: 4BLOCK: BASICNUM_BLOCKS:- 4- 4- 4- 4NUM_CHANNELS:- 32- 64- 128- 256FUSE_METHOD: SUM
LOSS:USE_TARGET_WEIGHT: true
TRAIN:BATCH_SIZE_PER_GPU: 128SHUFFLE: trueBEGIN_EPOCH: 0END_EPOCH: 2100OPTIMIZER: adamLR: 0.001LR_FACTOR: 0.1LR_STEP:- 170- 200WD: 0.0001GAMMA1: 0.99GAMMA2: 0.0MOMENTUM: 0.9NESTEROV: false
TEST:BATCH_SIZE_PER_GPU: 4COCO_BBOX_FILE: 'data/coco/person_detection_results/COCO_val2017_detections_AP_H_56_person.json'BBOX_THRE: 1.0IMAGE_THRE: 0.0IN_VIS_THRE: 0.2MODEL_FILE: ''NMS_THRE: 1.0OKS_THRE: 0.2USE_GT_BBOX: trueFLIP_TEST: FalsePOST_PROCESS: trueSHIFT_HEATMAP: trueSOFT_NMS: False
DEBUG:DEBUG: trueSAVE_BATCH_IMAGES_GT: trueSAVE_BATCH_IMAGES_PRED: trueSAVE_HEATMAPS_GT: trueSAVE_HEATMAPS_PRED: true

lib nums nums.py

def oks_iou(g, d, a_g, a_d, sigmas=None, in_vis_thre=None):if not isinstance(sigmas, np.ndarray):sigmas = np.array([.26, .25, .25, .35, .35, .79, .79, .72, .72, .62, .62, 1.07, 1.07, .87, .87, .89, .89]) / 10.0.......return ious

注意 训练的时候作者提供的是多GPU模型单GPU测试结果可能是错的。自己训练模型测试是OK的。

这篇关于deep-hight-relolution-net.pytorch训练自己的关键点检测数据步骤的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1074593

相关文章

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

Python pip下载包及所有依赖到指定文件夹的步骤说明

《Pythonpip下载包及所有依赖到指定文件夹的步骤说明》为了方便开发和部署,我们常常需要将Python项目所依赖的第三方包导出到本地文件夹中,:本文主要介绍Pythonpip下载包及所有依... 目录步骤说明命令格式示例参数说明离线安装方法注意事项总结要使用pip下载包及其所有依赖到指定文件夹,请按照以

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

java对接海康摄像头的完整步骤记录

《java对接海康摄像头的完整步骤记录》在Java中调用海康威视摄像头通常需要使用海康威视提供的SDK,下面这篇文章主要给大家介绍了关于java对接海康摄像头的完整步骤,文中通过代码介绍的非常详细,需... 目录一、开发环境准备二、实现Java调用设备接口(一)加载动态链接库(二)结构体、接口重定义1.类型

SpringBoot3中使用虚拟线程的完整步骤

《SpringBoot3中使用虚拟线程的完整步骤》在SpringBoot3中使用Java21+的虚拟线程(VirtualThreads)可以显著提升I/O密集型应用的并发能力,这篇文章为大家介绍了详细... 目录1. 环境准备2. 配置虚拟线程方式一:全局启用虚拟线程(Tomcat/Jetty)方式二:异步

使用Python实现base64字符串与图片互转的详细步骤

《使用Python实现base64字符串与图片互转的详细步骤》要将一个Base64编码的字符串转换为图片文件并保存下来,可以使用Python的base64模块来实现,这一过程包括解码Base64字符串... 目录1. 图片编码为 Base64 字符串2. Base64 字符串解码为图片文件3. 示例使用注意