100%完美运行,期间踩过许多坑,数据集的坑、代码的坑都有。现在做一下总结,也希望后来者能快速上手,避免浪费不必要的时间。
写在前面,读者们就不要偷懒了,按照我这边的步骤一步一步操作是肯定能够成功的。鄙人踩过的大多数坑皆是因为耍小聪明。coco数据集的准备我在【Yolact数据集制作-labelme使用与转coco详解】已有详细介绍,按步骤操作即可
【Yolact数据集制作-labelme使用与转coco详解】
一、训练自己的数据与预测
1、训练自己的数据train.py
1.1准备权重文件
准备数据集和权重文件,数据集在第一章已经配置好,这里介绍权重文件
进入https://github.com/dbolya/yolact下载权重文件到根目录./weights文件夹下(weights文件夹自己创建)
对应权重文件如下:
For Resnet101, download resnet101_reducedfc.pth from here.
For Resnet50, download resnet50-19c8e357.pth from here.
For Darknet53, download darknet53.pth from here.
1.2 修改默认配置文件config.py
进入data文件夹下找到config.py打开,按自己的需求修改下面的内容
1.2训练数据
#使用默认基础配置yolact_base_config训练,batch_size=8 (报错就改小一点)
python train.py --config=yolact_base_config
#使用默认基础配置yolact_base_config训练,指定batch_size=5
accordingly. python train.py --config=yolact_base_config --batch_size=5
#使用吗,默认基础配置yolact_base_config训练,指定权重文件
train.py --config=yolact_base_config --resume=weights/yolact_base_10_32100.pth --start_iter=-1
#使用帮助查看各参数的作用
python train.py --help
1.3训练结果
- 开始训练后,模型每10000次迭代保存一次模型到weights文件夹;也可以通过Ctrl + c 中断训练并保存模型到weights文件夹
- 训练完成后会在weights文件夹生成权重模型,用于后续的预测。
2、预测eval.py
数据准备好以后后面的其实没太多好说的,直接看下面
–trained_model:选择权重模型
–image/images/video :选择是对单张图像预测还是一些图像或者视频文件
后面的是指定输入文件(夹)和输出文件夹,以 “ :”分隔
python eval.py --trained_model=weights/yolact_base_54_800000.pth --score_threshold=0.15 --top_k=15 --images=path/to/input/folder:path/to/output/folder
二、Yolact模型各代码部分解读
划 ############################################
是我修改的地方
1、data\config.py
1.1训练自己数据必修改的文件
config.py 比较重要的几个部分:
1) COCO_CLASSES :按labels.txt文件里的标签类型对应填好就行了
2) COCO_LABEL_MAP : 键值对数量与上面标签类型数量对应
3) DATASETS:主要包括 训练数据地址、验证数据地址指定class_name(COCO_CLASSES),指定label_map(COCO_LABEL_MAP)等
- dataset_base = Config({ }):默认基础设置,如果train.py和eval.py中不指定config的话就使用当前配置
4) YOLACT v1.0 CONFIGS :自定义数据集必修改的部分,最高级的参数配置部分。
- yolact_base_config = coco_base_config.copy({ }) : config 默认配置,他调用了上一部分的coco_base_config、DATASETS
2、train.py
2.1 训练时可指定的参数
- –batch_size:大小取决于你的显卡,报错就调小一点
- –num_workers:线程数,一般都是需要设置为 “0”
- –lr:学习率
- –save_folder:训练完后模型保存的路径
- –config:config配置文件选择,none为默认配置
- –save_interval:每10000次迭代保存模型权重
- –validation_epoch:每2个epoch进行一次验证计算
- –dataset:数据集路径
parser.add_argument('--batch_size', default=4, type=int, ########################################################################
help='Batch size for training')
parser.add_argument('--num_workers', default=0, type=int, #######################################################################
help='Number of workers used in dataloading')
parser.add_argument('--lr', '--learning_rate', default=None, type=float,
help='Initial learning rate. Leave as None to read this from the config.')
parser.add_argument('--momentum', default=None, type=float,
help='Momentum for SGD. Leave as None to read this from the config.')
parser.add_argument('--decay', '--weight_decay', default=None, type=float,
help='Weight decay for SGD. Leave as None to read this from the config.')
parser.add_argument('--save_folder', default='weights/',
help='Directory for saving checkpoint models.')
parser.add_argument('--config', default=None,
help='The config object to use.')
parser.add_argument('--save_interval', default=10000, type=int,
help='The number of iterations between saving the model.')
parser.add_argument('--validation_size', default=5000, type=int,
help='The number of images to use for validation.')
parser.add_argument('--validation_epoch', default=2, type=int,
help='Output validation information every n iterations. If -1, do no validation.')
parser.add_argument('--dataset', default=None, type=str,
help='If specified, override the dataset specified in the config with this one (example: coco2017_dataset).')
3、eval.py
3.1模型预测时可指定的参数
- –trained_model:选择模型文件
- –top_k:保存置信率最高的前15个目标
- –config:选择config配置文件
- –image:对单张图像进行预测,路径为单张图像
- –images:对多张图像/图像路径文件夹 进行预测
- –video:指定视频进行预测
- –score_threshold :剔除掉低于0.15置信度的目标
- –dataset:数据集路径
parser.add_argument('--trained_model',##########################################################################################
default='weights/yolact_base_290_16000.pth', type=str,
help='Trained state_dict file path to open. If "interrupt", this will open the interrupt file.')
parser.add_argument('--top_k', default=15, type=int, #################################################################
help='Further restrict the number of predictions to parse')
parser.add_argument('--config', default=None, ##############################################################################
help='The config object to use.')
parser.add_argument('--image', default=None, type=str,
help='A path to an image to use for display.')
parser.add_argument('--images', default='D:/y/XM_TEST/yolact-master/data/coco/real_test', type=str,################################################################################
help='An input folder of images and output folder to save detected images. Should be in the format input->output.')
parser.add_argument('--video', default=None, type=str,
help='A path to a video to evaluate on. Passing in a number will use that index webcam.')
parser.add_argument('--score_threshold', default=0.15, type=float, #################################################################################
help='Detections with a score under this threshold will not be considered. This currently only works in display mode.')
parser.add_argument('--dataset', default=None, type=str,
help='If specified, override the dataset specified in the config with this one (example: coco2017_dataset).')
三、报错解决
1、train报错
1.1 AssertionError: Image path does not exist: /mnt/sda/yolact/data/coco/images/train2017/000000423104.jpg
1.1和1.2都是每次训练完两个epoch就莫名其妙终止了。
解决办法:将验证集数据val中的数据复制到train文件夹的和训练数据放在一起。(不要问我为什么,我也没搞明白)https://github.com/dbolya/yolact/issues/379
1.2 TypeError: ‘NoneType’ object is not iterable
解决办法:数据集的问题,按照上面第一章json转coco部分重新准备数据集。
2、eval报错
2.1ValueError: not enough values to unpack
解决办法:eval中的884行的“ :”要与命令中的“ :”对应,如果还是不行就都改成“ :: ”https://github.com/dbolya/yolact/issues/99
四、训练自己的数据第二种办法
说明:从实战到调参训练自己样本数据的第二种办法,其实说白了想要训练无非以下几个步骤:
- 准备与之匹配的数据集(两组图像+汇总起来的两个json文件)
- 指定训练集样本数据地址、标签地址;指定验证集样本数据地址、标签地址。
- 指定标签种类、label_map
- 指定预训练模型
- 其他超参数如batchsize、预测样本地址等
1、准备coco数据集
参考我的另一篇文章,内容很详细
【Yolact数据集制作-labelme使用与转coco详解】
2、config配置
指定训练集样本数据地址、标签地址;指定验证集样本数据地址、标签地址。
-
config配置1
打开./data/config.py文件,在dataset下新添加内容如下。新添加数据集orange_dataset是copy的dataset_base,里面的内容是对dataset_base内容的重定义
class_name改为自己标签种类,label_map对种类数量对应
-
config配置2
定义自己的config配置,同样是copy的coco_base_config,内容是对coco_base_config的重定义
这里name、dataset、num_classes是必须要修改的
dataset是config配置1自己定义的;num_classes是自己样本的种类数+1;max_size我这里图像大小是384*384
3、开始训练train
训练之前,因为个人显卡比较低,改了两个数值
在terminal输入以下内容开始训练:
python ./train.py --config=yolact_resnet50_orange_sorting_config