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)

 

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