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

相关文章

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则