This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathResNetFeat.py
49 lines (34 loc) · 1.91 KB
/
ResNetFeat.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
# Copyright 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import ResNetBasic
import torch
import torch.nn as nn
from torch.autograd import Variable
# Modification of ResNet so that it also outputs a feature vector
class ResNetFeat(ResNetBasic.ResNet):
def __init__(self, block, list_of_num_layers, list_of_out_dims, num_classes=1000, only_trunk=False, classifier_has_bias=True):
super(ResNetFeat, self).__init__(block, list_of_num_layers, list_of_out_dims, num_classes, only_trunk)
if not classifier_has_bias:
self.classifier = nn.Linear(self.final_feat_dim, num_classes, bias=classifier_has_bias)
def forward(self, x):
out = self.trunk(x)
if self.only_trunk:
return out
out = out.view(out.size(0),-1)
scores = self.classifier(out)
return scores, out
def get_classifier_weight(self):
return self.classifier.weight
def ResNet10(num_classes=1000, only_trunk=False):
return ResNetFeat(ResNetBasic.SimpleBlock, [1,1,1,1],[64,128,256,512], num_classes, only_trunk, classifier_has_bias=False)
def ResNet18(num_classes=1000, only_trunk=False):
return ResNetFeat(ResNetBasic.SimpleBlock, [2,2,2,2],[64,128,256,512],num_classes, only_trunk, classifier_has_bias=False)
def ResNet34(num_classes=1000, only_trunk=False):
return ResNetFeat(ResNetBasic.SimpleBlock, [3,4,6,3],[64,128,256,512],num_classes, only_trunk, classifier_has_bias=False)
def ResNet50(num_classes=1000, only_trunk=False):
return ResNetFeat(ResNetBasic.BottleneckBlock, [3,4,6,3], [256,512,1024,2048], num_classes, only_trunk, classifier_has_bias=False)
def ResNet101(num_classes=1000, only_trunk=False):
return ResNetFeat(ResNetBasic.BottleneckBlock, [3,4,23,3],[256,512,1024,2048], num_classes, only_trunk, classifier_has_bias=False)