Write all scalars to a tensorboard file.
| 128 | |
| 129 | |
| 130 | class TensorboardXWriter(EventWriter): |
| 131 | """ |
| 132 | Write all scalars to a tensorboard file. |
| 133 | """ |
| 134 | |
| 135 | def __init__(self, log_dir: str, window_size: int = 20, **kwargs): |
| 136 | """ |
| 137 | Args: |
| 138 | log_dir (str): the directory to save the output events |
| 139 | window_size (int): the scalars will be median-smoothed by this window size |
| 140 | |
| 141 | kwargs: other arguments passed to `torch.utils.tensorboard.SummaryWriter(...)` |
| 142 | """ |
| 143 | self._window_size = window_size |
| 144 | from torch.utils.tensorboard import SummaryWriter |
| 145 | |
| 146 | self._writer = SummaryWriter(log_dir, **kwargs) |
| 147 | self._last_write = -1 |
| 148 | |
| 149 | def write(self): |
| 150 | storage = get_event_storage() |
| 151 | new_last_write = self._last_write |
| 152 | for k, (v, iter) in storage.latest_with_smoothing_hint(self._window_size).items(): |
| 153 | if iter > self._last_write: |
| 154 | self._writer.add_scalar(k, v, iter) |
| 155 | new_last_write = max(new_last_write, iter) |
| 156 | self._last_write = new_last_write |
| 157 | |
| 158 | # storage.put_{image,histogram} is only meant to be used by |
| 159 | # tensorboard writer. So we access its internal fields directly from here. |
| 160 | if len(storage._vis_data) >= 1: |
| 161 | for img_name, img, step_num in storage._vis_data: |
| 162 | self._writer.add_image(img_name, img, step_num) |
| 163 | # Storage stores all image data and rely on this writer to clear them. |
| 164 | # As a result it assumes only one writer will use its image data. |
| 165 | # An alternative design is to let storage store limited recent |
| 166 | # data (e.g. only the most recent image) that all writers can access. |
| 167 | # In that case a writer may not see all image data if its period is long. |
| 168 | storage.clear_images() |
| 169 | |
| 170 | if len(storage._histograms) >= 1: |
| 171 | for params in storage._histograms: |
| 172 | self._writer.add_histogram_raw(**params) |
| 173 | storage.clear_histograms() |
| 174 | |
| 175 | def close(self): |
| 176 | if hasattr(self, "_writer"): # doesn't exist when the code fails at import |
| 177 | self._writer.close() |
| 178 | |
| 179 | |
| 180 | class CommonMetricPrinter(EventWriter): |
no outgoing calls
no test coverage detected