pytorch-receptive-field
在pytorch中计算CNN感受野大小。支持2D CNN和3D CNN。
创建2D可视化gif
# pip install git+https://github.com/Fangyh09/pytorch-receptive-field.git
from torch_receptive_field import receptive_field
from torch_receptive_field import receptive_field_visualization_2d
receptive_field_dict = receptive_field(model, (3, 256, 256))
image_path = "./examples/example.jpg"
output_path_without_extension = "./examples/example_receptive_field_2d"
receptive_field_visualization_2d(receptive_field_dict, image_path, output_path_without_extension)
使用方法
pip install git+https://github.com/Fangyh09/pytorch-receptive-field.git
from torch_receptive_field import receptive_field
receptive_field(model, input_size=(channels, H, W))
或者
from torch_receptive_field import receptive_field
dict = receptive_field(model, input_size=(channels, H, W))
receptive_field_for_unit(receptive_field_dict, "2", (2,2))
2D CNN示例
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_receptive_field import receptive_field
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
self.bn = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.avgpool = nn.AvgPool2d(kernel_size=3, stride=2, padding=1)
def forward(self, x):
y = self.conv(x)
y = self.bn(y)
y = self.relu(y)
y = self.maxpool(y)
y = self.avgpool(y)
return y
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # PyTorch v0.4.0
model = Net().to(device)
receptive_field_dict = receptive_field(model, (3, 256, 256))
receptive_field_for_unit(receptive_field_dict, "2", (1,1))
------------------------------------------------------------------------------
层 (类型) 特征图大小 起始点 步长 感受野
==============================================================================
0 [256, 256] 0.5 1.0 1.0
1 [128, 128] 0.5 2.0 7.0
2 [128, 128] 0.5 2.0 7.0
3 [128, 128] 0.5 2.0 7.0
4 [64, 64] 0.5 4.0 11.0
5 [32, 32] 0.5 8.0 19.0
==============================================================================
第2层,单元位置(1, 1)的感受野大小为
[(0, 6.0), (0, 6.0)]
更多说明
起始点
是特征图网格中第一个项的中心。
步长
是特征图网格中相邻项之间的距离。
感受野
是特征图网格中项的视野大小。
3D CNN示例
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_receptive_field import receptive_field
class Net3D(nn.Module):
def __init__(self):
super(Net3D, self).__init__()
self.conv = nn.Conv3d(3, 6, kernel_size=3, stride=1, padding=1, bias=False)
self.bn = nn.BatchNorm3d(6)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool3d(kernel_size=2, stride=2, padding=1)
def forward(self, x):
y = self.conv(x)
y = self.bn(y)
y = self.relu(y)
y = self.maxpool(y)
return y
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # PyTorch v0.4.0
model = Net().to(device)
receptive_field_dict = receptive_field(model, (3, 16, 16, 16))
receptive_field_for_unit(receptive_field_dict, "2", (1,1,1))
------------------------------------------------------------------------------
层 (类型) 特征图大小 起始点 步长 感受野
==============================================================================
0 [16, 16, 16] 0.5 1.0 1.0
1 [16, 16, 16] 0.5 1.0 3.0
2 [16, 16, 16] 0.5 1.0 3.0
3 [16, 16, 16] 0.5 1.0 3.0
4 [9, 9, 9] 0.0 2.0 4.0
==============================================================================
第2层,单元位置(1, 1, 1)的感受野大小为
[(0, 3.0), (0, 3.0), (0, 3.0)]
相关
感谢 @pytorch-summary