在本次组队学习中,参考水哥的baseline进行ocr场景识别,具体步骤如下
1 下载数据,数据url保存在每个csv文件‘原始数据’特征列,因此可以将多个csv拼接后利用csv文件提取数据
train = [
"Xeon1OCR_round1_train1_20210526.csv",
"Xeon1OCR_round1_train_20210524.csv",
"Xeon1OCR_round1_train2_20210526.csv"
]
test = [
"Xeon1OCR_round1_test1_20210528.csv",
"Xeon1OCR_round1_test2_20210528.csv",
"Xeon1OCR_round1_test3_20210528.csv"
]
df = []
for csv in train:
df.append(pd.read_csv(csv))
df = pd.concat(df)
df["链接"] = df["原始数据"].apply(lambda x: json.loads(x)["tfspath"])
df["链接"].to_csv("train.txt", header=False, index=False)
test_df = []
for i, csv in enumerate(test):
df = pd.read_csv(csv)
test_df.append(df)
df["链接"] = df["原始数据"].apply(lambda x: json.loads(x)["tfspath"])
df["链接"].to_csv(f"test{i+1}.txt", header=False, index=False)
后续进行数据下载
%shell
wget -i train.txt -P train
wget -i test1.txt -P test1
wget -i test2.txt -P test2
wget -i test3.txt -P test3
#如果是aistudio等linux系统,用如下代码
%shell
!wget -i train.txt -P train
!wget -i test1.txt -P test1
!wget -i test2.txt -P test2
!wget -i test3.txt -P test3
这样图片就能下载到一个文件内,之后加载预训练模型
mkdir inference && cd inference/
# 下载模型
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_server_v2.0_det_infer.tar
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_server_v2.0_rec_infer.tar
# 解压模型
tar -xf ch_ppocr_server_v2.0_rec_infer.tar
tar -xf ch_ppocr_server_v2.0_det_infer.tar
tar -xf ch_ppocr_mobile_v2.0_cls_infer.tar
可以测试一下
python3 tools/infer/predict_system.py --image_dir="./1.jpg" --det_model_dir="./inference/ch_ppocr_server_v2.0_det_infer/" --rec_model_dir="./inference/ch_ppocr_server_v2.0_rec_infer/" --cls_model_dir='./inference/ch_ppocr_mobile_v2.0_cls_infer/' --use_angle_cls=True --use_space_char=True
加载检验模块的预训练模型
cd inference
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_server_v2.0_det_train.tar
tar -xf ch_ppocr_server_v2.0_det_train.tar
然后进行finetune,这里训练4个epoch,30分钟左右完成训练。
python3 tools/train.py -c configs/det/ch_ppocr_v2.0/ch_det_res18_db_v2.0.yml -o Global.pretrain_weights=./inference/ch_ppocr_server_v2.0_det_train/
训练完成后,接下来需要将模型权重导出,用于预测。并对测试集的图片进行预测,写入json。
# 将模型导出
python3 tools/export_model.py -c configs/det/ch_ppocr_v2.0/ch_det_res18_db_v2.0.yml -o Global.pretrained_model=output/ch_db_res18/best_accuracy Global.save_inference_dir=output/ch_db_res18/
# 对测试集进行预测
python3 tools/infer/predict_system_tianchi.py --image_dir="./doc/imgs/11.jpg" --det_model_dir="output/ch_db_res18/" --rec_model_dir="./inference/ch_ppocr_server_v2.0_rec_infer/" --cls_model_dir='./inference/ch_ppocr_mobile_v2.0_cls_infer/' --use_angle_cls=True --use_space_char=True
# 将结果文件压缩
zip -r submit.zip Xeon1OCR_round1_test*