EFFICIENTNET#

Classes#

class backbone.EfficientNet.BlockDecoder[source]#

Bases: object

Block Decoder for readability, straight from the official TensorFlow repository.

static decode(string_list)[source]#

Decode a list of string notations to specify blocks inside the network.

Parameters:

string_list (list[str]) – A list of strings, each string is a notation of block.

Returns:

A list of BlockArgs namedtuples of block args.

Return type:

blocks_args

static encode(blocks_args)[source]#

Encode a list of BlockArgs to a list of strings.

Parameters:

blocks_args (list[namedtuples]) – A list of BlockArgs namedtuples of block args.

Returns:

A list of strings, each string is a notation of block.

Return type:

block_strings

class backbone.EfficientNet.Conv2dDynamicSamePadding(in_channels, out_channels, kernel_size, stride=1, dilation=1, groups=1, bias=True)[source]#

Bases: Conv2d

2D Convolutions like TensorFlow, for a dynamic image size. The padding is operated in forward function by calculating dynamically.

forward(x)[source]#
class backbone.EfficientNet.Conv2dStaticSamePadding(in_channels, out_channels, kernel_size, stride=1, image_size=None, **kwargs)[source]#

Bases: Conv2d

2D Convolutions like TensorFlow’s ‘SAME’ mode, with the given input image size. The padding mudule is calculated in construction function, then used in forward.

forward(x)[source]#
class backbone.EfficientNet.EfficientNet(blocks_args=None, global_params=None, hookme=False)[source]#

Bases: MammothBackbone

EfficientNet model.

Most easily loaded with the .from_name or .from_pretrained methods.

Parameters:
  • blocks_args (list[namedtuple]) – A list of BlockArgs to construct blocks.

  • global_params (namedtuple) – A set of GlobalParams shared between blocks.

References

[1] https://arxiv.org/abs/1905.11946 (EfficientNet)

Example

>>> import torch
>>> from efficientnet.model import EfficientNet
>>> inputs = torch.rand(1, 3, 224, 224)
>>> model = EfficientNet.from_pretrained('efficientnet-b0')
>>> model.eval()
>>> outputs = model(inputs)
activations_hook(grad)[source]#
extract_endpoints(inputs)[source]#

Use convolution layer to extract features from reduction levels i in [1, 2, 3, 4, 5].

Parameters:

inputs (tensor) – Input tensor.

Returns:

Dictionary of last intermediate features with reduction levels i in [1, 2, 3, 4, 5]. .. rubric:: Example

>>> import torch
>>> from efficientnet.model import EfficientNet
>>> inputs = torch.rand(1, 3, 224, 224)
>>> model = EfficientNet.from_pretrained('efficientnet-b0')
>>> endpoints = model.extract_endpoints(inputs)
>>> print(endpoints['reduction_1'].shape)  # torch.Size([1, 16, 112, 112])
>>> print(endpoints['reduction_2'].shape)  # torch.Size([1, 24, 56, 56])
>>> print(endpoints['reduction_3'].shape)  # torch.Size([1, 40, 28, 28])
>>> print(endpoints['reduction_4'].shape)  # torch.Size([1, 112, 14, 14])
>>> print(endpoints['reduction_5'].shape)  # torch.Size([1, 320, 7, 7])
>>> print(endpoints['reduction_6'].shape)  # torch.Size([1, 1280, 7, 7])

extract_features(inputs)[source]#

use convolution layer to extract feature .

Parameters:

inputs (tensor) – Input tensor.

Returns:

Output of the final convolution layer in the efficientnet model.

forward(inputs, returnt='out')[source]#
EfficientNet’s forward function.

Calls extract_features to extract features, applies final linear layer, and returns logits.

Parameters:

inputs (tensor) – Input tensor.

Returns:

Output of this model after processing.

classmethod from_name(model_name, in_channels=3, **override_params)[source]#

Create an efficientnet model according to name.

Parameters:
  • model_name (str) – Name for efficientnet.

  • in_channels (int) – Input data’s channel number.

  • override_params (other key word params) –

    Params to override model’s global_params. Optional key:

    ’width_coefficient’, ‘depth_coefficient’, ‘image_size’, ‘dropout_rate’, ‘num_classes’, ‘batch_norm_momentum’, ‘batch_norm_epsilon’, ‘drop_connect_rate’, ‘depth_divisor’, ‘min_depth’

Returns:

An efficientnet model.

classmethod from_pretrained(model_name, weights_path=None, advprop=False, in_channels=3, num_classes=1000, **override_params)[source]#

Create an efficientnet model according to name.

Parameters:
  • model_name (str) – Name for efficientnet.

  • weights_path (None or str) – str: path to pretrained weights file on the local disk. None: use pretrained weights downloaded from the Internet.

  • advprop (bool) – Whether to load pretrained weights trained with advprop (valid when weights_path is None).

  • in_channels (int) – Input data’s channel number.

  • num_classes (int) – Number of categories for classification. It controls the output size for final linear layer.

  • override_params (other key word params) –

    Params to override model’s global_params. Optional key:

    ’width_coefficient’, ‘depth_coefficient’, ‘image_size’, ‘dropout_rate’, ‘batch_norm_momentum’, ‘batch_norm_epsilon’, ‘drop_connect_rate’, ‘depth_divisor’, ‘min_depth’

Returns:

A pretrained efficientnet model.

classmethod get_image_size(model_name)[source]#

Get the input image size for a given efficientnet model.

Parameters:

model_name (str) – Name for efficientnet.

Returns:

Input image size (resolution).

set_swish(memory_efficient=True)[source]#

Sets swish function as memory efficient (for training) or standard (for export).

