0% found this document useful (0 votes)
8 views5 pages

NB4-09 PT IV Data Augmentation and Early Stopping

The document outlines a project focused on using data augmentation and early stopping techniques in a deep learning model for skin lesion classification across seven classes. It details the importance of these techniques, the setup of the workspace, and the process of loading and transforming the dataset for training. Additionally, it describes the architecture of a convolutional neural network (CNN) and the steps for training, validating, and testing the model, including performance evaluation through confusion matrices and classification reports.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

NB4-09 PT IV Data Augmentation and Early Stopping

The document outlines a project focused on using data augmentation and early stopping techniques in a deep learning model for skin lesion classification across seven classes. It details the importance of these techniques, the setup of the workspace, and the process of loading and transforming the dataset for training. Additionally, it describes the architecture of a convolutional neural network (CNN) and the steps for training, validating, and testing the model, including performance evaluation through confusion matrices and classification reports.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

NB4-09: Biomedical Imaging and PyTorch IV

Goal: Let's try to create a NB where we use data augmentation and early stopping
when i.e., we reach a model that does not improve for a certain number of epochs. We
will use a skin lesion dataset similar to the previous one but with 7 classes.

1. Introduction

Data Augmentation and Early Stopping are two crucial techniques in machine
learning and deep learning that help improve the performance and generalization of
models. Here’s an explanation of their importance:

A. Data Augmentation

Definition: Data augmentation involves creating new training examples from the
existing data by applying random transformations such as rotation, translation,
flipping, scaling, noise addition, and more.

Importance:

 Enhances Model Generalization: By introducing variability in the training


data, data augmentation helps the model generalize better to unseen data. This
reduces overfitting, where the model performs well on the training data but
poorly on new data.

 Improves Performance on Small Datasets: When there is limited data, data


augmentation effectively increases the size of the training dataset, giving the
model more diverse examples to learn from.

 Increases Model Robustness: Augmented data often simulates real-world


variations and distortions, making the model more robust to such scenarios
during inference.

 Supports Balanced Learning: In cases of imbalanced datasets (where some


classes have significantly fewer examples), data augmentation can help balance
the dataset by generating more examples of the minority classes.

Example: In image classification, rotating an image slightly or flipping it horizontally


creates a new training example. The model learns to recognize the object regardless
of its orientation, making it more versatile.

B. Early Stopping

Definition: Early stopping is a regularization technique where the training of a model


is halted when the performance on a validation dataset stops improving or begins to
degrade.

Importance:

 Prevents Overfitting: As training progresses, a model might start to


memorize the training data, leading to overfitting. Early stopping detects when
this occurs by monitoring the validation loss or accuracy and stops training
before the model overfits.
 Saves Computational Resources: Training deep models can be
computationally expensive. Early stopping can prevent unnecessary training
epochs once optimal performance is reached, saving time and resources.

 Optimizes Model Performance: By stopping at the point where the model


performs best on the validation data, early stopping ensures that the model is
as generalized as possible without compromising on performance.

 Reduces the Need for Manual Tuning: Finding the right number of training
epochs manually can be challenging. Early stopping automates this process by
dynamically determining the best point to stop training.

Example: Suppose you're training a neural network and notice that the validation
loss starts increasing after 50 epochs, while the training loss continues to decrease.
Early stopping would halt the training process at this point, preventing the model from
overfitting to the training data.

2. Setting up Our Workspace

First, we check if GPU is connected. The nvidia-smi command (NVIDIA System


Management Interface) is used to monitor and manage NVIDIA GPUs (Graphics
Processing Units) in a system. It provides detailed information about the status and
performance of the GPUs, including GPU utilization, temperature, memory usage,
processes utilizing the GPU, and more.

nvidia-smi is a command-line utility provided by NVIDIA that helps you manage and
monitor NVIDIA GPU devices. It stands for NVIDIA System Management Interface.

Setting our workspace: /content and /content/datasets

Setting our Home

We save the root directory of the project '/content' as 'HOME' since we will be
navigating through the directory to have multiple projects under the same HOME.
Additionally, we will have the datasets in the 'datasets' directory, so all datasets are
easily accessible for any project.

Mount Google Drive

Next, it imports the drive module from the google.colab library, which provides
functionalities for mounting Google Drive in Google Colab.

Additionally, Google Drive is mounted in Google Colab and made available at the
path /content/drive. The user will be prompted to authorize access to Google Drive.
Once authorized, the content of Google Drive will be accessible from that point
onwards in the Colab notebook.

3. Load a Dataset (Dataloader)

Create a directory where we can save our dataset

