!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')
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 background255,0,0,# index 1 is red255,255,0,# index 2 is yellow255,153,0,# index 3 is orange])
mask
import os
import numpy as np
import torch
import torch.utils.data
from PIL import Image
classPennFudanDataset(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 inrange(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