xbe.at

Computer vision

Comparing 2-Layer CNN Models on CIFAR-10

8 min read, 14 sections

Contents
  1. Model Comparison: CNN Architectures
  2. Factors Affecting Model Performance
  3. Efficient Model Deployment
  4. Knowledge Distillation (KD)
  5. DistilBERT: A Practical Example
  6. Limitations of Knowledge Distillation
  7. Teacher Assistant Approach
  8. Benefits of Teacher Assistant Approach
  9. Real-Life Example: Image Classification
  10. Real-Life Example: Text Classification
  11. Evaluating Knowledge Distillation
  12. Challenges and Considerations
  13. Future Directions and Research
  14. Additional Resources

Model Comparison: CNN Architectures

Two 2-layer CNN models were trained on the CIFAR-10 dataset, resulting in different accuracies. Model A achieved 70% accuracy, while Model B reached 74%. This difference is not due to hyperparameter tuning, suggesting that other factors are at play. Let's explore the possible reasons for this performance gap.

import tensorflow as tf
from tensorflow.keras import layers, models

def create_cnn_model():
    model = models.Sequential([
        layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
        layers.MaxPooling2D((2, 2)),
        layers.Conv2D(64, (3, 3), activation='relu'),
        layers.MaxPooling2D((2, 2)),
        layers.Flatten(),
        layers.Dense(64, activation='relu'),
        layers.Dense(10, activation='softmax')
    ])
    return model

model_a = create_cnn_model()
model_b = create_cnn_model()

# Train and evaluate models
# ...

print(f"Model A accuracy: {model_a_accuracy:.2f}")
print(f"Model B accuracy: {model_b_accuracy:.2f}")

Factors Affecting Model Performance

Several factors can contribute to the performance difference between two seemingly identical CNN models. These include initialization of weights, data shuffling, and small variations in the training process. Even with the same architecture, these factors can lead to different local optima during training.

import numpy as np
import matplotlib.pyplot as plt

def plot_loss_curves(model_a_history, model_b_history):
    plt.figure(figsize=(10, 6))
    plt.plot(model_a_history.history['loss'], label='Model A Loss')
    plt.plot(model_b_history.history['loss'], label='Model B Loss')
    plt.title('Training Loss Comparison')
    plt.xlabel('Epoch')
    plt.ylabel('Loss')
    plt.legend()
    plt.show()

# Simulate training histories
np.random.seed(42)
epochs = 50
model_a_history = {'loss': np.random.rand(epochs) * 0.5 + 0.5}
model_b_history = {'loss': np.random.rand(epochs) * 0.4 + 0.3}

plot_loss_curves(model_a_history, model_b_history)

Efficient Model Deployment

To ensure efficient deployment of ML models in production, two main approaches are commonly used: training small models from scratch or using knowledge distillation to transfer knowledge from larger models to smaller ones. Both methods aim to reduce computational requirements and memory usage in production environments.

def small_model():
    return models.Sequential([
        layers.Conv2D(16, (3, 3), activation='relu', input_shape=(32, 32, 3)),
        layers.MaxPooling2D((2, 2)),
        layers.Flatten(),
        layers.Dense(32, activation='relu'),
        layers.Dense(10, activation='softmax')
    ])

small_model = small_model()
print(f"Small model parameter count: {small_model.count_params():,}")

Knowledge Distillation (KD)

Knowledge distillation is a technique where a smaller, simpler model (student) is trained to mimic the output of a larger, more complex model (teacher). This process allows the student model to benefit from the knowledge captured by the teacher model while maintaining a smaller size and lower computational requirements.

import tensorflow as tf

def knowledge_distillation_loss(y_true, y_pred, teacher_pred, temperature=2.0):
    soft_targets = tf.nn.softmax(teacher_pred / temperature)
    soft_prob = tf.nn.softmax(y_pred / temperature)
    return tf.keras.losses.categorical_crossentropy(soft_targets, soft_prob) * (temperature ** 2)

# Example usage
teacher_model = create_cnn_model()
student_model = small_model()

# Train student model using KD loss
# ...

DistilBERT: A Practical Example

DistilBERT is a notable example of knowledge distillation in natural language processing. It is a smaller version of the BERT model, retaining approximately 97% of BERT's capabilities while being 40% smaller. This significant reduction in size makes DistilBERT more suitable for deployment in resource-constrained environments.

from transformers import DistilBertTokenizer, DistilBertModel
import torch

tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
model = DistilBertModel.from_pretrained('distilbert-base-uncased')

text = "Knowledge distillation helps create efficient models."
inputs = tokenizer(text, return_tensors="pt")
outputs = model(**inputs)

print(f"Output shape: {outputs.last_hidden_state.shape}")
print(f"Model size: {sum(p.numel() for p in model.parameters()):,} parameters")

Limitations of Knowledge Distillation

In practice, knowledge distillation has some limitations. There is a limit to how much a student model can learn from a teacher model of a given size. Additionally, for a given teacher model, there is a minimum size for the student model below which effective knowledge transfer becomes challenging.

