CoolFace
Modelpublic

abhuse/EfficientNet-V2-S

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes10downloads
Model Card

EfficientNetV2-S in Pytorch with pretrained weights

A single-file implementation of EfficientNetV2 as introduced in: [\[Tan & Le 2021\]: EfficientNetV2: Smaller Models and Faster Training](https://arxiv.org/pdf/2104.00298.pdf)

Pretrained Weights

Original implementations of EfficientNetV2 include pretrained weigths in Tensorflow format. These weigths were converted to Pytorch format and are provided in this repository.

Source code, usage examples and evaluation script: Github

Accuracy

ModelImageNet 1k Top-1 accuracy, %
EfficientNetV2-b077.590%
EfficientNetV2-b178.872%
EfficientNetV2-b279.388%
EfficientNetV2-b382.260%
EfficientNetV2-S84.282%
EfficientNetV2-M85.596%
EfficientNetV2-L86.298%
EfficientNetV2-XL86.414%

Usage

Check out cifar100_train.ipynb if you would like to experiment with models. To evaluate pretrained models against Imagenet validation set, run imagenet_eval.ipynb.

The example below creates an EfficientNetV2-S model that takes 3-channel image of shape [224, 224] as input and outputs distribution over 50 classes, model weights are initialized with weights pretrained on ImageNet dataset:

python
import torch
from efficientnet_v2 import EfficientNetV2

model = EfficientNetV2('s',
                        in_channels=3,
                        n_classes=50,
                        pretrained=True)

# x - tensor of shape [batch_size, in_channels, image_height, image_width]
x = torch.randn([10, 3, 224, 224])

# to get predictions:
pred = model(x) 
print('out shape:', pred.shape)
# >>> out shape: torch.Size([10, 50])

# to extract features:
features = model.get_features(x)
for i, feature in enumerate(features):
    print('feature %d shape:' % i, feature.shape)
# >>> feature 0 shape: torch.Size([10, 48, 56, 56])
# >>> feature 1 shape: torch.Size([10, 64, 28, 28])
# >>> feature 2 shape: torch.Size([10, 160, 14, 14])
# >>> feature 3 shape: torch.Size([10, 256, 7, 7])

Parameters

  • *model_name, (str)* - Model name, one of 'b0', 'b1', 'b2', 'b3', 's', 'm', 'l', 'xl'
  • *in_channels, (int), (Default=3)* - Number of channels in input image
  • *n_classes, (int), (Default=1000)* - Number of output classes
  • *tf_style_conv, (bool), (Default=False) - Whether to simulate "SAME" padding of Tensorflow's convolution op. Set to True* when evaluating pretrained models against Imagenet dataset
  • *in_spatial_shape, (int or iterable of ints), (Default=None) - Spatial dimensionality of input image, tuple (height, width) or single integer size for shape (size, size). It is recommended to specify this parameter only when tfstyleconv=True*
  • *activation, (str), (Default='silu')* - Activation function
  • *activation_kwargs, (dict), (Default=None)* - Keyword arguments to pass to activation function
  • *bias, (bool), (Default=False)* - Enable bias in convolution operations
  • *drop_connect_rate, (float), (Default=0.2)* - DropConnect rate, set to 0 to disable DropConnect
  • *dropout_rate, (float or None), (Default=None) - Dropout rate, set to None* to use default dropout rate for each model
  • *bn_epsilon, (float), (Default=0.001)* - Batch normalizaton epsilon
  • *bn_momentum, (float), (Default=0.01)* - Batch normalization momentum
  • *pretrained, (bool), (Default=False)* - Initialize model with weights pretrained on ImageNet dataset
  • *progress, (bool), (Default=False)* - Show progress bar when downloading pretrained weights

The default parameter values are the ones that were used in original implementation.

Requirements

  • Python v3.5+
  • Pytorch v1.0+