deeplabcut demo实验到应用

source activate deeplabcut-py36
ipython
import deeplabcut
import tensorflow as tf
import os
from pathlib import Path

task = 'Test-zcfan-DLC'
exprimenter = 'zcfan'
video =['/home/zcfan/DeepLabCut-master/Test-zcfan-DLC/videos/test.mp4']   
 
1、# 创建项目,并指定工作文件夹  工作文件夹的命名规则: task-exprimenter-日期
# 本案例指定工作文件夹为home/zcfan/DeepLabCut-master/Test-zcfan-DLC/videos
# 本案例 则为 Test-zcfan-DLC-zcfan-2019-12-17。
deeplabcut.create_new_project(task,experimenter,video,working_directory='/home/zcfan/DeepLabCut-master/Test-zcfan-DLC/videos',copy_videos=True)


2、# 设置一个文件配置变量,方便下面使用。
path_config_file = '/home/zcfan/DeepLabCut-master/Test-zcfan-DLC/videos/Test-zcfan-DLC-zcfan-2019-12-17/config.yaml'

3、# 在文件 labeled-data/test/ 下生成若干提取图片  。
deeplabcut.extract_frames(path_config_file,'automatic','uniform',crop=True, checkcropping=True)
另一种手动捕捉
deeplabcut.extract_frames(path_config_file,'manual')拖动视频进度条,在合适的图像时点击“Grab Frames”。新增捕获的图像帧,也会存放在文件 labeled-data/test/ 下。

4、标记图像
deeplabcut.label_frames(path_config_file)

左键移动,右键标记。标记完成后,点击 “Save”。之后会在 labeled-data/test/文件下生成 CollectedData_felix.csv, CollectedData_felix.h5。
注意:“select a bodypart to label” 由config.yaml 文件中 bodyparts 中的参数决定。每一个项目根据自己的需求自定义。

5、检查标签:
deeplabcut.check_labels(path_config_file)
新生成一个 test_labeled 文件夹,其中保存着被标记的图片。(labeled-data 下的两个文件夹 test 和test_labeled中保存的图像是一致的,唯一的区别在于是否被标记)。接下来就可以使用  “create_training_dataset(path_config_file)” 开始创建训练。

6、训练集的数据将保存在 “training-datasets” 和 “dlc-models” 文件夹中,未开始训练前是空的。

    deeplabcut.create_training_dataset(path_config_file)
    # 可以通过可选参数  num_shuffles = n 创建多个训练子集
    # deeplabcut.create_training_dataset(path_config_file, num_shuffles = 3)
     
7、开始训练:

   可ctrl+c 停止训练。

    a、deeplabcut.train_network(path_config_file)
    b、deeplabcut.train_network(path_config_file, shuffle=1, saveiters=300, displayiters=10)
    # 此函数有很多可选参数,学会基本操作后,可以慢慢学习其他参数的使用。我使用了下面那种。


8、# ctrl + c 停止训练
# 开始评估模型
deeplabcut.evaluate_network(path_config_file,plotting=True)
# 此函数用于评估特定训练状态(快照)或所有状态下特定随机播放/随机播放的训练模型。
# 在数据集(图像)上对网络进行评估,并将结果作为.csv文件存储在评估结果下的子目录中。

9、# 创建路径
videofile_path = ['/home/DeepLabCut-Felix/examples/Reaching-Mackenzie-2018-08-30/videos/MovieS2_Perturbation_noLaser_compressed.avi']
# 注意保存视频的目录下有两个视频,但不是同一个视频。
# 一个用于训练,一个(MovieS2_Perturbation_noLaser_compressed.avi)用于分析

10、# 分析视频
print("Start Analyzing the video!")
deeplabcut.analyze_videos(path_config_file, videofile_path )
# 需要较长的时间分析


11、创建带有标签的视频:
 deeplabcut.create_labeled_video(path_config_file,videofile_path)

12、绘制分析视频的轨迹:
(此函数绘制整个视频中所有身体部位的轨迹。每个身体部位都有一种独特的颜色。底层函数可以很容易地定制)新增的四幅图片在 videos 文件夹下的子文件夹 plot-poses。此时由上一步的 17 directories, 87 files 变成 19directories, 91 files。
deeplabcut.plot_trajectories(path_config_file,videofile_path,showfigures=True)

#####################################
1、当提取异常值帧,预测结束
(这是可选步骤,允许在评估结果较差时添加更多训练数据。 在这种情况下,用户可以使用以下功能来提取错误预测标签的帧。 确保提供正确的“迭代”值,因为它将用于创建将保存提取的帧的唯一目录。)
deeplabcut.extract_outlier_frames(path_config_file,videofile_path,outlieralgorithm='uncertain',p_bound=.2)

2、手动更正标签:
(此步骤允许用户更正提取的帧中的标签。)
deeplabcut.refine_labels(path_config_file)

3、合并数据集:
deeplabcut.merge_datasets(path_config_file)

4、创建训练数据集的新迭代,检查并训练...(现在剩下的步骤便是重复上面的步骤,在新的数据集上训练)
    # 检查标签
    deeplabcut.check_labels(path_config_file)
     
    # 创建训练数据集
    deeplabcut.create_training_dataset(path_config_file)

5、重新训练:
    # 在新的数据集重新训练
    deeplabcut.train_network(path_config_file)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章