CoolFace
Apppublic

hololens/stable-diffusion-webui-depthmap-script

sourceHugging Faceupdated 2y agoView on Hugging Face
1likes
Resnet.py200 linesDownload Raw Back to lib
1import torch.nn as nn
2import torch.nn as NN
3
4__all__ = ['ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101',
5           'resnet152']
6
7
8model_urls = {
9    'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
10    'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
11    'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
12    'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
13    'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
14}
15
16
17def conv3x3(in_planes, out_planes, stride=1):
18    """3x3 convolution with padding"""
19    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
20                     padding=1, bias=False)
21
22
23class BasicBlock(nn.Module):
24    expansion = 1
25
26    def __init__(self, inplanes, planes, stride=1, downsample=None):
27        super(BasicBlock, self).__init__()
28        self.conv1 = conv3x3(inplanes, planes, stride)
29        self.bn1 = NN.BatchNorm2d(planes) #NN.BatchNorm2d
30        self.relu = nn.ReLU(inplace=True)
31        self.conv2 = conv3x3(planes, planes)
32        self.bn2 = NN.BatchNorm2d(planes) #NN.BatchNorm2d
33        self.downsample = downsample
34        self.stride = stride
35
36    def forward(self, x):
37        residual = x
38
39        out = self.conv1(x)
40        out = self.bn1(out)
41        out = self.relu(out)
42
43        out = self.conv2(out)
44        out = self.bn2(out)
45
46        if self.downsample is not None:
47            residual = self.downsample(x)
48
49        out += residual
50        out = self.relu(out)
51
52        return out
53
54
55class Bottleneck(nn.Module):
56    expansion = 4
57
58    def __init__(self, inplanes, planes, stride=1, downsample=None):
59        super(Bottleneck, self).__init__()
60        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
61        self.bn1 = NN.BatchNorm2d(planes) #NN.BatchNorm2d
62        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
63                               padding=1, bias=False)
64        self.bn2 = NN.BatchNorm2d(planes) #NN.BatchNorm2d
65        self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1, bias=False)
66        self.bn3 = NN.BatchNorm2d(planes * self.expansion) #NN.BatchNorm2d
67        self.relu = nn.ReLU(inplace=True)
68        self.downsample = downsample
69        self.stride = stride
70
71    def forward(self, x):
72        residual = x
73
74        out = self.conv1(x)
75        out = self.bn1(out)
76        out = self.relu(out)
77
78        out = self.conv2(out)
79        out = self.bn2(out)
80        out = self.relu(out)
81
82        out = self.conv3(out)
83        out = self.bn3(out)
84
85        if self.downsample is not None:
86            residual = self.downsample(x)
87
88        out += residual
89        out = self.relu(out)
90
91        return out
92
93
94class ResNet(nn.Module):
95
96    def __init__(self, block, layers, num_classes=1000):
97        self.inplanes = 64
98        super(ResNet, self).__init__()
99        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
100                               bias=False)
101        self.bn1 = NN.BatchNorm2d(64)  #NN.BatchNorm2d
102        self.relu = nn.ReLU(inplace=True)
103        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
104        self.layer1 = self._make_layer(block, 64, layers[0])
105        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
106        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
107        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
108        #self.avgpool = nn.AvgPool2d(7, stride=1)
109        #self.fc = nn.Linear(512 * block.expansion, num_classes)
110
111        for m in self.modules():
112            if isinstance(m, nn.Conv2d):
113                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
114            elif isinstance(m, nn.BatchNorm2d):
115                nn.init.constant_(m.weight, 1)
116                nn.init.constant_(m.bias, 0)
117
118    def _make_layer(self, block, planes, blocks, stride=1):
119        downsample = None
120        if stride != 1 or self.inplanes != planes * block.expansion:
121            downsample = nn.Sequential(
122                nn.Conv2d(self.inplanes, planes * block.expansion,
123                          kernel_size=1, stride=stride, bias=False),
124                NN.BatchNorm2d(planes * block.expansion), #NN.BatchNorm2d
125            )
126
127        layers = []
128        layers.append(block(self.inplanes, planes, stride, downsample))
129        self.inplanes = planes * block.expansion
130        for i in range(1, blocks):
131            layers.append(block(self.inplanes, planes))
132
133        return nn.Sequential(*layers)
134
135    def forward(self, x):
136        features = []
137
138        x = self.conv1(x)
139        x = self.bn1(x)
140        x = self.relu(x)
141        x = self.maxpool(x)
142
143        x = self.layer1(x)
144        features.append(x)
145        x = self.layer2(x)
146        features.append(x)
147        x = self.layer3(x)
148        features.append(x)
149        x = self.layer4(x)
150        features.append(x)
151
152        return features
153
154
155def resnet18(pretrained=True, **kwargs):
156    """Constructs a ResNet-18 model.
157    Args:
158        pretrained (bool): If True, returns a model pre-trained on ImageNet
159    """
160    model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
161    return model
162
163
164def resnet34(pretrained=True, **kwargs):
165    """Constructs a ResNet-34 model.
166    Args:
167        pretrained (bool): If True, returns a model pre-trained on ImageNet
168    """
169    model = ResNet(BasicBlock, [3, 4, 6, 3], **kwargs)
170    return model
171
172
173def resnet50(pretrained=True, **kwargs):
174    """Constructs a ResNet-50 model.
175    Args:
176        pretrained (bool): If True, returns a model pre-trained on ImageNet
177    """
178    model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
179
180    return model
181
182
183def resnet101(pretrained=True, **kwargs):
184    """Constructs a ResNet-101 model.
185    Args:
186        pretrained (bool): If True, returns a model pre-trained on ImageNet
187    """
188    model = ResNet(Bottleneck, [3, 4, 23, 3], **kwargs)
189
190    return model
191
192
193def resnet152(pretrained=True, **kwargs):
194    """Constructs a ResNet-152 model.
195    Args:
196        pretrained (bool): If True, returns a model pre-trained on ImageNet
197    """
198    model = ResNet(Bottleneck, [3, 8, 36, 3], **kwargs)
199    return model
200