IBA.utils

class WelfordEstimator[source]

Bases: object

Estimates the mean and standard derivation. For the algorithm see wikipedia.

Example

Given a batch of images imgs with shape (10, 3, 64, 64), the mean and std could be estimated as follows:

# exemplary data source: 5 batches of size 10, filled with random data
batch_generator = (torch.randn(10, 3, 64, 64) for _ in range(5))

estim = WelfordEstimator(3, 64, 64)
for batch in batch_generator:
    estim(batch)

# returns the estimated mean
estim.mean()

# returns the estimated std
estim.std()

# returns the number of seen samples, here 10
estim.n_samples()

# returns a mask with active neurons
estim.active_neurons()
reset()[source]

Resets the estimates.

fit(x)[source]

Update estimates without altering x

n_samples()[source]

Returns the number of seen samples.

mean()[source]

Returns the estimate of the mean.

std()[source]

Returns the estimate of the standard derivation.

active_neurons(threshold=0.01)[source]

Returns a mask of all active neurons. A neuron is considered active if n_nonzero / n_samples  > threshold

state_dict()[source]

Returns internal state. Useful for saving to disk.

load_state_dict(state)[source]

Loads the internal state of the estimator.

get_tqdm()[source]

Tries to import tqdm from tqdm.auto if fails uses cli tqdm.

ifnone(a, b)[source]

If a is None return b.

to_unit_interval(x)[source]

Scales x to be in [0, 1].

load_monkeys(center_crop=True, size=224, pil=False)[source]

Returns the monkey test image.

plot_saliency_map(saliency_map, img=None, ax=None, colorbar_label='Bits / Pixel', colorbar_fontsize=14, min_alpha=0.2, max_alpha=0.7, vmax=None, colorbar_size=0.3, colorbar_pad=0.08)[source]

Plots the heatmap with an bits/pixel colorbar and optionally overlays the image.

Parameters
  • saliency_map (np.ndarray) – the saliency_map.

  • img (np.ndarray) – show this image under the saliency_map.

  • ax – matplotlib axis. If None, a new plot is created.

  • colorbar_label (str) – label for the colorbar.

  • colorbar_fontsize (int) – fontsize of the colorbar label.

  • min_alpha (float) – minimum alpha value for the overlay. only used if img is given.

  • max_alpha (float) – maximum alpha value for the overlay. only used if img is given.

  • vmax – maximum value for colorbar.

  • colorbar_size – width of the colorbar. default: Fixed(0.3).

  • colorbar_pad – width of the colorbar. default: Fixed(0.08).

Returns

The matplotlib axis ax.