def prepare_col(im, dataset, split, nbrs, mean, sd, eig_vals=None, eig_vecs=None): if "cifar" not in dataset: train_size = cfg.TRAIN.IM_SIZE if split == "train": if "imagenet" in dataset: im = transforms.random_sized_crop(im=im, size=train_size, area_frac=0.08) elif "cityscapes" in dataset: random_scale = np.power(2, -1 + 2 * np.random.uniform()) random_size = int(max(train_size, cfg.TEST.IM_SIZE * random_scale)) im = transforms.scale(random_size, im) im = transforms.random_crop(im, train_size, order="HWC") else: im = transforms.scale(cfg.TEST.IM_SIZE, im) im = transforms.center_crop(train_size, im) if split == "train": im = transforms.horizontal_flip(im=im, p=0.5, order="HWC") # Best before rgb2lab because otherwise need to flip together with label im_lab = color.rgb2lab(im.astype(np.uint8, copy=False)) im = transforms.HWC2CHW(im_lab[:, :, 0:1]).astype(np.float32, copy=False) # Ad hoc normalization of the L channel im = im / 100.0 im = im - np.mean(mean) im = im / np.mean(sd) label = nbrs.kneighbors(im_lab[:, :, 1:].reshape(-1, 2), return_distance=False).reshape(im_lab.shape[0], im_lab.shape[1]) return im, label
def prepare_strong_aug(im, dataset, split, mean, sd, eig_vals=None, eig_vecs=None): if "imagenet" in dataset: # Train and test setups differ train_size = cfg.TRAIN.IM_SIZE if split == "train": # Scale and aspect ratio then horizontal flip im = transforms.random_sized_crop(im=im, size=train_size, area_frac=0.08) im = transforms.horizontal_flip(im=im, p=0.5, order="HWC") else: # Scale and center crop im = transforms.scale(cfg.TEST.IM_SIZE, im) im = transforms.center_crop(train_size, im) elif "cityscapes" in dataset: train_size = cfg.TRAIN.IM_SIZE if split == "train": # Scale random_scale = np.power(2, -1 + 2 * np.random.uniform()) random_size = int(max(train_size, cfg.TEST.IM_SIZE * random_scale)) im = transforms.scale(random_size, im) # Crop im = transforms.random_crop(im, train_size, order="HWC") # Flip im = transforms.horizontal_flip(im=im, p=0.5, order="HWC") else: # Scale im = transforms.scale(cfg.TEST.IM_SIZE, im) # Crop im = transforms.center_crop(train_size, im) im = transforms.HWC2CHW(im) im = im / 255.0 if "cifar" not in dataset: im = im[:, :, ::-1] # RGB -> BGR # PCA jitter if split == "train": im = transforms.lighting(im, 0.1, eig_vals, eig_vecs) # Color normalization im = transforms.color_norm(im, mean, sd) if "cifar" in dataset: if split == "train": if cfg.TASK == 'fix': im = transforms.horizontal_flip(im=im, p=0.5) im = transforms.random_crop( im=im, size=cfg.TRAIN.IM_SIZE, pad_size=4 ) # Best after color_norm because of zero padding im = randaugment.random_augment(im) else: im = transforms.horizontal_flip(im=im, p=0.5) im = transforms.random_crop(im=im, size=cfg.TRAIN.IM_SIZE, pad_size=4) return im
def _prepare_im(self, im, transform=True): # Train and test setups differ train_size, test_size = cfg.TRAIN.IM_SIZE, cfg.TEST.IM_SIZE if self._split == "train" and transform: # For training use random_sized_crop, horizontal_flip, augment, lighting im = transforms.random_sized_crop(im, train_size) im = transforms.horizontal_flip(im, 0.5) im = transforms.augment(im, cfg.TRAIN.AUGMENT) im = transforms.lighting(im, cfg.TRAIN.PCA_STD, _EIG_VALS, _EIG_VECS) else: # For testing use scale and center crop im = transforms.scale_and_center_crop(im, test_size, train_size) if transform: # For training and testing use color normalization im = transforms.color_norm(im, _MEAN, _STD) # Convert HWC/RGB/float to CHW/BGR/float format im = np.ascontiguousarray(im[:, :, ::-1].transpose([2, 0, 1])) return im
def _prepare_im(self, im): """Prepares the image for network input (HWC/BGR/int -> CHW/BGR/float).""" # Convert HWC/BGR/int to HWC/RGB/float format for applying transforms im = im[:, :, ::-1].astype(np.float32) / 255 # Train and test setups differ train_size, test_size = cfg.TRAIN.IM_SIZE, cfg.TEST.IM_SIZE if self._split == "train": # For training use random_sized_crop, horizontal_flip, augment, lighting im = transforms.random_sized_crop(im, train_size) im = transforms.horizontal_flip(im, prob=0.5) im = transforms.augment(im, cfg.TRAIN.AUGMENT) im = transforms.lighting(im, cfg.TRAIN.PCA_STD, _EIG_VALS, _EIG_VECS) else: # For testing use scale and center crop im = transforms.scale_and_center_crop(im, test_size, train_size) # For training and testing use color normalization im = transforms.color_norm(im, _MEAN, _STD) # Convert HWC/RGB/float to CHW/BGR/float format im = np.ascontiguousarray(im[:, :, ::-1].transpose([2, 0, 1])) return im
def _prepare_im(self, im): """Prepares the image for network input.""" # Train and test setups differ train_size = cfg.TRAIN.IM_SIZE if self._split == "train": # Scale and aspect ratio then horizontal flip im = transforms.random_sized_crop(im=im, size=train_size, area_frac=0.08) im = transforms.horizontal_flip(im=im, p=0.5, order="HWC") else: # Scale and center crop im = transforms.scale(cfg.TEST.IM_SIZE, im) im = transforms.center_crop(train_size, im) # HWC -> CHW im = im.transpose([2, 0, 1]) # [0, 255] -> [0, 1] im = im / 255.0 # PCA jitter if self._split == "train": im = transforms.lighting(im, 0.1, _EIG_VALS, _EIG_VECS) # Color normalization im = transforms.color_norm(im, _MEAN, _SD) return im
def _prepare_im(self, im): """Prepares the image for network input.""" # Train and test setups differ if self._split == 'train': # Scale and aspect ratio im = transforms.random_sized_crop(image=im, size=224, area_frac=0.08) # Horizontal flip im = transforms.horizontal_flip(image=im, prob=0.5, order='HWC') else: # Scale and center crop im = transforms.scale(256, im) im = transforms.center_crop(224, im) # HWC -> CHW im = transforms.HWC2CHW(im) # [0, 255] -> [0, 1] im = im / 255.0 # PCA jitter if self._split == 'train': im = transforms.lighting(im, 0.1, _EIG_VALS, _EIG_VECS) # Color normalization im = transforms.color_normalization(im, _MEAN, _SD) return im