def get_transform(train): transforms = [] transforms.append(T.ToTensor()) if train: # Augmentations go here... Won't do any for now pass # transforms.append(T.RandomHorizontalFlip(0.5)) return T.Compose(transforms)
def get_transform(train): transforms = [] transforms.append(T.ToTensor()) if train: transforms.append(T.RandomHorizontalFlip(0.5)) # 注意:这里将`.ToTensor()`放到`.RandomHorizontalFlip()`前 # 会导致报错 `TypeError: img should be PIL Image. Got <class `torch.Tensor`>` return T.Compose(transforms)
def get_transform(train): transforms = [] # converts the image, a PIL image, into a PyTorch Tensor transforms.append(T.ToTensor()) if train: # during training, randomly flip the training images # and ground-truth for data augmentation transforms.append(T.RandomHorizontalFlip(0.5)) return T.Compose(transforms)
boxes = np.zeros([mask.shape[-1], 4], dtype=np.int32) for i in range(mask.shape[-1]): m = mask[:, :, i] # Bounding box. horizontal_indicies = np.where(np.any(m, axis=0))[0] vertical_indicies = np.where(np.any(m, axis=1))[0] if horizontal_indicies.shape[0]: x1, x2 = horizontal_indicies[[0, -1]] y1, y2 = vertical_indicies[[0, -1]] # x2 and y2 should not be part of the box. Increment by 1. x2 += 1 y2 += 1 else: # No mask for this instance. Might happen due to # resizing or cropping. Set bbox to zeros x1, x2, y1, y2 = 0, 0, 0, 0 # boxes[i] = np.array([y1, x1, y2, x2]) boxes[i] = np.array([x1, y1, x2, y2]) return boxes.astype(np.int32, copy=False) if __name__ == "__main__": dataset = PennFudanDataset(root='./data-ins/train/', transforms=T.Compose([T.ToTensor()])) loader = DataLoader(dataset, batch_size=16, shuffle=False, num_workers=0) for i, image, label in enumerate(loader): print(image.shape)
def get_transform(train): transforms = [] transforms.append(T.ToTensor()) if train: transforms.append(T.RandomHorizontalFlip(0.5)) return T.Compose(transforms)
def get_transform(train): transforms = [] transforms.append(T.ToTensor()) # transforms.append(T.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))) return T.Compose(transforms)