How to Use EfficientNet in PyTorch: Syntax and Example
To use
EfficientNet in PyTorch, import it from torchvision.models, load a pretrained model with efficientnet_b0(pretrained=True), and pass input tensors to get predictions. You can fine-tune or use it for feature extraction by modifying the final layer.Syntax
Here is the basic syntax to load and use EfficientNet in PyTorch:
from torchvision.models import efficientnet_b0: Import the EfficientNet B0 model.model = efficientnet_b0(pretrained=True): Load the pretrained model weights.model.eval(): Set the model to evaluation mode for inference.outputs = model(inputs): Pass input tensors to get predictions.
You can replace efficientnet_b0 with other variants like efficientnet_b1 to efficientnet_b7.
python
from torchvision.models import efficientnet_b0 import torch # Load pretrained EfficientNet B0 model = efficientnet_b0(pretrained=True) model.eval() # Example input: batch of 1 image, 3 color channels, 224x224 pixels inputs = torch.randn(1, 3, 224, 224) # Get model predictions outputs = model(inputs)
Example
This example shows how to load EfficientNet B0 pretrained on ImageNet, run a dummy input through it, and print the output shape.
python
from torchvision.models import efficientnet_b0 import torch # Load pretrained EfficientNet B0 model = efficientnet_b0(pretrained=True) model.eval() # Create a dummy input tensor (batch size 1, 3 channels, 224x224 image) inputs = torch.randn(1, 3, 224, 224) # Forward pass to get predictions outputs = model(inputs) # Print output shape (should be [1, 1000] for ImageNet classes) print('Output shape:', outputs.shape)
Output
Output shape: torch.Size([1, 1000])
Common Pitfalls
- Wrong input size: EfficientNet expects 224x224 images by default; using other sizes without resizing causes errors or poor results.
- Forgetting
model.eval(): Not setting evaluation mode disables dropout and batch norm behavior needed for inference. - Not normalizing inputs: Inputs should be normalized with ImageNet mean and std for best results.
- Using pretrained weights incorrectly: Ensure
pretrained=Trueto load weights; otherwise, the model is random.
python
from torchvision.models import efficientnet_b0 import torch # Wrong way: no eval mode model = efficientnet_b0(pretrained=True) inputs = torch.randn(1, 3, 224, 224) outputs_wrong = model(inputs) # Dropout active, results vary # Right way: set eval mode model.eval() outputs_right = model(inputs)
Quick Reference
Tips for using EfficientNet in PyTorch:
- Use
torchvision.models.efficientnet_b0toefficientnet_b7for different sizes. - Input images must be resized to 224x224 and normalized with ImageNet stats.
- Call
model.eval()before inference to disable training layers. - For fine-tuning, replace the final classifier layer with your own.
Key Takeaways
Import EfficientNet from torchvision.models and load pretrained weights with pretrained=True.
Resize and normalize input images to 224x224 with ImageNet mean and std before passing to the model.
Always call model.eval() before inference to get consistent predictions.
You can fine-tune EfficientNet by replacing the final classification layer.
EfficientNet variants range from b0 (smallest) to b7 (largest) for different accuracy and speed trade-offs.