Image Regression

Regression tasks estimate a numeric variable, such as the price of a house or voter
turnout.

This example is adapted from a
notebook which
estimates a person's age from their image, trained on the
IMDB-WIKI photographs of famous
people.

First, prepare your image data in a numpy.ndarray or tensorflow.Dataset format. Each
image must have the same shape, meaning each has the same width, height, and color
channels as other images in the set.

Training using AutoKeras

# Initialize the image regressorreg=ak.ImageRegressor(max_trials=15)# AutoKeras tries 15 different models.# Find the best model for the given training datareg.fit(train_imgs,train_ages)# Predict with the chosen model:# predict_y = reg.predict(test_images) # Uncomment if required# Evaluate the chosen model with testing dataprint(reg.evaluate(test_images,test_ages))

Validation Data

By default, AutoKeras use the last 20% of training data as validation data. As shown in
the example below, you can use validation_split to specify the percentage.

reg.fit(train_imgs,train_ages,# Split the training data and use the last 15% as validation data.validation_split=0.15,epochs=3,)

You can also use your own validation set instead of splitting it from the training data
with validation_data.

split=460000x_val=train_imgs[split:]y_val=train_ages[split:]x_train=train_imgs[:split]y_train=train_ages[:split]reg.fit(x_train,y_train,# Use your own validation set.validation_data=(x_val,y_val),epochs=3,)

Customized Search Space

For advanced users, you may customize your search space by using AutoModel instead of
ImageRegressor. You can configure the ImageBlock for some high-level configurations,
e.g., block_type for the type of neural network to search, normalize for whether to do
data normalization, augment for whether to do data augmentation. You can also choose not
to specify these arguments, which would leave the different choices to be tuned
automatically. See the following example for detail.

input_node=ak.ImageInput()output_node=ak.ImageBlock(# Only search ResNet architectures.block_type="resnet",# Normalize the dataset.normalize=True,# Do not do data augmentation.augment=False,)(input_node)output_node=ak.RegressionHead()(output_node)reg=ak.AutoModel(inputs=input_node,outputs=output_node,max_trials=10)reg.fit(x_train,y_train,epochs=3)

The usage of AutoModel is similar to the functional API of Keras. Basically, you are
building a graph, whose edges are blocks and the nodes are intermediate outputs of
blocks. To add an edge from input_node to output_node with output_node =
ak.some_block(input_node).
You can even also use more fine grained blocks to customize the search space even
further. See the following example.

Data Format

The AutoKeras ImageClassifier is quite flexible for the data format.

For the image, it accepts data formats both with and without the channel dimension. The
images in the IMDB-Wiki dataset do not have a channel dimension. Each image is a matrix
with shape (128, 128). AutoKeras also accepts images with a channel dimension at last,
e.g., (32, 32, 3), (28, 28, 1).

For the classification labels, AutoKeras accepts both plain labels, i.e. strings or
integers, and one-hot encoded labels, i.e. vectors of 0s and 1s.

So if you prepare your data in the following way, the ImageClassifier should still work.

We also support using tf.data.Dataset format for the training data. In this case, the
images would have to be 3-dimentional. The labels have to be one-hot encoded for
multi-class classification to be wrapped into tensorflow Dataset.

train_set=tf.data.Dataset.from_tensor_slices(((train_imgs,),(train_ages,)))test_set=tf.data.Dataset.from_tensor_slices(((test_imgs,),(test_ages,)))reg=ak.ImageRegressor(max_trials=15)# Feed the tensorflow Dataset to the classifier.reg.fit(train_set)# Predict with the best model.predicted_y=clf.predict(test_set)# Evaluate the best model with testing data.print(clf.evaluate(test_set))