Package: efficientnet-pytorch
EfficientNet PyTorchQuickstartInstall withpip install efficientnet_pytorchand load a pretrained EfficientNet with:fromefficientnet_pytorchimportEfficientNetmodel=EfficientNet.from_pretrained('efficientnet-b0')UpdatesUpdate (April 2, 2021)TheEfficientNetV2 paperhas been released! I am working on implementing it as you read this :)About EfficientNetV2:EfficientNetV2 is a new family of convolutional networks that have faster training speed and better parameter efficiency than previous models. To develop this family of models, we use a combination of training-aware neural architecture search and scaling, to jointly optimize training speed and parameter efficiency. The models were searched from the search space enriched with new ops such as Fused-MBConv.Here is a comparison:Update (Aug 25, 2020)This update adds:A newinclude_top(default:True) option (#208)Continuous testing withsotabenchCode quality improvements and fixes (#215#223)Update (May 14, 2020)This update adds comprehensive comments and documentation (thanks to @workingcoder).Update (January 23, 2020)This update adds a new category of pre-trained model based on adversarial training, calledadvprop. It is important to note that the preprocessing required for the advprop pretrained models is slightly different from normal ImageNet preprocessing. As a result, by default, advprop models are not used. To load a model with advprop, use:model=EfficientNet.from_pretrained("efficientnet-b0",advprop=True)There is also a new, largeefficientnet-b8pretrained model that is only available in advprop form. When using these models, replace ImageNet preprocessing code as follows:ifadvprop:# for models using advprop pretrained weightsnormalize=transforms.Lambda(lambdaimg:img*2.0-1.0)else:normalize=transforms.Normalize(mean=[0.485,0.456,0.406],std=[0.229,0.224,0.225])This update also addresses multiple other issues (#115,#128).Update (October 15, 2019)This update allows you to choose whether to use a memory-efficient Swish activation. The memory-efficient version is chosen by default, but it cannot be used when exporting using PyTorch JIT. For this purpose, we have also included a standard (export-friendly) swish activation function. To switch to the export-friendly version, simply callmodel.set_swish(memory_efficient=False)after loading your desired model. This update addresses issues#88and#89.Update (October 12, 2019)This update makes the Swish activation function more memory-efficient. It also addresses pull requests#72,#73,#85, and#86. Thanks to the authors of all the pull requests!Update (July 31, 2019)Upgrade the pip package withpip install --upgrade efficientnet-pytorchThe B6 and B7 models are now available. Additionally,allpretrained models have been updated to use AutoAugment preprocessing, which translates to better performance across the board. Usage is the same as before:fromefficientnet_pytorchimportEfficientNetmodel=EfficientNet.from_pretrained('efficientnet-b7')Update (June 29, 2019)This update adds easy model exporting (#20) and feature extraction (#38).Example: Export to ONNXExample: Extract featuresAlso: fixed a CUDA/CPU bug (#32)It is also now incredibly simple to load a pretrained model with a new number of classes for transfer learning:model=EfficientNet.from_pretrained('efficientnet-b1',num_classes=23)Update (June 23, 2019)The B4 and B5 models are now available. Their usage is identical to the other models:fromefficientnet_pytorchimportEfficientNetmodel=EfficientNet.from_pretrained('efficientnet-b4')OverviewThis repository contains an op-for-op PyTorch reimplementation ofEfficientNet, along with pre-trained models and examples.The goal of this implementation is to be simple, highly extensible, and easy to integrate into your own projects. This implementation is a work in progress -- new features are currently being implemented.At the moment, you can easily:Load pretrained EfficientNet modelsUse EfficientNet models for classification or feature extractionEvaluate EfficientNet models on ImageNet or your own imagesUpcoming features: In the next few days, you will be able to:Train new models from scratch on ImageNet with a simple commandQuickly finetune an EfficientNet on your own datasetExport EfficientNet models for productionTable of contentsAbout EfficientNetAbout EfficientNet-PyTorchInstallationUsageLoad pretrained modelsExample: ClassifyExample: Extract featuresExample: Export to ONNXContributingAbout EfficientNetIf you're new to EfficientNets, here is an explanation straight from the official TensorFlow implementation:EfficientNets are a family of image classification models, which achieve state-of-the-art accuracy, yet being an order-of-magnitude smaller and faster than previous models. We develop EfficientNets based on AutoML and Compound Scaling. In particular, we first useAutoML Mobile frameworkto develop a mobile-size baseline network, named as EfficientNet-B0; Then, we use the compound scaling method to scale up this baseline to obtain EfficientNet-B1 to B7.EfficientNets achieve state-of-the-art accuracy on ImageNet with an order of magnitude better efficiency:In high-accuracy regime, our EfficientNet-B7 achieves state-of-the-art 84.4% top-1 / 97.1% top-5 accuracy on ImageNet with 66M parameters and 37B FLOPS, being 8.4x smaller and 6.1x faster on CPU inference than previous bestGpipe.In middle-accuracy regime, our EfficientNet-B1 is 7.6x smaller and 5.7x faster on CPU inference thanResNet-152, with similar ImageNet accuracy.Compared with the widely usedResNet-50, our EfficientNet-B4 improves the top-1 accuracy from 76.3% of ResNet-50 to 82.6% (+6.3%), under similar FLOPS constraint.About EfficientNet PyTorchEfficientNet PyTorch is a PyTorch re-implementation of EfficientNet. It is consistent with theoriginal TensorFlow implementation, such that it is easy to load weights from a TensorFlow checkpoint. At the same time, we aim to make our PyTorch implementation as simple, flexible, and extensible as possible.If you have any feature requests or questions, feel free to leave them as GitHub issues!InstallationInstall via pip:pipinstallefficientnet_pytorchOr install from source:gitclonehttps://github.com/lukemelas/EfficientNet-PyTorchcdEfficientNet-Pytorch
pipinstall-e.UsageLoading pretrained modelsLoad an EfficientNet:fromefficientnet_pytorchimportEfficientNetmodel=EfficientNet.from_name('efficientnet-b0')Load a pretrained EfficientNet:fromefficientnet_pytorchimportEfficientNetmodel=EfficientNet.from_pretrained('efficientnet-b0')Details about the models are below:Name# ParamsTop-1 Acc.Pretrained?efficientnet-b05.3M76.3✓efficientnet-b17.8M78.8✓efficientnet-b29.2M79.8✓efficientnet-b312M81.1✓efficientnet-b419M82.6✓efficientnet-b530M83.3✓efficientnet-b643M84.0✓efficientnet-b766M84.4✓Example: ClassificationBelow is a simple, complete example. It may also be found as a jupyter notebook inexamples/simpleor as aColab Notebook.We assume that in your current directory, there is aimg.jpgfile and alabels_map.txtfile (ImageNet class names). These are both included inexamples/simple.importjsonfromPILimportImageimporttorchfromtorchvisionimporttransformsfromefficientnet_pytorchimportEfficientNetmodel=EfficientNet.from_pretrained('efficientnet-b0')# Preprocess imagetfms=transforms.Compose([transforms.Resize(224),transforms.ToTensor(),transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225]),])img=tfms(Image.open('img.jpg')).unsqueeze(0)print(img.shape)# torch.Size([1, 3, 224, 224])# Load ImageNet class nameslabels_map=json.load(open('labels_map.txt'))labels_map=[labels_map[str(i)]foriinrange(1000)]# Classifymodel.eval()withtorch.no_grad():outputs=model(img)# Print predictionsprint('-----')foridxintorch.topk(outputs,k=5).indices.squeeze(0).tolist():prob=torch.softmax(outputs,dim=1)[0,idx].item()print('{label:<75}({p:.2f}%)'.format(label=labels_map[idx],p=prob*100))Example: Feature ExtractionYou can easily extract features withmodel.extract_features:fromefficientnet_pytorchimportEfficientNetmodel=EfficientNet.from_pretrained('efficientnet-b0')# ... image preprocessing as in the classification example ...print(img.shape)# torch.Size([1, 3, 224, 224])features=model.extract_features(img)print(features.shape)# torch.Size([1, 1280, 7, 7])Example: Export to ONNXExporting to ONNX for deploying to production is now simple:importtorchfromefficientnet_pytorchimportEfficientNetmodel=EfficientNet.from_pretrained('efficientnet-b1')dummy_input=torch.randn(10,3,240,240)model.set_swish(memory_efficient=False)torch.onnx.export(model,dummy_input,"test-b1.onnx",verbose=True)Hereis a Colab example.ImageNetSeeexamples/imagenetfor details about evaluating on ImageNet.ContributingIf you find a bug, create a GitHub issue, or even better, submit a pull request. Similarly, if you have questions, simply post them as GitHub issues.I look forward to seeing what the community does with these models!