Ticker

6/recent/ticker-posts

Deep Learning Lab-BAI701-Program 7

 

Program 7

Write a program to enable pre-train models to classify a given image dataset

1. Import Libraries

You load TensorFlow and Keras modules needed for building and training a deep learning model.

2. Enable GPU Memory Growth

This prevents TensorFlow from allocating all GPU memory upfront, which helps avoid crashes in Colab or limited-resource environments.

3. Load CIFAR-10 Dataset

You load 60,000 color images (32×32 pixels) across 10 classes like airplane, cat, truck, etc.

  • x_train, y_train: training images and labels
  • x_test, y_test: test images and labels

4. One-Hot Encode Labels

Converts integer labels (e.g., 3 for cat) into one-hot vectors like [0, 0, 0, 1, 0, 0, 0, 0, 0, 0].

5. Preprocess Images

You resize each image to 96×96 (required by MobileNetV2) and normalize pixel values to the range [0, 1].

6. Create Efficient Data Pipelines

Using tf.data.Dataset, you:

  • Shuffle and batch the training data
  • Batch and prefetch the test data This improves performance and memory efficiency.

7. Load MobileNetV2 Base Model

You use a pretrained MobileNetV2 (without its top classification layer) as a feature extractor.

  • include_top=False: removes the original classifier
  • trainable=False: freezes the base model weights

8. Build Custom Classifier

You add:

  • GlobalAveragePooling2D: reduces spatial dimensions
  • Dense(128, relu): learns new features
  • Dense(10, softmax): outputs probabilities for 10 classes

9. Compile the Model

You use:

  • adam optimizer for adaptive learning
  • categorical_crossentropy for multi-class classification
  • accuracy as the evaluation metric

10. Train the Model

You train for 2 epochs using the prepared datasets. You can increase this later for better performance.

11. Save the Model

You save the trained model in the modern .keras format for future use or deployment.

 

Visualize predictions from your trained MobileNetV2 model on the CIFAR-10 dataset:

1. Imports Libraries

Loads TensorFlow, NumPy, Matplotlib, and the Keras model loader.

2. Loads CIFAR-10 Test Data

Retrieves 10,000 test images from the CIFAR-10 dataset. Each image is 32×32 pixels and belongs to one of 10 classes (e.g., airplane, cat, truck).

3. Defines Class Labels

Maps numeric predictions (0–9) to human-readable class names.

4. Loads Your Trained Model

Loads the MobileNetV2 model you previously trained and saved as 'mobilenetv2_cifar10.keras'.

5. Preprocesses Each Image

Resizes each test image to 96×96 pixels (required by MobileNetV2) and normalizes pixel values to the range [0, 1].

6. Predicts and Displays 9 Images

For each of the first 9 test images:

  • Preprocesses the image
  • Runs it through the model to get class probabilities
  • Extracts the predicted class and confidence score
  • Displays the original image with the predicted label and confidence

7. Visualizes Results

Uses Matplotlib to show a 3×3 grid of test images, each labeled with the model’s prediction and confidence.

Testing trained MobileNetV2 model on a few CIFAR-10 images:

1. Imports Required Libraries

Loads TensorFlow, NumPy, and the Keras model loader to handle image data and predictions.

2. Loads CIFAR-10 Test Data

Retrieves 10,000 test images from the CIFAR-10 dataset. Each image is 32×32 pixels and belongs to one of 10 classes (like airplane, cat, truck, etc.).

3. Defines Class Labels

Maps numeric predictions (0–9) to human-readable class names.

4. Loads Your Trained Model

Loads the MobileNetV2 model you previously trained and saved as 'mobilenetv2_cifar10.keras'.

5. Preprocesses Each Image

Resizes each test image to 96×96 pixels (required by MobileNetV2) and normalizes pixel values to the range [0, 1].

6. Predicts Classes for First 5 Images

For each of the first 5 test images:

  • Preprocesses the image
  • Runs it through the model to get class probabilities
  • Extracts the predicted class and confidence score
  • Prints the result in a readable format

Evaluation pipeline for our trained MobileNetV2 model on the CIFAR-10 test set:

Overall Accuracy: 78.78%

This means your MobileNetV2 model correctly classified about 79 out of every 100 test images from CIFAR-10.

Confusion Matrix

Each row represents the true class, and each column represents the predicted class. For example:

  • Row 0 (airplane): 793 correctly predicted as airplane, 61 misclassified as ship, 37 as truck, etc.
  • Row 5 (dog): Only 581 correctly predicted as dog, but 179 misclassified as cat—indicating confusion between similar-looking classes.

 

Post a Comment

0 Comments