Lab 8a: ANN using Keras¶
Image Classification Using Keras¶
We are using Tensorflow for this lab. Install it using: pip install tensorflow
The libraries used in this lab:
1 2 3 4 5 | |
This section will go through how to build and train a simple image classification model using Keras.
Load and Preprocess Data¶
We’ll use the CIFAR-10 dataset, which is included in Keras. Load the dataset into the training and testing sets using the code below.
1 | |
Normalize data by scaling pixel values to be between 0 and 1. There are several benefits to normalizing data, with the most important being that it prevents any specific range of values from dominating the learning process.
1 | |
Split the training data into training and validation sets.
1 2 3 | |
Let's define label for data visualization. The class label is not used during training. It is simply a name that indicates what the class number represents.
1 | |
Plot some of the images to see how the data look like.
1 2 3 4 5 6 | |
Build the Model¶
We are building a 3-layers Convolutional Neural Network (CNN). A typical convolutional block had a convolutinal layer, activation function, pooling operator.
The convolutional layer is responsible for feature extraction, while the associated activation function introduces non-linearity. The pooling layer reduces the spatial dimensions of the feature map.
The last layer is the output layer, with the number of neurons in the dense layer. In this case, there are 10 neurons. This is typically used for a classification task with 10 classes (e.g., digits 0-9 in digit classification). This layer converts the logits into probabilities. The Softmax function normalizes the output so that the sum of all probabilities is 1, making it easier to interpret the model's predictions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Train the Model¶
model.compile is used to configure the model for training.
- The Adam optimizer is an adaptive learning rate optimization algorithm that’s widely used in training deep learning models.
- Sparse Categorical Crossentropy loss function is used for multi-class classification problems where the target labels are integers (not one-hot encoded). It measures the difference between the true labels and the predicted probabilities. logits is the model's output.
- from_logits=True: This parameter indicates that the model’s output is not a probability distribution (i.e., the output layer does not use a softmax activation function).
- Accuracy Metric specifies that the model's performance will be evaluated using accuracy, which is the proportion of correctly predicted instances out of the total instances. It's a common metric for classification tasks.
1 2 3 | |
model.fit trains the model on the provided training data.
- train_images: The input images for training.
- train_labels: The corresponding labels for the training images.
- epochs: The number of times the model will iterate over the entire training dataset. In this case, the model will train for 5 epochs.
- validation_data=(test_images, test_labels): This tuple provides the validation data, which is used to evaluate the model's performance on unseen data after each epoch. It consists of:
- test_images: The input images for validation.
- test_labels: The corresponding labels for the validation images.
- batch_size: The number of samples per gradient update. The training data will be divided into batches of 8 samples, and the model's weights will be updated after each batch.
1 | |
Evaluate the Model¶
model.evaluate evaluates the performance of the trained model on the test dataset.
- test_images: The images from the test dataset.
- test_labels: The corresponding labels for the test images.
- verbose=2: This parameter controls the verbosity mode. A value of 2 means that the function will print one line per epoch.
1 2 | |
1 2 3 4 5 6 7 | |
Make Predictions¶
model.predict(test_images) takes the test images as input and outputs the predicted probabilities for each class.
- predictions: This variable stores the predicted probabilities for each test image. Each element in predictions is an array of probabilities corresponding to the different classes.
1 | |
Visualise Predictions¶
1 2 3 4 5 6 7 8 9 10 | |