paddle训练安装

这里共有三个模块安装部署与训练。版面区域检测、文本检测、文本识别。

其中分为两种部署训练模式:paddleOCR、paddlex模式。

一、版面区域检测模块训练

复制代码
  说明:训练这里只需要用到paddlepaddle、paddlex。所以ocr这里不安 装。

以下为CPU安装方式运行,GPU请参考下面的链接。数据集需要自己下载或自己准备,采用COCO类型数据集。

1、下载paddlex项目后,进入该目录并创建虚拟环境3.10。

参考链接:https://paddlepaddle.github.io/PaddleX/latest/installation/paddlepaddle_install.html#docker

2、先用用 pip 在当前环境中安装飞桨 PaddlePaddle。
python -m pip install paddlepaddle==3.0.0 -i https://www.paddlepaddle.org.cn/packages/stable/cpu/

测试输出结果:
python -c "import paddle; print(paddle.__version__)"

3、安装paddlex
pip install -e .

4、安装插件,比如PaddleDetection、

```

paddlex --install PaddleXXX # 例如PaddleOCR、PaddleDetection

paddlex --install #安装所有插件,不建议,中间出过多次错误。

复制代码
  paddlex --install --platform gitee.com   # gitee源,安装所有插件。
复制代码
复制代码
5、执行数据集验证:
	```

python main.py -c paddlex/configs/modules/layout_detection/PP-DocLayout-L.yaml -o Global.mode=check_dataset -o Global.dataset_dir=./study/train/dataset/det_layout_examples

复制代码
	"Check dataset passed !"为验证成功



	执行训练命令:
		```
python main.py -c paddlex/configs/modules/layout_detection/PP-DocLayout-L.yaml -o Global.mode=train -o Global.dataset_dir=./study/train/dataset/det_layout_examples

	会报错,因为这个`PP-DocLayout-L.yaml`配置文件默认是采用GPU运行的,所以配置文件需要修改。
	```

mode: train #原本为check_dataset

device: cpu # 关键修改:从gpu:0,1,2,3改为cpu

复制代码
		log查看异常,并处理
			```
pip install numba==0.56.4
			pip install scikit-learn

二、文本检测模块训练

(一)、这里基于PaddleOCR的文件检测模块训练。

复制代码
	这里需要安装OCR。参考链接:[https://paddlepaddle.github.io/PaddleOCR/main/version3.x/installation.html](https://paddlepaddle.github.io/PaddleOCR/main/version3.x/installation.html)

	1、安装OCR
		```

python -m pip install paddleocr

复制代码
		git clone https://gitee.com/paddlepaddle/PaddleOCR.git
		# 切换分支
		git checkout release/3.0
		# 安装依赖
		python -m pip install -r requirements.txt

		中间报错,numpy切换版本:
		pip install numpy==1.26.4


		
		2、文本检测 命令测试demo:
			`paddleocr text_detection -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_001.png`
		
		3、下载训练数据集与预训练模型
			```
https://paddle-model-ecology.bj.bcebos.com/paddlex/data/ocr_det_dataset_examples.tar
			# 下载 PP-OCRv5_server_det 预训练模型
			wget https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/PP-OCRv5_server_det_pretrained.pdparams

	3.1、这里需要修改配置文件。`configs/det/PP-OCRv5/PP-OCRv5_server_det.yml`,CPU训练的里面几个参数可进行改动
		```

use_gpu: false #启用CPU

epoch_num: &epoch_num 2 #训练轮次

distributed: false #✅ 禁用分布式

