EFFICIENTNET#
Classes#
- class backbone.EfficientNet.BlockDecoder[source]#
Bases:
object
Block Decoder for readability, straight from the official TensorFlow repository.
- 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.
- 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.
- 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)
- 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.
- class backbone.EfficientNet.MBConvBlock(block_args, global_params, image_size=None)[source]#
Bases:
Module
Mobile Inverted Residual Bottleneck Block.
- Parameters:
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)
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.
- 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.
- 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.
- 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.
- 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.
- 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