Safemotion Lib
Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | List of all members
fastreid.utils.events.EventStorage Class Reference

Public Member Functions

 __init__ (self, start_iter=0)
 
 put_image (self, img_name, img_tensor)
 
 put_scalar (self, name, value, smoothing_hint=True)
 
 put_scalars (self, *smoothing_hint=True, **kwargs)
 
 put_histogram (self, hist_name, hist_tensor, bins=1000)
 
 history (self, name)
 
 histories (self)
 
 latest (self)
 
 latest_with_smoothing_hint (self, window_size=20)
 
 smoothing_hints (self)
 
 step (self)
 
 iter (self)
 
 iteration (self)
 
 __enter__ (self)
 
 __exit__ (self, exc_type, exc_val, exc_tb)
 
 name_scope (self, name)
 
 clear_images (self)
 
 clear_histograms (self)
 

Protected Attributes

 _history
 
 _smoothing_hints
 
 _latest_scalars
 
 _iter
 
 _current_prefix
 
 _vis_data
 
 _histograms
 

Detailed Description

The user-facing class that provides metric storage functionalities.
In the future we may add support for storing / logging other types of data if needed.

Definition at line 250 of file events.py.

Constructor & Destructor Documentation

◆ __init__()

fastreid.utils.events.EventStorage.__init__ ( self,
start_iter = 0 )
Args:
    start_iter (int): the iteration number to start with

Definition at line 256 of file events.py.

256 def __init__(self, start_iter=0):
257 """
258 Args:
259 start_iter (int): the iteration number to start with
260 """
261 self._history = defaultdict(HistoryBuffer)
262 self._smoothing_hints = {}
263 self._latest_scalars = {}
264 self._iter = start_iter
265 self._current_prefix = ""
266 self._vis_data = []
267 self._histograms = []
268

Member Function Documentation

◆ __enter__()

fastreid.utils.events.EventStorage.__enter__ ( self)

Definition at line 413 of file events.py.

413 def __enter__(self):
414 _CURRENT_STORAGE_STACK.append(self)
415 return self
416

◆ __exit__()

fastreid.utils.events.EventStorage.__exit__ ( self,
exc_type,
exc_val,
exc_tb )

Definition at line 417 of file events.py.

417 def __exit__(self, exc_type, exc_val, exc_tb):
418 assert _CURRENT_STORAGE_STACK[-1] == self
419 _CURRENT_STORAGE_STACK.pop()
420

◆ clear_histograms()

fastreid.utils.events.EventStorage.clear_histograms ( self)
Delete all the stored histograms for visualization.
This should be called after histograms are written to tensorboard.

Definition at line 440 of file events.py.

440 def clear_histograms(self):
441 """
442 Delete all the stored histograms for visualization.
443 This should be called after histograms are written to tensorboard.
444 """
445 self._histograms = []

◆ clear_images()

fastreid.utils.events.EventStorage.clear_images ( self)
Delete all the stored images for visualization. This should be called
after images are written to tensorboard.

Definition at line 433 of file events.py.

433 def clear_images(self):
434 """
435 Delete all the stored images for visualization. This should be called
436 after images are written to tensorboard.
437 """
438 self._vis_data = []
439

◆ histories()

fastreid.utils.events.EventStorage.histories ( self)
Returns:
    dict[name -> HistoryBuffer]: the HistoryBuffer for all scalars

Definition at line 356 of file events.py.

356 def histories(self):
357 """
358 Returns:
359 dict[name -> HistoryBuffer]: the HistoryBuffer for all scalars
360 """
361 return self._history
362

◆ history()

fastreid.utils.events.EventStorage.history ( self,
name )
Returns:
    HistoryBuffer: the scalar history for name

Definition at line 346 of file events.py.

346 def history(self, name):
347 """
348 Returns:
349 HistoryBuffer: the scalar history for name
350 """
351 ret = self._history.get(name, None)
352 if ret is None:
353 raise KeyError("No history metric available for {}!".format(name))
354 return ret
355

◆ iter()

fastreid.utils.events.EventStorage.iter ( self)

Definition at line 405 of file events.py.

405 def iter(self):
406 return self._iter
407

◆ iteration()

fastreid.utils.events.EventStorage.iteration ( self)

Definition at line 409 of file events.py.

409 def iteration(self):
410 # for backward compatibility
411 return self._iter
412

◆ latest()

