{"id":9720,"library":"efficientnet-pytorch","title":"EfficientNet (PyTorch)","description":"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.","status":"maintenance","version":"0.7.1","language":"en","source_language":"en","source_url":"https://github.com/lukemelas/EfficientNet-PyTorch","tags":["pytorch","computer-vision","efficientnet","deep-learning","image-classification","pretrained-models"],"install":[{"cmd":"pip install efficientnet-pytorch","lang":"bash","label":"Install latest version"},{"cmd":"pip install efficientnet_pytorch==0.7.1","lang":"bash","label":"Install specific version"}],"dependencies":[{"reason":"Core deep learning framework","package":"torch","optional":false},{"reason":"For progress bars in utility functions","package":"tqdm","optional":true}],"imports":[{"note":"The primary class is directly available under the top-level package.","wrong":"from efficientnet_pytorch.model import EfficientNet","symbol":"EfficientNet","correct":"from efficientnet_pytorch import EfficientNet"},{"note":"Common utility functions like `get_model_params` or `model_params` are exposed via the `utils` submodule, not directly from the top-level.","wrong":"from efficientnet_pytorch.model import utils","symbol":"utils","correct":"from efficientnet_pytorch import utils"}],"quickstart":{"code":"import torch\nfrom efficientnet_pytorch import EfficientNet\nfrom torchvision import transforms\nfrom PIL import Image\n\n# 1. Load a pretrained EfficientNet model\nmodel = EfficientNet.from_pretrained('efficientnet-b0')\nmodel.eval() # Set model to evaluation mode\n\n# 2. Define standard ImageNet preprocessing\npreprocess = transforms.Compose([\n    transforms.Resize(256),\n    transforms.CenterCrop(224),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n])\n\n# 3. Create a dummy image (replace with actual image loading)\n# For a real scenario, use Image.open('path/to/image.jpg').convert('RGB')\nimg = Image.new('RGB', (256, 256), color = 'red')\n\n# 4. Preprocess the image and add batch dimension\ninput_tensor = preprocess(img)\ninput_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model\n\n# 5. Move input to the appropriate device (CPU or GPU)\nif torch.cuda.is_available():\n    input_batch = input_batch.to('cuda')\n    model.to('cuda')\n\n# 6. Perform inference\nwith torch.no_grad():\n    output = model(input_batch)\n\n# The output 'output' contains the logits for the classes\nprint(f\"Output logits shape: {output.shape}\")\n# Example: get predicted class\n# _, predicted_idx = torch.max(output, 1)\n# print(f\"Predicted class index: {predicted_idx.item()}\")","lang":"python","description":"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."},"warnings":[{"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.).","message":"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).","severity":"gotcha","affected_versions":"0.1.0 - 0.7.1"},{"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).","message":"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.","severity":"deprecated","affected_versions":"0.7.1 (and prior)"},{"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.","message":"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.","severity":"gotcha","affected_versions":"0.7.0 - 0.7.1"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"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')`.","cause":"Input image was loaded as grayscale (1 channel) instead of RGB (3 channels).","error":"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"},{"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.","cause":"The model name specified in `from_pretrained()` is incorrect or a typo, or there's no internet connection to download the weights.","error":"FileNotFoundError: Cannot find pretrained model for efficientnet-bX"},{"fix":"After preprocessing an individual image, add a batch dimension using `input_tensor.unsqueeze(0)` before passing it to the model.","cause":"The input tensor's dimensions do not match the expected `[batch_size, channels, height, width]` format, often due to a missing batch dimension.","error":"RuntimeError: shape '[-1, 3, 224, 224]' is invalid for input of size X"}]}