-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
86 lines (66 loc) · 2.63 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from CFG import CFG
import torch
import torch.nn as nn
import torchvision.models as models
# ====================================================
# MODEL
# ====================================================
class CustomModel(nn.Module):
def __init__(self, model_name=CFG.model_name, pretrained=CFG.if_pretrained):
super().__init__()
if model_name == 'resnet18':
self.base = models.resnet18(weights='ResNet18_Weights.DEFAULT')
if model_name == 'resnet34':
self.base = models.resnet34(weights='ResNet34_Weights.DEFAULT')
if model_name == 'resnet152':
self.base = models.resnet152(weights='ResNet152_Weights.DEFAULT')
if model_name == 'resnet101':
self.base = timm.create_model(model_name)
elif model_name == 'resnext50_32x4d':
self.base = timm.create_model(model_name)
n_features = self.base.fc.in_features # 512
self.base.fc = nn.Linear(n_features, 64)
self.bn1 = nn.BatchNorm1d(64)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(p=0.2)
self.meta_net = nn.Sequential(nn.Linear(34, 128),
nn.BatchNorm1d(128),
nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(128, 64),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.Dropout(p=0.5),
nn.Linear(64, 32)
)
self.fc3 = nn.Linear(96, 40)
self.bn3 = nn.BatchNorm1d(40)
self.layer_out = nn.Linear(40, CFG.target_size)
def forward(self, imgs, metas):
cnn1 = self.base(imgs)
x = self.bn1(cnn1)
x = self.relu(x)
x = self.dropout(x)
meta_ = self.meta_net(metas)
x = torch.cat((x, meta_), 1)
x = self.fc3(x)
x = self.bn3(x)
x = self.relu(x)
x = self.dropout(x)
x = self.layer_out(x)
return x
# ====================================================
# Hooks
# ====================================================
class Hook():
def __init__(self, name, module, backward=False):
self.name = name
if backward == False:
self.hook = module.register_forward_hook(self.hook_fn)
else:
self.hook = module.register_backward_hook(self.hook_fn)
def hook_fn(self, module, input, output):
self.input = input
self.output = output
def close(self):
self.hook.remove()