MCPcopy Create free account
hub / github.com/AllentDan/LibtorchTutorials / get

Method get

lesson4-DatasetUtilization/dataSet.h:23–31  ·  view source on GitHub ↗

Override get() function to return tensor at location index

Source from the content-addressed store, hash-verified

21 }
22 // Override get() function to return tensor at location index
23 torch::data::Example<> get(size_t index) override{
24 std::string image_path = image_paths.at(index);
25 cv::Mat image = cv::imread(image_path);
26 cv::resize(image, image, cv::Size(224, 224));
27 int label = labels.at(index);
28 torch::Tensor img_tensor = torch::from_blob(image.data, { image.rows, image.cols, 3 }, torch::kByte).permute({ 2, 0, 1 }); // Channels x Height x Width
29 torch::Tensor label_tensor = torch::full({ 1 }, label);
30 return {img_tensor.clone(), label_tensor.clone()};
31 }
32 // Return the length of data
33 torch::optional<size_t> size() const override {
34 return image_paths.size();

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected