More outputs from a torchscript model

I converted a torch model into a torchscript. Then I was planning to get the outputs of my torchscript model.

#include <iostream>
#include <torch/script.h>
#include <torch/torch.h>
#include <opencv2/opencv.hpp>

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 << "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<torch::IValue> 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<cv::Mat> 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<<"Done"<<endl;
    return 0;
}

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})

test_image

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 result_decoder[3] to represent the output that I want to use. However, I got an error as below,
no match for ‘operator[]’ (operand types are ‘c10::IValue’ and ‘int’)

Next, I removed and recompile my project.
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)

this error means that this operation result_decoder.toTensor(). couldn’t be executed because result_decoder is not a single tensor.

finally, how to fix this error? any cues would be highly appreciated.

Hi, I just released PyTorch C++ MiDaS for running single-image relative depth prediction. The code should be usable for Monodepth2. Feel free to try it out and let me know.