More outputs from a torchscript model

jingyu_han: <p>I  converted a torch <a href="https://drive.google.com/file/d/1kZ0H_dWjjv07TvmgbENdhPnkuKQEXt9g/view?usp=sharing" rel="noopener nofollow ugc">model</a> into a torchscript. Then I was planning to get the outputs of my torchscript model.</p>
<pre><code class="lang-auto">#include &lt;iostream&gt;
#include &lt;torch/script.h&gt;
#include &lt;torch/torch.h&gt;
#include &lt;opencv2/opencv.hpp&gt;

using namespace std;
using namespace cv;
int main(int argc,char**argv)
{
    torch::jit::script::Module decoder = torch::jit::load("/opt/depth/model.pt");
    std::cout &lt;&lt; "monodepth loaded!\n";
    decoder.to(at::kCUDA);
    cv::Mat src=cv::imread("/opt/depth/test_image.jpg");
    cv::Mat input_mat;
    int w=640;
    int h=192;
    while (1) {
        
        cv::resize(src,input_mat,cv::Size(w,h));
        input_mat.convertTo(input_mat,CV_32FC3,1./255.);
        torch::Tensor tensor_image = torch::from_blob(input_mat.data, {1,input_mat.rows, input_mat.cols,3}, torch::kF32);
        tensor_image = tensor_image.permute({0,3,1,2});
        tensor_image = tensor_image.to(at::kCUDA);

        std::vector&lt;torch::IValue&gt; batch;
        batch.push_back(tensor_image);
       //this model actually return four ouputs, I only need to use one of them,
        torch::IValue result_decoder=decoder.forward(batch);

        torch::Tensor tensor_result = result_decoder[3].toTensor().to(at::kCPU);
        
        tensor_result = tensor_result.permute({0,3,2,1});
       
        cv::Mat disp=cv::Mat(h,w,CV_32FC1,tensor_result.data_ptr());
        cv::resize(disp,disp,cv::Size(src.cols,src.rows));
        disp*=512;

        disp.convertTo(disp,CV_8UC1);
        cv::cvtColor(disp,disp,cv::COLOR_BGR2GRAY);
        src.push_back(disp);
        // vector&lt;cv::Mat&gt; channels={disp,disp,disp};
        // cv::merge(channels,disp);
        cv::resize(src,src,cv::Size(),0.5,0.5);
        // cv::imshow("result",disp);
        cv::imwrite("src.jpg",src);
        if(cv::waitKey(1)==27)
            break;
    }

    cout&lt;&lt;"Done"&lt;&lt;endl;
    return 0;
}
</code></pre>
<pre><code class="lang-auto">
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

PROJECT ("DEPTH_EXAMPLE")

set(CMAKE_BUILD_TYPE Debug)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

SET(CMAKE_CXX_STANDARD 11)

SET(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_PREFIX_PATH ${CMAKE_CURRENT_SOURCE_DIR}/libtorch)

find_package(Torch REQUIRED)

find_package(CUDA)

find_package(OpenCV REQUIRED)

add_executable(monodepth main.cpp)
target_link_libraries(monodepth ${OpenCV_LIBS} ${TORCH_LIBRARIES})
</code></pre>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/1/91fd7b8f41bbc17039b4b73f3060f9f09c40fafe.jpeg" alt="test_image" data-base62-sha1="kPujFH0ZwJzzoniutKtxYapUAi2" width="638" height="235"></p>
<p>Just as the middle part of this code said, my model returns four outputs, but I don’t know how to set the types of return values. Originally, I used <code>result_decoder[3]</code> to represent the output that I want to use. However, I got an error as below,<br>
<code>no match for ‘operator[]’ (operand types are ‘c10::IValue’ and ‘int’)</code></p>
<p>Next, I removed <span class="chcklst-box fa fa-square-o fa-fw"></span> and recompile my project.<br>
<code> isTensor() INTERNAL ASSERT FAILED at /opt/depth/libtorch/include/ATen/core/ivalue_inl.h:90, please report a bug to PyTorch. Expected Tensor but got Tuple (toTensor at /opt/depth/libtorch/include/ATen/core/ivalue_inl.h:90)</code></p>
<p>this error means that this operation <code> result_decoder.toTensor().</code> couldn’t be executed because result_decoder is not a single tensor.</p>
<p>finally, how to fix this error? any cues would be highly appreciated.</p>

Shing_Yan_Loo: <p>Hi, I just released <a href="https://github.com/yan99033/MiDaS-cpp" rel="noopener nofollow ugc">PyTorch C++ MiDaS</a> for running single-image relative depth prediction. The code should be usable for Monodepth2. Feel free to try it out and let me know.</p>