fastreid.utils.events.EventStorage.latest ( self)
Returns:
    dict[str -> (float, int)]: mapping from the name of each scalar to the most
        recent value and the iteration number its added.

Definition at line 363 of file events.py.

363 def latest(self):
364 """
365 Returns:
366 dict[str -> (float, int)]: mapping from the name of each scalar to the most
367 recent value and the iteration number its added.
368 """
369 return self._latest_scalars
370

◆ latest_with_smoothing_hint()

fastreid.utils.events.EventStorage.latest_with_smoothing_hint ( self,
window_size = 20 )
Similar to :meth:`latest`, but the returned values
are either the un-smoothed original latest value,
or a median of the given window_size,
depend on whether the smoothing_hint is True.
This provides a default behavior that other writers can use.

Definition at line 371 of file events.py.

371 def latest_with_smoothing_hint(self, window_size=20):
372 """
373 Similar to :meth:`latest`, but the returned values
374 are either the un-smoothed original latest value,
375 or a median of the given window_size,
376 depend on whether the smoothing_hint is True.
377 This provides a default behavior that other writers can use.
378 """
379 result = {}
380 for k, (v, itr) in self._latest_scalars.items():
381 result[k] = (
382 self._history[k].median(window_size) if self._smoothing_hints[k] else v,
383 itr,
384 )
385 return result
386

◆ name_scope()

fastreid.utils.events.EventStorage.name_scope ( self,
name )
Yields:
    A context within which all the events added to this storage
    will be prefixed by the name scope.

Definition at line 422 of file events.py.

422 def name_scope(self, name):
423 """
424 Yields:
425 A context within which all the events added to this storage
426 will be prefixed by the name scope.
427 """
428 old_prefix = self._current_prefix
429 self._current_prefix = name.rstrip("/") + "/"
430 yield
431 self._current_prefix = old_prefix
432

◆ put_histogram()

fastreid.utils.events.EventStorage.put_histogram ( self,
hist_name,
hist_tensor,
bins = 1000 )
Create a histogram from a tensor.
Args:
    hist_name (str): The name of the histogram to put into tensorboard.
    hist_tensor (torch.Tensor): A Tensor of arbitrary shape to be converted
        into a histogram.
    bins (int): Number of histogram bins.

Definition at line 317 of file events.py.

317 def put_histogram(self, hist_name, hist_tensor, bins=1000):
318 """
319 Create a histogram from a tensor.
320 Args:
321 hist_name (str): The name of the histogram to put into tensorboard.
322 hist_tensor (torch.Tensor): A Tensor of arbitrary shape to be converted
323 into a histogram.
324 bins (int): Number of histogram bins.
325 """
326 ht_min, ht_max = hist_tensor.min().item(), hist_tensor.max().item()
327
328 # Create a histogram with PyTorch
329 hist_counts = torch.histc(hist_tensor, bins=bins)
330 hist_edges = torch.linspace(start=ht_min, end=ht_max, steps=bins + 1, dtype=torch.float32)
331
332 # Parameter for the add_histogram_raw function of SummaryWriter
333 hist_params = dict(
334 tag=hist_name,
335 min=ht_min,
336 max=ht_max,
337 num=len(hist_tensor),
338 sum=float(hist_tensor.sum()),
339 sum_squares=float(torch.sum(hist_tensor ** 2)),
340 bucket_limits=hist_edges[1:].tolist(),
341 bucket_counts=hist_counts.tolist(),
342 global_step=self._iter,
343 )
344 self._histograms.append(hist_params)
345

◆ put_image()

fastreid.utils.events.EventStorage.put_image ( self,
img_name,
img_tensor )
Add an `img_tensor` associated with `img_name`, to be shown on
tensorboard.
Args:
    img_name (str): The name of the image to put into tensorboard.
    img_tensor (torch.Tensor or numpy.array): An `uint8` or `float`
        Tensor of shape `[channel, height, width]` where `channel` is
        3. The image format should be RGB. The elements in img_tensor
        can either have values in [0, 1] (float32) or [0, 255] (uint8).
        The `img_tensor` will be visualized in tensorboard.

Definition at line 269 of file events.py.

269 def put_image(self, img_name, img_tensor):
270 """
271 Add an `img_tensor` associated with `img_name`, to be shown on
272 tensorboard.
273 Args:
274 img_name (str): The name of the image to put into tensorboard.
275 img_tensor (torch.Tensor or numpy.array): An `uint8` or `float`
276 Tensor of shape `[channel, height, width]` where `channel` is
277 3. The image format should be RGB. The elements in img_tensor
278 can either have values in [0, 1] (float32) or [0, 255] (uint8).
279 The `img_tensor` will be visualized in tensorboard.
280 """
281 self._vis_data.append((img_name, img_tensor, self._iter))
282

