告别项目中“依index生成路径”的方法,直接使用我们在生成.json标签时就已经写入的图片路径(这里我写入的是绝对路径 full path)来获取图片。
需要做的,用以下代码替换SNIPER/lib/dataset/coco.py
# ---------------------------------------------------------------
# SNIPER: Efficient Multi-scale Training
# Licensed under The Apache-2.0 License [see LICENSE for details]
# Modified from https://github.com/msracver/Deformable-ConvNets
# Modified by Mahyar Najibi
# Second Modified by aimhabo
# ---------------------------------------------------------------
import cPickle
import os
import json
import numpy as np
from imdb import IMDB
# coco api
from .pycocotools.coco import COCO
from .pycocotools.cocoeval import COCOeval
from mask.mask_voc2coco import mask_voc2coco
from bbox.bbox_transform import clip_boxes, bbox_overlaps_py
import multiprocessing as mp
def coco_results_one_category_kernel(data_pack):
cat_id = data_pack['cat_id']
ann_type = data_pack['ann_type']
binary_thresh = data_pack['binary_thresh']
all_im_info = data_pack['all_im_info']
boxes = data_pack['boxes']
if ann_type == 'bbox':
masks = []
elif ann_type == 'segm':
masks = data_pack['masks']
else:
print 'unimplemented ann_type: ' + ann_type
cat_results = []
for im_ind, im_info in enumerate(all_im_info):
index = im_info['index']
dets = boxes[im_ind].astype(np.float)
if len(dets) == 0:
continue
scores = dets[:, -1]
if ann_type == 'bbox':
xs = dets[:, 0]
ys = dets[:, 1]
ws = dets[:, 2] - xs + 1
hs = dets[:, 3] - ys + 1
result = [{'image_id': index,
'category_id': cat_id,
'bbox': [round(xs[k], 1), round(ys[k], 1), round(ws[k], 1), round(hs[k], 1)],
'score': round(scores[k], 8)} for k in xrange(dets.shape[0])]
elif ann_type == 'segm':
width = im_info['width']
height = im_info['height']
dets[:, :4] = clip_boxes(dets[:, :4], [height, width])
mask_encode = mask_voc2coco(masks[im_ind], dets[:, :4], height, width, binary_thresh)
result = [{'image_id': index,
'category_id': cat_id,
'segmentation': mask_encode[k],
'score': scores[k]} for k in xrange(len(mask_encode))]
cat_results.extend(result)
return cat_results
class coco(IMDB):
def __init__(self, image_set, root_path, data_path, result_path=None, mask_size=-1, binary_thresh=None,
load_mask=False):
"""
fill basic information to initialize imdb
:param image_set: train2014, val2014, test2015
:param root_path: 'data', will write 'rpn_data', 'cache'
:param data_path: 'data/coco'
"""
super(coco, self).__init__('COCO', image_set, root_path, data_path, result_path)
self.root_path = root_path
self.data_path = data_path
self.coco = COCO(self._get_ann_file())
# deal with class names
cats = [cat['name'] for cat in self.coco.loadCats(self.coco.getCatIds())]
self.classes = ['__background__'] + cats
self.num_classes = len(self.classes)
self._class_to_ind = dict(zip(self.classes, xrange(self.num_classes)))
self._class_to_coco_ind = dict(zip(cats, self.coco.getCatIds()))
self._coco_ind_to_class_ind = dict([(self._class_to_coco_ind[cls], self._class_to_ind[cls])
for cls in self.classes[1:]])
# load image file names
self.image_set_index = self._load_image_set_index()
self.num_images = len(self.image_set_index)
print 'num_images', self.num_images
self.mask_size = mask_size
self.binary_thresh = binary_thresh
self.load_mask = load_mask
# deal with data name
view_map = {'minival2014': 'val2014',
'sminival2014': 'val2014',
'valminusminival2014': 'val2014',
'test-dev2015': 'test2015'}
self.data_name = view_map[image_set] if image_set in view_map else image_set
self.image_id = self.coco.getImgIds()
# print 'image_id = ', self.image_id
self.file_name = [