forked from pranavmkoundinya/VIPCUP2023_OLIVES
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmodel.py
43 lines (36 loc) · 1.95 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
import torchvision.models as models
import torchvision
class ResNet(nn.Module):
"""encoder + classifier"""
def __init__(self, name='resnet50', num_classes=2):
super(ResNet, self).__init__()
if (name == 'resnet50'):
self.encoder = torchvision.models.resnet50(zero_init_residual=True)
self.encoder.conv1 = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
self.encoder.fc = nn.Identity()
self.fc = nn.Linear(2048, num_classes)
elif(name == 'resnet34'):##dd
self.encoder = torchvision.models.resnet34(zero_init_residual=True)
self.encoder.conv1 = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
self.encoder.fc = nn.Identity()
self.fc = nn.Linear(512, num_classes)
elif(name == 'resnet101'):##dd
self.encoder = torchvision.models.resnet101(zero_init_residual=True)
self.encoder.conv1 = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
self.encoder.fc = nn.Identity()
self.fc = nn.Linear(2048, num_classes)
elif(name == 'resnet152'):##dd
self.encoder = torchvision.models.resnet152(zero_init_residual=True)
self.encoder.conv1 = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
self.encoder.fc = nn.Identity()
self.fc = nn.Linear(2048, num_classes)
else:
self.encoder = torchvision.models.resnet18(zero_init_residual=True)
self.encoder.conv1 = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
self.encoder.fc = nn.Identity()
self.fc = nn.Linear(512, num_classes)
def forward(self, x):
return self.fc(self.encoder(x))