Introduction to Convolutional Neural Networks with Weights & Biases
Introduction to Convolutional Neural Networks using the CIFAR-10 dataset
Created on September 20|Last edited on September 20
Comment
In this tutorial we'll walk through a simple convolutional neural network to classify the images in the cifar10 dataset.
You can find the accompanying code here. We highly encourage you to fork this notebook, tweak the parameters, or try the model with your own 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.

Creating A Basic Convolutional Neural Network
# Define modelmodel = tf.keras.models.Sequential()# Conv2D adds a convolution layer with 32 filters that generates 2 dimensionalfeature maps to learn different aspects of our imagemodel.add(tf.keras.layers.Conv2D(32, (3, 3), padding='same',input_shape=X_train.shape[1:], activation='relu'))# MaxPooling2D layer reduces the size of the image representation ourconvolutional layers learnt, and in doing so it reduces the number of parametersand computations the network needs to perform.model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))# Dropout layer turns off a percentage of neurons at every stepmodel.add(tf.keras.layers.Dropout(config.dropout))# Flattens our array so we can feed the convolution layer outputs (a matrix)into our fully connected layer (an array)model.add(tf.keras.layers.Flatten())# Dense layer creates dense, fully connected layers with x inputs and y outputs- it simply outputs the dot product of our inputs and weightsmodel.add(tf.keras.layers.Dense(config.dense_layer_nodes, activation='relu'))model.add(tf.keras.layers.Dropout(config.dropout))model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))# Compile the model and specify the optimizer and loss functionmodel.compile(loss='categorical_crossentropy',optimizer=tf.keras.optimizers.Adam(config.learn_rate),metrics=['accuracy'])# Fit the model to the training data, specify the batch size and theWandbCallback() to track modelmodel.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)])
Convolutional Neural Network with Data Augmentation
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 modelmodel.compile(loss='categorical_crossentropy',optimizer="adam",metrics=['accuracy'])# Add data augmentationdatagen = ImageDataGenerator(featurewise_center=False, # set input mean to 0 over the datasetsamplewise_center=False, # set each sample mean to 0featurewise_std_normalization=False, # divide inputs by std of the datasetsamplewise_std_normalization=False, # divide each input by its stdzca_whitening=False, # apply ZCA whiteningrotation_range=15, # randomly rotate images in the range (degrees, 0 to 180)width_shift_range=0.1, # randomly shift images horizontally (fraction oftotal width)height_shift_range=0.1, # randomly shift images vertically (fraction oftotal height)horizontal_flip=True, # randomly flip imagesvertical_flip=False) # randomly flip imagesdatagen.fit(X_train)# Fit the model on the batches generated by datagen.flow()model.fit_generator(datagen.flow(X_train, y_train,batch_size=config.batch_size),steps_per_epoch=X_train.shape[0] // config.batch_size,epochs=config.epochs,validation_data=(X_test, y_test),callbacks=[WandbCallback(data_type="image",labels=class_names)])
Project Overview
- Check out the project page to see your results in the shared project.
- Press 'option+space' to expand the runs table, comparing all the results from everyone who has tried this script.
- Click on the name of a run to dive in deeper to that single run on its own run page.

Visualize Performance
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.

Review Code
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.

Visualize System Metrics
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.

Next Steps
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!
More about Weights & Biases
We're always free for academics and open source projects. Email carey@wandb.com with any questions or feature suggestions. Here are some more resources:
- Documentation - Python docs
- Gallery - example reports in W&B
- Articles - blog posts and tutorials
- Community - join our Slack community forum
Add a comment
Tags: Beginner
Iterate on AI agents and models faster. Try Weights & Biases today.