第一步:解压数据集
在aistudio中选择安全帽检测数据集即可。
第二步:统计数据集信息
!unzip -q /home/aistudio/data/data50329/HelmetDetection.zip -d /home/aistudio/helmet
第二步:统计数据集信息
这步是查看数据集的内容,知晓数据集后更易调参。
import re
import glob
import matplotlib.pyplot as plt
files = glob.glob("/home/aistudio/helmet/annotations/*.xml")
width_and_height_regex = re.compile(r'<width>(?P<width>.*?)</width>.*?<height>(?P<height>.*?)</height>',re.S)
class_name = re.compile(r'<name>(?P<class_name>.*?)</name>')
all_class = {}
target_num = 0
heights = []
widths = []
for file in files:
with open(file, mode="r") as f:
text=f.read()
hw = width_and_height_regex.findall(text)
heights.append(hw[0][0])
widths.append(hw[0][1])
names = class_name.findall(text)
for name in names:
if name not in all_class.keys():
all_class.setdefault(name, 0)
else:
all_class[name]+=1
print(f"数据集有{len(files)}张图片")
for key,item in all_class.items():
print(f"{key}类有{item}个")
plt.title("heights,widths")
plt.scatter(heights,widths)
第三步:下载所需套件和包
下载完PaddleDetection套件后运行一下命令
%cd
!pip install -r PaddleDetection-2.5.0/requirements.txtglob. Glob
!pip install paddlex
分割数据集
!paddlex --split_dataset --format voc --dataset_dir /home/aistudio/helmet/ --val_value 0.2
第四步:开始训练
%cd
%cd PaddleDetection-2.5.0
!python tools/train.py -c /home/aistudio/utils/ppyoloe_plus_crn_m_80e_coco.yml \
--eval --amp --use_vdl=True --vdl_log_dir="../output"
第五步:预测
%cd PaddleDetection-2.5.0
!python tools/infer.py -c /home/aistudio/utils/ppyoloe_plus_crn_m_80e_coco.yml \
--infer_img=/home/aistudio/elsedata/1313.jpg \
--output_dir=/home/aistudio/inferout/\
-o weights=/home/aistudio/PaddleDetection-2.5.0/output/ppyoloe_plus_crn_m_80e_coco/best_model.pdparams \
详情见:https://aistudio.baidu.com/aistudio/projectdetail/6347916