forked from torch-points3d/torch-points3d
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathdataset_factory.py
47 lines (39 loc) · 1.55 KB
/
dataset_factory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import importlib
import copy
import hydra
import logging
from torch_points3d.datasets.base_dataset import BaseDataset
log = logging.getLogger(__name__)
def get_dataset_class(dataset_config):
task = dataset_config.task
# Find and create associated dataset
try:
dataset_config.dataroot = hydra.utils.to_absolute_path(
dataset_config.dataroot)
except Exception:
log.error("This should happen only during testing")
dataset_class = getattr(dataset_config, "class")
dataset_paths = dataset_class.split(".")
module = ".".join(dataset_paths[:-1])
class_name = dataset_paths[-1]
dataset_module = ".".join(["torch_points3d.datasets", task, module])
datasetlib = importlib.import_module(dataset_module)
target_dataset_name = class_name
for name, cls in datasetlib.__dict__.items():
if name.lower() == target_dataset_name.lower() and issubclass(cls, BaseDataset):
dataset_cls = cls
if dataset_cls is None:
raise NotImplementedError(
"In %s.py, there should be a subclass of BaseDataset with class name that matches %s in lowercase."
% (module, class_name)
)
return dataset_cls
def instantiate_dataset(dataset_config) -> BaseDataset:
"""Import the module "data/[module].py".
In the file, the class called {class_name}() will
be instantiated. It has to be a subclass of BaseDataset,
and it is case-insensitive.
"""
dataset_cls = get_dataset_class(dataset_config)
dataset = dataset_cls(dataset_config)
return dataset