def next_batch(self): """Return the next minibatch of augmented data.""" next_train_index = self.curr_train_index + self.hparams.batch_size if next_train_index > self.num_train: # Increase epoch number epoch = self.epochs + 1 self.reset() self.epochs = epoch batched_data = ( self.train_images[self.curr_train_index:self.curr_train_index + self.hparams.batch_size], self.train_labels[self.curr_train_index:self.curr_train_index + self.hparams.batch_size]) final_imgs = [] images, labels = batched_data for data in images: epoch_policy = self.good_policies[np.random.choice( len(self.good_policies))] final_img = augmentation_transforms.apply_policy( epoch_policy, data) final_img = augmentation_transforms.random_flip( augmentation_transforms.zero_pad_and_crop(final_img, 4)) # Apply cutout final_img = augmentation_transforms.cutout_numpy(final_img) final_imgs.append(final_img) batched_data = (np.array(final_imgs, np.float32), labels) self.curr_train_index += self.hparams.batch_size return batched_data
def func_autoaug(img, selected_policies): epoch_policy = selected_policies[np.random.choice(len(selected_policies))] img = np.array(img) img = AT.apply_policy(epoch_policy, img) img = np.array(img) img = AT.random_flip(AT.zero_pad_and_crop(img, 4)) img = AT.cutout_numpy(img) return img
def OriginalGAutoAug(self,batch_xs,batch_ys): import augmentation_transforms gp = self.current_policy x = [] y = [] for idx, label in enumerate(batch_ys): data = batch_xs[idx] data2 = augmentation_transforms.apply_policy(gp, data) data2 = np.array(data2) x.append(data2) y.append(label) return np.array(x), np.array(y)
def func_gautoaug(img, selected_policies): alpha = 2 p1 = 1 p_list = [] p_list.append(p1) for i in range(2, 16): tmp = pow((p1 / i), alpha) p_list.append(tmp) r = random.uniform(0, 1) for gdx in reversed(range(len(selected_policies))): if r <= p_list[gdx]: gp = selected_policies[gdx] img = np.array(img) img = AT.apply_policy(gp, img) # good_policies[20] return img
def next_batch(self): """Return the next minibatch of augmented data.""" next_train_index = self.curr_train_index + self.hparams.batch_size if next_train_index > self.num_train: # Increase epoch number epoch = self.epochs + 1 self.reset() self.epochs = epoch batched_data = ( self.train_images[self.curr_train_index:self.curr_train_index + self.hparams.batch_size], self.train_labels[self.curr_train_index:self.curr_train_index + self.hparams.batch_size]) final_imgs = [] images, labels = batched_data if self.hparams.augment_type == 'mixup': images, labels = augmentation_transforms.mixup_batch( images, labels, self.hparams.mixup_alpha) elif self.hparams.augment_type == 'image_freq': images, labels = augmentation_transforms.freq_augment( images, labels, amplitude=self.hparams.freq_augment_amplitude, magnitude=self.hparams.augmentation_magnitude, proportion_f=self.hparams.freq_augment_ffrac, probability=self.hparams.augmentation_probability) for data in images: if self.hparams.augment_type == 'autoaugment': epoch_policy = self.good_policies[np.random.choice( len(self.good_policies))] final_img = augmentation_transforms.apply_policy(epoch_policy, data) elif self.hparams.augment_type == 'random': epoch_policy = found_policies.random_policy( self.hparams.num_augmentation_layers, self.hparams.augmentation_magnitude, self.hparams.augmentation_probability) final_img = augmentation_transforms.apply_policy(epoch_policy, data) else: final_img = np.copy(data) if self.hparams.apply_flip_crop: final_img = augmentation_transforms.random_flip( augmentation_transforms.zero_pad_and_crop(data, 4)) # Apply cutout if self.hparams.apply_cutout: final_img = augmentation_transforms.cutout_numpy(final_img) final_imgs.append(final_img) final_imgs = np.array(final_imgs, np.float32) if self.hparams.noise_type == 'radial': labels = augmentation_transforms.add_radial_noise( final_imgs, labels, self.hparams.frequency, self.hparams.amplitude, self.hparams.noise_class, self.hparams.normalize_amplitude) elif self.hparams.noise_type == 'random' or self.hparams.noise_type == 'fourier' or self.hparams.noise_type == 'f' or self.hparams.noise_type == '1/f': labels = augmentation_transforms.add_sinusoidal_noise( final_imgs, labels, self.hparams.frequency, self.hparams.amplitude, self.direction, self.hparams.noise_class, self.hparams.normalize_amplitude) elif self.hparams.noise_type == 'uniform': labels = augmentation_transforms.add_uniform_noise( labels, self.hparams.amplitude, self.hparams.noise_class) batched_data = (final_imgs, labels) self.curr_train_index += self.hparams.batch_size return batched_data