|
| 1 | +""" |
| 2 | +Log using `neptune <https://www.neptune.ml>`_ |
| 3 | +
|
| 4 | +Neptune logger can be used in the online mode or offline (silent) mode. |
| 5 | +To log experiment data in online mode, NeptuneLogger requries an API key: |
| 6 | +
|
| 7 | +.. code-block:: python |
| 8 | +
|
| 9 | + from pytorch_lightning.logging import NeptuneLogger |
| 10 | + # arguments made to NeptuneLogger are passed on to the neptune.experiments.Experiment class |
| 11 | +
|
| 12 | + neptune_logger = NeptuneLogger( |
| 13 | + api_key=os.environ["NEPTUNE_API_TOKEN"], |
| 14 | + project_name="USER_NAME/PROJECT_NAME", |
| 15 | + experiment_name="default", # Optional, |
| 16 | + params={"max_epochs": 10}, # Optional, |
| 17 | + tags=["pytorch-lightning","mlp"] # Optional, |
| 18 | + ) |
| 19 | + trainer = Trainer(max_epochs=10, logger=neptune_logger) |
| 20 | +
|
| 21 | +Use the logger anywhere in you LightningModule as follows: |
| 22 | +
|
| 23 | +.. code-block:: python |
| 24 | +
|
| 25 | + def train_step(...): |
| 26 | + # example |
| 27 | + self.logger.experiment.log_metric("acc_train", acc_train) # log metrics |
| 28 | + self.logger.experiment.log_image("worse_predictions", prediction_image) # log images |
| 29 | + self.logger.experiment.log_artifact("model_checkpoint.pt", prediction_image) # log model checkpoint |
| 30 | + self.logger.experiment.whatever_neptune_supports(...) |
| 31 | +
|
| 32 | + def any_lightning_module_function_or_hook(...): |
| 33 | + self.logger.experiment.log_metric("acc_train", acc_train) # log metrics |
| 34 | + self.logger.experiment.log_image("worse_predictions", prediction_image) # log images |
| 35 | + self.logger.experiment.log_artifact("model_checkpoint.pt", prediction_image) # log model checkpoint |
| 36 | + self.logger.experiment.whatever_neptune_supports(...) |
| 37 | +
|
| 38 | +
|
| 39 | +""" |
| 40 | + |
| 41 | +from logging import getLogger |
| 42 | + |
| 43 | +try: |
| 44 | + import neptune |
| 45 | +except ImportError: |
| 46 | + raise ImportError('Missing neptune package. Run `pip install neptune-client`') |
| 47 | + |
| 48 | +from torch import is_tensor |
| 49 | + |
| 50 | +# from .base import LightningLoggerBase, rank_zero_only |
| 51 | +from pytorch_lightning.logging.base import LightningLoggerBase, rank_zero_only |
| 52 | + |
| 53 | +logger = getLogger(__name__) |
| 54 | + |
| 55 | + |
| 56 | +class NeptuneLogger(LightningLoggerBase): |
| 57 | + def __init__(self, api_key=None, project_name=None, offline_mode=False, |
| 58 | + experiment_name=None, upload_source_files=None, |
| 59 | + params=None, properties=None, tags=None, **kwargs): |
| 60 | + """Initialize a neptune.ml logger. |
| 61 | + Requires either an API Key (online mode) or a local directory path (offline mode) |
| 62 | +
|
| 63 | + :param str|None api_key: Required in online mode. Neputne API token, found on https://neptune.ml. |
| 64 | + Read how to get your API key https://docs.neptune.ml/python-api/tutorials/get-started.html#copy-api-token. |
| 65 | + :param str project_name: Required in online mode. Qualified name of a project in a form of |
| 66 | + "namespace/project_name" for example "tom/minst-classification". |
| 67 | + If None, the value of NEPTUNE_PROJECT environment variable will be taken. |
| 68 | + You need to create the project in https://neptune.ml first. |
| 69 | + :param bool offline_mode: Optional default False. If offline_mode=True no logs will be send to neptune. |
| 70 | + Usually used for debug purposes. |
| 71 | + :param str|None experiment_name: Optional. Editable name of the experiment. |
| 72 | + Name is displayed in the experiment’s Details (Metadata section) and in experiments view as a column. |
| 73 | + :param list|None upload_source_files: Optional. List of source files to be uploaded. |
| 74 | + Must be list of str or single str. Uploaded sources are displayed in the experiment’s Source code tab. |
| 75 | + If None is passed, Python file from which experiment was created will be uploaded. |
| 76 | + Pass empty list ([]) to upload no files. Unix style pathname pattern expansion is supported. |
| 77 | + For example, you can pass '*.py' to upload all python source files from the current directory. |
| 78 | + For recursion lookup use '**/*.py' (for Python 3.5 and later). For more information see glob library. |
| 79 | + :param dict|None params: Optional. Parameters of the experiment. After experiment creation params are read-only. |
| 80 | + Parameters are displayed in the experiment’s Parameters section and each key-value pair can be |
| 81 | + viewed in experiments view as a column. |
| 82 | + :param dict|None properties: Optional default is {}. Properties of the experiment. |
| 83 | + They are editable after experiment is created. Properties are displayed in the experiment’s Details and |
| 84 | + each key-value pair can be viewed in experiments view as a column. |
| 85 | + :param list|None tags: Optional default []. Must be list of str. Tags of the experiment. |
| 86 | + They are editable after experiment is created (see: append_tag() and remove_tag()). |
| 87 | + Tags are displayed in the experiment’s Details and can be viewed in experiments view as a column. |
| 88 | + """ |
| 89 | + super().__init__() |
| 90 | + self.api_key = api_key |
| 91 | + self.project_name = project_name |
| 92 | + self.offline_mode = offline_mode |
| 93 | + self.experiment_name = experiment_name |
| 94 | + self.upload_source_files = upload_source_files |
| 95 | + self.params = params |
| 96 | + self.properties = properties |
| 97 | + self.tags = tags |
| 98 | + self._experiment = None |
| 99 | + self._kwargs = kwargs |
| 100 | + |
| 101 | + if offline_mode: |
| 102 | + self.mode = "offline" |
| 103 | + neptune.init(project_qualified_name='dry-run/project', |
| 104 | + backend=neptune.OfflineBackend()) |
| 105 | + else: |
| 106 | + self.mode = "online" |
| 107 | + neptune.init(api_token=self.api_key, |
| 108 | + project_qualified_name=self.project_name) |
| 109 | + |
| 110 | + logger.info(f"NeptuneLogger was initialized in {self.mode} mode") |
| 111 | + |
| 112 | + @property |
| 113 | + def experiment(self): |
| 114 | + if self._experiment is not None: |
| 115 | + return self._experiment |
| 116 | + else: |
| 117 | + self._experiment = neptune.create_experiment(name=self.experiment_name, |
| 118 | + params=self.params, |
| 119 | + properties=self.properties, |
| 120 | + tags=self.tags, |
| 121 | + upload_source_files=self.upload_source_files, |
| 122 | + **self._kwargs) |
| 123 | + return self._experiment |
| 124 | + |
| 125 | + @rank_zero_only |
| 126 | + def log_hyperparams(self, params): |
| 127 | + for key, val in vars(params).items(): |
| 128 | + self.experiment.set_property(f"param__{key}", val) |
| 129 | + |
| 130 | + @rank_zero_only |
| 131 | + def log_metrics(self, metrics, step=None): |
| 132 | + """Log metrics (numeric values) in Neptune experiments |
| 133 | +
|
| 134 | + :param float metric: Dictionary with metric names as keys and measured quanties as values |
| 135 | + :param int|None step: Step number at which the metrics should be recorded, must be strictly increasing |
| 136 | +
|
| 137 | + """ |
| 138 | + |
| 139 | + for key, val in metrics.items(): |
| 140 | + if is_tensor(val): |
| 141 | + val = val.cpu().detach() |
| 142 | + |
| 143 | + if step is None: |
| 144 | + self.experiment.log_metric(key, val) |
| 145 | + else: |
| 146 | + self.experiment.log_metric(key, x=step, y=val) |
| 147 | + |
| 148 | + @rank_zero_only |
| 149 | + def finalize(self, status): |
| 150 | + self.experiment.stop() |
| 151 | + |
| 152 | + @property |
| 153 | + def name(self): |
| 154 | + if self.mode == "offline": |
| 155 | + return "offline-name" |
| 156 | + else: |
| 157 | + return self.experiment.name |
| 158 | + |
| 159 | + @property |
| 160 | + def version(self): |
| 161 | + if self.mode == "offline": |
| 162 | + return "offline-id-1234" |
| 163 | + else: |
| 164 | + return self.experiment.id |
| 165 | + |
| 166 | + @rank_zero_only |
| 167 | + def log_metric(self, metric_name, metric_value, step=None): |
| 168 | + """Log metrics (numeric values) in Neptune experiments |
| 169 | +
|
| 170 | + :param str metric_name: The name of log, i.e. mse, loss, accuracy. |
| 171 | + :param str metric_value: The value of the log (data-point). |
| 172 | + :param int|None step: Step number at which the metrics should be recorded, must be strictly increasing |
| 173 | +
|
| 174 | + """ |
| 175 | + if step is None: |
| 176 | + self.experiment.log_metric(metric_name, metric_value) |
| 177 | + else: |
| 178 | + self.experiment.log_metric(metric_name, x=step, y=metric_value) |
| 179 | + |
| 180 | + @rank_zero_only |
| 181 | + def log_text(self, log_name, text, step=None): |
| 182 | + """Log text data in Neptune experiment |
| 183 | +
|
| 184 | + :param str log_name: The name of log, i.e. mse, my_text_data, timing_info. |
| 185 | + :param str text: The value of the log (data-point). |
| 186 | + :param int|None step: Step number at which the metrics should be recorded, must be strictly increasing |
| 187 | +
|
| 188 | + """ |
| 189 | + if step is None: |
| 190 | + self.experiment.log_metric(log_name, text) |
| 191 | + else: |
| 192 | + self.experiment.log_metric(log_name, x=step, y=text) |
| 193 | + |
| 194 | + @rank_zero_only |
| 195 | + def log_image(self, log_name, image, step=None): |
| 196 | + """Log image data in Neptune experiment |
| 197 | +
|
| 198 | + :param str log_name: The name of log, i.e. bboxes, visualisations, sample_images. |
| 199 | + :param str|PIL.Image|matplotlib.figure.Figure image: The value of the log (data-point). |
| 200 | + Can be one of the following types: PIL image, matplotlib.figure.Figure, path to image file (str) |
| 201 | + :param int|None step: Step number at which the metrics should be recorded, must be strictly increasing |
| 202 | +
|
| 203 | + """ |
| 204 | + if step is None: |
| 205 | + self.experiment.log_image(log_name, image) |
| 206 | + else: |
| 207 | + self.experiment.log_image(log_name, x=step, y=image) |
| 208 | + |
| 209 | + @rank_zero_only |
| 210 | + def log_artifact(self, artifact, destination=None): |
| 211 | + """Save an artifact (file) in Neptune experiment storage. |
| 212 | +
|
| 213 | + :param str artifact: A path to the file in local filesystem. |
| 214 | + :param str|None destination: Optional default None. |
| 215 | + A destination path. If None is passed, an artifact file name will be used. |
| 216 | +
|
| 217 | + """ |
| 218 | + self.experiment.log_artifact(artifact, destination) |
| 219 | + |
| 220 | + @rank_zero_only |
| 221 | + def set_property(self, key, value): |
| 222 | + """Set key-value pair as Neptune experiment property. |
| 223 | +
|
| 224 | + :param str key: Property key. |
| 225 | + :param obj value: New value of a property. |
| 226 | +
|
| 227 | + """ |
| 228 | + self.experiment.set_property(key, value) |
| 229 | + |
| 230 | + @rank_zero_only |
| 231 | + def append_tags(self, tags): |
| 232 | + """appends tags to neptune experiment |
| 233 | +
|
| 234 | + :param str|tuple|list(str) tags: Tags to add to the current experiment. |
| 235 | + If str is passed, singe tag is added. |
| 236 | + If multiple - comma separated - str are passed, all of them are added as tags. |
| 237 | + If list of str is passed, all elements of the list are added as tags. |
| 238 | +
|
| 239 | + """ |
| 240 | + if not isinstance(tags, (list, set, tuple)): |
| 241 | + tags = [tags] # make it as an iterable is if it is not yet |
| 242 | + self.experiment.append_tags(*tags) |
0 commit comments