7 PyTorch 官网教材之 TORCHVISION 0.3 OBJECT DETECTION FINETUNING TUTORIAL

本文是PyTorch官网提供的torchvision 0.3目标检测微调教程,涵盖从构建数据集到定义模型的全过程。教程详细介绍了如何从预训练模型进行微调,修改模型添加不同backbone,实例分割模型的应用,以及整合所有组件的main函数。
摘要由CSDN通过智能技术生成


6 PyTorch 官网教材之 TORCHVISION 0.3 OBJECT DETECTION FINETUNING TUTORIAL

0. 官网链接

1. TORCHVISION 0.3 OBJECT DETECTION FINETUNING TUTORIAL
在这里插入图片描述

1. 构建数据集

import os
import numpy as np
import torch
from PIL import Image


class PennFudanDataset(object):
    def __init__(self, root, transforms):  # 根据路径 root 分别加载 imgs 和 masks 的名称列表
        self.root = root
        self.transforms = transforms
        # load all image files, sorting them to
        # ensure that they are aligned
        self.imgs = list(sorted(os.listdir(os.path.join(root, "PNGImages"))))
        self.masks = list(sorted(os.listdir(os.path.join(root, "PedMasks"))))

    def __getitem__(self, idx):
        # load images ad masks
        img_path = os.path.join(self.root, "PNGImages", self.imgs[idx])
        mask_path = os.path.join(self.root, "PedMasks", self.masks[idx])
        img = Image.open(img_path).convert("RGB")
        # note that we haven't converted the mask to RGB,
        # because each color corresponds to a different instance
        # with 0 being background
        mask = Image.open(mask_path)
        # convert the PIL Image into a numpy array
        mask = np.array(mask)
        # instances are encoded as different colors
        obj_ids = np.unique(mask)  # 去除一维元组、列表中的重复元素 
        # first id is the background, so remove it
        obj_ids = obj_ids[1:]

        # split the color-encoded mask into a set
        # of binary masks
        masks = mask == obj_ids[:, None, None]   #  ? 这两个None 不理解

        # get bounding box coordinates for each mask
        num_objs &
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值