提取C3D視訊特徵(官方文件&自己實踐)
C3D Introduction
卷積神經網路(CNN)近年被廣泛應用於計算機視覺中,包括分類、檢測、分割等任務。這些任務一般都是針對影象進行的,使用的是二維卷積(即卷積核的維度為二維)。而對於基於視訊分析的問題,2D convolution不能很好得捕獲時序上的資訊。因此3D convolution就被提出來了。
簡而言之,C3D適合學習時序特徵。
C3D Installation
C3D User Guide
Du Tran (Last modified Mar 20, 2017) C3D-v1.1 is released with new models (Mar 01, 2017). No documentation for v1.1 yet, but some examples for feature extraction, training, and fine-tuning are provided.
文件介紹了C3D 1.0的用法,至於1.1,只有examples。
I.C3D Feature Extration
-
安裝好C3D,下載預訓練的模型,儲存到
YOUR_PATH_TO_C3D/C3D-master/C3D-v1.0
-
change dir to
YOUR_PATH_TO_C3D/C3D-master/C3D-v1.0/examples/c3d_feature_extraction
-
Run:
sh c3d_sport1m_feature_extraction_frm.sh or sh c3d_sport1m_feature_extraction_video.sh
執行成功將在output資料夾找到特徵檔案。
遇到“out of memeory”記憶體不足的錯誤,調整min_batch_size引數。參見章節 I.B。
如能使用圖片輸入,不能使用視訊輸入。請確保編譯OpenCV和Ffmpeg時”shared-flags”為”on”。
I.A Extract C3D features for your own videos or frames
a.準備輸入檔案
輸入為視訊或視訊幀
對於視訊檔案,由於使用opencv獲得幀,幀數從0開始。
對於視訊幀,命名為“video_folder/%06d.jpg”,幀數從1開始。一個資料夾最多999999個幀,如果視訊超出999999幀,需要分成多個資料夾。
b.準備配置檔案
兩個設定選項:輸入列表(input_list) 和 輸出字首(output_prefix)
在example中,輸入列表的配置檔案為: “prototxt/input_list_video.txt”和”prototxt/input_list_frm.txt”。
input_list檔案需要制定輸入的列表,格式為每行制定一個輸入。每行的格式為
<string_path> <starting_frame> <label>
“string_path”: 為路徑,對視訊,為視訊路徑和檔名;對frames,是包含視訊截圖的目錄路徑。
“starting_frame”: C3D能從長為16幀的視訊中提取特徵。一個視訊包含了大量的幀,我們需要指定C3D從哪一幀開始提取特徵。
“label”: 這個僅對訓練、測試、調優起作用,提取特徵的時候會被忽略,設定為0。
output_prefix檔案要為每一個輸入指定一個輸出字首。即行數與input_list一致。每行的格式為:<output_prefix>
C3D將特徵輸出到 output_prefix.[feature_name]檔案(例如prefix.fc6)。為了與輸入對應,輸出建議採用如下格式`sprintf(“output_folder/%06d”, starting_frame)。
c.提取特徵
在prototxt中,通過後綴名為.prototxt的文件來指向你的輸入列表檔案。
主要修改這兩行:
source: “prototxt/input_list_video.txt” use_image: false shuffle: false
source 修改為輸入列表檔案
如果使用影象檔案,use_image修改為true。
提取特徵時確保”shuffle”為false。
(.prototxt檔案第一行是name,接下來是若干個layer,layer用json表示,我們只需要修改第一個輸入layer。)
接下來使用”extract_image_features”工具來提取特徵。
該工具使用的引數如下
extract_image_features.bin <feature_extractor_prototxt_file> <c3d_pre_trained_model> <gpu_id> <mini_batch_size> <number_of_mini_batches> <output_prefix_file> <feature_name1> <feature_name2> ...
-
feature_extractor_prototxt_file:
.prototxt檔案,指向input_list_file
-
c3d_pre_trained_model:
下載的C3D預訓練模型
-
gpu_id:
GPU ID,從0開始。設為-1則使用CPU
-
mini_batch_size:
批處理大小。預設值為50。根據GPU的效能修改。
-
number_of_mini_batches
批處理數量。
如果有100個clips,<mini_batch_size>設定為50,則<number_of_mini_batches>為2。
如果有101個clips,<mini_batch_size>設定為50,則<number_of_mini_batches>為3。
(就是ceil除法嘛)
-
output_prefix_file:
輸出字首檔案
-
feature_name1:
特徵名。(參見.prototxt檔案的layers, 如 fc6-1, fc7-1, fc8-1, pool5, conv5b, prob,…)
example中的命令列如下:
GLOG_logtosterr=1 ../../build/tools/extract_image_features.bin prototxt/c3d_sport1m_feature_extractor_frm.prototxt conv3d_deepnetA_sport1m_iter_1900000 0 50 1 prototxt/output_list_prefix.txt fc7-1 fc6-1 prob