Complete the code to apply a random horizontal flip to an image tensor using PyTorch transforms.
transform = torchvision.transforms.RandomHorizontalFlip(p=[1])The probability parameter p in RandomHorizontalFlip controls how often the flip happens. 0.5 means the image flips half the time.
Complete the code to convert a PIL image to a tensor using PyTorch transforms.
transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor(), torchvision.transforms.Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5])]) image_tensor = transform([1])
The ToTensor() transform expects a PIL Image or numpy array as input. Here, image_pil is the correct input.
Fix the error in the code to resize an image to 128x128 pixels using PyTorch transforms.
transform = torchvision.transforms.Resize([1])The Resize transform expects a size as an int or a tuple. To resize to width and height, use a tuple like (128, 128).
Fill both blanks to create a transform that converts an image to grayscale and then to a tensor.
transform = torchvision.transforms.Compose([torchvision.transforms.[1](), torchvision.transforms.[2]()])
Resize or Normalize instead of Grayscale.The Grayscale() transform converts the image to grayscale. Then ToTensor() converts it to a tensor.
Fill all three blanks to create a transform pipeline that resizes an image to 64x64, converts it to a tensor, and normalizes it with mean 0.5 and std 0.5.
transform = torchvision.transforms.Compose([
torchvision.transforms.[1]([2]),
torchvision.transforms.[3](),
torchvision.transforms.Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5])
])Grayscale instead of ToTensor.First, Resize((64, 64)) resizes the image. Then ToTensor() converts it to a tensor. Finally, Normalize adjusts the values.