-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
146 lines (116 loc) · 4.25 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
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
import torch
import torch.nn as nn
import torchvision.models as models
from transformers import DistilBertForSequenceClassification, DistilBertModel
import torch.nn.functional as F
from transformers import BertTokenizerFast, DistilBertTokenizerFast
import os
os.environ['CURL_CA_BUNDLE'] = ''
def EfficientNetB4(num_classes, seed=123, pretrained=True):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
model = models.efficientnet_b4(pretrained=pretrained)
model.classifier[1] = nn.Linear(1792, num_classes)
return model
def ViT_B_16(num_classes=10, seed=123, pretrained=True):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
model = models.vit_b_16(pretrained=pretrained)
model.heads.head = nn.Linear(768, num_classes)
return model
def ResNet18(num_classes=10, seed=123, pretrained=True):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
resnet18 = models.resnet18(pretrained=pretrained)
resnet18.fc = nn.Linear(512, num_classes)
return resnet18
def ResNet50(num_classes=10, seed=123, pretrained=True):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
resnet50 = models.resnet50(pretrained=pretrained).cuda()
resnet50.fc = nn.Linear(2048, num_classes).cuda()
return resnet50
def ResNet101(num_classes=10, seed=123, pretrained=True):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
resnet101 = models.resnet101(pretrained=pretrained).cuda()
resnet101.fc = nn.Linear(2048, num_classes).cuda()
return resnet101
def DenseNet121(num_classes=10, seed=123, pretrained=True):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
densenet121 = models.densenet121(pretrained=pretrained).cuda()
densenet121.classifier = nn.Linear(1024, num_classes).cuda()
return densenet121
def VGG11(num_classes=10, seed=123, pretrained=True):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
vgg11 = models.vgg11(pretrained=pretrained).cuda()
vgg11.classifier[-1] = nn.Linear(4096, num_classes).cuda()
return vgg11
class DistilBertClassifier(DistilBertForSequenceClassification):
def __init__(self, config):
super().__init__(config)
def __call__(self, x):
input_ids = x[:, :, 0]
attention_mask = x[:, :, 1]
outputs = super().__call__(
input_ids=input_ids,
attention_mask=attention_mask,
)[0]
return outputs
def initialize_bert_based_model(num_classes):
model = DistilBertClassifier.from_pretrained(
'distilbert-base-uncased',
num_labels=num_classes
)
return model
def initialize_bert_transform(net, max_token_length=512):
# assert 'bert' in config.model
# assert config.max_token_length is not None
tokenizer = getBertTokenizer(net)
def transform(text):
tokens = tokenizer(
text,
padding='max_length',
truncation=True,
max_length=max_token_length,
return_tensors='pt')
if net == 'bert-base-uncased':
x = torch.stack(
(tokens['input_ids'],
tokens['attention_mask'],
tokens['token_type_ids']),
dim=2)
elif net == 'distilbert-base-uncased':
x = torch.stack(
(tokens['input_ids'],
tokens['attention_mask']),
dim=2)
x = torch.squeeze(x, dim=0) # First shape dim is always 1
return x
return transform
def getBertTokenizer(model):
if model == 'bert-base-uncased':
tokenizer = BertTokenizerFast.from_pretrained(model)
elif model == 'distilbert-base-uncased':
tokenizer = DistilBertTokenizerFast.from_pretrained(model)
else:
raise ValueError(f'Model: {model} not recognized.')
return tokenizer
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(3, 20, 5, 1)
self.conv2 = nn.Conv2d(20, 50, 5, 1)
self.fc1 = nn.Linear(4 * 4 * 50, 500)
self.fc2 = nn.Linear(500, 2)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2, 2)
x = x.view(-1, 4 * 4 * 50)
x = F.relu(self.fc1(x))
logits = self.fc2(x)
return logits