需要:pytorch>=1.7
去官网查看对应的CUDA版本号:https://pytorch.org/get-started/previous-versions/,我的CUDA是10.2的
conda create -n torch1.7.0 python=3.7 #创建新环境
conda install pytorch==1.7.0 torchvision==0.8.0 torchaudio==0.7.0 cudatoolkit=10.2
在github下载模型权重到YOLOX 文件夹下(用chrome中的github加速插件可快速下载权重文件)
自定义数据集的目录结构
- datasets
- VOCdevkit
- VOC2021 (自定义)
- Annotations
- ImageSets
- Main
- test.py
- train.py
- val.py
- trainval.py
- JPEGImages
生成.txt文件内容的代码
import os
import random
random.seed(0)
xmlfilepath='VOC数据集Annotations文件夹路径'#xml文件存放地址,在训练自己数据集的时候,改成自己的数据路径
saveBasePath="VOC数据集ImageSets/Main文件夹路径"#存放test.txt,train.txt,trainval.txt,val.txt文件路径
#----------------------------------------------------------------------#
# 根据自己的需求更改trainval_percent和train_percent的比例
#----------------------------------------------------------------------#
trainval_percent=0.9
train_percent=1
temp_xml = os.listdir(xmlfilepath)
total_xml = []
for xml in temp_xml:
if xml.endswith(".xml"):
total_xml.append(xml)
num=len(total_xml)
list=range(num)
tv=int(num*trainval_percent)
tr=int(tv*train_percent)
trainval= random.sample(list,tv)
train=random.sample(trainval,tr)
print("train and val size",tv)
print("traub suze",tr)
ftrainval = open(os.path.join(saveBasePath,'trainval.txt'), 'w')
ftest = open(os.path.join(saveBasePath,'test.txt'), 'w')
ftrain = open(os.path.join(saveBasePath,'train.txt'), 'w')
fval = open(os.path.join(saveBasePath,'val.txt'), 'w')
for i in list:
name=total_xml[i][:-4]+'\n'
if i in trainval:
ftrainval.write(name)
if i in train:
ftrain.write(name)
else:
fval.write(name)
else:
ftest.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest .close()
git clone [email protected]:Megvii-BaseDetection/YOLOX.git
cd YOLOX
pip3 install -U pip && pip3 install -r requirements.txt
python3 setup.py develop
pip3 install cython; pip3 install 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'
YOLOX/yolox/data/datasets/voc_classes.py,各个类别之间必须用逗号隔开,最后一个类别后也必须加逗号
VOC_CLASSES = (
"fire",
)
修改YOLOX/yolox/exp/yolox_base.py文件下的相关参数
class Exp(BaseExp):
def __init__(self):
super().__init__()
# ---------------- model config ---------------- #
self.num_classes = 1
self.depth = 1.33
self.width = 1.25
self.act = 'silu'
修改YOLOX/exps/example/yolox_voc/yolox_voc_s.py
image_sets=[('2021', 'trainval')]
image_sets=[('2021', 'test')]
修改YOLOX/yolox/data/datasets/voc.py
annopath = os.path.join(rootpath, "Annotations", "{}.xml")
rootpath = self.root + '/VOC2021'
YOLOX/tools/train.py
parser.add_argument("-n", "--name", type=str, default='yolox-x', help="model name")
parser.add_argument("-b", "--batch-size", type=int, default=4, help="batch size")```
CUDA_VISIBLE_DEVICES=1 python train.py
parser.add_argument(
"-d", "--devices", default=0, type=int, help="device for training"
)
parser.add_argument(
"-f",
"--exp_file",
default='YOLOX/exps/example/yolox_voc/yolox_voc_s.py',
type=str,
help="plz input your experiment description file",
)
parser.add_argument("-c", "--ckpt", default='/YOLOX/yolox_x.pth', type=str, help="checkpoint file")
开始训练:python train.py
tensorboard --logdir=./ --host= 127.0.0.1
(在本地训练时可用),如果是在服务器上进行训练的话:ssh -L 16006:服务器ip地址:6006 [email protected]
tensorboard --logdir /home/XX/YOLOX/tools/YOLOX_outputs/yolox_voc_s/ --bind_all
http://127.0.0.1:16006/
修改 YOLOX/tools/demo.py
from yolox.data.datasets import voc_classes # 引入voc文件对应的类
predictor = Predictor(model, exp, voc_classes.VOC_CLASSES, trt_file, decoder, args.device)
cls_names=voc_classes.VOC_CLASSES
parser.add_argument(
"demo", default="image", help="demo type, eg. image, video and webcam"
)
parser.add_argument(
"--path", default="YOLOX/datasets/VOCdevkit/VOC2021/JPEGImages/0ce61215-6d34-49f9-aa6d-5f0feed430f8.jpg", help="path to images or video"
)
parser.add_argument(
"-f",
"--exp_file",
default='YOLOX/exps/example/yolox_voc/yolox_voc_s.py',
type=str,
help="pls input your experiment description file",
)
parser.add_argument("-c", "--ckpt", default='YOLOX/tools/YOLOX_outputs/yolox_voc_s/latest_ckpt.pth', type=str, help="ckpt for eval")
parser.add_argument(
"--device",
default="gpu",
type=str,
help="device to run our model, can either be cpu or gpu",
)