-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmodels_resnet.py
61 lines (47 loc) · 2.15 KB
/
models_resnet.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import torch
import torch.nn as nn
from torchvision.models import resnet
def replace_first_conv(model, in_c):
old_conv = model.conv1
model.conv1 = nn.Conv2d(in_c, old_conv.out_channels, kernel_size=old_conv.kernel_size,
stride=old_conv.stride, padding=old_conv.padding, bias=False)
model.conv1.weight.data[:, :3, :, :] = old_conv.weight.data
return model
@torch.jit.ignore
def no_weight_decay(self):
return {}
def resnet18(in_c, num_classes=62, pretrained=True, **kwargs):
model = resnet.resnet18(pretrained=pretrained, **kwargs)
model = replace_first_conv(model, in_c)
in_features = model.fc.in_features
model.fc = nn.Linear(in_features, num_classes, bias=True)
model.no_weight_decay = no_weight_decay.__get__(model)
return model
def resnet34(in_c, num_classes=62, pretrained=True, **kwargs):
model = resnet.resnet34(pretrained=pretrained, **kwargs)
model = replace_first_conv(model, in_c)
in_features = model.fc.in_features
model.fc = nn.Linear(in_features, num_classes, bias=True)
model.no_weight_decay = no_weight_decay.__get__(model)
return model
def resnet50(in_c, num_classes=62, pretrained=True, **kwargs):
model = resnet.resnet50(pretrained=pretrained, **kwargs)
model = replace_first_conv(model, in_c)
in_features = model.fc.in_features
model.fc = nn.Linear(in_features, num_classes, bias=True)
model.no_weight_decay = no_weight_decay.__get__(model)
return model
def resnet101(in_c, num_classes=62, pretrained=True, **kwargs):
model = resnet.resnet101(pretrained=pretrained, **kwargs)
model = replace_first_conv(model, in_c)
in_features = model.fc.in_features
model.fc = nn.Linear(in_features, num_classes, bias=True)
model.no_weight_decay = no_weight_decay.__get__(model)
return model
def resnet152(in_c, num_classes=62, pretrained=True, **kwargs):
model = resnet.resnet152(pretrained=pretrained, **kwargs)
model = replace_first_conv(model, in_c)
in_features = model.fc.in_features
model.fc = nn.Linear(in_features, num_classes, bias=True)
model.no_weight_decay = no_weight_decay.__get__(model)
return model