EfficientNet (PyTorch)
efficientnet-pytorch is a PyTorch implementation of the EfficientNet model series (B0-B7), offering highly optimized convolutional neural networks for image classification. As of version 0.7.1, it provides access to pretrained models for various EfficientNet scales, including those trained with AdvProp. The library has seen limited updates since its last major release in 2020, but remains functional and widely used for its robust performance.
Common errors
-
RuntimeError: Given groups=1, weight of size [N, 3, H, W], expected input[1, 1, 224, 224] to have 3 channels, but got 1 channels instead
cause Input image was loaded as grayscale (1 channel) instead of RGB (3 channels).fixWhen loading an image with PIL, ensure to call `.convert('RGB')` after `Image.open()`, e.g., `Image.open('path/to/image.jpg').convert('RGB')`. -
FileNotFoundError: Cannot find pretrained model for efficientnet-bX
cause The model name specified in `from_pretrained()` is incorrect or a typo, or there's no internet connection to download the weights.fixDouble-check the model name against the list of available models (e.g., 'efficientnet-b0', 'efficientnet-b1', ..., 'efficientnet-b7'). Ensure an active internet connection for initial download. -
RuntimeError: shape '[-1, 3, 224, 224]' is invalid for input of size X
cause The input tensor's dimensions do not match the expected `[batch_size, channels, height, width]` format, often due to a missing batch dimension.fixAfter preprocessing an individual image, add a batch dimension using `input_tensor.unsqueeze(0)` before passing it to the model.
Warnings
- gotcha Input images must be preprocessed correctly. The pretrained models expect images normalized with ImageNet mean and standard deviation, and resized to the appropriate input size (e.g., 224x224 for b0).
- deprecated The `efficientnet-pytorch` library has not seen significant updates or new releases since 2020. While functional, it may not leverage the latest PyTorch features, optimizations, or include bug fixes for very recent PyTorch versions. Consider alternative implementations or the official `torchvision.models` for more actively maintained EfficientNet models.
- gotcha When loading AdvProp models (e.g., `efficientnet-b0-ap`), ensure your preprocessing aligns with the specific training methodology. While standard ImageNet normalization is generally applicable, some nuances might exist.
Install
-
pip install efficientnet-pytorch -
pip install efficientnet_pytorch==0.7.1
Imports
- EfficientNet
from efficientnet_pytorch.model import EfficientNet
from efficientnet_pytorch import EfficientNet
- utils
from efficientnet_pytorch.model import utils
from efficientnet_pytorch import utils
Quickstart
import torch
from efficientnet_pytorch import EfficientNet
from torchvision import transforms
from PIL import Image
# 1. Load a pretrained EfficientNet model
model = EfficientNet.from_pretrained('efficientnet-b0')
model.eval() # Set model to evaluation mode
# 2. Define standard ImageNet preprocessing
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
# 3. Create a dummy image (replace with actual image loading)
# For a real scenario, use Image.open('path/to/image.jpg').convert('RGB')
img = Image.new('RGB', (256, 256), color = 'red')
# 4. Preprocess the image and add batch dimension
input_tensor = preprocess(img)
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
# 5. Move input to the appropriate device (CPU or GPU)
if torch.cuda.is_available():
input_batch = input_batch.to('cuda')
model.to('cuda')
# 6. Perform inference
with torch.no_grad():
output = model(input_batch)
# The output 'output' contains the logits for the classes
print(f"Output logits shape: {output.shape}")
# Example: get predicted class
# _, predicted_idx = torch.max(output, 1)
# print(f"Predicted class index: {predicted_idx.item()}")