Search code examples
c++pytorchimage-segmentationsemantic-segmentationlibtorch

I want to deploy a pytorch segmentation model in a C++ application .. C++ equivalent preprocessing


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)


Solution

  • 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