Spaces:
Running
Running
File size: 6,246 Bytes
c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 c608946 c74a070 |
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as tvm
class ResNet18(nn.Module):
def __init__(self, pretrained=False) -> None:
super().__init__()
self.net = tvm.resnet18(pretrained=pretrained)
def forward(self, x):
self = self.net
x1 = x
x = self.conv1(x1)
x = self.bn1(x)
x2 = self.relu(x)
x = self.maxpool(x2)
x4 = self.layer1(x)
x8 = self.layer2(x4)
x16 = self.layer3(x8)
x32 = self.layer4(x16)
return {32: x32, 16: x16, 8: x8, 4: x4, 2: x2, 1: x1}
def train(self, mode=True):
super().train(mode)
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
m.eval()
pass
class ResNet50(nn.Module):
def __init__(
self,
pretrained=False,
high_res=False,
weights=None,
dilation=None,
freeze_bn=True,
anti_aliased=False,
) -> None:
super().__init__()
if dilation is None:
dilation = [False, False, False]
if anti_aliased:
pass
else:
if weights is not None:
self.net = tvm.resnet50(
weights=weights, replace_stride_with_dilation=dilation
)
else:
self.net = tvm.resnet50(
pretrained=pretrained, replace_stride_with_dilation=dilation
)
self.high_res = high_res
self.freeze_bn = freeze_bn
def forward(self, x):
net = self.net
feats = {1: x}
x = net.conv1(x)
x = net.bn1(x)
x = net.relu(x)
feats[2] = x
x = net.maxpool(x)
x = net.layer1(x)
feats[4] = x
x = net.layer2(x)
feats[8] = x
x = net.layer3(x)
feats[16] = x
x = net.layer4(x)
feats[32] = x
return feats
def train(self, mode=True):
super().train(mode)
if self.freeze_bn:
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
m.eval()
pass
class ResNet101(nn.Module):
def __init__(self, pretrained=False, high_res=False, weights=None) -> None:
super().__init__()
if weights is not None:
self.net = tvm.resnet101(weights=weights)
else:
self.net = tvm.resnet101(pretrained=pretrained)
self.high_res = high_res
self.scale_factor = 1 if not high_res else 1.5
def forward(self, x):
net = self.net
feats = {1: x}
sf = self.scale_factor
if self.high_res:
x = F.interpolate(x, scale_factor=sf, align_corners=False, mode="bicubic")
x = net.conv1(x)
x = net.bn1(x)
x = net.relu(x)
feats[2] = (
x
if not self.high_res
else F.interpolate(
x, scale_factor=1 / sf, align_corners=False, mode="bilinear"
)
)
x = net.maxpool(x)
x = net.layer1(x)
feats[4] = (
x
if not self.high_res
else F.interpolate(
x, scale_factor=1 / sf, align_corners=False, mode="bilinear"
)
)
x = net.layer2(x)
feats[8] = (
x
if not self.high_res
else F.interpolate(
x, scale_factor=1 / sf, align_corners=False, mode="bilinear"
)
)
x = net.layer3(x)
feats[16] = (
x
if not self.high_res
else F.interpolate(
x, scale_factor=1 / sf, align_corners=False, mode="bilinear"
)
)
x = net.layer4(x)
feats[32] = (
x
if not self.high_res
else F.interpolate(
x, scale_factor=1 / sf, align_corners=False, mode="bilinear"
)
)
return feats
def train(self, mode=True):
super().train(mode)
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
m.eval()
pass
class WideResNet50(nn.Module):
def __init__(self, pretrained=False, high_res=False, weights=None) -> None:
super().__init__()
if weights is not None:
self.net = tvm.wide_resnet50_2(weights=weights)
else:
self.net = tvm.wide_resnet50_2(pretrained=pretrained)
self.high_res = high_res
self.scale_factor = 1 if not high_res else 1.5
def forward(self, x):
net = self.net
feats = {1: x}
sf = self.scale_factor
if self.high_res:
x = F.interpolate(x, scale_factor=sf, align_corners=False, mode="bicubic")
x = net.conv1(x)
x = net.bn1(x)
x = net.relu(x)
feats[2] = (
x
if not self.high_res
else F.interpolate(
x, scale_factor=1 / sf, align_corners=False, mode="bilinear"
)
)
x = net.maxpool(x)
x = net.layer1(x)
feats[4] = (
x
if not self.high_res
else F.interpolate(
x, scale_factor=1 / sf, align_corners=False, mode="bilinear"
)
)
x = net.layer2(x)
feats[8] = (
x
if not self.high_res
else F.interpolate(
x, scale_factor=1 / sf, align_corners=False, mode="bilinear"
)
)
x = net.layer3(x)
feats[16] = (
x
if not self.high_res
else F.interpolate(
x, scale_factor=1 / sf, align_corners=False, mode="bilinear"
)
)
x = net.layer4(x)
feats[32] = (
x
if not self.high_res
else F.interpolate(
x, scale_factor=1 / sf, align_corners=False, mode="bilinear"
)
)
return feats
def train(self, mode=True):
super().train(mode)
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
m.eval()
pass
|