DeepLabCut使用篇(三):训练自己的模型(1)

10 篇文章 0 订阅
7 篇文章 2 订阅

DeepLabCut使用篇(三):训练自己的模型(1)

0x00 开始之前的废话

刚刚发现上传顺序错了,为了不影响阅读,决定删了重新发一篇

DeepLabCut使用篇(二)中我们已经自己创建了一个项目并对数据进行了标记,接下来我将介绍如何训练已经标记好的数据,得到数据模型

不要忘了首先在ipython中定义一个配置文件的变量:

config_path='/home/houiin/DeepLabCut/houiin_dlc_empty/dlc_wy-houiin-2019-01-18/config.yaml'

标记完数据后的文件层级目录如下所示

├── config.yaml
├── dlc-models			#dlc模型文件夹为空
├── labeled-data
│   ├── CollectedData_Mackenzie.csv
│   ├── CollectedData_Mackenzie.h5
│   └── reachingvideo1
│       ├── CollectedData_Mackenzie.csv
│       ├── CollectedData_Mackenzie.h5
│       ├── img000.png
│       ├── img091.png
│       ├── img116.png
│      	├── ……这里省略一大堆图片文件名……
│       ├── img46.png
│       └── img90.png
├── training-datasets	#训练数据文件夹为空
└── videos
    └── reachingvideo1.avi

5 directories, 29 files

0x01 开始动手吧!

1. 检验标记数据

上一步标记后生成的一个*.csv*.h5是检验标记数据的重要文件

deeplabcut.check_labels(config_path)

确保找到的标记数据就是当前项目配置文件config_path的标记数据——不过一般不会出现问题。

check_labels之后自动生成reachingvideo1_labeled文件夹

check后生成的文件夹

reachingvideo1_labeled文件夹中的图片已经打上了标记点,直接打开图片就能看到

标记后生成的图像文件夹

