How can I apply a single transform?

I’m new to PyTorch, and I am just trying to use the pretrained Mask R-CNN model to segment a single image. To do this, I’m reading the image using img=PIL.Image.open("apple.jpg"). Now I simply need to transform the image to a tensor to pass to the model, but how can I do this in the simplest way possible? I can use torchvision.transforms.transforms.Compose([transforms.ToTensor()])(img), but is it not possible to apply the ToTensor transform without using Compose?

You don’t even need to use pil.
You can use any image reader you want imageio, opencv, scikit-image (all those returns numpy arrays)
and just

img = load(...)
img = torch.from_numpy(img)/255.

and there you go
or alternatevely you can do transforms.ToTensor()(img)

1 Like