File size: 2,054 Bytes
ce91ea1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torchvision
import torch.nn as nn
import pretrainedmodels
import torch.nn.functional as F
from collections import OrderedDict
from constant import SCALE_FACTOR
import math

class Resnet(nn.Module):
    
    def __init__(self, variant):
        super(Resnet, self).__init__()
        assert variant in ['senet154', 'se_resnext101_32x4d', 'se_resnext50_32x4d', 'se_resnet152', 'se_resnet101', 'se_resnet50']
        
        # load retrain model
        model = pretrainedmodels.__dict__[variant](num_classes=1000, pretrained='imagenet')
        self.features = nn.Sequential(OrderedDict([
            ('layer0', model.layer0),
            ('layer1', model.layer1),
            ('layer2', model.layer2),
            ('layer3', model.layer3),
            ('layer4', model.layer4)
        ]))
        '''

        Dropout

        - For SENet154: 0.2

        - For SE-ResNet models: None

        - For SE-ResNeXt models: None

        '''
        self.dropout = model.dropout
        num_ftrs = model.last_linear.in_features
        self.classifier = nn.Sequential(
            nn.Linear(num_ftrs, 14),
            nn.Sigmoid()
        )
        
        # load other info
        # load other info
        self.mean = model.mean
        self.std = model.std
        self.input_size = model.input_size[1] # assume every input is a square image
        self.input_range = model.input_range
        self.input_space = model.input_space
        self.resize_size = int(math.floor(self.input_size / SCALE_FACTOR))
        
    def forward(self, x):
        x = self.features(x) # 1x2048x7x7
        s = x.size()[3] # 7 if input image is 224x224, 16 if input image is 512x512
        x = F.avg_pool2d(x, kernel_size=s, stride=1) # 1x2048x1x1
        x = x.view(x.size(0), -1) # 1x2048
        x = self.classifier(x) # 1x1000
        return x
        
    def extract(self, x):
        return self.features(x)
    
def build(variant):
    net = Resnet(variant).cuda()
    return net

architect='senet'