训练检测口罩模型
下载项目
https://github.com/Megvii-BaseDetection/YOLOX
打开项目
安装环境
pip install -r requirements.txt
如果报错
pip install -c esri pycocotools
第一步下载数据集
在datasets文件夹下新建VOCdevkit文件夹并将下载的data文件夹放进去,并改名为VOC2020。
在VOC2020文件夹下新建ImageSets文件夹,在建Main文件夹。
下载make_train_and_test.py文件并运行
import os
import random
train_percent = 0.8
test_percent = 0.2
xmlfilepath = 'VOCdevkit/VOC2020/Annotations'
txtsavepath = 'VOCdevkit/VOC2020/ImageSets'
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = range(num)
tr = int(num * train_percent)
te = int(num * test_percent)
train = random.sample(list, tr)
test = random.sample(list, te)
ftest = open('VOCdevkit/VOC2020/ImageSets/Main/test.txt', 'w')
ftrain = open('VOCdevkit/VOC2020/ImageSets/Main/train.txt', 'w')
for i in list:
name = total_xml[i][:-4] + '\n'
if i in train:
ftrain.write(name)
else:
ftest.write(name)
ftrain.close()
ftest.close()
生成训练样本和测试样本的下标
第二步修改代码
yolox_voc_s.py相关代码改为
self.num_classes = 2
然后
voc_classea.pu改为
VOC_CLASSES = (
"no_masked",
"masked",
)
voc.py修改
训练模型
python tools/train.py -f ./exps/example/yolox_voc/yolox_voc_s.py -d 1 -b 4 --fp 16 -o
会报错
Traceback (most recent call last):
File “./tools/train.py”, line 13, in
from yolox.core import launch
ModuleNotFoundError: No module named ‘yolox’
路径错误
在tools/train.py开头加上开头路径
import sys
sys.path.append(r"D:\PycharmProjects\YOLOX-main")
报错 DataLoader worker
yolox_voc_s.py添加
self.data_num_workers = 0
报错OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
环境冲突
tools/train.py添加代码
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
报错 ERROR | yolox.core.launch:98 - An error has been caught in function ‘launch’, process ‘MainProcess’ (22360), thread ‘MainThread’ (18700):
修改
voc.py