实例分割-Yolact/Yolact++训练自己数据集

前言
本文主要用于记录实例分割模型yolact和yolact++的环境配置,以及成功训练自己数据集的整个过程~

注意:这里要重点提醒一下,DCNv2对RTX系列不友好,我第一次使用4090服务器,编译持续有问题,被迫放弃,这里使用2080TI进行操作!

源码地址https://github.com/dbolya/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

在这里插入图片描述
权重文件地址:https://pan.baidu.com/s/1nsU6R61FWDY98OjgYJ7rEw 提取码: vb78

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
在这里插入图片描述

注:iteration、batch和epoch的概念及区别

iteration:迭代次数,指的是模型在训练过程中参数更新的次数。
batch_size:批次,指的是一次迭代中使用的数据量。
epoch:训练轮数,指的是模型在整个数据集上训练的次数。
关系:
一个epoch中,数据被分为多个batch,1个iteration等于使用batch size个样本训练一次。训练一个Batch就是一次Iteration,就更新一次参数。
举个例子:
如果我们有一个包含 1000 (number of samples)个样本的数据集,每个batch的大小为 50(batch size),那么一个 epoch 中就会有 1000 / 50 = 20 个 batch,每个 batch 包含 50 个样本。
如果我们将模型训练 10 个 epoch,那么模型会在整个数据集上训练 10 次。
共进行10*20 =200 次迭代。

(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

效果如下:

在这里插入图片描述

五、Yolact++环境配置

由于前面使用pytorch=1.7.0和python=3.8出现DCNv2编译报错的情况,这里更换基础环境为:python=3.7、pytorch=1.1.0、cuda=10.0、ubuntu18.04(这里是运行yolact++报错最多的地方)

1、在yolact安装好的环境下,配置yolact++,yolact++使用DCNv2模块,需提前对其编译。

cd external/DCNv2
python setup.py build develop

编译成功!

在这里插入图片描述

2、开始训练

python train.py --config=yolact_plus_base_config

训练如下:
在这里插入图片描述

3、图片推理

#单张
python eval.py --config=yolact_plus_base_config --trained_model=weights/yolact_plus_base_79_68000.pth --score_threshold=0.15 --top_k=15 --image=/root/yolact-master/images/2.png:2png
#多张
python eval.py --config=yolact_plus_base_config --trained_model=weights/yolact_plus_base_79_68000.pth --score_threshold=0.15 --top_k=15 --images=/root/yolact-master/data/coco/images/val2017:images

效果如下:

在这里插入图片描述

### 使用 YOLACT 训练自定义数据集上的实例分割模型 为了使 YOLACT 模型能够处理特定的数据集,需遵循一系列配置调整和准备步骤。这不仅涉及环境搭建还涵盖了数据预处理以及参数设置。 #### 准备工作 确保已成功安装 YOLACT 所依赖的所有库并验证其版本兼容性[^1]。对于新数据集的支持,通常需要创建符合 COCO 数据格式的标注文件,该文件描述了每张图片中的对象类别及其对应的掩码信息。可以利用工具如 Labelme 或 VGG Image Annotator (VIA) 来辅助完成这项任务。 #### 配置数据路径与标签映射 修改 `data/config.py` 文件内的相应部分以指明本地存储的新数据位置,并更新类别的名称列表以便于识别不同类型的物体。此外,在同一目录下还需提供一个 JSON 文件用于保存上述提到过的 COCO 格式的标注记录[^3]。 #### 修改训练脚本 编辑位于 `train.py` 中的部分超参设定,比如批次大小(batch size),学习率(learning rate)等,使之更贴合当前硬件条件和个人需求。值得注意的是,如果打算继续从前一次中断处恢复训练,则应指定加载之前保存下来的权重作为初始化起点[^4]。 ```python import torch from yolact import Yolact from data.config import cfg, set_cfg set_cfg('custom_dataset') # 设置为自定义数据集模式 net = Yolact() net.train() # 加载预训练模型或先前保存的状态字典 if args.resume is not None: net.load_weights(args.resume) optimizer = torch.optim.SGD(net.parameters(), lr=cfg.lr_0, momentum=cfg.momentum, weight_decay=cfg.weight_decay) ``` #### 启动训练流程 一切就绪后即可执行训练指令: ```bash python train.py --config=yolact_custom_config --dataset=/path/to/your/dataset \ --num_workers=8 --batch_size=4 --lr=1e-4 --max_iter=200000 ``` 此命令行假设读者已经准备好了一个名为 `yolact_custom_config.yaml` 的配置文档来指导整个过程;同时设定了多线程读取样本(`--batch_size`)、初始的学习速率(`--lr`)及最大循环次数(`--max_iter`)等重要选项[^2]。
评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值