File size: 1,914 Bytes
51ba5d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f8ef33
51ba5d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f8ef33
51ba5d6
 
 
 
 
0f2d9f6
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
import sys
import torch.nn as nn
import os.path as osp
from torchvision import models
import torch.nn.functional as F
from registry import MODEL_REGISTRY

root_path = osp.abspath(osp.join(__file__, osp.pardir, osp.pardir))
sys.path.append(root_path)

# ============================= ResNets =============================


@MODEL_REGISTRY.register()
class ResNet18(nn.Module):
    def __init__(self, model_args):
        super(ResNet18, self).__init__()
        self.num_classes = model_args.get("num_classes", 1)
        self.resnet = models.resnet18(weights=None)
        self.regression_head = nn.Linear(1000, self.num_classes)

    def forward(self, x, masks=None):
        # Calculate the padding dynamically based on the input size
        height, width = x.shape[2], x.shape[3]
        pad_height = max(0, (224 - height) // 2)
        pad_width = max(0, (224 - width) // 2)

        # Apply padding
        x = F.pad(x, (pad_width, pad_width, pad_height, pad_height), mode="constant", value=0)
        x = self.resnet(x)
        x = self.regression_head(x)
        return x


@MODEL_REGISTRY.register()
class ResNet50(nn.Module):
    def __init__(self, model_args):
        super(ResNet50, self).__init__()
        self.num_classes = model_args.get("num_classes", 1)
        self.resnet = models.resnet50(weights=None)
        self.regression_head = nn.Linear(1000, self.num_classes)

    def forward(self, x, masks=None):
        # Calculate the padding dynamically based on the input size
        height, width = x.shape[2], x.shape[3]
        pad_height = max(0, (224 - height) // 2)
        pad_width = max(0, (224 - width) // 2)

        # Apply padding
        x = F.pad(x, (pad_width, pad_width, pad_height, pad_height), mode="constant", value=0)
        x = self.resnet(x)
        x = self.regression_head(x)
        return x


# print("Registered models in MODEL_REGISTRY:", MODEL_REGISTRY.keys())