import numpy as np
import matplotlib.pyplot as plt

def plot_kd_effectiveness(teacher_sizes, student_sizes, effectiveness):
    plt.figure(figsize=(10, 6))
    plt.imshow(effectiveness, cmap='viridis', aspect='auto')
    plt.colorbar(label='KD Effectiveness')
    plt.xlabel('Student Model Size')
    plt.ylabel('Teacher Model Size')
    plt.title('Knowledge Distillation Effectiveness')
    plt.xticks(range(len(student_sizes)), student_sizes)
    plt.yticks(range(len(teacher_sizes)), teacher_sizes)
    plt.show()

teacher_sizes = [1e6, 5e6, 1e7, 5e7, 1e8]
student_sizes = [1e5, 5e5, 1e6, 5e6, 1e7]
effectiveness = np.random.rand(len(teacher_sizes), len(student_sizes))

plot_kd_effectiveness(teacher_sizes, student_sizes, effectiveness)

Teacher Assistant Approach

To address the limitations of direct knowledge distillation, an intermediate model called the "teacher assistant" can be introduced. This approach involves a two-step process: first, the assistant model learns from the teacher model, and then the student model learns from the assistant model.

def create_teacher_model():
    return models.Sequential([
        layers.Conv2D(64, (3, 3), activation='relu', input_shape=(32, 32, 3)),
        layers.MaxPooling2D((2, 2)),
        layers.Conv2D(128, (3, 3), activation='relu'),
        layers.MaxPooling2D((2, 2)),
        layers.Flatten(),
        layers.Dense(128, activation='relu'),
        layers.Dense(10, activation='softmax')
    ])

def create_assistant_model():
    return models.Sequential([
        layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
        layers.MaxPooling2D((2, 2)),
        layers.Conv2D(64, (3, 3), activation='relu'),
        layers.MaxPooling2D((2, 2)),
        layers.Flatten(),
        layers.Dense(64, activation='relu'),
        layers.Dense(10, activation='softmax')
    ])

teacher_model = create_teacher_model()
assistant_model = create_assistant_model()
student_model = small_model()

# Implement two-step KD process
# ...

Benefits of Teacher Assistant Approach

The teacher assistant approach can significantly enhance the performance and efficiency of the final student model. While it adds an additional training step, the benefits often outweigh the extra computational cost, especially in production environments where model efficiency is crucial.

import numpy as np
import matplotlib.pyplot as plt

def plot_model_comparison(models, accuracies):
    plt.figure(figsize=(10, 6))
    plt.bar(models, accuracies)
    plt.title('Model Accuracy Comparison')
    plt.xlabel('Model')
    plt.ylabel('Accuracy')
    plt.ylim(0, 1)
    for i, v in enumerate(accuracies):
        plt.text(i, v + 0.01, f'{v:.2f}', ha='center')
    plt.show()

models = ['Teacher', 'Assistant', 'Student (Direct KD)', 'Student (TA KD)']
accuracies = [0.95, 0.92, 0.88, 0.91]

plot_model_comparison(models, accuracies)

Real-Life Example: Image Classification

Let's consider an image classification task for identifying different types of fruits. We'll use a pre-trained MobileNetV2 as the teacher model and create a smaller custom CNN as the student model. The goal is to achieve comparable performance with a much smaller model size.

from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Teacher model (pre-trained MobileNetV2)
teacher_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
teacher_model = tf.keras.Sequential([
    teacher_model,
    layers.GlobalAveragePooling2D(),
    layers.Dense(5, activation='softmax')
])

# Student model (custom CNN)
student_model = models.Sequential([
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(5, activation='softmax')
])

# Data preparation
datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)
train_generator = datagen.flow_from_directory(
    'path/to/fruit/dataset',
    target_size=(224, 224),
    batch_size=32,
    class_mode='categorical',
    subset='training'
)

# Knowledge distillation training
# ...

print(f"Teacher model size: {teacher_model.count_params():,} parameters")
print(f"Student model size: {student_model.count_params():,} parameters")

Real-Life Example: Text Classification

In this example, we'll use BERT as the teacher model and a simpler LSTM-based model as the student for sentiment analysis on movie reviews. The goal is to create a more lightweight model suitable for deployment on mobile devices while maintaining good performance.

from transformers import BertTokenizer, TFBertForSequenceClassification
import tensorflow as tf

# Teacher model (BERT)
teacher_model = TFBertForSequenceClassification.from_pretrained('bert-base-uncased')
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

# Student model (LSTM-based)
max_length = 128
vocab_size = 10000

student_model = tf.keras.Sequential([
    layers.Embedding(vocab_size, 100, input_length=max_length),
    layers.LSTM(64, return_sequences=True),
    layers.LSTM(32),
    layers.Dense(64, activation='relu'),
    layers.Dense(1, activation='sigmoid')
])

# Prepare data
# ...

# Knowledge distillation training
# ...

print(f"Teacher model size: {teacher_model.count_params():,} parameters")
print(f"Student model size: {student_model.count_params():,} parameters")

