|
| 1 | +--- |
| 2 | +layout: hub_detail |
| 3 | +background-class: hub-background |
| 4 | +body-class: hub |
| 5 | +title: EfficientNet |
| 6 | +summary: Memory and Performance Efficient Networks with 8 configurations. |
| 7 | +category: researchers |
| 8 | +image: efficientnet1.png |
| 9 | +author: Pytorch Team |
| 10 | +tags: [vision, scriptable] |
| 11 | +github-link: https://github.com/pytorch/vision/blob/main/torchvision/models/efficientnet.py |
| 12 | +github-id: pytorch/vision |
| 13 | +featured_image_1: efficientnet1.png |
| 14 | +featured_image_2: no-image |
| 15 | +accelerator: cuda-optional |
| 16 | +order: 10 |
| 17 | +--- |
| 18 | + |
| 19 | +```python |
| 20 | +import torch |
| 21 | +model = torch.hub.load('pytorch/vision:v0.11.0', 'efficientnet_b0', pretrained=True) |
| 22 | +# or any of these variants |
| 23 | +# model = torch.hub.load('pytorch/vision:v0.11.0', 'efficientnet_b1', pretrained=True) |
| 24 | +# model = torch.hub.load('pytorch/vision:v0.11.0', 'efficientnet_b2', pretrained=True) |
| 25 | +# model = torch.hub.load('pytorch/vision:v0.11.0', 'efficientnet_b3', pretrained=True) |
| 26 | +# model = torch.hub.load('pytorch/vision:v0.11.0', 'efficientnet_b4', pretrained=True) |
| 27 | +# model = torch.hub.load('pytorch/vision:v0.11.0', 'efficientnet_b5', pretrained=True) |
| 28 | +# model = torch.hub.load('pytorch/vision:v0.11.0', 'efficientnet_b6', pretrained=True) |
| 29 | +# model = torch.hub.load('pytorch/vision:v0.11.0', 'efficientnet_b7', pretrained=True) |
| 30 | +model.eval() |
| 31 | +``` |
| 32 | + |
| 33 | +All pre-trained models expect input images normalized in the same way, |
| 34 | +i.e. mini-batches of 3-channel RGB images of shape `(3 x H x W)`, where `H` and `W` are expected to be at least `224`. |
| 35 | +The images have to be loaded in to a range of `[0, 1]` and then normalized using `mean = [0.485, 0.456, 0.406]` |
| 36 | +and `std = [0.229, 0.224, 0.225]`. |
| 37 | + |
| 38 | +Here's a sample execution. |
| 39 | + |
| 40 | +```python |
| 41 | +# Download an example image from the pytorch website |
| 42 | +import urllib |
| 43 | +url, filename = ("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg") |
| 44 | +try: urllib.URLopener().retrieve(url, filename) |
| 45 | +except: urllib.request.urlretrieve(url, filename) |
| 46 | +``` |
| 47 | + |
| 48 | +```python |
| 49 | +# sample execution (requires torchvision) |
| 50 | +from PIL import Image |
| 51 | +from torchvision import transforms |
| 52 | +input_image = Image.open(filename) |
| 53 | +preprocess = transforms.Compose([ |
| 54 | + transforms.Resize(256), |
| 55 | + transforms.CenterCrop(224), |
| 56 | + transforms.ToTensor(), |
| 57 | + transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), |
| 58 | +]) |
| 59 | +input_tensor = preprocess(input_image) |
| 60 | +input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model |
| 61 | + |
| 62 | +# move the input and model to GPU for speed if available |
| 63 | +if torch.cuda.is_available(): |
| 64 | + input_batch = input_batch.to('cuda') |
| 65 | + model.to('cuda') |
| 66 | + |
| 67 | +with torch.no_grad(): |
| 68 | + output = model(input_batch) |
| 69 | +# Tensor of shape 1000, with confidence scores over Imagenet's 1000 classes |
| 70 | +print(output[0]) |
| 71 | +# The output has unnormalized scores. To get probabilities, you can run a softmax on it. |
| 72 | +probabilities = torch.nn.functional.softmax(output[0], dim=0) |
| 73 | +print(probabilities) |
| 74 | +``` |
| 75 | + |
| 76 | +``` |
| 77 | +# Download ImageNet labels |
| 78 | +!wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt |
| 79 | +``` |
| 80 | + |
| 81 | +``` |
| 82 | +# Read the categories |
| 83 | +with open("imagenet_classes.txt", "r") as f: |
| 84 | + categories = [s.strip() for s in f.readlines()] |
| 85 | +# Show top categories per image |
| 86 | +top5_prob, top5_catid = torch.topk(probabilities, 5) |
| 87 | +for i in range(top5_prob.size(0)): |
| 88 | + print(categories[top5_catid[i]], top5_prob[i].item()) |
| 89 | +``` |
| 90 | + |
| 91 | +### Model Description |
| 92 | + |
| 93 | +The EfficientNet architecture description. |
| 94 | + |
| 95 | +| Model structure | Top-1 error | Top-5 error | |
| 96 | +| ------------------ | ----------- | ----------- | |
| 97 | +| efficientnet_b0 | 22.30 | 6.46 | |
| 98 | +| efficientnet_b1 | 21.35 | 5.82 | |
| 99 | +| efficientnet_b2 | 19.39 | 4.69 | |
| 100 | +| efficientnet_b3 | 17.99 | 3.94 | |
| 101 | +| efficientnet_b4 | 16.61 | 3.40 | |
| 102 | +| efficientnet_b5 | 16.55 | 3.37 | |
| 103 | +| efficientnet_b6 | 15.99 | 3.08 | |
| 104 | +| efficientnet_b7 | 15.87 | 3.09 | |
| 105 | + |
| 106 | + |
| 107 | +### References |
| 108 | + |
| 109 | + - [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks](https://arxiv.org/abs/1905.11946) |
| 110 | + - [EfficientNet Blog by Google Research](https://ai.googleblog.com/2019/05/efficientnet-improving-accuracy-and.html) |
0 commit comments