Create the dataset directory (if it doesn't exist), where we are going to save the
dataset with which we are going to train our CNN.

Inspect the Dataset: Skin Lesion Detection in 7 Classes


The dataset contains several thousand photos of cell images in seven subdirectories
(classes) with one cell image per class. The directory structure is as follows thanks to
this snipet:

 0: 'akiec' - actinic keratosis

 1: 'bcc' - basal cell carcinoma

 2: 'bkl' - benign keratosis

 3: 'df' - dermatofibroma

 4: 'mel' - melanoma

 5: 'nv' - melanocytic nevus

 6: 'vasc' - vascular lesion

Setting a Dataloader

The purpose of a DataLoader is fundamental in the context of machine learning and


deep learning, especially when working with large or complex datasets. Its main
purpose is to facilitate the efficient loading and manipulation of data during model
training.

Transform the dataloaders for data augmentation

 Initial Transformations: Various augmentation techniques like flipping,


cropping, rotating, color jittering, affine transformations, blurring, and
perspective changes are defined.

 Enhancing Transformations: Each augmentation technique is enhanced by


converting images to tensors and normalizing them.

 Final Output: The list of transformations is updated and printed.

These transformations are typically applied during the training process to increase the
diversity of the training data, helping to improve the generalization of the deep
learning model.

Normalize the dataloaders using Statistics

 Normalization: Normalization is crucial for ensuring that pixel values across


images are on a similar scale [0 1], which helps in stabilizing and speeding up
the training process of deep neural networks.

 Dataset Preparation: Each dataset (train_data, val_set, test_set) is prepared


with consistent transformations and normalization, facilitating uniformity in data
processing across training, validation, and testing phases.

This setup ensures that the datasets are properly preprocessed and ready to be used
in training and evaluating machine learning models, particularly deep neural
networks, using PyTorch.

The train set is unmodified in size because ``transform()`` transform the data but it
don't augment the dataset.
Data Augmentation

The train set is modified in size because ConcatDataset() augment the dataset.

Displaying all classes

Let us show one example for each class, for fun. As we've transformed the image by
normalizing it, we should undo the transformation before visualizing the image.

Settings Hyperparameters

Define batch_size, epochs and obtain the number of classes

We are going to define some training parameters for the network, such as the number
of batches, epochs, and classes in the dataset because they are needed for
dataloaders in order to set up our training loop. We will run only 10 epochs to check
functionality. Later, we will load a model that has already been trained for 30 epochs.

Display all images and its ground truth from a random batch

To see how the DataLoader works and how it handles the loaded data, we will select a
random batch and display it, indicating its class label as well. It is said, we can display
all images and its ground truth from a random batch in a easy way with dataloaders.

Alongside 'normal' images, we should observe transformed images.

4. Define a Convolutional Neural Network

Define the Model

To enhance previous CNN model, we can make several adjustments:

1. Dropout Layers: Add dropout layers to reduce overfitting.

2. Residual Connections: Adding residual connections can improve the learning


capability of deep networks.

3. Global Average Pooling: Replace the Flatten layer with a global average
pooling layer to reduce the number of parameters and prevent overfitting.

4. Learning Rate Scheduler: Implement a scheduler to dynamically adjust the


learning rate during training.

5. Weight Initialization: Properly initialize the weights to improve convergence.

6. Data Augmentation: While not part of the model architecture, performing data
augmentation during training can improve performance.

Explanation of Changes:

1. Dropout: Added nn.Dropout after the linear layers to reduce overfitting.

2. Global Average Pooling: Replaced nn.Flatten with nn.AdaptiveAvgPool2d to


reduce dimensionality without explicitly flattening.

3. Weight Initialization: Added a weight initialization function that uses Kaiming


initialization to improve convergence.
4. Residual Connections: Not included in this version but can be considered for a
more advanced version.

These changes can improve the model's generalization capability and efficiency.

5. Train the network

Create Train directories

To create directories named train1, train2, etc., each time you execute a training loop,
you can modify the code to check the number of existing training directories and then
create the next directory in sequence. Here's an example of how you could do this:

6. Validating our model

Todo código.

7. Predictions (Inference)

Testing

For this multiclass classification test , we need change some things:

1. Class Number Check:

o The code now checks if the confusion matrix is 2x2 to determine if it is a


binary classification problem. If so, it extracts the values of TN, FP, FN,
and TP. If not, it simply prints the entire confusion matrix.

2. Confusion Matrix Visualization:

o It uses seaborn.heatmap to visualize the confusion matrix, which helps to


better understand the model's performance.

3. Classification Report:

o Prints a detailed classification report


using classification_report from sklearn, providing additional metrics like
precision, recall, and F1-score for each class.

These changes should help you better understand your model's results and ensure
that the code handles different numbers of classes correctly.

You might also like