Skip to content

Vision

TorchVisionProblem

TorchVisionProblem(task_name: str, network: Module, batch_size: int = 1024)

Bases: Problem

TorchVision Problem for Computer Vision Optimization.

Evaluates the parameters of a classification network on random batches of an image dataset. A solution is a PyTree of network parameters. The constructor downloads the dataset to ~/data if needed and loads the full train and test splits as JAX arrays, with images in channel-last layout.

Parameters:

  • task_name (str) –

    Name of the dataset: "MNIST", "FashionMNIST", "CIFAR10" or "SVHN". Any other value raises a ValueError.

  • network (Module) –

    Flax module called as network.apply(network_params, images, key) that returns the class logits for a batch of images, e.g. CNN from evosax.problems.networks with identity_output_fn.

  • batch_size (int, default: 1024 ) –

    Number of images sampled for every eval call.

Note

Requires the optional torch and torchvision packages.

References

input_shape property

input_shape: tuple[int]

Get image shape.

num_classes property

num_classes: int

Get the number of classes.

num_dims property

num_dims: int

Number of search dimensions, i.e. the total size of a solution.

init

init(key: Array) -> State

Initialize the problem state with an evaluation counter of zero.

eval

eval(key: Array, solutions: Population, state: State) -> tuple[Fitness, State, Metrics]

Evaluate a population of networks on a batch of the train split.

One batch of batch_size images is sampled without replacement from the train split and used for all population members.

Parameters:

  • key (Array) –

    PRNG key.

  • solutions (Population) –

    Population of network parameters, a PyTree with a leading population axis on every leaf.

  • state (State) –

    Current problem state.

Returns:

  • tuple[Fitness, State, Metrics] –

    A tuple (fitness, state, info). fitness is the mean softmax cross-entropy loss of each member on the batch (lower is better). state has its counter incremented. info["accuracy"] is the classification accuracy of each member on the batch.

eval_test

eval_test(key: Array, solutions: Population, state: State) -> tuple[Fitness, State, Metrics]

Evaluate a population of networks on a batch of the test split.

One batch of batch_size images is sampled without replacement from the test split and used for all population members.

Parameters:

  • key (Array) –

    PRNG key.

  • solutions (Population) –

    Population of network parameters, a PyTree with a leading population axis on every leaf.

  • state (State) –

    Current problem state.

Returns:

  • tuple[Fitness, State, Metrics] –

    A tuple (fitness, state, info). fitness is the mean softmax cross-entropy loss of each member on the batch (lower is better). state has its counter incremented. info["accuracy"] is the classification accuracy of each member on the batch.

sample

sample(key: Array) -> Solution

Sample network parameters.

Initializes network on a batch of batch_size train images.

Parameters:

  • key (Array) –

    PRNG key.

Returns:

  • Solution –

    A PyTree of network parameters.

get_mnist

get_mnist()

Get the MNIST dataset.

Returns:

  • datasets ( tuple ) –

    A tuple (dataset_train, dataset_test, loader_train, loader_test) of TorchVision datasets and PyTorch data loaders. Each loader yields its whole split as a single batch.

get_fashion_mnist

get_fashion_mnist()

Get the Fashion MNIST dataset.

Returns:

  • datasets ( tuple ) –

    A tuple (dataset_train, dataset_test, loader_train, loader_test) of TorchVision datasets and PyTorch data loaders. Each loader yields its whole split as a single batch.

get_cifar10

get_cifar10()

Get the CIFAR-10 dataset.

Returns:

  • datasets ( tuple ) –

    A tuple (dataset_train, dataset_test, loader_train, loader_test) of TorchVision datasets and PyTorch data loaders. Each loader yields its whole split as a single batch.

get_svhn

get_svhn()

Get the SVHN dataset.

Returns:

  • datasets ( tuple ) –

    A tuple (dataset_train, dataset_test, loader_train, loader_test) of TorchVision datasets and PyTorch data loaders. Each loader yields its whole split as a single batch.