-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnets.py
49 lines (42 loc) · 1.85 KB
/
nets.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
47
48
49
import torch.nn as nn
import torch.nn.functional as F
'''
define networks
'''
class Net(nn.Module):
def __init__(self, num_in_channel, num_filter):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(num_in_channel, num_filter, 3, padding=1)
self.bn1 = nn.BatchNorm2d(num_filter)
self.conv2 = nn.Conv2d(num_filter, num_filter, 3, padding=1)
self.bn2 = nn.BatchNorm2d(num_filter)
self.conv3 = nn.Conv2d(num_filter, num_filter, 3, padding=1)
self.bn3 = nn.BatchNorm2d(num_filter)
self.conv4 = nn.Conv2d(num_filter, num_filter, 3, padding=1)
self.bn4 = nn.BatchNorm2d(num_filter)
self.pool = nn.MaxPool2d(2, stride=2)
def forward(self, x):
x = self.pool(F.relu(self.bn1(self.conv1(x)))) # 40x40
x = self.pool(F.relu(self.bn2(self.conv2(x)))) # 20x20
x = F.relu(self.bn3(self.conv3(x))) # 20x20
x = F.relu(self.bn4(self.conv4(x))) # bx64x20x20
return x
class RelationNet(nn.Module):
def __init__(self, num_in_channel, num_filter, num_fc1, num_fc2, drop_prob):
super(RelationNet, self).__init__()
self.conv1 = nn.Conv2d(num_in_channel, num_filter, 3, padding=1)
self.bn1 = nn.BatchNorm2d(num_filter)
self.conv2 = nn.Conv2d(num_filter, num_filter, 3, padding=1)
self.bn2 = nn.BatchNorm2d(num_filter)
self.fc1 = nn.Linear(num_fc1, num_fc2)
self.fc2 = nn.Linear(num_fc2, 1)
self.pool = nn.MaxPool2d(2, stride=2)
self.drop_prob = drop_prob
def forward(self, x):
x = self.pool(F.relu(self.bn1(self.conv1(x)))) # 10x10
x = self.pool(F.relu(self.bn2(self.conv2(x)))) # 5x5
x = x.view(x.size()[0], -1) # 6400
x = F.relu(self.fc1(x)) #8
x = F.dropout(x, p=self.drop_prob, training=self.training)
x = F.sigmoid(self.fc2(x)) #1
return x