繁体   English   中英

如何修改 PyTorch 中的预训练 Torchvision 模型以返回两个输出用于多标签图像分类

[英]How to modify Pretrained Torchvision Models in PyTorch to return two outputs for multilabel Image Classification

输入:十个“元音”的集合,十个“辅音”的集合,图像数据集,在每个图像中都写有一个元音和一个辅音。

任务:从给定图像中识别元音和辅音。

方法:首先在图像上应用 CNN 隐藏层,然后应用两个并行的全连接/密集层,其中一个将在图像中分类元音,另一个将在图像中分类辅音。

问题:我正在使用预训练的 Model,例如 VGG 或 GoogleNet。 如何修改预训练的 model 以应用两个并行密集层并返回两个输出。

我尝试了两种不同的模型,但我的问题是我们可以为这个任务修改预训练的 model。

现在我的 model 只有一个“fc”层。 我已经修改了最终“fc”层中的神经元数量,就像这样

final_in_features = googlenet.fc.in_features

googlenet.fc = nn.Linear(final_in_features, 10)

但是我需要再添加一个 fc 层,以便两个“fc”层都与隐藏层并行连接。

现在 model 只返回一个 output。

outputs1 = googlenet(inputs)

任务是从两个“fc”层返回两个输出,所以它应该看起来像这样

outputs1, outputs2 = googlenet(inputs)

这是Pytorch中线性层的来源:

class Linear(Module):
    r"""Applies a linear transformation to the incoming data: :math:`y = xA^T + b`

    Args:
        in_features: size of each input sample
        out_features: size of each output sample
        bias: If set to ``False``, the layer will not learn an additive bias.
            Default: ``True``

    Shape:
        - Input: :math:`(N, *, H_{in})` where :math:`*` means any number of
          additional dimensions and :math:`H_{in} = \text{in\_features}`
        - Output: :math:`(N, *, H_{out})` where all but the last dimension
          are the same shape as the input and :math:`H_{out} = \text{out\_features}`.

    Attributes:
        weight: the learnable weights of the module of shape
            :math:`(\text{out\_features}, \text{in\_features})`. The values are
            initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})`, where
            :math:`k = \frac{1}{\text{in\_features}}`
        bias:   the learnable bias of the module of shape :math:`(\text{out\_features})`.
                If :attr:`bias` is ``True``, the values are initialized from
                :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where
                :math:`k = \frac{1}{\text{in\_features}}`

    Examples::

        >>> m = nn.Linear(20, 30)
        >>> input = torch.randn(128, 20)
        >>> output = m(input)
        >>> print(output.size())
        torch.Size([128, 30])
    """
    __constants__ = ['bias']

    def __init__(self, in_features, out_features, bias=True):
        super(Linear, self).__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.weight = Parameter(torch.Tensor(out_features, in_features))
        if bias:
            self.bias = Parameter(torch.Tensor(out_features))
        else:
            self.register_parameter('bias', None)
        self.reset_parameters()

    def reset_parameters(self):
        init.kaiming_uniform_(self.weight, a=math.sqrt(5))
        if self.bias is not None:
            fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight)
            bound = 1 / math.sqrt(fan_in)
            init.uniform_(self.bias, -bound, bound)

    @weak_script_method
    def forward(self, input):
        return F.linear(input, self.weight, self.bias)

    def extra_repr(self):
        return 'in_features={}, out_features={}, bias={}'.format(
            self.in_features, self.out_features, self.bias is not None
        )

您可以像这样创建一个DoubleLinear类:

class DoubleLinear(Module):
    def __init__(self, Linear1, Linear2):
        self.Linear1 = Linear1
        self.Linear2 = Linear2
    @weak_script_method
    def forward(self, input):
        return self.Linear1(input), self.Linear2(input)

然后,创建两个线性层:

Linear_vow = nn.Linear(final_in_features, 10)
Linear_con = nn.Linear(final_in_features, 10)
final_layer = DoubleLinear(Linear_vow, Linear_con)

现在outputs1, outputs2 = final_layer(inputs)将按预期工作。

class DoubleLinear(torch.nn.Module):
    def __init__(self, Linear1, Linear2):
        super(DoubleLinear, self).__init__()
        self.Linear1 = Linear1
        self.Linear2 = Linear2

    def forward(self, input):
        return self.Linear1(input), self.Linear2(input)


in_features = model._fc.in_features

Linear_first = nn.Linear(in_features, 10)
Linear_second = nn.Linear(in_features, 5)

model._fc = DoubleLinear(Linear_first, Linear_second)

我使用 resnet 作为我从 torchvision.models 预训练的torchvision.models
我正在使用nn.Identity()使fc层无效

class MyModel(nn.Module):
    def __init__(self, num_classes1, num_classes2):
        super(MyModel, self).__init__()
        self.model_resnet = models.resnet18(pretrained=True)
        num_ftrs = self.model_resnet.fc.in_features
        self.model_resnet.fc = nn.Identity()
        self.fc1 = nn.Linear(num_ftrs, num_classes1)
        self.fc2 = nn.Linear(num_ftrs, num_classes2)

    def forward(self, x):
        x = self.model_resnet(x)
        out1 = self.fc1(x)
        out2 = self.fc2(x)
        return out1, out2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM