110. Preprocessing Images

When testing out your model’s output for computer vision tasks, you’ll need to apply the same preprocessing methods you’ve applied during training, to the images you want to test out. Here is one way to implement.

  1. Import Images
    from PIL import Image
    input_img = Image.open("PATH_TO_IMAGE.jpg")
    
  2. Define preprocessing method same as the method used during training
    import torchvision
    input_preprocess = torchvision.transforms.Compose([
                            torchvision.transforms.Resize((512,512)), #Resize Image to 512x512
                            torchvision.transforms.ToTensor()]) # change values from 0~255 to 0~1
    
  3. Apply preprocessing method
    processed_input_img = input_preprocess(input_img)