Torchvision custom transforms

how do i use torchvision.transforms on images read by opencv?

class resize_rescale():
def init(self, output_size):
assert isinstance(output_size, int)
self.output_size = output_size

def __call__(self, all_3_imgs):
    print('in resize_rescale !!!')

anc_img, pos_img, neg_img = all_3_imgs

    return anc_img, pos_img, neg_img

dataset = siamese_dataset(att_faces_dataset, transform = transforms.Compose([resize_rescale(256),
]))

If you want to transform your images using torchvision.transforms, they should be read by using PIL and not opencv.
However Opencv is faster, so you need to create your own functions to transform your images if you want to use opencv.

1 Like

Hi, There exist a drop-in library https://github.com/uploadcare/pillow-simd which speeds up Pillow 4-6 times. I would recommend to use it instead of rewriting torchvision for opencv.

Anyway, as comment for the OP, Compose works with any function, if during the transformation chain you cast your data to numpy you may use whatever you want.

i did read images using opencv, then i had to make my own transform functions. Then i used transforms.ToTensor() It worked pretty well for me. thanks again !!! :smile:

class resize_rescale():
def init(self, output_size):
assert isinstance(output_size, int)
self.output_size = output_size

def __call__(self, img):         
    img = cv2.resize(img, (self.output_size, self.output_size)) #(width, height)
    return img

dataset = siamese_dataset(att_faces_dataset,
transform = transforms.Compose([resize_rescale(224), transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])]))

If you want to use opencv for transformation and image augmentation, have a look at this repo.

1 Like