Evaluating Knowledge Distillation

To assess the effectiveness of knowledge distillation, we need to compare the performance of the student model trained with and without KD. We'll use metrics such as accuracy, inference time, and model size to evaluate the trade-offs between performance and efficiency.

import time

def evaluate_model(model, test_data, test_labels):
    start_time = time.time()
    predictions = model.predict(test_data)
    inference_time = time.time() - start_time
    
    accuracy = np.mean(np.argmax(predictions, axis=1) == np.argmax(test_labels, axis=1))
    model_size = model.count_params()
    
    return accuracy, inference_time, model_size

# Evaluate teacher model
teacher_accuracy, teacher_time, teacher_size = evaluate_model(teacher_model, test_data, test_labels)

# Evaluate student model (without KD)
student_accuracy, student_time, student_size = evaluate_model(student_model, test_data, test_labels)

# Evaluate student model (with KD)
student_kd_accuracy, student_kd_time, student_kd_size = evaluate_model(student_model_kd, test_data, test_labels)

# Plot results
# ...

Challenges and Considerations

While knowledge distillation can be highly effective, there are challenges to consider. These include selecting the right teacher-student model pair, determining the optimal temperature for softening probability distributions, and balancing the trade-off between model size and performance. It's crucial to carefully evaluate these factors for each specific use case.

def plot_size_performance_tradeoff(models, sizes, accuracies):
    plt.figure(figsize=(10, 6))
    plt.scatter(sizes, accuracies)
    for i, model in enumerate(models):
        plt.annotate(model, (sizes[i], accuracies[i]))
    plt.xlabel('Model Size (parameters)')
    plt.ylabel('Accuracy')
    plt.title('Model Size vs. Performance Trade-off')
    plt.xscale('log')
    plt.grid(True)
    plt.show()

models = ['Teacher', 'Student (No KD)', 'Student (KD)', 'Student (TA KD)']
sizes = [1e8, 1e6, 1e6, 1e6]
accuracies = [0.95, 0.85, 0.89, 0.91]

plot_size_performance_tradeoff(models, sizes, accuracies)

Future Directions and Research

Knowledge distillation continues to be an active area of research. Future directions include exploring multi-teacher distillation, developing more effective intermediate representations, and investigating the theoretical foundations of knowledge transfer. These advancements may lead to even more efficient and powerful models in the future.

def plot_research_trends():
    years = [2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023]
    kd_papers = [10, 25, 50, 100, 200, 350, 500, 700, 900]
    
    plt.figure(figsize=(10, 6))
    plt.plot(years, kd_papers, marker='o')
    plt.title('Knowledge Distillation Research Trend')
    plt.xlabel('Year')
    plt.ylabel('Number of Published Papers')
    plt.grid(True)
    plt.show()

plot_research_trends()

Additional Resources

For those interested in delving deeper into knowledge distillation and model compression techniques, here are some valuable resources:

  1. "Distilling the Knowledge in a Neural Network" by Hinton et al. (2015) ArXiv: https://arxiv.org/abs/1503.02531
  2. "TinyBERT: Distilling BERT for Natural Language Understanding" by Jiao et al. (2020) ArXiv: https://arxiv.org/abs/1909.10351
  3. "Knowledge Distillation: A Survey" by Gou et al. (2021) ArXiv: https://arxiv.org/abs/2006.05525
  4. "Born-Again Neural Networks" by Furlanello et al. (2018) ArXiv: https://arxiv.org/abs/1805.04770
  5. "Data-Free Knowledge Distillation for Deep Neural Networks" by Lopes et al. (2017) ArXiv: https://arxiv.org/abs/1710.07535

These papers provide a comprehensive overview of knowledge distillation techniques, their applications, and recent advancements in the field. They cover both theoretical foundations and practical implementations, making them excellent starting points for researchers and practitioners alike.

More in Computer vision

  1. Convolutional Neural Network with Python10 min

    Convolutional operations are fundamental building blocks in neural networks, particularly in computer vision tasks. They enable the network to learn spatial...

  2. Advanced ECG Analysis Using Transfer Learning and CNNs7 min

    This project focuses on classifying ECG images using advanced deep learning techniques. We'll explore data preparation, model development, and the...

  3. Convolutional Neural Networks and Kernels in Python6 min

    Convolutional Neural Networks (CNNs) have revolutionized the field of computer vision and image processing. They are designed to automatically and adaptively...

  4. Active Shape Model for Face Image Generation8 min

    Active Shape Models (ASM) are statistical models of shape that can deform to fit new examples. They are particularly useful in face recognition and image...

  5. Convolutional Neural Networks on Manifolds with Python10 min

    Convolutional Neural Networks (CNNs) have revolutionized image processing tasks, but their application to non-Euclidean domains like manifolds presents...

  6. 7 Transfer Learning Models for Convolutional Neural Networks12 min

    Transfer learning is a powerful technique in deep learning where knowledge gained from training on one task is applied to a different but related task. In...