-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
32 lines (23 loc) · 804 Bytes
/
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
import torch.nn as nn
import torch.nn.functional as F
import torchvision
class Model(nn.Module):
def __init__(self):
super().__init__()
self.resnet = torchvision.models.resnet18(pretrained=True)
fc_in_shape = self.resnet.fc.in_features
self.logit = nn.Linear(fc_in_shape, 4)
def forward(self, x):
batch_size, C, H, W = x.shape
x = self.resnet.conv1(x)
x = self.resnet.bn1(x)
x = self.resnet.relu(x)
x = self.resnet.maxpool(x)
x = self.resnet.layer1(x)
x = self.resnet.layer2(x)
x = self.resnet.layer3(x)
x = self.resnet.layer4(x)
x = F.adaptive_avg_pool2d(x, 1).reshape(batch_size, -1)
x = F.dropout(x, 0.25, self.training)
x = self.logit(x)
return x