ResNet (Residual Networks) fundamentally changed deep learning by introducing "skip connections" or "residual blocks." Prior to ResNet, stacking many convolutional layers led to vanishing or exploding gradients, making very deep networks difficult to train and often resulting in worse performance. Residual blocks mitigate this by allowing the network to learn a residual mapping H(x) - x, where H(x) is the desired underlying mapping, instead of directly learning H(x). This simple yet powerful innovation enabled the training of extraordinarily deep networks (e.g., ResNet-50, ResNet-101, ResNet-152) and significantly boosted performance on image recognition tasks, establishing ResNet as a cornerstone architecture for many computer vision backbones.
Building upon CNN foundations, EfficientNet focuses on the critical balance between model accuracy and computational efficiency. Instead of arbitrary scaling, EfficientNet proposes a principled compound scaling method that uniformly scales network depth, width, and image resolution using a fixed set of scaling coefficients derived through a neural architecture search. This allows EfficientNet models to achieve state-of-the-art accuracy with significantly fewer parameters and FLOPs compared to other CNNs. For an ML engineer, EfficientNet offers a powerful toolkit for deploying high-performing models, particularly in resource-constrained environments or when latency and inference costs are paramount, by providing a family of models (EfficientNet-B0 to B7) optimized for different computational budgets.
Vision Transformers (ViT) represent a paradigm shift, adapting the highly successful Transformer architecture, originally dominant in NLP, directly to computer vision tasks, moving away from the convolutional inductive biases. ViT processes images by splitting them into fixed-size patches, linearly embedding these patches, and treating them as a sequence of tokens. These tokens are then fed into a standard Transformer encoder, relying on self-attention mechanisms to capture global dependencies across the image. While ViTs often require massive datasets for pre-training to achieve superior performance due to their lack of inherent inductive biases (like locality and translation equivariance), they have demonstrated state-of-the-art results, especially on large-scale vision benchmarks, and offer compelling advantages in modeling long-range interactions within images that traditional CNNs might struggle with.
Key Takeaways
- ResNet's skip connections resolved vanishing gradients, enabling ultra-deep, high-performing CNNs.
- EfficientNet introduced principled compound scaling to efficiently balance CNN accuracy and computational cost.
- Vision Transformers adapt NLP's self-attention to image patches, challenging CNNs and excelling with large datasets.
- Each architecture addresses different challenges: depth (ResNet), efficiency (EfficientNet), or global context (ViT).
- Choice depends on performance needs, available data, and computational constraints for the target application.
Code Example
import tensorflow as tf
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.models import Model
# Load ResNet50 pre-trained on ImageNet, exclude top classification layer
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
# Add custom classification layers for a new task
x = base_model.output
x = Flatten()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x) # Example: 10 classes
model = Model(inputs=base_model.input, outputs=predictions)
# Freeze base model layers for initial training to use it as a feature extractor
for layer in base_model.layers:
layer.trainable = False
model.summary()How this code works
This code demonstrates a powerful technique called transfer learning, where a pre-trained deep learning model is adapted for a new task. Its job is to efficiently set up a neural network for image classification by leveraging the features learned by ResNet50 on a vast dataset, then adding specific layers to classify images into a custom set of categories.
The process begins by loading ResNet50 from tensorflow.keras.applications. The weights='imagenet' argument tells it to load weights from the ImageNet competition, meaning the model already understands many general image features. Crucially, include_top=False removes ResNet50's original classification head, allowing us to attach our own layers. The input_shape specifies the expected image size. On top of the base_model.output, new layers like Flatten() and Dense layers are added to create predictions for, in this example, 10 specific classes using a softmax activation. Finally, model = Model(...) constructs the complete network by connecting the base_model.input to these new prediction layers.
A subtle but vital step is the for layer in base_model.layers: layer.trainable = False loop. This "freezes" the weights of the pre-trained ResNet50 layers. Freezing them means they won't be updated during initial training, making the base_model act purely as a feature extractor. This is often done to prevent messing up the well-learned features and to speed up training of the new top layers, especially when the custom dataset is smaller. The model.summary() then provides an overview of the combined architecture and confirms which layers are trainable.