test_experiment.py 3.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

import os
import unittest
from pathlib import Path

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
11
from hydra import compose, initialize_config_dir
12
13
from omegaconf import OmegaConf

14
15
from .. import experiment

16
17
18
19
20
21
22
23
24
25

def interactive_testing_requested() -> bool:
    """
    Certain tests are only useful when run interactively, and so are not regularly run.
    These are activated by this funciton returning True, which the user requests by
    setting the environment variable `PYTORCH3D_INTERACTIVE_TESTING` to 1.
    """
    return os.environ.get("PYTORCH3D_INTERACTIVE_TESTING", "") == "1"


Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
26
27
28
internal = os.environ.get("FB_TEST", False)


29
DATA_DIR = Path(__file__).resolve().parent
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
30
IMPLICITRON_CONFIGS_DIR = Path(__file__).resolve().parent.parent / "configs"
31
32
33
34
35
36
37
38
39
40
41
42
43
44
DEBUG: bool = False

# TODO:
# - add enough files to skateboard_first_5 that this works on RE.
# - share common code with PyTorch3D tests?
# - deal with the temporary output files this test creates


class TestExperiment(unittest.TestCase):
    def setUp(self):
        self.maxDiff = None

    def test_from_defaults(self):
        # Test making minimal changes to the dataclass defaults.
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
45
        if not interactive_testing_requested() or not internal:
46
47
48
49
50
51
52
53
54
55
56
57
58
59
            return
        cfg = OmegaConf.structured(experiment.ExperimentConfig)
        cfg.data_source_args.dataset_map_provider_class_type = (
            "JsonIndexDatasetMapProvider"
        )
        dataset_args = (
            cfg.data_source_args.dataset_map_provider_JsonIndexDatasetMapProvider_args
        )
        dataloader_args = (
            cfg.data_source_args.data_loader_map_provider_SequenceDataLoaderMapProvider_args
        )
        dataset_args.category = "skateboard"
        dataset_args.test_restrict_sequence_id = 0
        dataset_args.dataset_root = "manifold://co3d/tree/extracted"
60
        dataset_args.dataset_JsonIndexDataset_args.limit_sequences_to = 5
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
61
62
        dataset_args.dataset_JsonIndexDataset_args.image_height = 80
        dataset_args.dataset_JsonIndexDataset_args.image_width = 80
63
        dataloader_args.dataset_length_train = 1
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
64
        dataloader_args.dataset_length_val = 1
65
66
        cfg.solver_args.max_epochs = 2

Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
67
        experiment.run_training(cfg)
68
69
70
71
72
73
74

    def test_yaml_contents(self):
        cfg = OmegaConf.structured(experiment.ExperimentConfig)
        yaml = OmegaConf.to_yaml(cfg, sort_keys=False)
        if DEBUG:
            (DATA_DIR / "experiment.yaml").write_text(yaml)
        self.assertEqual(yaml, (DATA_DIR / "experiment.yaml").read_text())
Jeremy Reizenstein's avatar
Jeremy Reizenstein committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

    def test_load_configs(self):
        config_files = []

        for pattern in ("repro_singleseq*.yaml", "repro_multiseq*.yaml"):
            config_files.extend(
                [
                    f
                    for f in IMPLICITRON_CONFIGS_DIR.glob(pattern)
                    if not f.name.endswith("_base.yaml")
                ]
            )

        for file in config_files:
            with self.subTest(file.name):
                with initialize_config_dir(config_dir=str(IMPLICITRON_CONFIGS_DIR)):
                    compose(file.name)