复制代码
		3、解压后进行训练
			# 单卡训练,多卡训练参考官网
			```
python tools/train.py -c configs/det/PP-OCRv5/PP-OCRv5_server_det.yml -o Global.pretrained_model=./study/train/dataset/PP-OCRv5_server_det_pretrained.pdparams Train.dataset.data_dir=./study/train/dataset/ocr_det_dataset_examples Train.dataset.label_file_list='[./study/train/dataset/ocr_det_dataset_examples/train.txt]' Eval.dataset.data_dir=./ocr_det_dataset_examples Eval.dataset.label_file_list='[./study/train/dataset/ocr_det_dataset_examples/val.txt]'
			# https://paddlepaddle.github.io/PaddleOCR/main/version3.x/module_usage/text_detection.html#_42

	4、训练完成后,命令进行验证。
		```

python tools/eval.py -c configs/det/PP-OCRv5/PP-OCRv5_server_det.yml -o Global.pretrained_model=./output/PP-OCRv5_server_det_pretrained.pdparams Eval.dataset.data_dir=./study/train/dataset/ocr_det_dataset_examples Eval.dataset.label_file_list='[./study/train/dataset/ocr_det_dataset_examples/val.txt]'

复制代码
> ##	(二)、这里是基于paddlex的文本检测模块的数据集验证、训练与评估、导出等方法。
		1、验证数据集
			```
python main.py -c paddlex/configs/modules/text_detection/PP-OCRv5_mobile_det.yaml -o Global.mode=check_dataset -o Global.dataset_dir=./study/train/dataset/ocr_det_dataset_examples
			
		能正常运行OK,不能看问题是否是报错【AttributeError: 'FigureCanvasAgg' object has no attribute 'tostring_rgb'】
		我这边报错,修改【PaddleX\paddlex\modules\text_detection\dataset_checker\dataset_src\analyse_dataset.py】这个代码后ok了,
		内容如下(屏蔽掉的为原代码):
			```

canvas.draw()

复制代码
			# width, height = fig.get_size_inches() * fig.get_dpi()
			# bar_array = np.frombuffer(canvas.tostring_rgb(), dtype="uint8").reshape(
			#     int(height), int(width), 3
			# )
			canvas.draw()
			width, height = fig.get_size_inches() * fig.get_dpi()
			rgba_array = np.asarray(canvas.buffer_rgba(), dtype="uint8").reshape(int(height), int(width), 4)
			bar_array = cv2.cvtColor(rgba_array, cv2.COLOR_RGBA2BGR)
			```
		第二段代码:
```
			# canvas.draw()
			# width, height = fig.get_size_inches() * fig.get_dpi()
			# pie_array = np.frombuffer(canvas.tostring_rgb(), dtype="uint8").reshape(
			#     int(height), int(width), 3
			# )
			canvas.draw()
			width, height = fig.get_size_inches() * fig.get_dpi()
			rgba_array = np.asarray(canvas.buffer_rgba(), dtype="uint8").reshape(int(height), int(width), 4)
			pie_array = cv2.cvtColor(rgba_array, cv2.COLOR_RGBA2BGR)


			
			2、训练
				`python main.py -c paddlex/configs/modules/text_detection/PP-OCRv5_mobile_det.yaml -o Global.mode=train -o Global.dataset_dir=./study/train/dataset/ocr_det_dataset_examples`
			
			3、评估
				`python main.py -c paddlex/configs/modules/text_detection/PP-OCRv5_mobile_det.yaml -o Global.mode=evaluate -o Global.dataset_dir=./study/train/dataset/ocr_det_dataset_examples`
	
	
	
			如果安装报错,说缺少目录,则新建这个目录
			`### D:\developSoft\anaconda3\envs\ocr310\Lib\site-packages\paddlex\repo_manager\repos`
			
			```
参考链接
https://paddlepaddle.github.io/PaddleOCR/latest/version3.x/pipeline_usage/PP-ChatOCRv4.html#1-pp-chatocrv4
			https://paddlepaddle.github.io/PaddleOCR/latest/version3.x/module_usage/text_detection.html#43

三、文本识别模块训练

(一)、PaddleOCR版文本识别模块的训练

复制代码
	1、文本识别模块跟文本检测模式都是依赖于PaddleOCR,所以上面下载好PaddleOCR源码后,进入源码目录。
	自己下载好模型与数据集
	`https://paddlepaddle.github.io/PaddleOCR/main/version3.x/module_usage/text_recognition.html#411`
	
	2、修改配置文件`configs/rec/PP-OCRv5/PP-OCRv5_server_rec.yml`以下内容:
	 ```

use_gpu: false

epoch_num: 2

character_dict_path: ./study/train/dataset/ocr_rec_dataset_examples/dict.txt

... ...

复制代码
	  Train:
		dataset:
		   data_dir: ./study/train/dataset/ocr_rec_dataset_examples/
		   label_file_list:
			   - ./study/train/dataset/ocr_rec_dataset_examples/train.txt
	  Eval:
		dataset:
		   data_dir: ./study/train/dataset/ocr_rec_dataset_examples/
		   label_file_list:
			  - ./study/train/dataset/ocr_rec_dataset_examples/val.txt
		
```		
	
	3、Python训练命令如下:
	`python tools/train.py -c configs/rec/PP-OCRv5/PP-OCRv5_server_rec.yml -o Global.pretrained_model=./study/train/dataset/PP-OCRv5_server_rec_pretrained.pdparams`
	
	4、模型评估:
	`python tools/eval.py -c configs/rec/PP-OCRv5/PP-OCRv5_server_rec.yml -o Global.pretrained_model=output/PP-OCRv5_server_rec/latest.pdparams`
	
	5、模型导出指令:
	`python tools/export_model.py -c configs/rec/PP-OCRv5/PP-OCRv5_server_rec.yml -o Global.pretrained_model=output/PP-OCRv5_server_rec/latest.pdparams Global.save_inference_dir=".out/tt/PP-OCRv5_server_rec_infer/"`

