forked from Fangyh09/pytorch-receptive-field
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
46 lines (39 loc) · 1.52 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_receptive_field import receptive_field, receptive_field_for_unit
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
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, 256, 256))
receptive_field_for_unit(receptive_field_dict, "2", (1,1))
model = Net3D().to(device)
receptive_field_dict = receptive_field(model, (3, 16, 16, 16))
receptive_field_for_unit(receptive_field_dict, "2", (1,1,1))