前言
本文主要用于记录实例分割模型yolact和yolact++的环境配置,以及成功训练自己数据集的整个过程~
注意:这里要重点提醒一下,DCNv2对RTX系列不友好,我第一次使用4090服务器,编译持续有问题,被迫放弃,这里使用2080TI进行操作!
源码地址:https://github.com/dbolya/yolact
目录
- 一、Yolact环境配置
- 二、训练准备
- 三、yolact训练
- 四、 数据集测试
- 五、Yolact++环境配置
一、Yolact环境配置
基础环境:python=3.8、pytorch=1.7.0、cuda=11.0、ubuntu18.04
1、创建一个anaconda虚拟环境:
conda create -n yolact python=3.8 //环境名为yolact ,python版本选择3.8
conda activate yolact //激活yolact 环境
2.查看安装的:
nvcc --version #查看cuda版本python #查看pytorch
>>> import torch
>>> print(torch.cuda.is_available())
True
>>> print(torch.version.cuda)
11.0
3.安装所需要的依赖:
pip install cython
pip install opencv-python
pip install pillow
pip install pycocotools#用此方式:pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI
pip install matplotlib
二、训练准备
1、下载预训练权重:
把权重文件放到yolact-master下的weights文件夹里
下载地址:https://github.com/dbolya/yolact
2、数据集文件夹结构
coco文件夹,里面包含annotations以及images。其中annotations包含instances_train2017.json以及instances_val2017.json; images中包含train2017以及val2017用于存放训练与验证数据集。
3、参数修改
(1)coco_classes与coco_label_map
修改数据加载的位置即可,具体来说,找到./data/config.py目录,为自己的数据集格式
(2)dataset_base地址
将训练数据集和验证数据集的加载地址切换为自己的数据集地址,注意此处只要修改了images的地址就可以
(3)修改coco2014_dataset或coco2017_dataset的信息,如下:
(4)修改coco_base_config
此处的max_iter并不是控制训练轮数的
(5)修改yolact_base_config
(6)其他的训练参数在train.py文件处修改
parser = argparse.ArgumentParser(description='Yolact Training Script')
parser.add_argument('--batch_size', default=8, type=int,help='Batch size for training')
parser.add_argument('--resume', default=None, type=str,help='Checkpoint state_dict file to resume training from. If this is "interrupt"'\', the model will resume training from the interrupt file.')
parser.add_argument('--start_iter', default=-1, type=int,help='Resume training at this iter. If this is -1, the iteration will be'\'determined from the file name.')
parser.add_argument('--num_workers', default=4, type=int,help='Number of workers used in dataloading')
parser.add_argument('--cuda', default=True, type=str2bool,help='Use CUDA to train model')
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('--gamma', default=None, type=float,help='For each lr step, what to multiply the lr by. 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('--log_folder', default='logs/',help='Directory for saving logs.')
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('--keep_latest', dest='keep_latest', action='store_true',help='Only keep the latest checkpoint instead of each one.')
parser.add_argument('--keep_latest_interval', default=100000, type=int,help='When --keep_latest is on, don\'t delete the latest file at these intervals. This should be a multiple of save_interval or 0.')
parser.add_argument('--dataset', default=None, type=str,help='If specified, override the dataset specified in the config with this one (example: coco2017_dataset).')
parser.add_argument('--no_log', dest='log', action='store_false',help='Don\'t log per iteration information into log_folder.')
parser.add_argument('--log_gpu', dest='log_gpu', action='store_true',help='Include GPU information in the logs. Nvidia-smi tends to be slow, so set this with caution.')
parser.add_argument('--no_interrupt', dest='interrupt', action='store_false',help='Don\'t save an interrupt when KeyboardInterrupt is caught.')
parser.add_argument('--batch_alloc', default=None, type=str,help='If using multiple GPUS, you can set this to be a comma separated list detailing which GPUs should get what local batch size (It should add up to your total batch size).')
parser.add_argument('--no_autoscale', dest='autoscale', action='store_false',help='YOLACT will automatically scale the lr and the number of iterations depending on the batch size. Set this if you want to disable that.')
三、yolact训练
python train.py --config=yolact_base_config
训练如下:
四、 数据集测试
python eval.py --trained_model=weights/yolact_base_0_500.pth --benchmark --max_images=1000
效果如下: