Cuda安装
wget https://developer.download.nvidia.com/compute/cuda/11.6.0/local_installers/cuda_11.6.0_510.39.01_linux.run
sh cuda_11.6.0_510.39.01_linux.run
#可能会报错,查看/var/log/nvidia-installer.log ,kill -9 [ID]可以解决
vim ~/.bashrc
source ~/.bashrc
nvcc -V
cp cudnn-linux-x86_64-8.4.1.50_cuda11.6-archive/include/* /usr/local/cuda-11.6/include/
cp cudnn-linux-x86_64-8.4.1.50_cuda11.6-archive/lib/* /usr/local/cuda-11.6/lib64/
chmod +x /usr/local/cuda-11.6/include/cudnn.h
chmod +x /usr/local/cuda-11.6/lib64/libcudnn*
检测
cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
以文本识别训练为例
环境使用anconda创建了”paddle”的虚拟环境,该环境适合paddleOCR。
conda activate paddle
数据准备
如图1-1,数据集需要提供train_list.txt,和val_list.txt,具体内容如图1-2,修改配置文件位置图1-3
图1-1
图1-2 train_list.txt,和val_list.txt具体内容
图1-3 修改配置文件位置
训练模型
git clone https://github.com/PaddlePaddle/PaddleOCR.git
cd PaddleOCR
#下载预训练模型,按需求下载
wget -P ./pretrain_models/ https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_rec_train.tar
# 解压模型参数
cd pretrain_models
tar -xf en_PP-OCRv3_rec_train.tar && rm -rf en_PP-OCRv3_rec_train.tar
#cd /configs/rec/PP-OCRv3/ ,创建my_en_PP-OCRv3_rec.yml
#注意配置数据集地址
启动训练:
#cd到PaddleOCR目录下
python3 tools/train.py -c configs/rec/PP-OCRv3/my_en_PP-OCRv3_rec.yml -o Global.pretrained_model=./pretrained_model/en_PPOCRv3_rec_train/best_accuracy
如果出现中断,重启训练:
python3 tools/train.py -c configs/rec/PP-OCRv3/en_PP-OCRv3_rec.yml -o Global.checkpoints=./your/trained/model
指标评估
python3 tools/train.py -c configs/rec/PP-OCRv3/en_PP-OCRv3_rec.yml -o Global.checkpoints=./output/v3_en_mobile/best_accuracy
导出模型
# -c 后面设置训练算法的yml配置文件
# -o 配置可选参数
# Global.pretrained_model 参数设置待转换的训练模型地址,不用添加文件后缀 .pdmodel,.pdopt或.pdparams。
# Global.save_inference_dir参数设置转换的模型将保存的地址。
python3 tools/export_model.py -c configs/rec/PP-OCRv3/en_PP-OCRv3_rec.yml -o Global.pretrained_model=./pretrain_models/en_PP-OCRv3_rec_train/best_accuracy Global.save_inference_dir=./inference/en_PP-OCRv3_rec/
#清除内存
fuser -v /dev/nvidia*
kill -9 [nvidia的PID]
第二章 部署
安装Cmake,OpenCV,OpenCV-contribute
yum install mesa-libGL-devel mesa-libGLU-devel
wget https://cmake.org/files/v3.26/cmake-3.26.0-rc4.tar.gz
tar -zxvf cmake-3.26.0-rc.tar.gz
cd cmake-3.26.0-rc
./bootstrap && make -j4 && make install
cmake --version
#安装opencv
yum localinstall –nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm
yum localinstall –nogpgcheck https://download1.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-7.noarch.rpm
rpm –import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
yum -y install ffmpeg ffmpeg-devel
yum install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev
git clone https://github.com/opencv/opencv.git
cd opencv
git clone https://github.com/opencv/opencv_contrib.git
#上传缺少文件
cd opencv_contrib/modules/xfeatures2d/src
cd opencv
mkdir build
cd build
#anconda中的lib可能会干扰安装
cmake .. \
-DCMAKE_INSTALL_PREFIX=/Hefei-AI/meiyiwork/software/opencv4/ \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=OFF \
-DWITH_IPP=OFF \
-D WITH_OPENEXR=0 \
-DBUILD_IPP_IW=OFF \
-DWITH_LAPACK=OFF \
-DWITH_EIGEN=OFF \
-DCMAKE_INSTALL_LIBDIR=lib64 \
-DWITH_ZLIB=ON \
-DBUILD_ZLIB=ON \
-DWITH_JPEG=ON \
-DBUILD_JPEG=ON \
-DWITH_PNG=ON \
-DBUILD_PNG=ON \
-DWITH_TIFF=ON \
-DBUILD_TIFF=ON
make -j8 #多次
make install
预测
git clone https://github.com/PaddlePaddle/Paddle.git
cd Paddle
mkdir build
cd build
cmake .. \
-DPYTHON_INCLUDE_DIR=/usr/bin/python3 \
-DWITH_CONTRIB=OFF \
-DWITH_MKL=ON \
-DWITH_MKLDNN=ON \
-DWITH_TESTING=OFF \
-DCMAKE_BUILD_TYPE=Release \
-DWITH_INFERENCE_API_TEST=OFF \
-DON_INFER=ON \
-DWITH_PYTHON=ON
make -j #时间很长,多次尝试
make inference_lib_dist
sh tools/build.sh
#修改
OPENCV_DIR=/Hefei-AI/meiyiwork/software/opencv4
LIB_DIR=/Hefei-AI/meiyiwork/paddle_inference_install_dir
CUDA_LIB_DIR=/usr/local/cuda/lib64
CUDNN_LIB_DIR=/usr/local/cuda/lib64
#编译前,需要注意替换args.cpp中的rec_char_dict_path,不然会检测乱码
#编译
sh tools/build.sh
#可能会出现问题1
cc1plus: error: too many filenames given. Type cc1plus --help for usage
cc1plus: error: too many filenames given. Type cc1plus --help for usage
cc1plus: error: too many filenames given. Type cc1plus --help for usage
cc1plus: error: too many filenames given. Type cc1plus --help for usage
cc1plus: fatal error: CMakeFiles/ppocr.dir/src/postprocess_op.cpp.d: 没有那个文件或目录
cc1plus: fatal error: CMakeFiles/ppocr.dir/src/ocr_rec.cpp.d: 没有那个文件或目录
compilation terminated.
compilation terminated.
cc1plus: error: too many filenames given. Type cc1plus --help for usage
cc1plus: fatal error: CMakeFiles/ppocr.dir/src/main.cpp.d: 没有那个文件或目录
cc1plus: fatal error: CMakeFiles/ppocr.dir/src/ocr_det.cpp.d: 没有那个文件或目录
compilation terminated.
compilation terminated.
cc1plus: error: too many filenames given. Type cc1plus --help for usage
cc1plus: fatal error: CMakeFiles/ppocr.dir/src/ocr_cls.cpp.d: 没有那个文件或目录
compilation terminated.
cc1plus: fatal error: CMakeFiles/ppocr.dir/src/utility.cpp.d: 没有那个文件或目录
compilation terminated.
解决办法:
#解决办法/PaddleOCR/deploy/cpp_infer/CMakeLists.txt将 "-o3" 改成 "-O3" 就可以了,修改后的结果如下,
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O3 ${FLAG_OPENMP} -std=c++11")
网速会影响下载third-party导致报错。
测试
./build/ppocr --rec_model_dir=../../inference/en_PP-OCRv3_rec --image_dir=../../doc/imgs_words/en/word_2.png --use_angle_cls=false --det=false --rec=true