我的AI之路(51)--用自己的UCF101数据集训练3D识别模型video-caffe

C3D(C3D project website)的初始版本https://github.com/facebookarchive/C3D太老了,下载一个比较新的版本的源码:

     https://github.com/chuckcho/video-caffe

安装video-caffe的编译环境支持包:

     apt install libopencv-dev 

    apt-get install git cmake
     apt install cmake-qt-gui
     apt-get install libprotobuf-dev  protobuf-compiler libleveldb-dev libsnappy-dev libhdf5-serial-dev libgflags-dev  \
             libgoogle-glog-dev  install liblmdb-dev  libatlas-base-dev   gfortran
     apt-get install libopenblas-dev   #optional
     apt-get install libssl-dev
     apt-get install --no-install-recommends libboost-all-dev
     apt-get install python-dev
     apt-get install python-numpy
     pip install opencv-python     #jetson arm64不用
     pip install python-dateutil
     pip install pytest  #optional 仅供测试验证使用

修改Makefile.config和Makefile:

   cd video-caffe
   vi Makefile.config

      USE_CUDNN := 1

      OPENCV_VERSION := 3

      CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
                -gencode arch=compute_20,code=sm_21 \
                -gencode arch=compute_30,code=sm_30 \
                -gencode arch=compute_35,code=sm_35 \
                -gencode arch=compute_50,code=sm_50 \
                -gencode arch=compute_52,code=sm_52 \
                -gencode arch=compute_60,code=sm_60 \
                -gencode arch=compute_61,code=sm_61 \
                -gencode arch=compute_70,code=sm_70 \
                -gencode arch=compute_70,code=compute_70

       PYTHON_LIBRARIES := boost_python3 python3.6
       PYTHON_INCLUDE := /usr/include/python3.6 \
                 /usr/local/lib/python3.6/dist-packages/numpy/core/include

        WITH_PYTHON_LAYER := 1   #optional, if you want call caffe by python script

       INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
       LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/aarch64-linux-gnu /usr/lib/aarch64-linux-gnu/hdf5/serial

   vi Makefile

        LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial

然后编译:

mkdir build && cd build
cmake ..        

make all -j8     
make install
make runtest   #optional, 验证编译和安装

注意: 假如不是第一次编译,video-caffe/examples/某些文件夹下面有前面上一次编译出的可执行二进制文件了,会导致在执行 cmake .. 这里出错生成不了Makefile,从而后面进行不下去,这时需先把生成了可执行文件的文件夹移到外面去或者删掉,再执行cmake ..即可

假如你需要使用C++代码调用video-caffe,那么编译完后,还需执行:

   cp include/caffe/proto  ../include/caffe   # to get caffe.pb.h for application compiling

不然编译C++调用代码时会报错。

如果上面执行cmake .. 时报错:

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CUDA_cublas_device_LIBRARY (ADVANCED)

这是因为cmake版本低了,下载源码安装比较新的版本的cmake:

    apt remove cmake
    wget https://github.com/Kitware/CMake/releases/download/v3.17.3/cmake-3.17.3.tar.gz
    tar xf cmake-3.17.3.tar.gz
    cd cmake
    ./bootstrap
    make
    make install

然后把cmake加入path路径(也可以在上面用bootsrap设置make install时把cmake 安装到/usr/bin/下去,这样不用做下面的设置)

    vi ~/.bashrc
    export PATH=$PATH:/usr/local/bin
    source ~/.bashrc
然后再在video-caffe/build/下执行cmake ..即可解决上面的错误。

 

如果在安装opencv4了的环境下编译,可能会发生类似下面的错:

   /root/video-caffe/src/caffe/util/io.cpp   /root/video-caffe/src/caffe/layers/window_data_layer.cpp:293:42: error: ‘CV_LOAD_IMAGE_COLOR’ was not declared in this scope

解决办法是:编辑/home/user/caffe/src/caffe/util/io.cpp和 /root/video-caffe/src/caffe/layers/window_data_layer.cpp
   将CV_LOAD_IMAGE_COLOR 改成cv::IMREAD_COLOR
   将CV_LOAD_IMAGE_GRAYSCALE  改成  cv::IMREAD_GRAYSCALE

   在/root/video-caffe/src/caffe/util/io.cpp里面还需做下面的改动:
      CV_CAP_PROP_FRAME_COUNT  ->CAP_PROP_FRAME_COUNT
      CV_CAP_PROP_POS_FRAMES   ->CAP_PROP_POS_FRAMES

生成自己的UCF101数据集后面再补充。

在UCF101数据集准备好并且生成相关的list文件并修改examples/c3d_ucf101/c3d_ucf101_solver.prototxt等配置文件后,修改exmaples/c3d_ucf101/train_ucf101.sh:

   cd video-caffe
   vi exmaples/c3d_ucf101/train_ucf101.sh

   #!/usr/bin/env sh
   set -e

   ./build/tools/caffe \
    train \
     --solver=examples/c3d_ucf101/c3d_ucf101_solver.prototxt \
     --weights=pretrained-c3d_ucf101_iter_20000.caffemodel  \
     $@ \
     2>&1 | tee examples/c3d_ucf101/c3d_ucf101_train.log

执行训练:

    nohup ./examples/c3d_ucf101/train_ucf101.sh &

如果训练中途中断,为了从中断处附近开始resume训练(例如从iteration 17100处resume训练),创建一个脚本文件./examples/c3d_ucf101/train_resume_ucf101.sh :
   vi ./examples/c3d_ucf101/train_resume_ucf101.sh
   #!/usr/bin/env sh
   set -e

   ./build/tools/caffe \
     train \
     --solver=examples/c3d_ucf101/c3d_ucf101_solver.prototxt \
     --snapshot=examples/c3d_ucf101/c3d_ucf101_iter_17100.solverstate  \
     $@ \
     2>&1 | tee examples/c3d_ucf101/c3d_ucf101_train.log
  再执行这个脚本即可resume训练:
    nohup ./examples/c3d_ucf101/train_resume_ucf101.sh &

关于如果制作自己的UCF101数据集以及修改训练用的配置文件,后面有空再补充。

你可能感兴趣的:(C3D,video-caffe,UCF101)