-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:magic-research/Sa2VA
- Loading branch information
Showing
3 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
projects/llava_sam2/evaluation/dataset/base_eval_dataset.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from torch.utils.data import Dataset | ||
import copy | ||
from collections.abc import Mapping | ||
from typing import Union | ||
from mmengine.config import Config | ||
import logging | ||
from mmengine.fileio import list_from_file | ||
from mmengine.logging import print_log | ||
from abc import abstractmethod | ||
|
||
|
||
class BaseEvalDataset(Dataset): | ||
|
||
METAINFO: dict = dict(name='default') | ||
|
||
def __init__(self, metainfo: Union[Mapping, Config, None] = None): | ||
self._metainfo = self._load_metainfo(copy.deepcopy(metainfo)) | ||
|
||
@classmethod | ||
def _load_metainfo(cls, | ||
metainfo: Union[Mapping, Config, None] = None) -> dict: | ||
"""Collect meta information from the dictionary of meta. | ||
Args: | ||
metainfo (Mapping or Config, optional): Meta information dict. | ||
If ``metainfo`` contains existed filename, it will be | ||
parsed by ``list_from_file``. | ||
Returns: | ||
dict: Parsed meta information. | ||
""" | ||
# avoid `cls.METAINFO` being overwritten by `metainfo` | ||
cls_metainfo = copy.deepcopy(cls.METAINFO) | ||
if metainfo is None: | ||
return cls_metainfo | ||
if not isinstance(metainfo, (Mapping, Config)): | ||
raise TypeError('metainfo should be a Mapping or Config, ' | ||
f'but got {type(metainfo)}') | ||
|
||
for k, v in metainfo.items(): | ||
if isinstance(v, str): | ||
# If type of value is string, and can be loaded from | ||
# corresponding backend. it means the file name of meta file. | ||
try: | ||
cls_metainfo[k] = list_from_file(v) | ||
except (TypeError, FileNotFoundError): | ||
print_log( | ||
f'{v} is not a meta file, simply parsed as meta ' | ||
'information', | ||
logger='current', | ||
level=logging.WARNING) | ||
cls_metainfo[k] = v | ||
else: | ||
cls_metainfo[k] = v | ||
return cls_metainfo | ||
|
||
@property | ||
def metainfo(self) -> dict: | ||
"""Get meta information of dataset. | ||
Returns: | ||
dict: meta information collected from ``BaseDataset.METAINFO``, | ||
annotation file and metainfo argument during instantiation. | ||
""" | ||
return copy.deepcopy(self._metainfo) | ||
|
||
@abstractmethod | ||
def evaluate(self, results, work_dir): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters