def get_data(datadir, size=IMAGE_SIZE, isTrain=True, zmin=-1, zmax=1, batch=BATCH, shuffle_read=False): if isTrain: augs = [ imgaug.ResizeShortestEdge(int(size * 1.143)), imgaug.RandomCrop(size), imgaug.Flip(horiz=True), ] else: augs = [ imgaug.ResizeShortestEdge(int(size * 1.143)), imgaug.CenterCrop(size) ] def get_images(dir): files = glob.glob(os.path.join(dir, "*.jpg")) if shuffle_read: import random random.shuffle(files) else: files = sorted(files) image_df = ImageFromFile(files, channel=3, shuffle=isTrain) image_df = AugmentImageComponent(image_df, augs) random_df = RandomZData([size, size, 3], zmin, zmax) return JoinData([random_df, image_df]) names = ['train'] if isTrain else ['test'] df = get_images(*[os.path.join(datadir, n) for n in names]) df = BatchData(df, batch) return df
def get_data(image_ids, batch_size=1, is_train=False, shape=510): ds = ImageAndMaskFromFile(image_ids, channel=3, shuffle=True) if is_train: number_of_prefetch = 8 augs_with_label = [ imgaug.RandomCrop(shape), imgaug.Flip(horiz=True, prob=0.5), imgaug.Flip(vert=True, prob=0.5) ] augs_no_label = [ imgaug.RandomOrderAug( [imgaug.Brightness(delta=20), imgaug.Contrast((0.6, 1.4))]) ] # augmentors = [ # GoogleNetResize(), # # It's OK to remove the following augs if your CPU is not fast enough. # # Removing brightness/contrast/saturation does not have a significant effect on accuracy. # # Removing lighting leads to a tiny drop in accuracy. # imgaug.RandomOrderAug( # [imgaug.BrightnessScale((0.6, 1.4), clip=False), # imgaug.Contrast((0.6, 1.4), clip=False), # imgaug.Saturation(0.4, rgb=False), # # rgb-bgr conversion for the constants copied from fb.resnet.torch # imgaug.Lighting(0.1,— # eigval=np.asarray( # [0.2175, 0.0188, 0.0045][::-1]) * 255.0, # eigvec=np.array( # [[-0.5675, 0.7192, 0.4009], # [-0.5808, -0.0045, -0.8140], # [-0.5836, -0.6948, 0.4203]], # dtype='float32')[::-1, ::-1] # )]), # imgaug.Flip(horiz=True), # ] else: number_of_prefetch = 1 augs_with_label = [imgaug.CenterCrop(shape)] augs_no_label = [] ds = AugmentImageComponents(ds, augs_with_label, (0, 1)) ds = AugmentImageComponents(ds, augs_no_label, [0]) ds = BatchData(ds, batch_size) ds = PrefetchData(ds, 30, 1) #number_of_prefetch) return ds
def normal_augmentor(isTrain): """ Normal augmentor with random crop and flip only, for BGR images in range [0,255]. """ if isTrain: augmentors = [ imgaug.ResizeShortestEdge(256, cv2.INTER_CUBIC), imgaug.RandomCrop((DEFAULT_IMAGE_SHAPE, DEFAULT_IMAGE_SHAPE)), imgaug.Flip(horiz=True), ] else: augmentors = [ imgaug.ResizeShortestEdge(256, cv2.INTER_CUBIC), imgaug.CenterCrop((DEFAULT_IMAGE_SHAPE, DEFAULT_IMAGE_SHAPE)), ] return augmentors
def get_data(phase): is_train = phase == "train" ds = dataset.Cifar10(phase) pp_mean = ds.get_per_pixel_mean(("train", )) if is_train: augmentors = [ imgaug.CenterPaste((40, 40)), imgaug.RandomCrop((32, 32)), imgaug.Flip(horiz=True), imgaug.MapImage(lambda x: x - pp_mean), ] else: augmentors = [imgaug.MapImage(lambda x: x - pp_mean)] ds = AugmentImageComponent(ds, augmentors) ds = BatchData(ds, BATCH_SIZE, remainder=not is_train) return ds
def get_data(datadir, size=IMAGESIZE, isTrain=True): if isTrain: augs = [ imgaug.RandomCrop(size), imgaug.Flip(horiz=True), ] else: augs = [imgaug.CenterCrop(size)] def get_images(dir): files = sorted(glob.glob(os.path.join(dir, "*.jpg"))) df = ImageFromFile(files, channel=3, shuffle=isTrain) random_df = RandomZData([size, size, 3], -1, 1) return JoinData([random_df, AugmentImageComponent(df, augs)]) names = ['train'] if isTrain else ['test'] df = get_images(*[os.path.join(datadir, n) for n in names]) df = BatchData(df, BATCH if isTrain else TEST_BATCH) return df
def fbresnet_augmentor(is_training, option): if is_training: augmentors = [ imgaug.ToFloat32(), imgaug.Resize((option.final_size + 32, option.final_size + 32)), imgaug.RandomCrop((option.final_size, option.final_size)) ] flip = [imgaug.Flip(horiz=True), imgaug.ToUint8()] augmentors.extend(flip) else: augmentors = [ imgaug.ToFloat32(), imgaug.Resize((option.final_size + 32, option.final_size + 32)), imgaug.CenterCrop((option.final_size, option.final_size)), imgaug.ToUint8() ] return augmentors
def get_ilsvrc_data_alexnet(is_train, image_size, batchsize, directory): if is_train: if not directory.startswith('/'): ds = ILSVRCTTenthTrain(directory) else: ds = ILSVRC12(directory, 'train') augs = [ imgaug.RandomApplyAug(imgaug.RandomResize((0.9, 1.2), (0.9, 1.2)), 0.7), imgaug.RandomApplyAug(imgaug.RotationAndCropValid(15), 0.7), imgaug.RandomApplyAug( imgaug.RandomChooseAug([ imgaug.SaltPepperNoise(white_prob=0.01, black_prob=0.01), imgaug.RandomOrderAug([ imgaug.BrightnessScale((0.8, 1.2), clip=False), imgaug.Contrast((0.8, 1.2), clip=False), # imgaug.Saturation(0.4, rgb=True), ]), ]), 0.7), imgaug.Flip(horiz=True), imgaug.ResizeShortestEdge(256, cv2.INTER_CUBIC), imgaug.RandomCrop((224, 224)), ] ds = AugmentImageComponent(ds, augs) ds = PrefetchData(ds, 1000, multiprocessing.cpu_count()) ds = BatchData(ds, batchsize) ds = PrefetchData(ds, 10, 4) else: if not directory.startswith('/'): ds = ILSVRCTenthValid(directory) else: ds = ILSVRC12(directory, 'val') ds = AugmentImageComponent(ds, [ imgaug.ResizeShortestEdge(224, cv2.INTER_CUBIC), imgaug.CenterCrop((224, 224)), ]) ds = PrefetchData(ds, 100, multiprocessing.cpu_count()) ds = BatchData(ds, batchsize) return ds
def _augment(self, img, _): h, w = img.shape[:2] area = h * w for _ in range(10): targetArea = self.rng.uniform(self.crop_area_fraction, 1.0) * area aspectR = self.rng.uniform(self.aspect_ratio_low, self.aspect_ratio_high) ww = int(np.sqrt(targetArea * aspectR) + 0.5) hh = int(np.sqrt(targetArea / aspectR) + 0.5) if self.rng.uniform() < 0.5: ww, hh = hh, ww if hh <= h and ww <= w: x1 = 0 if w == ww else self.rng.randint(0, w - ww) y1 = 0 if h == hh else self.rng.randint(0, h - hh) out = img[y1:y1 + hh, x1:x1 + ww] out = cv2.resize(out, (self.target_shape, self.target_shape), interpolation=cv2.INTER_CUBIC) return out out = imgaug.ResizeShortestEdge(self.target_shape, interp=cv2.INTER_CUBIC).augment(img) out = imgaug.RandomCrop(self.target_shape).augment( out) # TODO : Random Crop? return out
def fbresnet_augmentor(isTrain, crop_method, color_augmentation): """ Augmentor used in fb.resnet.torch, for BGR images in range [0,255]. """ execution_lst = [] if isTrain: augmentors = [ # 1. crop_method # a) GoogleNetResize GoogleNetResize(), # b) ShortestEdgeResize imgaug.ResizeShortestEdge(256), # c) GlobalWarp imgaug.Resize(226), # NOTE: for CAM generation imgaug.RandomCrop((224, 224)), # d) CAMCrop # (when CAMCrop is set, the output from the original DataFlow has already been cropped) # 2. color_augmentation imgaug.RandomOrderAug([ imgaug.BrightnessScale((0.6, 1.4), clip=False), imgaug.Contrast((0.6, 1.4), clip=False), imgaug.Saturation(0.4, rgb=False), # rgb-bgr conversion for the constants copied from fb.resnet.torch imgaug.Lighting( 0.1, eigval=np.asarray([0.2175, 0.0188, 0.0045][::-1]) * 255.0, eigvec=np.array([[-0.5675, 0.7192, 0.4009], [-0.5808, -0.0045, -0.8140], [-0.5836, -0.6948, 0.4203]], dtype='float32')[::-1, ::-1]) ]), imgaug.Flip(horiz=True), ] # if crop_method == 'GoogleNetResize': print( '--> perform GoogleNetResize cropping method during the training pipeline' ) execution_lst.extend([0]) elif crop_method == 'ShortestEdgeResize': print( '--> perform ShortestEdgeResize cropping method during the training pipeline' ) execution_lst.extend([1, 3]) elif crop_method == 'GlobalWarp': print( '--> perform GlobalWarp cropping method during the training pipeline' ) execution_lst.extend([2, 3]) elif crop_method == 'CAMCrop': # enable CAMCrop @ 20171124 print( '*** Perform CAMCrop to better the training dynamics and the results ***' ) if color_augmentation: print( '--> perform color augmentation during the training pipeline') execution_lst.extend([4]) else: print( '--> discard the color jittering process during the training pipeline' ) # perform mirror reflection augmentation anyway execution_lst.extend([5]) else: augmentors = [ imgaug.ResizeShortestEdge(256, cv2.INTER_CUBIC), imgaug.CenterCrop((224, 224)), imgaug.RandomCrop((224, 224)), ] if crop_method == 'RandomCrop': execution_lst.extend([0, 2]) elif crop_method == 'CenterCrop': execution_lst.extend([0, 1]) return [ item_ for id_, item_ in enumerate(augmentors) if id_ in execution_lst ]