There is an issue when loading TorchScript model in Qt platform

KouWeiBin: <p>I have trained a model using python and the network was defined as follow:</p>
<pre><code class="lang-auto">class ResidualFeatureNet(torch.nn.Module):                                              
    def __init__(self): 
        super(ResidualFeatureNet, self).__init__()
        # Initial convolution layers                                                    
        self.conv1 = ConvLayer(1, 32, kernel_size=5, stride=2)                          
        self.conv2 = ConvLayer(32,64, kernel_size=3, stride=2)                          
        self.conv3 = ConvLayer(64,128, kernel_size=3, stride=1)                         
        self.resid1= ResidualBlock(128)                                                 
        self.resid2= ResidualBlock(128)                                                 
        self.resid3= ResidualBlock(128)                                                 
        self.resid4= ResidualBlock(128)                                                 
        self.conv4 = ConvLayer(128, 64, kernel_size=3, stride=1)                        
        self.conv5 = ConvLayer(64, 1, kernel_size=1, stride=1)                          
                
    def forward(self, X):
        conv1 = F.relu(self.conv1(X)) 
        conv2 = F.relu(self.conv2(conv1))                                               
        conv3 = F.relu(self.conv3(conv2))                                               
        resid1= self.resid1(conv3)                                                      
        resid2= self.resid2(resid1)                                                     
        resid3= self.resid3(resid2)
        resid4= self.resid4(resid3)
        conv4 = F.relu(self.conv4(resid4))                                              
        conv5 = F.relu(self.conv5(conv4))                                               
        return conv5

</code></pre>
<p>After that I convert the model into TorchScript model using following script:</p>
<pre><code class="lang-auto">import os
import argparse

import numpy as np
import pandas as pd

import torch
import torch.nn as nn
import torchvision
from torchvision import transforms
from netdef_128 import ResidualFeatureNet as RFN

np.random.seed(0)
torch.manual_seed(0)
print(torch.__version__)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Pytorch model scripted')
    parser.add_argument('--input', help='input model file')
    parser.add_argument('--output', help='output model to save')
    parser.add_argument('--imgsize', help='image size to predict')
    parser.add_argument('--chan', type=int, help='the channel of image to predict')

    args = parser.parse_args()
    device = torch.device('cuda:0')
    model = RFN()
    #model = UNet(n_channels=3, n_classes=2)
    model = model.to(device)
    #model.load_state_dict(torch.load(args.input, map_location=str(device)))
    state_dict = torch.load(args.input, map_location=str(device))
    for k in list(state_dict.keys()):
        if (k.find('running_mean')&gt;0) or (k.find('running_var')&gt;0):
            del state_dict[k]
    model.load_state_dict(state_dict)
    model.eval()

    width, height = args.imgsize.split(',')
    example = torch.rand(1, args.chan, int(width), int(height)).to('cuda:0')
    traced_script_module = torch.jit.trace(model, example)
    out = traced_script_module(example)
    traced_script_module.save(args.output)

</code></pre>
<p>But there is an issue when loading the TorchScript model, and the logs is listed as follow. so can anyone help me to solve this issue?  Thanks</p>
<p>what():  forward() Expected a value of type ‘Tensor’ for argument ‘input’ but instead found type ‘List[Tensor]’.<br>
Position: 1<br>
Declaration: forward(<strong>torch</strong>.netdef_128.ResidualFeatureNet self, Tensor input) → (Tensor)<br>
Exception raised from checkArg at /pytorch/aten/src/ATen/core/function_schema_inl.h:162 (most recent call first):<br>
frame <span class="hashtag">#0:</span> c10::Error::Error(c10::SourceLocation, std::string) + 0x42 (0x7f383c7252f2 in /usr/local/libtorch1.8/lib/libc10.so)<br>
frame <span class="hashtag">#1:</span> c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::string const&amp;) + 0x5b (0x7f383c72267b in /usr/local/libtorch1.8/lib/libc10.so)<br>
frame <span class="hashtag">#2:</span>  + 0xd9668f (0x7f382b32568f in /usr/local/libtorch1.8/lib/libtorch_cpu.so)<br>
frame <span class="hashtag">#3:</span> torch::jit::GraphFunction::operator()(std::vector&lt;c10::IValue, std::allocator<a>c10::IValue</a> &gt;, std::unordered_map&lt;std::string, c10::IValue, std::hash<a>std::string</a>, std::equal_to<a>std::string</a>, std::allocator&lt;std::pair&lt;std::string const, c10::IValue&gt; &gt; &gt; const&amp;) + 0x2d (0x7f382d7cb88d in /usr/local/libtorch1.8/lib/libtorch_cpu.so)<br>
frame <span class="hashtag">#4:</span> torch::jit::Method::operator()(std::vector&lt;c10::IValue, std::allocator<a>c10::IValue</a> &gt;, std::unordered_map&lt;std::string, c10::IValue, std::hash<a>std::string</a>, std::equal_to<a>std::string</a>, std::allocator&lt;std::pair&lt;std::string const, c10::IValue&gt; &gt; &gt; const&amp;) + 0x138 (0x7f382d7d91e8 in /usr/local/libtorch1.8/lib/libtorch_cpu.so)<br>
frame <span class="hashtag">#5:</span>  + 0x27fa4 (0x559b66363fa4 in /home/kevin/palmGPU/Release/Matching)<br>
frame <span class="hashtag">#6:</span>  + 0x1c17b (0x559b6635817b in /home/kevin/palmGPU/Release/Matching)<br>
frame <span class="hashtag">#7:</span>  + 0x20d5f (0x559b6635cd5f in /home/kevin/palmGPU/Release/Matching)<br>
frame <span class="hashtag">#8:</span>  + 0xa189d (0x7f382941989d in /home/kevin/anaconda3/lib/libQt5Core.so.5)<br>
frame <span class="hashtag">#9:</span>  + 0x9609 (0x7f3828e8d609 in /lib/x86_64-linux-gnu/libpthread.so.0)<br>
frame <span class="hashtag">#10:</span> clone + 0x43 (0x7f3828fcf293 in /lib/x86_64-linux-gnu/libc.so.6)</p>

ptrblck: <aside class="quote no-group" data-username="KouWeiBin" data-post="1" data-topic="130812">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kouweibin/48/39771_2.png" class="avatar"> KouWeiBin:</div>
<blockquote>
<p>what(): forward() Expected a value of type ‘Tensor’ for argument ‘input’ but instead found type ‘List[Tensor]’.</p>
</blockquote>
</aside>
<p>Based on this error message it seems you are passing a <code>list</code> of tensors to the model while a tensor is expected so you would have to check the inputs to the model.</p>

KouWeiBin: <p>Thanks for your valuable information!</p>