0 x01: overture

 

When applying the PyTorch trained model, the same operation needs to be maintained in order to ensure the model’s accuracy and stability.

The fewer dependencies the better in real application deployments, so I’ll show you how to implement data transformation operations in Python without using the “TorchVision.transforms” package. This article introduces how the author thought and implemented in order to help readers in Java and C++ platform can also complete similar operations.

We typically implement that transformation using a “TorchVision.transforms” package during model training and testing. You will need to Resize your images, ToTensor and Normalize.

Specific steps:

  1. Use “TorchVision.transforms” to define a data change method: trans_f.
  2. Data transformation is implemented by calling trans_f

As follows:

import cv2
import PIL
import numpy as np
import torchvision

trans_f = torchvision.transforms.Compose([

            torchvision.transforms.Resize((64.128)),
            torchvision.transforms.ToTensor(),
            torchvision.transforms.Normalize([0.485.0.456.0.406], [0.229.0.224.0.225])
    ])


bgr_img = cv2.imread("demo.png")

src_img = c
Copy the code