(二)、PaddleX版文本识别模块的训练

复制代码
	1、数据下载链接:
	
	2、数据集验证:
		`python main.py -c paddlex/configs/modules/text_recognition/PP-OCRv4_mobile_rec.yaml -o Global.mode=check_dataset -o Global.dataset_dir=./study/train/dataset/ocr_rec_dataset_examples`
		
		能正常运行OK,不能看问题是否是报错【AttributeError: 'FigureCanvasAgg' object has no attribute 'tostring_rgb'】
		我这边报错,修改【`PaddleX\paddlex\modules\text_recognition\dataset_checker\dataset_src\analyse_dataset.py`】这个代码后ok了,
		内容如下(屏蔽掉的为原代码):
			```

canvas.draw()

复制代码
			# width, height = fig.get_size_inches() * fig.get_dpi()
			# pie_array = np.frombuffer(canvas.tostring_rgb(), dtype="uint8").reshape(
			#     int(height), int(width), 3
			# )
			canvas.draw()
			width, height = fig.get_size_inches() * fig.get_dpi()
			rgba_array = np.asarray(canvas.buffer_rgba(), dtype="uint8").reshape(int(height), int(width), 4)
			pie_array = cv2.cvtColor(rgba_array, cv2.COLOR_RGBA2BGR)


		
		3、训练指令:
			`python main.py -c paddlex/configs/modules/text_recognition/PP-OCRv4_mobile_rec.yaml -o Global.mode=train -o Global.dataset_dir=./study/train/dataset/ocr_rec_dataset_examples`
		
		4、训练完成后,评估模型:
			`python main.py -c paddlex/configs/modules/text_recognition/PP-OCRv4_mobile_rec.yaml -o Global.mode=evaluate -o Global.dataset_dir=./study/train/dataset/ocr_rec_dataset_examples`
		
		5、推理模型:
			`python main.py -c paddlex/configs/modules/text_recognition/PP-OCRv4_mobile_rec.yaml -o Global.mode=predict -o Predict.model_dir="./output/ocr_rec_dataset_examples/best_accuracy/inference" -o Predict.input="general_ocr_rec_001.png"`