Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Use pycocotools instead of mmpycocotools #4939

Merged
merged 5 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions configs/lvis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@
* Run following scripts to install our forked lvis-api.

```shell
# mmlvis is fully compatible with official lvis
pip install mmlvis
```

or

```shell
pip install -r requirements/optional.txt
pip install git+https://github.com/lvis-dataset/lvis-api.git
```

* All experiments use oversample strategy [here](../../docs/tutorials/new_dataset.md#class-balanced-dataset) with oversample threshold `1e-3`.
Expand Down
3 changes: 3 additions & 0 deletions mmdet/datasets/api_wrappers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .coco_api import COCO, COCOeval

__all__ = ['COCO', 'COCOeval']
39 changes: 39 additions & 0 deletions mmdet/datasets/api_wrappers/coco_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This file add snake case alias for coco api

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.
"""

ZwwWayne marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, annotation_file=None):
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
10 changes: 1 addition & 9 deletions mmdet/datasets/coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@

import mmcv
import numpy as np
import pycocotools
from mmcv.utils import print_log
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
from terminaltables import AsciiTable

from mmdet.core import eval_recalls
from .api_wrappers import COCO, COCOeval
from .builder import DATASETS
from .custom import CustomDataset

Expand Down Expand Up @@ -44,12 +42,6 @@ def load_annotations(self, ann_file):
Returns:
list[dict]: Annotation info from COCO api.
"""
if not getattr(pycocotools, '__version__', '0') >= '12.0.2':
raise AssertionError(
'Incompatible version of pycocotools is installed. '
'Run pip uninstall pycocotools first. Then run pip '
'install mmpycocotools to install open-mmlab forked '
'pycocotools.')

self.coco = COCO(ann_file)
self.cat_ids = self.coco.get_cat_ids(cat_names=self.CLASSES)
Expand Down
39 changes: 9 additions & 30 deletions mmdet/datasets/lvis.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,18 +276,11 @@ def load_annotations(self, ann_file):
"""

try:
import lvis
assert lvis.__version__ >= '10.5.3'
from lvis import LVIS
except AssertionError:
raise AssertionError('Incompatible version of lvis is installed. '
'Run pip uninstall lvis first. Then run pip '
'install mmlvis to install open-mmlab forked '
'lvis. ')
except ImportError:
raise ImportError('Package lvis is not installed. Please run pip '
'install mmlvis to install open-mmlab forked '
'lvis.')
raise ImportError(
'Package lvis is not installed. Please run "pip install git+https://github.com/lvis-dataset/lvis-api.git".' # noqa: E501
)
self.coco = LVIS(ann_file)
self.cat_ids = self.coco.get_cat_ids()
self.cat2label = {cat_id: i for i, cat_id in enumerate(self.cat_ids)}
Expand Down Expand Up @@ -336,18 +329,11 @@ def evaluate(self,
"""

try:
import lvis
assert lvis.__version__ >= '10.5.3'
from lvis import LVISResults, LVISEval
except AssertionError:
raise AssertionError('Incompatible version of lvis is installed. '
'Run pip uninstall lvis first. Then run pip '
'install mmlvis to install open-mmlab forked '
'lvis. ')
except ImportError:
raise ImportError('Package lvis is not installed. Please run pip '
'install mmlvis to install open-mmlab forked '
'lvis.')
raise ImportError(
'Package lvis is not installed. Please run "pip install git+https://github.com/lvis-dataset/lvis-api.git".' # noqa: E501
)
assert isinstance(results, list), 'results must be a list'
assert len(results) == len(self), (
'The length of results is not equal to the dataset len: {} != {}'.
Expand Down Expand Up @@ -714,18 +700,11 @@ class LVISV1Dataset(LVISDataset):

def load_annotations(self, ann_file):
try:
import lvis
assert lvis.__version__ >= '10.5.3'
from lvis import LVIS
except AssertionError:
raise AssertionError('Incompatible version of lvis is installed. '
'Run pip uninstall lvis first. Then run pip '
'install mmlvis to install open-mmlab forked '
'lvis. ')
except ImportError:
raise ImportError('Package lvis is not installed. Please run pip '
'install mmlvis to install open-mmlab forked '
'lvis.')
raise ImportError(
'Package lvis is not installed. Please run "pip install git+https://github.com/lvis-dataset/lvis-api.git".' # noqa: E501
)
self.coco = LVIS(ann_file)
self.cat_ids = self.coco.get_cat_ids()
self.cat2label = {cat_id: i for i, cat_id in enumerate(self.cat_ids)}
Expand Down
1 change: 0 additions & 1 deletion requirements/optional.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
albumentations>=0.3.2
cityscapesscripts
imagecorruptions
mmlvis
ZwwWayne marked this conversation as resolved.
Show resolved Hide resolved
scipy
sklearn
2 changes: 1 addition & 1 deletion requirements/runtime.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
matplotlib
mmpycocotools
numpy
pycocotools
six
terminaltables