与二进制分类器实施InceptionV3的问题 - 与Pytorch进行转移学习

发布于 2025-02-01 20:07:42 字数 2148 浏览 5 评论 0 原文

我有一个问题,使Inception v3与Pytorch的二进制分类器一起作为功能提取器工作。我将启动的主要和辅助网更新为二进制类(如

但是我遇到了一个错误

#Parameters for Inception V3
num_classes= 2 
model_ft = models.inception_v3(pretrained=True)
# set_parameter_requires_grad(model_ft, feature_extract)
#handle auxilliary net 
num_ftrs = model_ft.AuxLogits.fc.in_features
model_ft.AuxLogits.fc = nn.Linear(num_ftrs, num_classes)
#handle primary net
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs,num_classes)
# input_size = 299

#simulate data input 
x = torch.rand([64, 3, 299, 299])

#create model with inception backbone
backbone = model_ft
num_filters = backbone.fc.in_features
layers = list(backbone.children())[:-1]
feature_extractor = nn.Sequential(*layers)

# use the pretrained model to classify damage 2 classes
num_target_classes = 2
classifier = nn.Linear(num_filters, num_target_classes)

feature_extractor.eval()
with torch.no_grad():
    representations = feature_extractor(x).flatten(1)
x = classifier(representations)

错误

RuntimeError                              Traceback (most recent call last)
<ipython-input-54-c2be64b8a99e> in <module>()
     11 feature_extractor.eval()
     12 with torch.no_grad():
---> 13     representations = feature_extractor(x)
     14 x = classifier(representations)

9 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
    442                             _pair(0), self.dilation, self.groups)
    443         return F.conv2d(input, weight, bias, self.stride,
--> 444                         self.padding, self.dilation, self.groups)
    445 
    446     def forward(self, input: Tensor) -> Tensor:

RuntimeError: Expected 3D (unbatched) or 4D (batched) input to conv2d, but got input of size: [64, 2]

,但是我会在将类更新为2(当它是1000时)之前遇到 64,1000]。这种创建骨干和添加分类器的方法可用于Resnet,但在这里不适合。我认为这是因为辅助网络结构,但不确定如何更新它来处理双重输出?谢谢

I'm having an issue getting Inception V3 to work as the feature extractor with a binary classifier in Pytorch. I update the primary and auxiliary nets in Inception to have the binary class (as done in https://pytorch.org/tutorials/beginner/finetuning_torchvision_models_tutorial.html)

but I'm getting an error

#Parameters for Inception V3
num_classes= 2 
model_ft = models.inception_v3(pretrained=True)
# set_parameter_requires_grad(model_ft, feature_extract)
#handle auxilliary net 
num_ftrs = model_ft.AuxLogits.fc.in_features
model_ft.AuxLogits.fc = nn.Linear(num_ftrs, num_classes)
#handle primary net
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs,num_classes)
# input_size = 299

#simulate data input 
x = torch.rand([64, 3, 299, 299])

#create model with inception backbone
backbone = model_ft
num_filters = backbone.fc.in_features
layers = list(backbone.children())[:-1]
feature_extractor = nn.Sequential(*layers)

# use the pretrained model to classify damage 2 classes
num_target_classes = 2
classifier = nn.Linear(num_filters, num_target_classes)

feature_extractor.eval()
with torch.no_grad():
    representations = feature_extractor(x).flatten(1)
x = classifier(representations)

But Im getting the error

RuntimeError                              Traceback (most recent call last)
<ipython-input-54-c2be64b8a99e> in <module>()
     11 feature_extractor.eval()
     12 with torch.no_grad():
---> 13     representations = feature_extractor(x)
     14 x = classifier(representations)

9 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
    442                             _pair(0), self.dilation, self.groups)
    443         return F.conv2d(input, weight, bias, self.stride,
--> 444                         self.padding, self.dilation, self.groups)
    445 
    446     def forward(self, input: Tensor) -> Tensor:

RuntimeError: Expected 3D (unbatched) or 4D (batched) input to conv2d, but got input of size: [64, 2]

before I updated the class to 2 (when it was 1000) I was getting the same error but with [64, 1000]. This method of creating a backbone and adding a classifier worked for Resnet but not here. I think it's because of the auxiliary net structure but not sure how to update it to deal with the dual output? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强辩 2025-02-08 20:07:42

继承 feature_extracture by 儿童 line layers = layers = List(Backbone.Children())[: - 1] 将从 Backbone to feature_extressure ,而不是 forward> forward 函数中的操作。

让我们看一下下面的代码:

class Example(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.avg = torch.nn.AdaptiveAvgPool2d((1,1))
        self.linear = torch.nn.Linear(10, 1)
    def forward(self, x):
        out = self.avg(x)
        out = out.squeeze()
        out = self.linear(out)
        return out

x = torch.randn(5, 10, 12, 12)

model = Example()
y = model(x) # work well

new_model = torch.nn.Sequential(*list(model.children()))
y = new_model(x) # error

模块模型 new_model 具有相同的块,但工作方式不相同。在 new_module 中,池中的输出尚未挤压,因此线性输入的形状违反了其假设,从而导致错误。

在您的情况下,最后两个注释是多余的,这就是为什么它返回错误的原因,您确实在line mode_ft.fc = nn.linear(num_ftrs,num_ftrs,num_ftrs, num_classes)。因此,将最后一个替换为下面的代码应正常工作:

with torch.no_grad():
    x = model_ft(x)

Inheriting feature_extracture by children function at line layers = list(backbone.children())[:-1] will bring the module from backbone to feature_extracture only, not the operation in forward function.

Let's take a look at the code below:

class Example(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.avg = torch.nn.AdaptiveAvgPool2d((1,1))
        self.linear = torch.nn.Linear(10, 1)
    def forward(self, x):
        out = self.avg(x)
        out = out.squeeze()
        out = self.linear(out)
        return out

x = torch.randn(5, 10, 12, 12)

model = Example()
y = model(x) # work well

new_model = torch.nn.Sequential(*list(model.children()))
y = new_model(x) # error

Module model and new_model have the same blocks but not the same way of working. In new_module, the output from the pooling layer is not squeezed yet, so the shape of linear input is violate its assumption which causes the error.

In your case, the last two comments are redundant and that's why it returns the error, you did create a new fc in the InceptionV3 module at line model_ft.fc = nn.Linear(num_ftrs,num_classes). Therefore, replace the last one as the code below should work fine:

with torch.no_grad():
    x = model_ft(x)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文