Hi Everyone,
I’m trying to convert multilabel classification model (pytorch 1.0) to torch script via annotation.
Here is my model:
class MultiLabelModel(nn.Module):
    def __init__(self, basemodel, basemodel_output, num_classes):
        super(MultiLabelModel, self).__init__()
        self.basemodel = basemodel
        self.num_classes = num_classes
        for index, num_class in enumerate(num_classes):
            setattr(self, "FullyConnectedLayer_" + str(index), nn.Linear(basemodel_output, num_class))
    
    def forward(self, x):
        x = self.basemodel.forward(x)
        outs = list()
        dir(self)
        for index, num_class in enumerate(self.num_classes):
            fun = eval("self.FullyConnectedLayer_" + str(index))
            out = fun(x)
            outs.append(out)
        return outs

The basemodel is predefined backbone model.
I tried this:
class MultiLabelModel(torch.jit.ScriptModule):
    __constants__ = ['num_classes']
    __constants__ = ['basemodel']
    __constants__ = ['basemodel_output']
    def __init__(self, basemodel, basemodel_output, num_classes):
        super(MultiLabelModel, self).__init__()
        self.basemodel = basemodel
        self.num_classes = num_classes
        self.basemodel_output = basemodel_output
        for index, num_class in enumerate(num_classes):
            setattr(self, "FullyConnectedLayer_" + str(index), nn.Linear(basemodel_output, num_class))
    @torch.jit.script_method
    def forward(self, x):
        x = self.basemodel.forward(x)
        outs = list()
        for index, num_class in enumerate(self.num_classes):
            fun = eval("self.FullyConnectedLayer_" + str(index))
            out = fun(x)
            outs.append(out)
        return outs

and get error:

expected a value of type Tensor for argument ‘0’ but found int

seems like I can’t define the multilabel classifier like this:
        for index, num_class in enumerate(num_classes):
            setattr(self, "FullyConnectedLayer_" + str(index), nn.Linear(basemodel_output, num_class))

Hope somebody can help me out.