foolbox.utils

foolbox.utils.softmax(logits)[source]

Transforms predictions into probability values.

Parameters:
logits : array_like

The logits predicted by the model.

Returns:
numpy.ndarray

Probability values corresponding to the logits.

foolbox.utils.crossentropy(label, logits)[source]

Calculates the cross-entropy.

Parameters:
logits : array_like

The logits predicted by the model.

label : int

The label describing the target distribution.

Returns:
float

The cross-entropy between softmax(logits) and onehot(label).

foolbox.utils.batch_crossentropy(label, logits)[source]

Calculates the cross-entropy for a batch of logits.

Parameters:
logits : array_like

The logits predicted by the model for a batch of inputs.

label : int

The label describing the target distribution.

Returns:
np.ndarray

The cross-entropy between softmax(logits[i]) and onehot(label) for all i.

foolbox.utils.binarize(x, values, threshold=None, included_in='upper')[source]

Binarizes the values of x.

Parameters:
values : tuple of two floats

The lower and upper value to which the inputs are mapped.

threshold : float

The threshold; defaults to (values[0] + values[1]) / 2 if None.

included_in : str

Whether the threshold value itself belongs to the lower or upper interval.

foolbox.utils.imagenet_example(shape=(224, 224), data_format='channels_last', bounds=(0, 255))[source]

Returns an example image and its imagenet class label.

Parameters:
shape : list of integers

The shape of the returned image.

data_format : str

“channels_first” or “channels_last”

bounds : tuple

smallest and largest allowed pixel value

Returns:
image : array_like

The example image.

label : int

The imagenet label associated with the image.

NOTE: This function is deprecated and will be removed in the future.
foolbox.utils.samples(dataset='imagenet', index=0, batchsize=1, shape=(224, 224), data_format='channels_last', bounds=(0, 255))[source]

Returns a batch of example images and the corresponding labels

Parameters:
dataset : string

The data set to load (options: imagenet, mnist, cifar10, cifar100, fashionMNIST)

index : int

For each data set 20 example images exist. The returned batch contains the images with index [index, index + 1, index + 2, …]

batchsize : int

Size of batch.

shape : list of integers

The shape of the returned image (only relevant for Imagenet).

data_format : str

“channels_first” or “channels_last”

bounds : tuple

smallest and largest allowed pixel value

Returns:
images : array_like

The batch of example images

labels : array of int

The labels associated with the images.

foolbox.utils.onehot_like(a, index, value=1)[source]

Creates an array like a, with all values set to 0 except one.

Parameters:
a : array_like

The returned one-hot array will have the same shape and dtype as this array

index : int

The index that should be set to value

value : single value compatible with a.dtype

The value to set at the given index

Returns:
numpy.ndarray

One-hot array with the given value at the given location and zeros everywhere else.