MCPcopy Create free account
hub / github.com/pytorch/tutorials / Net

Class Net

intermediate_source/spatial_transformer_tutorial.py:93–146  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

91
92
93class Net(nn.Module):
94 def __init__(self):
95 super(Net, self).__init__()
96 self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
97 self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
98 self.conv2_drop = nn.Dropout2d()
99 self.fc1 = nn.Linear(320, 50)
100 self.fc2 = nn.Linear(50, 10)
101
102 # Spatial transformer localization-network
103 self.localization = nn.Sequential(
104 nn.Conv2d(1, 8, kernel_size=7),
105 nn.MaxPool2d(2, stride=2),
106 nn.ReLU(True),
107 nn.Conv2d(8, 10, kernel_size=5),
108 nn.MaxPool2d(2, stride=2),
109 nn.ReLU(True)
110 )
111
112 # Regressor for the 3 * 2 affine matrix
113 self.fc_loc = nn.Sequential(
114 nn.Linear(10 * 3 * 3, 32),
115 nn.ReLU(True),
116 nn.Linear(32, 3 * 2)
117 )
118
119 # Initialize the weights/bias with identity transformation
120 self.fc_loc[2].weight.data.zero_()
121 self.fc_loc[2].bias.data.copy_(torch.tensor([1, 0, 0, 0, 1, 0], dtype=torch.float))
122
123 # Spatial transformer network forward function
124 def stn(self, x):
125 xs = self.localization(x)
126 xs = xs.view(-1, 10 * 3 * 3)
127 theta = self.fc_loc(xs)
128 theta = theta.view(-1, 2, 3)
129
130 grid = F.affine_grid(theta, x.size())
131 x = F.grid_sample(x, grid)
132
133 return x
134
135 def forward(self, x):
136 # transform the input
137 x = self.stn(x)
138
139 # Perform the usual forward pass
140 x = F.relu(F.max_pool2d(self.conv1(x), 2))
141 x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
142 x = x.view(-1, 320)
143 x = F.relu(self.fc1(x))
144 x = F.dropout(x, training=self.training)
145 x = self.fc2(x)
146 return F.log_softmax(x, dim=1)
147
148
149model = Net().to(device)

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected