-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
61 lines (58 loc) · 2.19 KB
/
model.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
50
51
52
53
54
55
56
57
58
59
60
61
import torch
from torch.nn import Module
from torch.nn import Sequential, Conv2d, Sigmoid, ReLU, MaxPool2d, Linear, Flatten
from torchvision import models
from dataset import *
import torch.nn as nn
torch.set_printoptions(precision=20)
class siamese(Module):
def __init__(self):
super(siamese, self).__init__()
self.cnn = Sequential(
Conv2d(1, 64, kernel_size=3, stride=1, padding=1),
ReLU(),
Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
ReLU(),
MaxPool2d(kernel_size=2,stride=2),
Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
ReLU(),
Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
ReLU(),
MaxPool2d(kernel_size=2,stride=2),
Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
ReLU(),
Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
ReLU(),
Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
ReLU(),
MaxPool2d(kernel_size=2,stride=2),
Conv2d(256, 512, kernel_size=3, stride=1, padding=1),
ReLU(),
Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
ReLU(),
Conv2d(512, 512, kernel_size=3, stride=1, padding=1),
ReLU(),
MaxPool2d(kernel_size=2,stride=2),
Flatten()
)
self.fully_connect = Sequential(
Linear(51200, 512), # 这里的51200是会随着h和w变的
Linear(512,1),
Sigmoid()
)
def forward (self, x1, x2):
batch_size = x1.shape[0]
x1 = self.cnn(x1)
x2 = self.cnn(x2)
ans = torch.abs(x1-x2)
ans = ans.view(batch_size, -1)
ans = self.fully_connect(ans)
ans = ans.view(-1)
return ans
if __name__ == "__main__":
train_test = dataset(max_num=100)
train_loader = DataLoader(train_test, batch_size=16, shuffle=False)
network = siamese()
for i, (first, second, label) in enumerate(train_loader):
ans = network(first, second)
torch.set_printoptions(precision=20)