◆ put_scalar()

fastreid.utils.events.EventStorage.put_scalar ( self,
name,
value,
smoothing_hint = True )
Add a scalar `value` to the `HistoryBuffer` associated with `name`.
Args:
    smoothing_hint (bool): a 'hint' on whether this scalar is noisy and should be
        smoothed when logged. The hint will be accessible through
        :meth:`EventStorage.smoothing_hints`.  A writer may ignore the hint
        and apply custom smoothing rule.
        It defaults to True because most scalars we save need to be smoothed to
        provide any useful signal.

Definition at line 283 of file events.py.

283 def put_scalar(self, name, value, smoothing_hint=True):
284 """
285 Add a scalar `value` to the `HistoryBuffer` associated with `name`.
286 Args:
287 smoothing_hint (bool): a 'hint' on whether this scalar is noisy and should be
288 smoothed when logged. The hint will be accessible through
289 :meth:`EventStorage.smoothing_hints`. A writer may ignore the hint
290 and apply custom smoothing rule.
291 It defaults to True because most scalars we save need to be smoothed to
292 provide any useful signal.
293 """
294 name = self._current_prefix + name
295 history = self._history[name]
296 value = float(value)
297 history.update(value, self._iter)
298 self._latest_scalars[name] = (value, self._iter)
299
300 existing_hint = self._smoothing_hints.get(name)
301 if existing_hint is not None:
302 assert (
303 existing_hint == smoothing_hint
304 ), "Scalar {} was put with a different smoothing_hint!".format(name)
305 else:
306 self._smoothing_hints[name] = smoothing_hint
307

◆ put_scalars()

fastreid.utils.events.EventStorage.put_scalars ( self,
* smoothing_hint = True,
** kwargs )
Put multiple scalars from keyword arguments.
Examples:
    storage.put_scalars(loss=my_loss, accuracy=my_accuracy, smoothing_hint=True)

Definition at line 308 of file events.py.

308 def put_scalars(self, *, smoothing_hint=True, **kwargs):
309 """
310 Put multiple scalars from keyword arguments.
311 Examples:
312 storage.put_scalars(loss=my_loss, accuracy=my_accuracy, smoothing_hint=True)
313 """
314 for k, v in kwargs.items():
315 self.put_scalar(k, v, smoothing_hint=smoothing_hint)
316

◆ smoothing_hints()

fastreid.utils.events.EventStorage.smoothing_hints ( self)
Returns:
    dict[name -> bool]: the user-provided hint on whether the scalar
        is noisy and needs smoothing.

Definition at line 387 of file events.py.

387 def smoothing_hints(self):
388 """
389 Returns:
390 dict[name -> bool]: the user-provided hint on whether the scalar
391 is noisy and needs smoothing.
392 """
393 return self._smoothing_hints
394

◆ step()

fastreid.utils.events.EventStorage.step ( self)
User should call this function at the beginning of each iteration, to
notify the storage of the start of a new iteration.
The storage will then be able to associate the new data with the
correct iteration number.

Definition at line 395 of file events.py.

395 def step(self):
396 """
397 User should call this function at the beginning of each iteration, to
398 notify the storage of the start of a new iteration.
399 The storage will then be able to associate the new data with the
400 correct iteration number.
401 """
402 self._iter += 1
403

Member Data Documentation

◆ _current_prefix

fastreid.utils.events.EventStorage._current_prefix
protected

Definition at line 265 of file events.py.

◆ _histograms

fastreid.utils.events.EventStorage._histograms
protected

Definition at line 267 of file events.py.

◆ _history

fastreid.utils.events.EventStorage._history
protected

Definition at line 261 of file events.py.

◆ _iter

fastreid.utils.events.EventStorage._iter
protected

Definition at line 264 of file events.py.

◆ _latest_scalars

fastreid.utils.events.EventStorage._latest_scalars
protected

Definition at line 263 of file events.py.

◆ _smoothing_hints

fastreid.utils.events.EventStorage._smoothing_hints
protected

Definition at line 262 of file events.py.

◆ _vis_data

fastreid.utils.events.EventStorage._vis_data
protected

Definition at line 266 of file events.py.


The documentation for this class was generated from the following file: