def deepaugment_image_generator(X, y, policy, batch_size=64, augment_chance=0.5): """Yields batch of images after applying random augmentations from the policy Each image is augmented by 50% chance. If augmented, one of the augment-chain in the policy is applied. Which augment-chain to apply is chosen randomly. Args: X (numpy.array): labels (numpy.array): policy (list): list of dictionaries Returns: """ if type(policy) == str: policy_df = pd.read_csv(policy) policy_df = policy_df[[ "aug1_type", "aug1_magnitude", "aug2_type", "aug2_magnitude", "portion" ]] policy = policy_df.to_dict(orient="records") print("Policies are:") print(policy) print() while True: ix = np.arange(len(X)) np.random.shuffle(ix) for i in range(len(X) // batch_size): _ix = ix[i * batch_size:(i + 1) * batch_size] _X = X[_ix] _y = y[_ix] tiny_batch_size = 4 aug_X = _X[0:tiny_batch_size] aug_y = _y[0:tiny_batch_size] for j in range(1, len(_X) // tiny_batch_size): tiny_X = _X[j * tiny_batch_size:(j + 1) * tiny_batch_size] tiny_y = _y[j * tiny_batch_size:(j + 1) * tiny_batch_size] if np.random.rand() <= augment_chance: aug_chain = np.random.choice(policy) aug_chain[ "portion"] = 1.0 # last element is portion, which we want to be 1 hyperparams = list(aug_chain.values()) aug_data = augment_by_policy(tiny_X, tiny_y, *hyperparams) aug_data["X_train"] = apply_default_transformations( aug_data["X_train"]) aug_X = np.concatenate([aug_X, aug_data["X_train"]]) aug_y = np.concatenate([aug_y, aug_data["y_train"]]) else: aug_X = np.concatenate([aug_X, tiny_X]) aug_y = np.concatenate([aug_y, tiny_y]) yield aug_X, aug_y
def getDataIteration(self): if(self.trial_no==0): print("Begin with no augmentation!!") self.trial_hyperparams = ["rotate", 0.0, "rotate", 0.0, "rotate", 0.0, "rotate", 0.0, "rotate", 0.0, "rotate", 0.0, "rotate", 0.0, "rotate", 0.0, "rotate", 0.0, "rotate", 0.0] else: self.trial_hyperparams = self.controller.ask() """ self.trial_hyperparams = ['brighten',0.8921855570502091,'shear',0.857902058468807, 'invert',0.5446000610299714, 'brighten',0.9562927411137008, 'sharpen',0.8628617700194842,'emboss',0.12277382759592961, 'dropout',0.6623048596672579,'translate-x',0.9731839138940244, 'invert',0.7829575191193677,'vertical-flip', 0.9729114349444642] """ print("parameters:",self.trial_hyperparams) augmented_data = augment_by_policy( self.data["X_train"], self.data["y_train"], *self.trial_hyperparams ) return augmented_data,self.trial_hyperparams
def evaluate(self, trial_no, trial_hyperparams): """Evaluates objective function Trains the child model k times with same augmentation hyperparameters. k is determined by the user by `opt_samples` argument. Args: trial_no (int): no of trial. needed for recording to notebook trial_hyperparams (list) Returns: float: trial-cost = 1 - avg. rewards from samples """ augmented_data = augment_by_policy( self.data["X_train"], self.data["y_train"], *trial_hyperparams ) sample_rewards = [] for sample_no in range(1, self.opt_samples + 1): self.child_model.load_pre_augment_weights() # TRAIN history = self.child_model.fit(self.data, augmented_data) # reward = self.calculate_reward(history) sample_rewards.append(reward) self.notebook.record( trial_no, trial_hyperparams, sample_no, reward, history ) trial_cost = 1 - np.mean(sample_rewards) self.notebook.save() log_and_print( f"{str(trial_no)}, {str(trial_cost)}, {str(trial_hyperparams)}", self.logging, ) return trial_cost
def evaluate(self, trial_no, trial_hyperparams): """Evaluates objective function Trains the child model k times with same augmentation hyperparameters. k is determined by the user by `opt_samples` argument. Args: trial_no (int): no of trial. needed for recording to notebook trial_hyperparams (list) Returns: float: trial-cost = 1 - avg. rewards from samples """ augmented_data = augment_by_policy(self.data["X_train"], self.data["y_train"], *trial_hyperparams) sample_rewards = [] #pytorch layers = 2 init_channels = 24 use_aux = True epochs = 30 lr = 0.01 momentum = 0.995 weight_decay = 0.995 drop_path_prob = 0.2 genotype = "Genotype(normal=[[('dil_conv_3x3', 0), ('sep_conv_5x5', 1)], [('sep_conv_3x3', 1), ('avg_pool_3x3', 0)],[('dil_conv_3x3', 1), ('dil_conv_3x3', 0)], [('sep_conv_3x3', 3), ('skip_connect', 1)]], normal_concat=range(2, 6), reduce=[[('sep_conv_3x3', 1), ('dil_conv_5x5', 0)], [('skip_connect', 0), ('sep_conv_5x5', 1)], [('sep_conv_5x5', 1),('sep_conv_5x5', 0)], [('max_pool_3x3', 1), ('sep_conv_3x3', 0)]], reduce_concat=range(2, 6))" model = AugmentCNN(self.input_size, self.input_channels, init_channels, self.n_classes, layers, use_aux, genotype) model = nn.DataParallel(model, device_ids='0').to(device) # model size mb_params = utils.param_size(model) logger.info("Model size = {:.3f} MB".format(mb_params)) # weights optimizer optimizer = torch.optim.SGD(model.parameters(), lr, momentum=momentum, weight_decay=weight_decay) a = 2 / 0 """ for sample_no in range(1, self.opt_samples + 1): self.child_model.load_pre_augment_weights() # TRAIN history = self.child_model.fit(self.data, augmented_data) # reward = self.calculate_reward(history) sample_rewards.append(reward) self.notebook.record( trial_no, trial_hyperparams, sample_no, reward, history ) """ best_top1 = -9999 for epoch in range(epochs): lr_scheduler.step() drop_prob = drop_path_prob * epoch / epochs model.module.drop_path_prob(drop_prob) # training train(train_loader, model, optimizer, criterion, epoch) # validation cur_step = (epoch + 1) * len(train_loader) top1 = validate(valid_loader, model, criterion, epoch, cur_step) # save if best_top1 < top1: best_top1 = top1 is_best = True else: is_best = False print('best_top1:', best_top1) #sample_rewards.append(reward) #self.notebook.record( # trial_no, trial_hyperparams, sample_no, reward, history #) #trial_cost = 1 - np.mean(sample_rewards) #self.notebook.save() log_and_print( f"{str(trial_no)}, {str(trial_cost)}, {str(trial_hyperparams)}", self.logging, ) #return trial_cost return best_top1