Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ICV-mmengine_basecode
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Florian Schiffel
ICV-mmengine_basecode
Commits
db2b45b7
Unverified
Commit
db2b45b7
authored
2 years ago
by
Mashiro
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[Fix] Fix the probable failure of the basedataset unit test (#210)
parent
8668c14d
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/test_data/test_base_dataset.py
+51
-55
51 additions, 55 deletions
tests/test_data/test_base_dataset.py
with
51 additions
and
55 deletions
tests/test_data/test_base_dataset.py
+
51
−
55
View file @
db2b45b7
...
...
@@ -33,29 +33,28 @@ class CustomDataset(BaseDataset):
class
TestBaseDataset
:
dataset_type
=
BaseDataset
d
ata_info
=
dict
(
filename
=
'
test_img.jpg
'
,
height
=
604
,
width
=
640
,
sample_idx
=
0
)
imgs
=
torch
.
rand
((
2
,
3
,
32
,
32
)
)
pipeline
=
MagicMock
(
return_value
=
dict
(
imgs
=
imgs
))
METAINFO
:
dict
=
dict
()
parse_data_info
=
MagicMock
(
return_value
=
data_info
)
def
_init_dataset
(
self
):
self
.
dataset_type
.
METAINFO
=
self
.
METAINFO
self
.
dataset_type
.
parse_data_info
=
self
.
parse_data_info
d
ef
setup
(
self
):
self
.
data_info
=
dict
(
filename
=
'
test_img.jpg
'
,
height
=
604
,
width
=
640
,
sample_idx
=
0
)
self
.
imgs
=
torch
.
rand
((
2
,
3
,
32
,
32
))
self
.
ori_meta
=
BaseDataset
.
METAINFO
BaseDataset
.
parse_data_info
=
MagicMock
(
return_value
=
self
.
data_info
)
self
.
pipeline
=
MagicMock
(
return_value
=
dict
(
imgs
=
self
.
imgs
))
def
teardown
(
self
):
BaseDataset
.
METAINFO
=
self
.
ori_meta
def
test_init
(
self
):
self
.
_init_dataset
()
# test the instantiation of self.base_dataset
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
)
assert
dataset
.
_fully_initialized
assert
hasattr
(
dataset
,
'
data_list
'
)
assert
hasattr
(
dataset
,
'
data_address
'
)
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
None
),
ann_file
=
'
annotations/dummy_annotation.json
'
)
...
...
@@ -65,7 +64,7 @@ class TestBaseDataset:
# test the instantiation of self.base_dataset with
# `serialize_data=False`
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
,
...
...
@@ -77,7 +76,7 @@ class TestBaseDataset:
assert
dataset
.
get_data_info
(
0
)
==
self
.
data_info
# test the instantiation of self.base_dataset with lazy init
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
,
...
...
@@ -88,40 +87,40 @@ class TestBaseDataset:
# test the instantiation of self.base_dataset if ann_file is not
# existed.
with
pytest
.
raises
(
FileNotFoundError
):
self
.
d
ataset
_type
(
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/not_existed_annotation.json
'
)
# Use the default value of ann_file, i.e., ''
with
pytest
.
raises
(
FileNotFoundError
):
self
.
d
ataset
_type
(
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
))
# test the instantiation of self.base_dataset when the ann_file is
# wrong
with
pytest
.
raises
(
ValueError
):
self
.
d
ataset
_type
(
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/annotation_wrong_keys.json
'
)
with
pytest
.
raises
(
TypeError
):
self
.
d
ataset
_type
(
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/annotation_wrong_format.json
'
)
with
pytest
.
raises
(
TypeError
):
self
.
d
ataset
_type
(
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
[
'
img
'
]),
ann_file
=
'
annotations/annotation_wrong_format.json
'
)
# test the instantiation of self.base_dataset when `parse_data_info`
# return `list[dict]`
self
.
d
ataset
_type
.
parse_data_info
=
MagicMock
(
BaseD
ataset
.
parse_data_info
=
MagicMock
(
return_value
=
[
self
.
data_info
,
self
.
data_info
.
copy
()])
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
)
...
...
@@ -136,24 +135,23 @@ class TestBaseDataset:
# test the instantiation of self.base_dataset when `parse_data_info`
# return unsupported data.
with
pytest
.
raises
(
TypeError
):
self
.
d
ataset
_type
.
parse_data_info
=
MagicMock
(
return_value
=
'
xxx
'
)
dataset
=
self
.
d
ataset
_type
(
BaseD
ataset
.
parse_data_info
=
MagicMock
(
return_value
=
'
xxx
'
)
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
)
with
pytest
.
raises
(
TypeError
):
self
.
d
ataset
_type
.
parse_data_info
=
MagicMock
(
BaseD
ataset
.
parse_data_info
=
MagicMock
(
return_value
=
[
self
.
data_info
,
'
xxx
'
])
self
.
d
ataset
_type
(
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
)
def
test_meta
(
self
):
self
.
_init_dataset
()
# test dataset.metainfo with setting the metainfo from annotation file
# as the metainfo of self.base_dataset.
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
)
...
...
@@ -163,10 +161,10 @@ class TestBaseDataset:
# test dataset.metainfo with setting METAINFO in self.base_dataset
dataset_type
=
'
new_dataset
'
self
.
d
ataset
_type
.
METAINFO
=
dict
(
BaseD
ataset
.
METAINFO
=
dict
(
dataset_type
=
dataset_type
,
classes
=
(
'
dog
'
,
'
cat
'
))
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
)
...
...
@@ -178,12 +176,12 @@ class TestBaseDataset:
# test dataset.metainfo with passing metainfo into self.base_dataset
metainfo
=
dict
(
classes
=
(
'
dog
'
,
),
task_name
=
'
new_task
'
)
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
,
metainfo
=
metainfo
)
assert
self
.
d
ataset
_type
.
METAINFO
==
dict
(
assert
BaseD
ataset
.
METAINFO
==
dict
(
dataset_type
=
dataset_type
,
classes
=
(
'
dog
'
,
'
cat
'
))
assert
dataset
.
metainfo
==
dict
(
dataset_type
=
dataset_type
,
...
...
@@ -192,8 +190,8 @@ class TestBaseDataset:
empty_list
=
[])
# reset `base_dataset.METAINFO`, the `dataset.metainfo` should not
# change
self
.
d
ataset
_type
.
METAINFO
[
'
classes
'
]
=
(
'
dog
'
,
'
cat
'
,
'
fish
'
)
assert
self
.
d
ataset
_type
.
METAINFO
==
dict
(
BaseD
ataset
.
METAINFO
[
'
classes
'
]
=
(
'
dog
'
,
'
cat
'
,
'
fish
'
)
assert
BaseD
ataset
.
METAINFO
==
dict
(
dataset_type
=
dataset_type
,
classes
=
(
'
dog
'
,
'
cat
'
,
'
fish
'
))
assert
dataset
.
metainfo
==
dict
(
dataset_type
=
dataset_type
,
...
...
@@ -206,7 +204,7 @@ class TestBaseDataset:
metainfo
=
dict
(
classes
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/meta/classes.txt
'
))
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
,
...
...
@@ -221,7 +219,7 @@ class TestBaseDataset:
# self.base_dataset
with
pytest
.
raises
(
TypeError
):
metainfo
=
'
dog
'
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
,
...
...
@@ -230,7 +228,7 @@ class TestBaseDataset:
# test dataset.metainfo with passing metainfo into self.base_dataset
# and lazy_init is True
metainfo
=
dict
(
classes
=
(
'
dog
'
,
))
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
,
...
...
@@ -243,26 +241,26 @@ class TestBaseDataset:
# test whether self.base_dataset.METAINFO is changed when a customize
# dataset inherit self.base_dataset
# test reset METAINFO in ToyDataset.
class
ToyDataset
(
self
.
d
ataset
_type
):
class
ToyDataset
(
BaseD
ataset
):
METAINFO
=
dict
(
xxx
=
'
xxx
'
)
assert
ToyDataset
.
METAINFO
==
dict
(
xxx
=
'
xxx
'
)
assert
self
.
d
ataset
_type
.
METAINFO
==
dict
(
assert
BaseD
ataset
.
METAINFO
==
dict
(
dataset_type
=
dataset_type
,
classes
=
(
'
dog
'
,
'
cat
'
,
'
fish
'
))
# test update METAINFO in ToyDataset.
class
ToyDataset
(
self
.
d
ataset
_type
):
METAINFO
=
copy
.
deepcopy
(
self
.
d
ataset
_type
.
METAINFO
)
class
ToyDataset
(
BaseD
ataset
):
METAINFO
=
copy
.
deepcopy
(
BaseD
ataset
.
METAINFO
)
METAINFO
[
'
classes
'
]
=
(
'
bird
'
,
)
assert
ToyDataset
.
METAINFO
==
dict
(
dataset_type
=
dataset_type
,
classes
=
(
'
bird
'
,
))
assert
self
.
d
ataset
_type
.
METAINFO
==
dict
(
assert
BaseD
ataset
.
METAINFO
==
dict
(
dataset_type
=
dataset_type
,
classes
=
(
'
dog
'
,
'
cat
'
,
'
fish
'
))
@pytest.mark.parametrize
(
'
lazy_init
'
,
[
True
,
False
])
def
test_length
(
self
,
lazy_init
):
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
,
...
...
@@ -311,7 +309,7 @@ class TestBaseDataset:
@pytest.mark.parametrize
(
'
lazy_init
'
,
[
True
,
False
])
def
test_getitem
(
self
,
lazy_init
):
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
,
...
...
@@ -353,8 +351,7 @@ class TestBaseDataset:
@pytest.mark.parametrize
(
'
lazy_init
'
,
[
True
,
False
])
def
test_get_data_info
(
self
,
lazy_init
):
self
.
_init_dataset
()
dataset
=
self
.
dataset_type
(
dataset
=
BaseDataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
,
...
...
@@ -386,8 +383,7 @@ class TestBaseDataset:
class_without_full_init
.
foo
()
def
test_full_init
(
self
):
self
.
_init_dataset
()
dataset
=
self
.
dataset_type
(
dataset
=
BaseDataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
,
...
...
@@ -404,7 +400,7 @@ class TestBaseDataset:
assert
dataset
[
0
]
==
dict
(
imgs
=
self
.
imgs
)
assert
dataset
.
get_data_info
(
0
)
==
self
.
data_info
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
'
imgs
'
),
ann_file
=
'
annotations/dummy_annotation.json
'
,
...
...
@@ -418,11 +414,11 @@ class TestBaseDataset:
assert
dataset
.
get_data_info
(
0
)
==
self
.
data_info
# test the instantiation of self.base_dataset when passing indices
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
None
),
ann_file
=
'
annotations/dummy_annotation.json
'
)
dataset_sliced
=
self
.
d
ataset
_type
(
dataset_sliced
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
None
),
ann_file
=
'
annotations/dummy_annotation.json
'
,
...
...
@@ -436,7 +432,7 @@ class TestBaseDataset:
def
test_get_subset_
(
self
,
lazy_init
,
serialize_data
):
# Test positive int indices.
indices
=
2
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
None
),
ann_file
=
'
annotations/dummy_annotation.json
'
,
...
...
@@ -514,7 +510,7 @@ class TestBaseDataset:
def
test_get_subset
(
self
,
lazy_init
,
serialize_data
):
# Test positive indices.
indices
=
2
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
None
),
ann_file
=
'
annotations/dummy_annotation.json
'
,
...
...
@@ -560,7 +556,7 @@ class TestBaseDataset:
def
test_rand_another
(
self
):
# test the instantiation of self.base_dataset when passing num_samples
dataset
=
self
.
d
ataset
_type
(
dataset
=
BaseD
ataset
(
data_root
=
osp
.
join
(
osp
.
dirname
(
__file__
),
'
../data/
'
),
data_prefix
=
dict
(
img
=
None
),
ann_file
=
'
annotations/dummy_annotation.json
'
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment