-1

I want to deploy a pytorch segmentation model in a C++ application. I knew that I have to convert the model to a Torch Script and use libtorch.

However, what is C++ equivalent to the following pre-preprocessing (It's Ok to convert opencv, but I don't know how to convert the others)?

    import torch.nn.functional as F

    train_tfms = transforms.Compose([transforms.ToTensor(), transforms.Normalize(channel_means, channel_stds)])
    input_width, input_height = input_size[0], input_size[1]

    img_1 = cv.resize(img, (input_width, input_height), cv.INTER_AREA)
    X = train_tfms(Image.fromarray(img_1))
    X = Variable(X.unsqueeze(0)).cuda()  # [N, 1, H, W]
    
    mask = model(X)

    mask = F.sigmoid(mask[0, 0]).data.cpu().numpy()
    mask = cv.resize(mask, (img_width, img_height), cv.INTER_AREA)

Mohamed Hedeya
  • 153
  • 5
  • 22
  • 1
    You'll have to learn C++ for that. Here are some [good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason Aug 04 '22 at 12:19
  • @JasonLiam I know C++, but I'm not familiar with pytorch, and I don't know how to convert from its syntax to C++ (except for opencv). I need help with that for an urgent project. – Mohamed Hedeya Aug 04 '22 at 12:26
  • There's no such thing like _"syntax conversion"_ from pytorch model to c++. And urgency doesn't matter here at all, sorry. – πάντα ῥεῖ Aug 04 '22 at 12:47
  • I mean equivalent syntax. How can I make the same pre-processing in C++. I’m asking for help. I found similar questions on stackoverflow and there are answers to them. If anybody can help, that will be really appreciated. – Mohamed Hedeya Aug 04 '22 at 14:14

1 Answers1

0

To create the transformed dataset, you will need to call MapDataset<DatasetType, TransformType> map(DatasetType dataset,TransformType transform) (see doc). You will likely have to implement your 2 transforms yourself, just look at how they implemented theirs and imitate that.

The libtorch tutorial will guide you through datasets and dataloaders

You can call the sigmoid function with torch::nn::functionql::sigmoid I believe

trialNerror
  • 3,255
  • 7
  • 18
  • Thank you @trialNerror very much for your answer. I'll try what you advised, and get back to you. Really appreciating your help. – Mohamed Hedeya Aug 05 '22 at 21:22
  • Hi @trialNerror, I've finalized the C++ code using libtorch, and it's working with a small output error. Could you pls look at my new question [link](https://stackoverflow.com/questions/73271286/libtorch-the-u-net-segmentation-output-is-correct-but-repeated-3-times-beside) , and see if you can help? Thanks a lot. – Mohamed Hedeya Aug 07 '22 at 22:48