| 5 | import torch.nn as nn |
| 6 | |
| 7 | class STN3d(nn.Module): |
| 8 | def __init__(self, channel): |
| 9 | super(STN3d, self).__init__() |
| 10 | self.conv1 = torch.nn.Conv1d(channel, 64, 1) |
| 11 | self.conv2 = torch.nn.Conv1d(64, 128, 1) |
| 12 | self.conv3 = torch.nn.Conv1d(128, 1024, 1) |
| 13 | self.fc1 = nn.Linear(1024, 512) |
| 14 | self.fc2 = nn.Linear(512, 256) |
| 15 | self.fc3 = nn.Linear(256, 9) |
| 16 | self.relu = nn.ReLU() |
| 17 | |
| 18 | self.bn1 = nn.BatchNorm1d(64) |
| 19 | self.bn2 = nn.BatchNorm1d(128) |
| 20 | self.bn3 = nn.BatchNorm1d(1024) |
| 21 | self.bn4 = nn.BatchNorm1d(512) |
| 22 | self.bn5 = nn.BatchNorm1d(256) |
| 23 | |
| 24 | def forward(self, x): |
| 25 | batchsize = x.size()[0] |
| 26 | x = F.relu(self.bn1(self.conv1(x))) |
| 27 | x = F.relu(self.bn2(self.conv2(x))) |
| 28 | x = F.relu(self.bn3(self.conv3(x))) |
| 29 | x = torch.max(x, 2, keepdim=True)[0] |
| 30 | x = x.view(-1, 1024) |
| 31 | |
| 32 | x = F.relu(self.bn4(self.fc1(x))) |
| 33 | x = F.relu(self.bn5(self.fc2(x))) |
| 34 | x = self.fc3(x) |
| 35 | |
| 36 | iden = Variable(torch.from_numpy(np.array([1, 0, 0, 0, 1, 0, 0, 0, 1]).astype(np.float32))).view(1, 9).repeat( |
| 37 | batchsize, 1) |
| 38 | if x.is_cuda: |
| 39 | iden = iden.cuda() |
| 40 | x = x + iden |
| 41 | x = x.view(-1, 3, 3) |
| 42 | return x |
| 43 | |
| 44 | class STNkd(nn.Module): |
| 45 | def __init__(self, k=64): |