-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
156 lines (142 loc) · 6.58 KB
/
models.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import gin
import torch
import torch.nn as nn
import torch.nn.functional as F
import random
class LanguageModel(nn.Module):
def __init__(self, vocab_size = 193, embed_size = 256, inp_size = 1024, hidden_size = 512,
num_layers = 1, dropout_p = 0.1):
super(LanguageModel, self).__init__()
self.hidden_size = hidden_size
self.embed_size = embed_size
self.dropout = nn.Dropout(dropout_p)
self.embedding = nn.Embedding(vocab_size, embed_size, padding_idx=2)
self.project = nn.Linear(inp_size, hidden_size)
self.gru = nn.GRU(input_size=embed_size, hidden_size=hidden_size,
num_layers=num_layers, batch_first=True)
# self.lstm = nn.LSTM(hidden_size, hidden_size, num_layers, batch_first=True)
self.linear = nn.Linear(hidden_size, vocab_size)
self.max_length = 15
self.teacher_forcing_ratio = 0.5
self.init_weights()
def init_weights(self):
self.embedding.weight.data.uniform_(-0.1, 0.1)
self.linear.weight.data.uniform_(-0.1, 0.1)
self.linear.bias.data.fill_(0)
def forward(self, img_feats, text):
text = text.squeeze()
preds = []
self.gru.flatten_parameters()
decoder_input = text[:, 0]
state = self.project(img_feats).unsqueeze(0)
for i in range(1, self.max_length):
use_teacher_forcing = True if (self.training and random.random() < self.teacher_forcing_ratio) else False
embeddings = self.dropout(self.embedding(decoder_input)).squeeze()
feats, state = self.gru(embeddings.unsqueeze(1), state)
pred = self.linear(state).squeeze()
if use_teacher_forcing:
decoder_input = text[:,i]
else:
output = F.log_softmax(pred, dim=-1)
decoder_input = torch.argmax(output, dim=-1)
preds.append(pred.unsqueeze(1))
return torch.cat(preds, 1)
@gin.configurable
class AutoEncoder(nn.Module):
def __init__(self, model_type, model = None):
super(AutoEncoder, self).__init__()
self.model_type = model_type
if model_type == 'self':
self.conv = nn.Sequential(nn.Conv2d(3, 32, (4, 4), 2, 1), # 224 -> 112
nn.ReLU(),
nn.Conv2d(32, 64, (4, 4), 2,1), # 112 -> 56
nn.ReLU(),
nn.Conv2d(64, 128, (4, 4), 2, 1), # 56 -> 28
nn.ReLU(),
nn.Conv2d(128, 128, (4, 4), 2, 1), # 28 -> 14
nn.ReLU(),
nn.Conv2d(128, 64, (4, 4), 2, 1), # 14 -> 7
nn.ReLU(),
nn.Conv2d(64, 5, (7, 7), 1, 0))
else:
self.conv = torch.nn.Sequential(*list(model.children())[:-1])
self.lin = nn.Linear(2048, 256)
self.deconv = nn.Sequential(
nn.ConvTranspose2d(256, 256, 7, 1, 0),
nn.BatchNorm2d(256),
nn.ReLU(),
# state size: (ngf * 8) x 4 x 4
nn.ConvTranspose2d(256, 256, 4, 2, 1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(),
# state size: ngf x 32 x 32
nn.ConvTranspose2d(256, 128, 4, 2, 1, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(),
# state size: (ngf * 4) x 8 x 8
nn.ConvTranspose2d(128, 64, 4, 2, 1, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(),
# state size: (ngf * 2) x 16 x 16
nn.ConvTranspose2d(64, 32, 4, 2, 1, bias=False),
nn.BatchNorm2d(32),
nn.ReLU(),
# state size: ngf x 32 x 32
nn.ConvTranspose2d(32, 3, 4, 2, 1),
nn.Tanh()
)
def forward(self, x):
out = self.conv(x).squeeze()
if self.model_type != 'self':
out = self.lin(F.relu(out))
out = self.deconv(out.view(-1, out.size(1), 1, 1))
return out
@gin.configurable
class AbnormalNet(nn.Module):
def __init__(self, model_type, model = None):
super(AbnormalNet, self).__init__()
self.model_type = model_type
if model_type == 'self':
self.conv = nn.Sequential(nn.Conv2d(3, 32, (4, 4), 2, 1), # 224 -> 112
nn.ReLU(),
nn.Conv2d(32, 64, (4, 4), 2,1), # 112 -> 56
nn.ReLU(),
nn.Conv2d(64, 128, (4, 4), 2, 1), # 56 -> 28
nn.ReLU(),
nn.Conv2d(128, 128, (4, 4), 2, 1), # 28 -> 14
nn.ReLU(),
nn.Conv2d(128, 64, (4, 4), 2, 1), # 14 -> 7
nn.ReLU(),
nn.Conv2d(64, 5, (7, 7), 1, 0))
else:
self.conv = torch.nn.Sequential(*list(model.children())[:-1])
self.lin = nn.Linear(2048, 5)
def forward(self, x):
out = self.conv(x).squeeze()
if self.model_type != 'self':
out = self.lin(F.relu(out))
return out
@gin.configurable
class MultiTaskModel(nn.Module):
def __init__(self, model, vocab_size, model_type = 'densenet121', in_feats = gin.REQUIRED):
super(MultiTaskModel, self).__init__()
self.model_type = model_type
if self.model_type == 'densenet121':
self.feature_extract = model.features
else:
self.feature_extract = torch.nn.Sequential(*list(model.children())[:-1])
self.disease_classifier = nn.Sequential(nn.Linear(in_feats, 512),
nn.ReLU(), nn.Linear(512, 5))
self.fine_disease_classifier = nn.Sequential(nn.Linear(in_feats, 512),
nn.ReLU(), nn.Linear(512, 321))
self.language_classifier = LanguageModel(inp_size = in_feats, vocab_size = vocab_size)
def forward(self, data,text):
features = self.feature_extract(data).squeeze()
out = F.relu(features)
if self.model_type == 'densenet121':
out = F.adaptive_avg_pool2d(out, (1, 1))
out = torch.flatten(out, 1)
return (self.disease_classifier(out),
self.fine_disease_classifier(out),
self.language_classifier(out, text)
)