在Faster RCNN中有使用一些额外的程序,类等等,为了使文章整体内容不过于分散,这里作为篇外卷学习。
文件地址:\py-faster-rcnn\lib\pycocotools
在这篇文章中讲解pycocotools中的coco.py,在所有代码中有两个coco.py代码
整个代码的主要作用就是数据的读取,下载,信息的分类。
先导入一些标准库和自定义的程序,其中mask在上篇文章中已经解析过了。
import json
import datetime
import time
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon
import numpy as np
from skimage.draw import polygon
import urllib
import copy
import itertools
import mask
import os
定义类:COCO,先对类进行初始化,在这个初始化过程中,如果annotation_file有输入,那么就读取相应的数据集(json.load(open(annotation_file, ‘r’)) ),并且赋值给self.dataset同时调用子函数createIndex(self)
class COCO:
def __init__(self, annotation_file=None):
# load dataset
self.dataset = {}
self.anns = []
self.imgToAnns = {}
self.catToImgs = {}
self.imgs = {}
self.cats = {}
if not annotation_file == None:
print 'loading annotations into memory...'
tic = time.time()
dataset = json.load(open(annotation_file, 'r'))
print 'Done (t=%0.2fs)'%(time.time()- tic)
self.dataset = dataset
self.createIndex()
定义子函数createIndex(self),
def createIndex(self):
# create index
print 'creating index...'
anns = {}
imgToAnns = {}
catToImgs = {}
cats = {}
imgs = {}
if 'annotations' in self.dataset:
imgToAnns = {ann['image_id']: [] for ann in self.dataset['annotations']}
anns = {ann['id']: [] for ann in self.dataset['annotations']}
for ann in self.dataset['annotations']: