YOLACT/YOLACT++安装测试与微调训练


简介

YOLACT / YOLACT++ 是在one-stage型检测器上添加一个mask分支来达到实例分割.

论文地址:

优势:

  1. 速度快,因为one-stage;
  2. mask质量高,因为不包含repooling类操作;
  3. 普适性强,这种生成原型mask和mask系数的思路可以应用在目前很多留下的检测器上。

性能对比:
与其他一些现有的实例分割模型在COCO数据集上的表现对比如下图所示

结果展示:

YOLACT 安装与测试

安装:

  • github开源地址: https://github.com/dbolya/yolact

  • 安装依赖: Pytorch 1.0.1 (or higher), TorchVision, cython, opencv-python, pillow , matplotlib

  • 由于pycocotools 并不支持Windows系统, pip无法直接安装, 需要折腾一下 查看教程:

    • 下载 Visual C++ 2015 Build Tools, 默认路径安装
    • 执行pip命令: pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI github访问不稳定时, 多试几次
  • 进入项目目录.\yolact-master\

  • 下载 预训练权重.\yolact-master\weights\文件夹

图像输入测试:

  • 将自己的测试图像放在该目录下.\yolact-master\test.jpg
  • 执行命令
python eval.py --trained_model=weights/yolact_base_54_800000.pth --score_threshold=0.15 --top_k=15 --image=test.jpg


webcam测试:
官方提供了测试api, 但我这里未能运行
python eval.py --trained_model=weights/yolact_base_54_800000.pth --score_threshold=0.15 --top_k=15 --video_multiframe=4 --video=0

opencv开启摄像头测试:

  • 完整代码已经上传: 点击下载 删除了命令参数输入与解析代码, 可以直接运行
  • 关键代码如下: 使用opencv打开摄像头, 并显示检测结果
def evalimage(net: Yolact, camera_index=0):
    camera = cv.VideoCapture(camera_index, cv.CAP_DSHOW)
    if not camera.isOpened():
        print("摄像头开启失败!")
    while True:
        ret, frame = camera.read()
        if not ret:
            continue
        frame = cv.flip(frame, 1)
        image = torch.from_numpy(frame).cuda().float()
        batch = FastBaseTransform()(image.unsqueeze(0))
        preds = net(batch)
        img_numpy = prep_display(preds, image, None, None, undo_transform=False)
        cv.imshow("YOLACT", img_numpy)
        if cv.waitKey(33) == 27:
            break
    cv.destroyAllWindows()
    camera.release()
    return

运行结果如下:

YOLACT ++ 安装与测试

DCNv2 安装失败, 暂未解决…

YOLACT 重训练: 人体识别

参考:

目标: 调整coco数据集, 重新训练模型, 只对 Person 类进行识别

调整COCO数据集

Just open the COCO 2017 train and validation json annotations (data/coco/annotations) in python or something and in the annotations array, delete every annotation with category ID not equal to 1 (person). Then save these modified annotations files and set up a custom dataset as specified in the readme.

代码如下:

import json

input_file = "instances_val2017.json"
output_file = "instances_val2017_person.json"
with open(input_file, 'r') as f:
    coco = json.load(f)

# 删除不相关的类别标注
annotation = coco['annotations']
del_list = []
for i, item in enumerate(annotation):
    if item["category_id"] != 1:
        del_list.append(i)
print(f"需要删除{len(del_list)}条不相关条目")
print("删除前的标注条目: ", len(coco['annotations']))
for i in sorted(del_list, reverse=True):
    del annotation[i]
    print("\r正在删除不相关条目", i, end="")
print("\n删除后的标注条目: ", len(coco['annotations']))

# 删除不相关的类别
del coco['categories'][1:]
print("删除后的类别数: ", len(coco['categories']))

# 保存
if len(del_list):
    print("正在写入Json文件...", end="")
    with open(output_file, 'w') as f:
        json.dump(coco, f, ensure_ascii=False)
    print("done!")

微调训练

只需要在config.py构造person数据集和配置参数

coco2017_dataset_person = dataset_base.copy({
    'name': 'COCO 2017 Person',
    'train_images': '你的路径/COCO2017/train2017',
    'train_info': '你的路径/COCO2017/annotations/instances_train2017_person.json',
    'valid_images': '你的路径/COCO2017/val2017',
    'valid_info': '你的路径/COCO2017/annotations/instances_val2017_person.json',
    'class_names': ('person',),
})
yolact_coco_person_config = yolact_base_config.copy({
    'name': 'yolact_coco_person',
    'dataset': coco2017_dataset_person,
    'num_classes': len(coco2017_dataset_person.class_names) + 1,
    # Training params
    'lr_steps': (200000, 300000, 330000, 380000),
    'max_iter': 400000,
})

修改模型文件yolact.py

调用训练命令:

python train.py --config=yolact_coco_person_config --resume=weights/yolact_base_54_800000.pth --start_iter=0

完整项目及微调模型见 https://download.csdn.net/download/Augurlee/12099955 可直接用于人体检测与分割

全部评论

相关推荐

头像
04-09 14:29
Java
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务