Search code examples
c++libtorch

how to define a torch model as an input of a function in c++


I'm loading a model in c++, which was trained in python. Now I want to write a function which tests the model with a random input but I can't define the model as an argument for the function. I've tried struct but it doesn't work.

void test(vector<struct comp*>& model){
    //pseudo input
    vector<torch::jit::IValue> inputs;
    inputs.push_back(torch::ones({1,3,224, 224}));

    at::Tensor output = model[0]->forward(inputs).toTensor();
    cout << output << endl;
}

int main(int argc, char *argv[]) {

    if (argc == 2){
        cout << argv[1] << endl;
        //model = load_model(argv[1]);
        torch::jit::script::Module module = torch::jit::load(argv[1]);

    }
    else {
        cerr << "no path of model is given" << endl;
    }
    // test
    vector<struct comp*> modul;
    modul.push_back(module);
    test(modul);
}

Solution

  • Edit: You need to put your module variable in scope!

    Your basic type is torch::jit::script::Module so define a name for it:

    using module_type = torch::jit::script::Module;
    

    Then use that in your code, also use a const reference for read-only parameter:

    void test(const vector<module_type>& model){
        //pseudo input
        vector<torch::jit::IValue> inputs;
        inputs.push_back(torch::ones({1,3,224, 224}));
    
        at::Tensor output = model[0]->forward(inputs).toTensor();
        cout << output << endl;
    }
    
    int main(int argc, char *argv[]) {
    
        if (argc == 2){
            cout << argv[1] << endl;            
        }
        else {
            cerr << "no path of model is given" << endl;
            return -1;
        }
    
        // test
        module_type module = torch::jit::load(argv[1]);;
        vector<module_type> modul;
        modul.push_back(module);
        test(modul);
    }