Improved real/dynamic FLOPs calculation tool of torchsummaryX.
Project description
torchsummaryDynamic
Improved tool of torchsummaryX.
torchsummaryDynamic support real FLOPs calculation of dynamic network or user-custom PyTorch ops.
Usage
pip install torchsummaryDynamic
and
from torchsummaryDynamic import summary
summary(your_model, torch.zeros((1, 3, 224, 224)))
# or
from torchsummaryDynamic import summary
summary(your_model, torch.zeros((1, 3, 224, 224)), calc_op_types=(nn.Conv2d, nn.Linear))
Args:
model(Module): Model to summarizex(Tensor): Input tensor of the model with [N, C, H, W] shape dtype and device have to match to the modelcalc_op_types(Tuple): Tuple of op types to be calculatedargs, kwargs: Other arguments used inmodel.forwardfunction
Examples
Calculate Dynamic Conv2d FLOPs/params
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchsummaryDynamic import summary
class USConv2d(nn.Conv2d):
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, us=[False, False]):
super(USConv2d, self).__init__(in_channels, out_channels, kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)
self.width_mult = None
self.us = us
def forward(self, inputs):
in_channels = inputs.shape[1] // self.groups if self.us[0] else self.in_channels // self.groups
out_channels = int(self.out_channels * self.width_mult) if self.us[1] else self.out_channels
weight = self.weight[:out_channels, :in_channels, :, :]
bias = self.bias[:out_channels] if self.bias is not None else self.bias
y = F.conv2d(inputs, weight, bias, self.stride, self.padding, self.dilation, self.groups)
return y
model = nn.Sequential(
USConv2d(3, 32, 3, us=[True, True]),
)
# width_mult=1.0
model.apply(lambda m: setattr(m, 'width_mult', 1.0))
summary(model, torch.zeros(1, 3, 224, 224))
# width_mult=0.5
model.apply(lambda m: setattr(m, 'width_mult', 0.5))
summary(model, torch.zeros(1, 3, 224, 224))
Output
# width_mult=1.0
==========================================================
Kernel Shape Output Shape Params Mult-Adds
Layer
0_0 [3, 32, 3, 3] [1, 32, 222, 222] 896 42581376
----------------------------------------------------------
Totals
Total params 896
Trainable params 896
Non-trainable params 0
Mult-Adds 42581376
==========================================================
# width_mult=0.5
==========================================================
Kernel Shape Output Shape Params Mult-Adds
Layer
0_0 [3, 32, 3, 3] [1, 16, 222, 222] 896 21290688
----------------------------------------------------------
Totals
Total params 896
Trainable params 896
Non-trainable params 0
Mult-Adds 21290688
==========================================================
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file torchsummaryDynamic-0.0.3.tar.gz.
File metadata
- Download URL: torchsummaryDynamic-0.0.3.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
935b23f2cd85c39a5c129fce6c5758050b7812b2a0ce5ec42632b3c41fca065a
|
|
| MD5 |
1adc9d675151b49938c3c4fa7f22bf3b
|
|
| BLAKE2b-256 |
2de79d46bbf7341688d4afe696f55310962296be0dac64005a61598a04533ae2
|