Reference page: Transform summary that can be used with Pytorch – torchvision
In transforms
used for preprocessing of image data, user-defined Transform can be created by passing Lambda function.
from torchvision import transforms
import cv2
import matplotlib.pyplot as plt
img = cv2.imread("sample.jpeg ")
plt.imshow(img)
def gray(img):
"""
Convert to RGB and grayscale
"""
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
return img
transform = transforms.Lambda(gray)
img_transformed = transform(img)
plt.imshow(img_transformed)
If you connect this process with Compose, you can incorporate it in the pipeline of pytorch's transform.
Recommended Posts