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

Method get

lesson6-Segmentation/SegDataset.cpp:113–135  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

111}
112
113torch::data::Example<> SegDataset::get(size_t index) {
114 std::string image_path = list_images.at(index);
115 std::string label_path = list_labels.at(index);
116 cv::Mat image = cv::imread(image_path);
117 cv::Mat mask = cv::Mat::zeros(image.rows, image.cols, CV_8UC3);
118 draw_mask(label_path,mask);
119
120 //Data augmentation like flip or rotate could be implemented here...
121 cv::resize(image, image,cv::Size(resize_width,resize_height));
122 cv::resize(mask,mask,cv::Size(resize_width,resize_height));
123 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
124 torch::Tensor colorful_label_tensor = torch::from_blob(mask.data, { mask.rows, mask.cols, 3 }, torch::kByte);
125 torch::Tensor label_tensor = torch::zeros({image.rows, image.cols});
126
127 //encode "colorful" tensor to class_index meaning tensor, [w,h,3]->[w,h], pixel value is the index of a class
128 for(int i = 0; i<name_list.size(); i++){
129 cv::Scalar color = name2color[name_list[i]];
130 torch::Tensor color_tensor = torch::tensor({color.val[0],color.val[1],color.val[2]});
131 label_tensor+=torch::all(colorful_label_tensor==color_tensor,-1)*i;
132 }
133 label_tensor = label_tensor.unsqueeze(0);
134 return { img_tensor.clone(), label_tensor.clone() };
135}

Callers

nothing calls this directly

Calls 1

sizeMethod · 0.45

Tested by

no test coverage detected