Complete the code to apply a random horizontal flip to an image tensor using PyTorch transforms.
transform = torchvision.transforms.Compose([torchvision.transforms.RandomHorizontalFlip(p=[1])])The RandomHorizontalFlip transform takes a probability p between 0 and 1 to flip the image horizontally. 0.5 means 50% chance.
Complete the code to normalize an image tensor with mean 0.5 and std 0.5 using PyTorch transforms.
transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor(), torchvision.transforms.Normalize(mean=[[1]], std=[0.5])])
The Normalize transform requires mean and std values for each channel. Here, mean is set to 0.5 to center the data.
Fix the error in the code to apply random rotation of up to 30 degrees to an image tensor.
transform = torchvision.transforms.Compose([torchvision.transforms.RandomRotation(degrees=[1])])The RandomRotation transform expects a range tuple for degrees to rotate randomly between. Passing a tuple like (-30, 30) is correct.
Fill both blanks to create a transform that converts an image to tensor and then applies a random vertical flip with 30% chance.
transform = torchvision.transforms.Compose([[1](), torchvision.transforms.RandomVerticalFlip(p=[2])])
First, images must be converted to tensors using ToTensor(). Then, RandomVerticalFlip with probability 0.3 flips images vertically 30% of the time.
Fill all three blanks to create a transform pipeline that resizes images to 128x128, converts to tensor, and normalizes with mean 0.5 and std 0.5.
transform = torchvision.transforms.Compose([torchvision.transforms.[1]((128, 128)), torchvision.transforms.[2](), torchvision.transforms.Normalize(mean=[[3]], std=[0.5])])
CenterCrop instead of Resize.The Resize transform changes image size, ToTensor converts images to tensors, and Normalize centers and scales pixel values.