-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
43 lines (37 loc) · 1.18 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
import torch
import torch.nn as nn
import torch.nn.functional as F
class nielsen(nn.Module):
def __init__(self):
super(nielsen, self).__init__()
self.rnn = nn.LSTM(720, 128)
self.conv1 = nn.Conv2d(3, 10, kernel_size=4, stride=2)
self.bn1 = nn.BatchNorm2d(10)
self.max1 = nn.MaxPool2d(kernel_size=2, stride=1, padding=1)
self.conv2 = nn.Conv2d(10,5, kernel_size=4, stride=2)
self.bn2 = nn.BatchNorm2d(5)
self.max2 = nn.MaxPool2d(1,2)
self.fc = nn.Linear(128,2)
def forward(self,x):
x = x / 255.0
x = F.relu(self.bn1(self.conv1(x)))
x = self.max1(x)
x = F.relu(self.bn2(self.conv2(x)))
x = self.max2(x)
x = torch.reshape(x,(-1,1,720))
x, _ = self.rnn(x)
x = self.fc(x)
x = F.softmax(x,-1)
return x
def train(data, t_label, model, optimizer):
criterion = nn.MSELoss()
model.zero_grad()
pred = model(data).squeeze()
loss = criterion(pred, t_label)
loss.backward()
optimizer.step()
return loss
def eval_label(model,data):
labels = model(data).squeeze()
label = torch.argmax(labels,-1)
return label.tolist()