`

2. 生成训练数据

deeplabcut.create_training_dataset(config_path,num_shuffles=1)

**num_shuffles=1 **意思就是创建一个训练子集。如果你想测试DeepLabCut的基准性能,可以把数字1改的更大一点以生成更多的训练集。

运行后在training-datasetsdlc-models下生成了iteration-0文件夹

文件目录结构如下:

/dlc-models
└── iteration-0
    └── dlc_wy一月18-trainset95shuffle1
        ├── test
        │   └── pose_cfg.yaml
        └── train
            └── pose_cfg.yaml

# -------------

/training-datasets
└── iteration-0
    └── UnaugmentedDataSet_dlc_wy一月18
        ├── CollectedData_Mackenzie.csv
        ├── CollectedData_Mackenzie.h5
        ├── dlc_wy_Mackenzie95shuffle1.mat
        └── Documentation_data-dlc_wy_95shuffle1.pickle

6 directories, 6 files

其中iteration-0中的CollectedData_Mackenzie.csv标记内容是和labeled-data/reachingvideo1/CollectedData_Mackenzie.csv是一致的

3. 开始训练模型

每次创建训练数据集的迭代都将创建一个.mat文件,该文件被特征检测器feature detectors.pickle包含有关训练数据集的元信息的文件所使用。

和绝大多数的学习框架一样,将会生成两个子集,一个train和一个test

在开始训练之前进入train文件夹,对pose_cfg.yaml修改训练参数–> 有关pose_cfg.yaml内参数的解释

开始训练

deeplabcut.train_network(config_path)
# 或者
deeplabcut.train_network(config_path,shuffle=1)

# 或者一下子带上所有的参数
deeplabcut.train_network(config_path,shuffle=1,trainingsetindex=0,gputouse=None,max_snapshots_to_keep=5,autotune=False,displayiters=None,saveiters=None)
官方对训练参数的解释:

Shuffles: list, optional -List of integers specifying the shuffle indices of the training dataset. The default is [1]

plotting: bool, optional -Plots the predictions on the train and test images. The default is False; if provided it must be either True or False

show_errors: bool, optional -Display train and test errors. The default is True

comparisonbodyparts: list of bodyparts, Default is all -The average error will be computed for those body parts only (Has to be a subset of the body parts).

gputouse: int, optional -Natural number indicating the number of your GPU (see number in nvidia-smi). If you do not have a GPU put None. See: https://nvidia.custhelp.com/app/answers/detail/a_id/3751/~/useful-nvidia-smi-queries

使用GPU进行训练

1) 从0开始训练

我使用GPU进行训练,首先查看本机已有的GPU编号

我等贫民百姓只有一个渣渣GTX1050,编号是0,就是它了

deeplabcut.train_network(config_path,gputouse=0,shuffle=1)

开始训练咯!!!!!!!!!!

2) 载入之前的训练过程 snapshot

找到之前训练生成的过程镜像(我实在不知道该如何描述这么个玩意,就是snapshot)

在训练目录的train文件夹中,找到checkpoint,里面到最后一项就是最后保存的保存点

找到pose_cfg.yaml,编辑,定位到init_weights参数,将冒号后面的内容修改为最后一个保存点点内容

例如:完整路径/snapshot-数字

init_weights: '/home/houiin/DeepLabCut/houiin_dlc_empty/dlc_wy-houiin-2019-01-18/dlc-models/iteration-0/dlc_wy一月18-trainset95shuffle1/train/snapshot-500000'

最后的snapshot-500000就是之前训练过程中保存的过程镜像文件

在log中可以看到

载入后训练的迭代仍然从0开始计数,损失率也不一定和保存之前相同,但是训练过程已经被载入进去了

MMathisLab : As we explained in several other issues, it does not start counting at the snapshot, it starts again at 0. Just as the first weights are also not “0”, it’s already trained on imagenet.

3)看看训练过程中的温度吧
watch -n 2 nvidia-smi
# 每隔2s刷新一次显卡数据

喜提冬日暖手神器一个~

也可以在显卡控制面板看到温度

如果你想看CPU温度的话可以使用 watch sensors 进行查看(需要安装lm-sensors

注意!

默认情况下TensorFlow会使用GPU几乎所有的显存。如果你想动态分配内存或者在多个GPU设备中进行自定义设置,请参见TensorFlow官方文档《使用 GPU》:https://www.tensorflow.org/guide/using_gpu

对应在DeepLabCut中的设置路径为:

非Anaconda环境下/deeplabcut/pose_estimation_tensorflow

Anaconda环境下:anaconda3/envs/你的环境名称/lib/python3.6/site-packages/deeplabcut/pose_estimation_tensorflow

0x03 有关pose_cfg.yaml内参数的解释

# 在终端刷新的迭代次数,默认是1000,改成10的话就是每迭代10次就在终端输出一次
display_iters: 1000

# 加载上次训练过程
init_weights: /home/houiin/DeepLabCut/houiin_dlc_empty/dlc_wy-houiin-2019-01-18/dlc-models/iteration-0/dlc_wy一月18-trainset95shuffle1/train/snapshot-250000

# 每迭代多少次保存一个过程snapshot快照,默认50000,个人觉得有点太大了
save_iters: 50000
附上两个配置文件
#第一份,来自example
all_joints:
- [0]
- [1]
- [2]
- [3]
all_joints_names: [hand, Finger1, Finger2, Joystick]
dataset: training-datasets/iteration-0/UnaugmentedDataSet_ReachingAug30/Reaching_Mackenzie95shuffle1.mat
display_iters: 10
global_scale: 0.8
init_weights: /home/houiin/anaconda3/envs/deeplabcut-py36/lib/python3.6/site-packages/deeplabcut/pose_estimation_tensorflow/models/pretrained/resnet_v1_50.ckpt
intermediate_supervision: false
intermediate_supervision_layer: 12
location_refinement: true
locref_huber_loss: true
locref_loss_weight: 0.05
locref_stdev: 7.2801
max_input_size: 1000
metadataset: training-datasets/iteration-0/UnaugmentedDataSet_ReachingAug30/Documentation_data-Reaching_95shuffle1.pickle
mirror: false
multi_step:
- [0.005, 10000]
- [0.02, 430000]
- [0.002, 730000]
- [0.001, 1030000]
net_type: resnet_50
num_joints: 4
pos_dist_thresh: 17
project_path: /home/houiin/DeepLabCut/houiin_dlc_empty/DeepLabCut/examples/Reaching-Mackenzie-2018-08-30
save_iters: 500
scale_jitter_lo: 0.5
scale_jitter_up: 1.5
# 第二份,我自己训练的参数--14小时迭代到250000,loss下降较慢
all_joints:
- [0]
- [1]
- [2]
- [3]
all_joints_names: [hand, Finger1, Finger2, Joystick]
bottomheight: 400
crop: true
cropratio: 0.4
dataset: "training-datasets/iteration-0/UnaugmentedDataSet_dlc_wy\u4E00\u670818/dlc_wy_Mackenzie95shuffle1.mat"
display_iters: 1000
global_scale: 0.8
init_weights: /home/houiin/DeepLabCut/houiin_dlc_empty/dlc_wy-houiin-2019-01-18/dlc-models/iteration-0/dlc_wy一月18-trainset95shuffle1/train/snapshot-250000
intermediate_supervision: false
intermediate_supervision_layer: 12
leftwidth: 400
location_refinement: true
locref_huber_loss: true
locref_loss_weight: 0.05
locref_stdev: 7.2801
max_input_size: 1000
metadataset: "training-datasets/iteration-0/UnaugmentedDataSet_dlc_wy\u4E00\u6708\
  18/Documentation_data-dlc_wy_95shuffle1.pickle"
minsize: 100
mirror: false
multi_step:
- [0.005, 10000]
- [0.02, 430000]
- [0.002, 730000]
- [0.001, 1030000]
net_type: resnet_50
num_joints: 4
pos_dist_thresh: 17
project_path: /home/houiin/DeepLabCut/houiin_dlc_empty/dlc_wy-houiin-2019-01-18
rightwidth: 400
save_iters: 50000
scale_jitter_lo: 0.5
scale_jitter_up: 1.25
topheight: 400

错误解决

1. 标记数据检验失败

错误类似于下图所示:

csv 中存有数据标记点的位置

h5 文件无法直接打开,目前不清楚内部信息。不过通过替换法可以确定的是h5和csv文件可以不用配套

举个例子:

项目创建者:Houiin(我)

数据标记员:Anda(我的小伙伴)


Houiin(项目创建者),新建了一个Project,

我拜托Anda帮忙标记了数据,Anda使用自己的名字创建了Project并标记了数据

于是在csv文件的开头和h5文件内部,标记者均为Anda,和试验环境的Houiin相冲突

创建项目的实验者姓名h5中的实验者姓名对不上时,会报错。

解决方法一(推荐):修改配置文件

修改项目配置文件config.yaml,将其中的标记者更改为标记人员的名字

解决方法二:

此时只需Houiin(项目创建者)运行标记程序,随便标注一个数据,重新生成csvh5文件,此时h5中的实验者此时已经是Houiin了,将Anda(数据标记人员)标注好后的csv文件替换掉Houiin乱标生成的csv文件即可使用Anda标记好的数据在Houiin的项目里面跑了

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值