plenoptic.plot.synthesis_histogram#

plenoptic.plot.synthesis_histogram(synthesis_object, batch_idx=None, channel_idx=None, iteration=None, ylim=False, xlim='range', ax=None, alpha=0.4, **kwargs)[source]#

Plot histogram of values of synthesis objects.

Added in version 2.0: Combines previously separate loss plotting functions for Metamer and MADCompetition, and adds support for Eigendistortion.

As a way to check whether there’s any values outside the preferred range.

The behavior of this function is slightly different depending on the type of synthesis_object:

  • Metamer and MADCompetition: compare the synthesized tensor against the target / reference image. iteration can be specified.

  • Eigendistortion: create histograms for eigendistortions. iteration must be None.

Parameters:
  • synthesis_object (Metamer | MADCompetition | Eigendistortion) – Synthesis object with the images whose values we want to plot.

  • batch_idx (int | None (default: None)) – Which index to take from the batch dimension. Note that for Eigendistortion, this is the eigenindex. If None, we plot all batches as separate histograms (intended use-case is for multiple eigendistortions).

  • channel_idx (int | None (default: None)) – Which index to take from the channel dimension. If None, we use all channels (assumed use-case is RGB(A) images).

  • iteration (int | None (default: None)) – Which iteration to display, for Metamer and MADCompetition objects. If None, we show the most recent one. Negative values are also allowed. If iteration!=None and synthesis_object.store_progress>1 (that is, the synthesized image was not cached on every iteration), then we use the cached image from the nearest iteration. For Eigendistortion, this must be None.

  • ylim (tuple[float, float] | Literal[False] (default: False)) – If tuple, the ylimit to set for this axis. If False, we leave it untouched.

  • xlim (tuple[float, float] | Literal[False, 'range'] (default: 'range')) – If "range", set the xlimits to the range across plotted data. If tuple, the xlimit to set for this axis. If False, we leave it untouched.

  • ax (Axes | None (default: None)) – Pre-existing axes for plot. If None, we call matplotlib.pyplot.gca.

  • alpha (float (default: 0.4)) – Alpha value for the histogram bars.

  • **kwargs (Any) – Passed to matplotlib.pyplot.hist.

Return type:

Axes

Returns:

ax – Creates axes.

Raises:
Warns:

UserWarning – If the iteration used for cached image is not the same as the argument iteration (because e.g., you set iteration=3 but synthesis_object.store_progress=2).

See also

histogram

The plotting function used to created this plot.

synthesis_status

Create a figure combining this with other axis-level plots to summarize synthesis status at a given iteration.

synthesis_animate

Create a video animating this and other axis-level plots changing over the course of synthesis.

Examples

Plot histogram for Metamer object:

>>> import plenoptic as po
>>> import torch
>>> img = po.data.einstein()
>>> model = po.models.Gaussian(30).eval()
>>> po.remove_grad(model)
>>> met = po.Metamer(img, model)
>>> met.to(torch.float64)
>>> met.load(po.data.fetch_data("example_metamer_gaussian.pt"))
>>> po.plot.synthesis_histogram(met)
<Axes: ... 'Histogram of tensor values'...>

(png, hires.png, pdf)

../../_images/plenoptic-plot-synthesis_histogram-1.png

Plot pixel values from a specified iteration (requires setting store_progress when synthesize was called):

>>> po.plot.synthesis_histogram(met, iteration=10)
<Axes: ... 'Histogram of tensor values'...>

(png, hires.png, pdf)

../../_images/plenoptic-plot-synthesis_histogram-2.png

Plot on an existing axis:

>>> fig, axes = plt.subplots(1, 2, figsize=(8, 4))
>>> po.plot.synthesis_histogram(met, ax=axes[1])
<Axes: ... 'Histogram of tensor values'...>

(png, hires.png, pdf)

../../_images/plenoptic-plot-synthesis_histogram-3.png

Plot histogram for MADCompetition object:

>>> img = po.data.einstein().to(torch.float64)
>>> def ds_ssim(x, y):
...     return 1 - po.metric.ssim(x, y, weighted=True, pad="reflect")
>>> mad = po.MADCompetition(img, ds_ssim, po.metric.mse, "max", 1e6)
>>> mad.load(po.data.fetch_data("example_mad.pt"))
>>> po.plot.synthesis_histogram(mad)
<Axes: ... 'Histogram of tensor values'...>

(png, hires.png, pdf)

../../_images/plenoptic-plot-synthesis_histogram-4.png

Plot histogram for Eigendistortion object. Notice how here we plot just the values from the synthesized eigendistortions, not the base image.

>>> img = po.data.einstein().to(torch.float64)
>>> lg = po.models.LuminanceGainControl(
...     (31, 31), pad_mode="circular", pretrained=True, cache_filt=True
... ).eval()
>>> lg = lg.to(torch.float64)
>>> po.remove_grad(lg)
>>> eig = po.Eigendistortion(img, lg)
>>> eig.load(
...     po.data.fetch_data("example_eigendistortion.pt"),
...     map_location="cpu",
... )
>>> po.plot.synthesis_histogram(eig)
<Axes: ... 'Histogram of tensor values'...>

(png, hires.png, pdf)

../../_images/plenoptic-plot-synthesis_histogram-5.png