Parameters:

memory_efficient (bool) – Whether to use memory-efficient version of swish.

class backbone.EfficientNet.MBConvBlock(block_args, global_params, image_size=None)[source]#

Bases: Module

Mobile Inverted Residual Bottleneck Block.

Parameters:
  • block_args (namedtuple) – BlockArgs, defined in utils.py.

  • global_params (namedtuple) – GlobalParam, defined in utils.py.

  • image_size (tuple or list) – [image_height, image_width].

References

[1] https://arxiv.org/abs/1704.04861 (MobileNet v1) [2] https://arxiv.org/abs/1801.04381 (MobileNet v2) [3] https://arxiv.org/abs/1905.02244 (MobileNet v3)

forward(inputs, drop_connect_rate=None)[source]#

MBConvBlock’s forward function.

Parameters:
  • inputs (tensor) – Input tensor.

  • drop_connect_rate (bool) – Drop connect rate (float, between 0 and 1).

Returns:

Output of this block after processing.

set_swish(memory_efficient=True)[source]#

Sets swish function as memory efficient (for training) or standard (for export).

Parameters:

memory_efficient (bool) – Whether to use memory-efficient version of swish.

class backbone.EfficientNet.MemoryEfficientSwish(*args, **kwargs)[source]#

Bases: Module

forward(x)[source]#
class backbone.EfficientNet.SwishImplementation(*args, **kwargs)[source]#

Bases: Function

static backward(ctx, grad_output)[source]#
static forward(ctx, i)[source]#

Functions#

backbone.EfficientNet.calculate_output_image_size(input_image_size, stride)[source]#
Calculates the output image size when using Conv2dSamePadding with a stride.

Necessary for static padding. Thanks to mannatsingh for pointing this out.

Parameters:
  • input_image_size (int, tuple or list) – Size of input image.

  • stride (int, tuple or list) – Conv2d operation’s stride.

Returns:

A list [H,W].

Return type:

output_image_size

backbone.EfficientNet.drop_connect(inputs, p, training)[source]#

Drop connect. :param input (tensor: BCWH): Input of this structure. :param p (float: 0.0~1.0): Probability of drop connection. :param training: The running mode. :type training: bool

Returns:

Output after drop connection.

Return type:

output

backbone.EfficientNet.efficientnet(width_coefficient=None, depth_coefficient=None, image_size=None, dropout_rate=0.2, drop_connect_rate=0.2, num_classes=1000, include_top=True)[source]#

Create BlockArgs and GlobalParams for efficientnet model.

Parameters:
  • width_coefficient (float) –

  • depth_coefficient (float) –

  • image_size (int) –

  • dropout_rate (float) –

  • drop_connect_rate (float) –

  • num_classes (int) –

  • suggests. (Meaning as the name) –

Returns:

blocks_args, global_params.

backbone.EfficientNet.efficientnet_params(model_name)[source]#

Get efficientnet params based on model name.

backbone.EfficientNet.efficientnet_tf(width_coefficient=None, depth_coefficient=None, dropout_rate=0.2, survival_prob=0.8)[source]#

Creates a efficientnet model.

backbone.EfficientNet.get_model_params(model_name, override_params)[source]#

Get the block args and global params for a given model name.

Parameters:
  • model_name (str) – Model’s name.

  • override_params (dict) – A dict to modify global_params.

Returns:

blocks_args, global_params

backbone.EfficientNet.get_model_params_tf(model_name, override_params)[source]#

Get the block args and global params for a given model.

backbone.EfficientNet.get_same_padding_conv2d(image_size=None)[source]#
Chooses static padding if you have specified an image size, and dynamic padding otherwise.

Static padding is necessary for ONNX exporting of models.

Parameters:

image_size (int or tuple) – Size of the image.

Returns:

Conv2dDynamicSamePadding or Conv2dStaticSamePadding.

backbone.EfficientNet.get_width_and_height_from_size(x)[source]#

Obtain height and width from x. :param x: Data size. :type x: int, tuple or list

Returns:

A tuple or list (H,W).

Return type:

size

backbone.EfficientNet.load_pretrained_weights(model, model_name, weights_path=None, load_fc=True, verbose=True)[source]#

Loads pretrained weights from weights path or download using url. :param model: The whole model of efficientnet. :type model: Module :param model_name: Model name of efficientnet. :type model_name: str :param weights_path: str: path to pretrained weights file on the local disk.

None: use pretrained weights downloaded from the Internet.

Parameters:
  • load_fc (bool) – Whether to load pretrained weights for fc layer at the end of the model.

  • advprop (bool) – Whether to load pretrained weights trained with advprop (valid when weights_path is None).

backbone.EfficientNet.mammoth_efficientnet(nclasses, model_name, pretrained=False)[source]#

Instantiates a ResNet18 network.

Parameters:
  • nclasses (int) – number of output classes

  • nf – number of filters

Returns:

ResNet network

backbone.EfficientNet.round_filters(filters, global_params)[source]#
Calculate and round number of filters based on width multiplier.

Use width_coefficient, depth_divisor and min_depth of global_params.

Parameters:
  • filters (int) – Filters number to be calculated.

  • global_params (namedtuple) – Global params of the model.

Returns:

New filters number after calculating.

Return type:

new_filters

backbone.EfficientNet.round_repeats(repeats, global_params)[source]#
Calculate module’s repeat number of a block based on depth multiplier.

Use depth_coefficient of global_params.

Parameters:
  • repeats (int) – num_repeat to be calculated.

  • global_params (namedtuple) – Global params of the model.

Returns:

New repeat number after calculating.

Return type:

new repeat