In this tutorial we'll walk through a simple convolutional neural network to classify the images in the cifar10 dataset.
The convolution layer is made up of a set of independent filters. Each filter slides over the image and creates feature maps that learn different aspects of an image.
A CNN uses convolutions to connected extract features from local regions of an input. Most CNNs contain a combination of convolutional, pooling and affine layers. CNNs offer fantastic performance on visual recognition tasks, where they have become the state of the art.
The pooling layer reduce the size of the image representation and so the number of parameters and computation in the network. Pooling usually involves taking either the maximum or average value across the pooled area.
# Define model
model = tf.keras.models.Sequential()
# Conv2D adds a convolution layer with 32 filters that generates 2 dimensional feature maps to learn different aspects of our image
model.add(tf.keras.layers.Conv2D(32, (3, 3), padding='same',
# MaxPooling2D layer reduces the size of the image representation our convolutional layers learnt, and in doing so it reduces the number of parameters and computations the network needs to perform.
# Dropout layer turns off a percentage of neurons at every step
# Flattens our array so we can feed the convolution layer outputs (a matrix) into our fully connected layer (an array)
# Dense layer creates dense, fully connected layers with x inputs and y outputs - it simply outputs the dot product of our inputs and weights
# Compile the model and specify the optimizer and loss function
# Fit the model to the training data, specify the batch size and the WandbCallback() to track model
model.fit(X_train, y_train, epochs=10, batch_size=128, validation_data=(X_test, y_test),
callbacks=[wandb.keras.WandbCallback(data_type="image", labels=class_names, save_model=False)])
Data augmentation artificially expands the training dataset by creating slightly modified versions of images in the dataset - by scaling, shifting and rotating the images in the training set.
# Define the model (same as above)
# Compile the model
# Add data augmentation
datagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range=15, # randomly rotate images in the range (degrees, 0 to 180)
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip=True, # randomly flip images
vertical_flip=False) # randomly flip images
# Fit the model on the batches generated by datagen.flow()
steps_per_epoch=X_train.shape // config.batch_size,
Click through to a single run to see more details about that run. For example, on this run page you can see the performance metrics I logged when I ran this script.
The overview tab picks up a link to the code. In this case, it's a link to the Google Colab. If you're running a script from a git repo, we'll pick up the SHA of the latest git commit and give you a link to that version of the code in your own GitHub repo.
The System tab on the runs page lets you visualize how resource efficient your model was. It lets you monitor the GPU, memory, CPU, disk, and network usage in one spot.
As you can see running sweeps is super easy! We highly encourage you to fork this notebook, tweak the parameters, or try the model with your own dataset!
We're always free for academics and open source projects. Email firstname.lastname@example.org with any questions or feature suggestions. Here are some more resources: