swin-object-detection遇到的问题

Author Avatar
patrickcty 6月 25, 2021

在配置 Swin-Transformer-Object-Detection 的过程中出现了 pycocotools 不兼容额问题,但是另一个类似的环境在安装 mmdetection 的时候并没有问题。(pycocotools 版本相同)

于是按照 mmdetection 的结构对 mmdet/datasets/coco.py 进行修改。

修改 COCOCOCOEval 的引用:

from .api_wrappers import COCO, COCOeval

去掉 get_img_ids 的参数

self.img_ids = self.coco.get_img_ids()

新建 mmdet/datasets/api_wrappers/coco_api.py,将以下内容粘贴进去

# This file add snake case alias for coco api

import warnings

import pycocotools
from pycocotools.coco import COCO as _COCO
from pycocotools.cocoeval import COCOeval as _COCOeval


class COCO(_COCO):
    """This class is almost the same as official pycocotools package.

    It implements some snake case function aliases. So that the COCO class has
    the same interface as LVIS class.
    """

    def __init__(self, annotation_file=None):
        if getattr(pycocotools, '__version__', '0') >= '12.0.2':
            warnings.warn(
                'mmpycocotools is deprecated. Please install official pycocotools by "pip install pycocotools"',  # noqa: E501
                UserWarning)
        super().__init__(annotation_file=annotation_file)
        self.img_ann_map = self.imgToAnns
        self.cat_img_map = self.catToImgs

    def get_ann_ids(self, img_ids=[], cat_ids=[], area_rng=[], iscrowd=None):
        return self.getAnnIds(img_ids, cat_ids, area_rng, iscrowd)

    def get_cat_ids(self, cat_names=[], sup_names=[], cat_ids=[]):
        return self.getCatIds(cat_names, sup_names, cat_ids)

    def get_img_ids(self, img_ids=[], cat_ids=[]):
        return self.getImgIds(img_ids, cat_ids)

    def load_anns(self, ids):
        return self.loadAnns(ids)

    def load_cats(self, ids):
        return self.loadCats(ids)

    def load_imgs(self, ids):
        return self.loadImgs(ids)


# just for the ease of import
COCOeval = _COCOeval