How to load Images in C++ (DataLoader etc.)

Hi, I am searching for a way to load my images as I do it in Python. I just want to instantiate a DataLoader object and give a directory as a parameter. Why is it so complicated in C++? I found some code but it is kind of hard for me to understad it. Here it is…

#include <torch/torch.h>

// You can for example just read your data and directly store it as tensor.
torch::Tensor read_data(const std::string& loc)
{
    torch::Tensor tensor = ...

    // Here you need to get your data.

    return tensor;
};

class MyDataset : public torch::data::Dataset<MyDataset>
{
    private:
        torch::Tensor states_, labels_;

    public:
        explicit MyDataset(const std::string& loc_states, const std::string& loc_labels) 
            : states_(read_data(loc_states)),
              labels_(read_data(loc_labels) {   };

        torch::data::Example<> get(size_t index) override;
};

torch::data::Example<> MyDataset::get(size_t index)
{
    // You may for example also read in a .csv file that stores locations
    // to your data and then read in the data at this step. Be creative.
    return {states_[index], labels_[index]};
} 

What does this do? torch::data::Example<> get(size_t index) override; and where does the size_t index come from? Is it really necessary to build my own Dataset class? How do I push mit imaga data into the tensor in the read_data Method?

Thank you all

Thank you for your post. To me it is inacceptable that there is no core functionality for using custom Datasets as intuatively as in the python version. One more reason for me to go back to Tensorflow. I mean…they want to have lots of users for this framework and build it like ‘We don dont care about any documentation for the c++ version. So please be a pro when you want more than python. And pleas don’t ask the forum because we won’t anwer you’.

Thank you for your help

1 Like

I have already complained about this.