Registry / ai-ml / efficientnet-pytorch

efficientnet-pytorch

JSON →
library0.7.1pypypi✓ verified 90d ago

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.

pip install efficientnet-pytorch
INSTALL
IMPORT
SIG · EFFICIENTNET-PYTOR
E
efficientnet-pytorch
ai-mlpythonv0.7.1
Install
71.9s avg
Import
5457ms
Disk
4787MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.7.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 78s
py 3.11
✕ build_error
✓ 73.38s
py 3.12
✕ build_error
✓ 70s
py 3.13
✕ build_error
✓ 66.23s
py 3.9
✕ build_error
✕ timeout
4787MB installed
● package 4787MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

EfficientNet
✓ from efficientnet_pytorch import EfficientNet
✗ from efficientnet_pytorch.model import EfficientNet
The primary class is directly available under the top-level package.
utils
✓ from efficientnet_pytorch import utils
✗ from efficientnet_pytorch.model import utils
Common utility functions like `get_model_params` or `model_params` are exposed via the `utils` submodule, not directly from the top-level.

This quickstart demonstrates how to load a pretrained EfficientNet-B0 model, prepare an input image using standard ImageNet preprocessing, and perform a forward pass for inference. It includes handling for GPU availability and sets the model to evaluation mode.

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()}")
Debug
Known issues
gotchaInput 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).
fix
Always apply `torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])` and resize to the model's expected input resolution (e.g., 224 for B0, 240 for B1, etc.).
affects: 0.1.0 - 0.7.1
deprecatedThe `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.
fix
For new projects or if encountering compatibility issues with very recent PyTorch versions, evaluate using `torchvision.models.efficientnet_b0` (or similar) from PyTorch itself, which is actively maintained. For existing projects, ensure your PyTorch version is compatible (e.g., <2.0 for best stability).
affects: 0.7.1 (and prior)
gotchaWhen 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.
fix
Refer to the `efficientnet-pytorch` documentation or source code if using AdvProp models for specific preprocessing recommendations. For most cases, standard ImageNet preprocessing should work, but be mindful of potential discrepancies if results are unexpected.
affects: 0.7.0 - 0.7.1
Errors
Common errors & fixes
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
Input image was loaded as grayscale (1 channel) instead of RGB (3 channels).
fix
When 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
The model name specified in `from_pretrained()` is incorrect or a typo, or there's no internet connection to download the weights.
fix
Double-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
The input tensor's dimensions do not match the expected `[batch_size, channels, height, width]` format, often due to a missing batch dimension.
fix
After preprocessing an individual image, add a batch dimension using `input_tensor.unsqueeze(0)` before passing it to the model.
Upgrade
Version history
0.7.1latest on PyPI · released Apr 15, 2021
Audit
Dependencies
torchrequiredCore deep learning framework
tqdmoptionalFor progress bars in utility functions
Agent activity
20 hits · last 30 days
node
18
Amazon
1
OpenAI (training)
1
Resources
efficientnet-pytorch — pip install efficientnet-pytorch · libregistry