基于pytorch的目标检测(rcnn+resnet50,行人数据集)

本文详细介绍了如何使用PyTorch框架结合ResNet50网络实现RCNN算法,针对行人检测任务进行深度学习模型训练。通过数据预处理、模型构建、训练过程及结果评估,展示了行人检测的完整流程。
摘要由CSDN通过智能技术生成
import torch
torch.cuda.is_available()
True
!pip install cython
# Install pycocotools, the version by default in Colab
# has a bug fixed in https://github.com/cocodataset/cocoapi/pull/354
!pip install -U 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'
Requirement already satisfied: cython in /home/qy/.conda/envs/pytorch/lib/python3.6/site-packages (0.29.19)
Collecting git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI
  Cloning https://github.com/cocodataset/cocoapi.git to /tmp/pip-req-build-al3q5nlj
  Running command git clone -q https://github.com/cocodataset/cocoapi.git /tmp/pip-req-build-al3q5nlj
Requirement already satisfied, skipping upgrade: setuptools>=18.0 in /home/qy/.conda/envs/pytorch/lib/python3.6/site-packages (from pycocotools==2.0) (46.4.0.post20200518)
Requirement already satisfied, skipping upgrade: cython>=0.27.3 in /home/qy/.conda/envs/pytorch/lib/python3.6/site-packages (from pycocotools==2.0) (0.29.19)
Collecting matplotlib>=2.1.0
  Downloading matplotlib-3.2.1-cp36-cp36m-manylinux1_x86_64.whl (12.4 MB)
[K     |████████████████████████████████| 12.4 MB 7.0 MB/s eta 0:00:01
[?25hCollecting kiwisolver>=1.0.1
  Downloading kiwisolver-1.2.0-cp36-cp36m-manylinux1_x86_64.whl (88 kB)
[K     |████████████████████████████████| 88 kB 837 kB/s  eta 0:00:01
[?25hCollecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1
  Downloading pyparsing-2.4.7-py2.py3-none-any.whl (67 kB)
[K     |████████████████████████████████| 67 kB 867 kB/s s eta 0:00:01
[?25hRequirement already satisfied, skipping upgrade: python-dateutil>=2.1 in /home/qy/.conda/envs/pytorch/lib/python3.6/site-packages (from matplotlib>=2.1.0->pycocotools==2.0) (2.8.1)
Collecting cycler>=0.10
  Downloading cycler-0.10.0-py2.py3-none-any.whl (6.5 kB)
Requirement already satisfied, skipping upgrade: numpy>=1.11 in /home/qy/.conda/envs/pytorch/lib/python3.6/site-packages (from matplotlib>=2.1.0->pycocotools==2.0) (1.18.1)
Requirement already satisfied, skipping upgrade: six>=1.5 in /home/qy/.conda/envs/pytorch/lib/python3.6/site-packages (from python-dateutil>=2.1->matplotlib>=2.1.0->pycocotools==2.0) (1.14.0)
Building wheels for collected packages: pycocotools
  Building wheel for pycocotools (setup.py) ... [?25ldone
[?25h  Created wheel for pycocotools: filename=pycocotools-2.0-cp36-cp36m-linux_x86_64.whl size=282788 sha256=5e2c6293ef8c17974cee3aeccb1cce1b9c57e5890a4670db02b4e46cbe90bef6
  Stored in directory: /tmp/pip-ephem-wheel-cache-p2ts_ztv/wheels/25/c1/63/8bee2969883497d2785c9bdbe4e89cae5efc59521553d528bf
Successfully built pycocotools
Installing collected packages: kiwisolver, pyparsing, cycler, matplotlib, pycocotools
Successfully installed cycler-0.10.0 kiwisolver-1.2.0 matplotlib-3.2.1 pycocotools-2.0 pyparsing-2.4.7
from PIL import Image
Image.open('PennFudanPed/PNGImages/FudanPed00001.png')

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-65KDMJhp-1590725697882)(output_2_0.png)]

mask = Image.open('PennFudanPed/PedMasks/FudanPed00001_mask.png')
# each mask instance has a different color, from zero to N, where
# N is the number of instances. In order to make visualization easier,
# let's adda color palette to the mask.
mask.putpalette([
    0, 0, 0, # black background
    255, 0, 0, # index 1 is red
    255, 255, 0, # index 2 is yellow
    255, 153, 0, # index 3 is orange
])
mask

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kYicYkPu-1590725697884)(output_3_0.png)]

import os
import numpy as np
import torch
import torch.utils.data
from PIL import Image


class PennFudanDataset(torch.utils.data.Dataset):
    def __init__(self, root, transforms=None):
        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)

        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]

        # get bounding box coordinates for each mask
        num_objs = len(obj_ids)
        boxes = []
        for i in range(num_objs):
            pos = np.where(masks[i])
            xmin = np.min(pos[1])
            xmax = np.max(pos[1])
            ymin = np.min(pos[0])
            ymax = np.max(pos[0])
            boxes.append([xmin, ymin, xmax, ymax])

        boxes = torch.as_tensor(boxes, dtype=torch.float32)
        # there is only one class
        labels = torch.ones((num_objs,), dtype=torch.int64)
        masks = torch.as_tensor(masks, dtype=torch.uint8)

        image_id = torch.tensor([idx])
        area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
        # suppose all instances are not crowd
        iscrowd = torch.zeros((num_objs,), dtype
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值