Skip to content

WandbLogger

WandbLogger

Bases: Logger

Source code in sequel/utils/loggers/wandb_logger.py
 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class WandbLogger(Logger):
    def __init__(self, config: Optional[OmegaConf] = None, disabled: bool = False):
        """Inits the Weights & Biases Logger. The class handles the initialization of the experiment, saving of source
        code, and metric tracking.

        Args:
            config (Optional[OmegaConf], optional): The configuration of the current experiment. Defaults to None.
            disabled (bool, optional): A utility boolean to quickly disable Weight&Biases logging. Useful when
                debugging. Defaults to False.
        """

        super().__init__()

        if config is None and not disabled:
            wandb.init()
        elif config is None and disabled:
            wandb.init(mode="disabled")
        else:
            if disabled:
                mode = "disabled"
            else:
                mode = getattr(config.wandb, "mode", "disabled")

            wandb.init(
                entity=config.wandb.entity,
                project=config.wandb.project,
                config=OmegaConf.to_container(config),
                tags=getattr(config.wandb, "tags", []),
                mode=mode,
                group=getattr(config.wandb, "group", None),
                name=getattr(config.wandb, "name", None),
            )

        self.log_code()

        # make sure that validation metrics are logged with epoch as the x-axis
        wandb.define_metric("epoch")
        wandb.define_metric("val/*", step_metric="epoch")
        wandb.define_metric("val/accuracy/*", step_metric="epoch")
        wandb.define_metric("val/loss/*", step_metric="epoch")

    def log(self, item, step=None, epoch=None):
        wandb.log(item)

    def log_code(self):
        wandb.run.log_code(get_original_root_dir())

    def log_parameters(self, config: dict):
        # The hyperparameters are logged when calling `wandb.init` via the config argument.
        pass

    def log_figure(self, figure, name, step=None, epoch=None):
        wandb.log({name: wandb.Image(figure)})

    def terminate(self):
        wandb.finish()

__init__(config=None, disabled=False)

Inits the Weights & Biases Logger. The class handles the initialization of the experiment, saving of source code, and metric tracking.

Parameters:

Name Type Description Default
config Optional[OmegaConf]

The configuration of the current experiment. Defaults to None.

None
disabled bool

A utility boolean to quickly disable Weight&Biases logging. Useful when debugging. Defaults to False.

False
Source code in sequel/utils/loggers/wandb_logger.py
 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
def __init__(self, config: Optional[OmegaConf] = None, disabled: bool = False):
    """Inits the Weights & Biases Logger. The class handles the initialization of the experiment, saving of source
    code, and metric tracking.

    Args:
        config (Optional[OmegaConf], optional): The configuration of the current experiment. Defaults to None.
        disabled (bool, optional): A utility boolean to quickly disable Weight&Biases logging. Useful when
            debugging. Defaults to False.
    """

    super().__init__()

    if config is None and not disabled:
        wandb.init()
    elif config is None and disabled:
        wandb.init(mode="disabled")
    else:
        if disabled:
            mode = "disabled"
        else:
            mode = getattr(config.wandb, "mode", "disabled")

        wandb.init(
            entity=config.wandb.entity,
            project=config.wandb.project,
            config=OmegaConf.to_container(config),
            tags=getattr(config.wandb, "tags", []),
            mode=mode,
            group=getattr(config.wandb, "group", None),
            name=getattr(config.wandb, "name", None),
        )

    self.log_code()

    # make sure that validation metrics are logged with epoch as the x-axis
    wandb.define_metric("epoch")
    wandb.define_metric("val/*", step_metric="epoch")
    wandb.define_metric("val/accuracy/*", step_metric="epoch")
    wandb.define_metric("val/loss/*", step_metric="epoch")