Hello
i defined a method inside a class but i don’t know why i’m getting the following error
AttributeError: ‘ResNet’ object has no attribute ‘get_features’
this is my class:
class ResNet(nn.Module):
def __init__(self, num_classes=1000):
super(ResNet, self).__init__()
resnet = resnet50(pretrained=True)
self.backbone = nn.Sequential(
resnet.conv1,
resnet.bn1,
resnet.relu,
resnet.maxpool,
resnet.layer1, # res_conv2
resnet.layer2, # res_conv3
resnet.layer3, # res_conv4
)
self.res_part = nn.Sequential(
Bottleneck(1024, 512, stride=1, downsample=nn.Sequential(
nn.Conv2d(1024, 2048, kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(2048),
)),
Bottleneck(2048, 512),
Bottleneck(2048, 512),
)
self.res_part.load_state_dict(resnet.layer4.state_dict())
reduction = nn.Sequential(
nn.Conv2d(2048, 512, 1),
nn.BatchNorm2d(512),
nn.ReLU()
)
self.global_avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.global_softmax = nn.Linear(512, num_classes)
self.global_softmax.apply(weights_init_kaiming)
self.global_reduction = copy.deepcopy(reduction)
self.global_reduction.apply(weights_init_kaiming)
def forward(self, x):
"""
:param x: input image tensor of (N, C, H, W)
:return: (prediction, triplet_losses, softmax_losses)
"""
x = self.backbone(x)
x = self.res_part(x)
#global branch
glob = self.global_avgpool(x)
global_triplet_feature = self.global_reduction(glob).squeeze()
global_softmax_class = self.global_softmax(global_triplet_feature)
return global_softmax_class
def get_features(self):
return nn.Sequential(
self.backbone,
self.res_part,
)