def background_process_augmentation_example_2(): augseq = iaa.Sequential( [iaa.Fliplr(0.5), iaa.CoarseDropout(p=0.1, size_percent=0.1)]) num_epoches = 2 for epoch in range(num_epoches): print('Started epoch {}.'.format(epoch)) batch_loader = ia.BatchLoader(load_batches_from_numpy) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq) while True: print('Next batch...') batch = bg_augmenter.get_batch() if batch is None: print('Finished epoch.') break #images = batch.images images_aug = batch.images_aug #keypoints = batch.keypoints #keypoints_aug = batch.keypoints_aug print('Image IDs:', batch.data) #ia.imshow(np.hstack(list(images_aug))) batch_loader.terminate() bg_augmenter.terminate()
def _augment_small_2(): batch_loader = ia.BatchLoader(load_images(n_batches=2), queue_size=100) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq_i) i = 0 while True: batch = bg_augmenter.get_batch() if batch is None: break i += 1
def __next__(self): # for train_index, valid_index in kf.split(range(self.n_folds)): if self.n <= self.n_folds: train_index, valid_index = next(self.kf_split_iter) # print(train_index, valid_index) train_datasets = self._accumulate_datasets_(train_index) valid_datasets = self._accumulate_datasets_(valid_index) # train_batchgenerator = WorkflowBatchGenerator(data=train_datasets, batch_size=self.batch_size, # n_batches=self.n_batches) # valid_batchgenerator = WorkflowBatchGenerator(data=valid_datasets, batch_size=self.batch_size, # n_batches=self.n_batches) train_batchloader = ia.BatchLoader( load_batches(train_datasets, self.batch_size, self.n_batches)) #, nb_workers=self.num_workers) valid_batchloader = ia.BatchLoader( load_batches(valid_datasets, self.batch_size, self.n_batches)) #, nb_workers=self.num_workers) train_bg_augmenter = ia.BackgroundAugmenter( train_batchloader, self.image_transform, nb_workers=self.num_workers) valid_bg_augmenter = ia.BackgroundAugmenter( valid_batchloader, self.image_transform, nb_workers=self.num_workers) # train_mt_generator = MultiThreadedAugmenter(train_dataloader, self.image_transform, self.num_workers) # valid_mt_generator = MultiThreadedAugmenter(valid_dataloader, self.image_transform, self.num_workers) self.n += 1 return train_bg_augmenter, valid_bg_augmenter else: raise StopIteration
def getBatches(self, *args, **kwargs): if self._is_label_augmented: # FIXME [fix] >> Do not check. batch_loader = ia.BatchLoader(self._loadBatchPairs) augmenter_det = self._augmenter.to_deterministic() bg_augmenter = ia.BackgroundAugmenter(batch_loader, augmenter_det) while True: batch = bg_augmenter.get_batch() if batch is None: break yield batch.images_aug, batch.keypoints_aug batch_loader.terminate() bg_augmenter.terminate() else: batch_loader = ia.BatchLoader(self._loadBatches) bg_augmenter = ia.BackgroundAugmenter(batch_loader, self._augmenter) while True: batch = bg_augmenter.get_batch() if batch is None: break #images = batch.images #images_aug = batch.images_aug #keypoints = batch.keypoints #keypoints_aug = batch.keypoints_aug #data = batch.data yield batch.images_aug, self._labels[batch.data] batch_loader.terminate() bg_augmenter.terminate()
def main(): augseq = iaa.Sequential( [iaa.Fliplr(0.5), iaa.CoarseDropout(p=0.1, size_percent=0.1)]) print("------------------") print("augseq.augment_batches(batches, background=True)") print("------------------") batches = list(load_images()) batches_aug = augseq.augment_batches(batches, background=True) images_aug = [] keypoints_aug = [] for batch_aug in batches_aug: images_aug.append(batch_aug.images_aug) keypoints_aug.append(batch_aug.keypoints_aug) ia.imshow(draw_grid(images_aug, keypoints_aug)) print("------------------") print("augseq.augment_batches(batches, background=True) -> only images") print("------------------") batches = list(load_images()) batches = [batch.images for batch in batches] batches_aug = augseq.augment_batches(batches, background=True) images_aug = [] keypoints_aug = None for batch_aug in batches_aug: images_aug.append(batch_aug) ia.imshow(draw_grid(images_aug, keypoints_aug)) print("------------------") print("BackgroundAugmenter") print("------------------") batch_loader = ia.BatchLoader(load_images) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq) images_aug = [] keypoints_aug = [] while True: print("Next batch...") batch = bg_augmenter.get_batch() if batch is None: print("Finished.") break images_aug.append(batch.images_aug) keypoints_aug.append(batch.keypoints_aug) ia.imshow(draw_grid(images_aug, keypoints_aug))
def background_process_augmentation_example_1(): # Example augmentation sequence to run in the background. augseq = iaa.Sequential( [iaa.Fliplr(0.5), iaa.CoarseDropout(p=0.1, size_percent=0.1)]) print('Started epoch.') # Background augmentation consists of two components: # (1) BatchLoader, which runs in a Thread and calls repeatedly a user-defined # function (here: load_batches) to load batches (optionally with keypoints # and additional information) and sends them to a queue of batches. # (2) BackgroundAugmenter, which runs several background processes (on other # CPU cores). Each process takes batches from the queue defined by (1), # augments images/keypoints and sends them to another queue. # The main process can then read augmented batches from the queue defined by (2). batch_loader = ia.BatchLoader(load_batches) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq) # Run until load_batches() returns nothing anymore. This also allows infinite training. while True: print('Next batch...') batch = bg_augmenter.get_batch() if batch is None: print('Finished epoch.') break #images = batch.images images_aug = batch.images_aug #keypoints = batch.keypoints #keypoints_aug = batch.keypoints_aug print('Image IDs:', batch.data) #ia.imshow(np.hstack(list(images_aug))) batch_loader.terminate() bg_augmenter.terminate()
def main(): batch_size = 100 output_txt = '/media/frelam/683cd494-d120-4dbd-81a4-eb3a90330106/ai_challange_scene_dataset_lmdb/aichallenger_arug20171116/dataarymentation/train_arug2.txt' dataset_root = '/home/frelam/ai_challenger_scene_train_20170904/train/scene_train_images_20170904/' dataarumation_root = '/media/frelam/683cd494-d120-4dbd-81a4-eb3a90330106/ai_challange_scene_dataset_lmdb/aichallenger_arug20171116/dataarymentation/scene_train_images_arug20171117/' duplicate_num_per_image = 10 seq = seq_setting() #input: # image_dir_array # image_label_array image_dir_array, image_label_array = load_image_dir(f1) dir_chunk_array = [ image_dir_array[x:x + batch_size] for x in xrange(0, len(image_dir_array), batch_size) ] label_chunk_array = [ image_label_array[x:x + batch_size] for x in xrange(0, len(image_label_array), batch_size) ] dir_for_save = [] label_for_save = [] #batch process for i in range(len(dir_chunk_array)): batch_loader = ia.BatchLoader( process_batch_image(dataset_root, dataarumation_root, duplicate_num_per_image, dir_chunk_array[i], label_chunk_array[i], seq)) dir_for_save.append(dirs) label_for_save.append(labels) print i with open(output_txt, 'w') as output: for i in range(len(dir_for_save)): for j in range(len(dir_for_save[i])): output.write(dir_for_save[i][j] + ' ' + label_for_save[i][j] + '\n')
def finetune(FLAG): # already exist (uploaded to github) with open('save/label_dict.pkl', 'rb') as f: y_dict = pickle.load(f) dtrain = pd.read_csv(FLAG.train_file, header=None, sep=" ", names=["img", "id"]) dvalid = pd.read_csv(FLAG.valid_file, header=None, sep=" ", names=["img", "id"]) train_list = [ os.path.join(FLAG.train_path, img) for img in list(dtrain.img) ] valid_list = [ os.path.join(FLAG.valid_path, img) for img in list(dvalid.img) ] print("Reading train and valid images") Xtrain = readImgList(train_list) print("train: {0}".format(Xtrain.shape)) Xvalid = readImgList(valid_list) print("valid: {0}".format(Xvalid.shape)) ytrain = transformLabel(list(dtrain.id), y_dict) yvalid = transformLabel(list(dvalid.id), y_dict) Ytrain = one_hot_encoding(ytrain, len(y_dict)) Yvalid = one_hot_encoding(yvalid, len(y_dict)) print("Building model") scope_name = "Model" model = VGG16_GAP(scope_name=scope_name) model.build(vgg16_npy_path=FLAG.init_from, shape=Xtrain.shape[1:], classes=len(y_dict), prof_type=FLAG.prof_type, conv_pre_training=True, fc_pre_training=True, new_bn=False) if FLAG.centers is not None: centers = np.load(FLAG.centers) model.add_centers(centers.astype(np.float32)) else: print( "please specify your center.npy or initialize centers with all zeros" ) model.add_centers() print("Setting operations at various levels") dp = [1.0, 0.75, 0.5] tasks = [str(int(p * 100)) for p in dp] model.set_idp_operation(dp=dp, decay=FLAG.decay, keep_prob=FLAG.keep_prob, lambda_c=FLAG.lambda_c) obj = 0.0 for cur_task in tasks: print(cur_task) obj += model.loss_dict[cur_task] tracking = list() for cur_task in tasks: tracking.append(model.accu_dict[cur_task]) # data augmenter sometimes = lambda aug: iaa.Sometimes(0.5, aug) transform = iaa.Sequential([ sometimes( iaa.Affine(translate_percent={ "x": (-0.15, 0.15), "y": (-0.15, 0.15) })), sometimes(iaa.Affine(scale={ "x": (0.85, 1.15), "y": (0.85, 1.15) })), sometimes(iaa.Affine(rotate=(-45, 45))), sometimes(iaa.Fliplr(0.5)) ]) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) augment = True # hyper parameters batch_size = 32 epoch = 100 early_stop_patience = 10 min_delta = 0.0001 # recorder epoch_counter = 0 history = list() # Passing global_step to minimize() will increment it at each step. learning_rate = FLAG.learning_rate opt = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.5) checkpoint_path = os.path.join(FLAG.save_dir, 'model.ckpt') # trainable variables train_vars = list() for var in tf.trainable_variables(): if model.scope_name in var.name: train_vars.append(var) for rm in model.gamma_var: train_vars.remove(rm) print('%s is not trainable.' % rm) for var in train_vars: if '_mean' in var.name: train_vars.remove(var) print('%s is not trainable.' % var) for var in train_vars: if '_beta' in var.name: train_vars.remove(var) print('%s is not trainable.' % var) for var in train_vars: if '_variance' in var.name: train_vars.remove(var) print('%s is not trainable.' % var) print(train_vars) train_op = opt.minimize(obj, var_list=train_vars) saver = tf.train.Saver(tf.global_variables(), max_to_keep=len(tasks)) # max step in a epoch ptrain_max = int(Xtrain.shape[0] / batch_size) pval_max = int(Xvalid.shape[0] / batch_size) # re-initialize def initialize_uninitialized(sess): global_vars = tf.global_variables() is_not_initialized = sess.run( [tf.is_variable_initialized(var) for var in global_vars]) not_initialized_vars = [ v for (v, f) in zip(global_vars, is_not_initialized) if not f ] if len(not_initialized_vars): sess.run(tf.variables_initializer(not_initialized_vars)) initialize_uninitialized(sess) # reset due to adding a new task patience_counter = 0 current_best_val_accu = 0 # optimize when the aggregated obj while (patience_counter < early_stop_patience and epoch_counter < epoch): # start training stime = time.time() train_loss, train_accu = 0.0, 0.0 if augment: def load_batches(): for i in range(int(Xtrain.shape[0] / batch_size)): print("Training: {0}/{1}".format(i, ptrain_max), end='\r') st = i * batch_size ed = (i + 1) * batch_size batch = ia.Batch(images=Xtrain[st:ed, :, :, :], data=Ytrain[st:ed, :]) yield batch batch_loader = ia.BatchLoader(load_batches) bg_augmenter = ia.BackgroundAugmenter( batch_loader=batch_loader, augseq=transform, nb_workers=1) while True: batch = bg_augmenter.get_batch() if batch is None: print("Finished epoch.") break x_images_aug = batch.images_aug y_images = batch.data loss, accu, _, _ = sess.run( [ obj, model.accu_dict[cur_task], train_op, model.centers_update_op ], feed_dict={ model.x: x_images_aug, model.y: y_images, model.is_train: True, model.bn_train: False }) train_loss += loss train_accu += accu batch_loader.terminate() bg_augmenter.terminate() else: for i in range(int(Xtrain.shape[0] / batch_size)): print("Training: {0}/{1}".format(i, ptrain_max), end='\r') st = i * batch_size ed = (i + 1) * batch_size loss, accu, _, _ = sess.run( [ obj, model.accu_dict[tasks[0]], train_op, model.centers_update_op ], feed_dict={ model.x: Xtrain[st:ed, :], model.y: Ytrain[st:ed, :], model.is_train: True, model.bn_train: False }) train_loss += loss train_accu += accu train_loss = train_loss / ptrain_max train_accu = train_accu / ptrain_max # validation val_loss, val_accu1, val_accu2 = 0.0, 0.0, 0.0 val_accu_dp = list() for i in range(int(Xvalid.shape[0] / batch_size)): print("Validating: {0}/{1}".format(i, pval_max), end='\r') st = i * batch_size ed = (i + 1) * batch_size loss, accu1, accu2, accu_dp = sess.run( [ obj, model.accu_dict[tasks[0]], model.accu_dict[tasks[-1]], tracking ], feed_dict={ model.x: Xvalid[st:ed, :], model.y: Yvalid[st:ed, :], model.is_train: False, model.bn_train: False }) val_loss += loss val_accu1 += accu1 val_accu2 += accu2 val_accu_dp.append(accu_dp) val_accu_dp = np.mean(val_accu_dp, axis=0).tolist() dp_str = "" for i in range(len(tasks)): dp_str += "{0}%:{1}, ".format(tasks[i], np.round(val_accu_dp[i], 4)) print(dp_str) val_loss = val_loss / pval_max val_accu1 = val_accu1 / pval_max val_accu2 = val_accu2 / pval_max val_accu = val_accu1 # used for early stopping # early stopping check if (val_accu - current_best_val_accu) > min_delta: current_best_val_accu = val_accu patience_counter = 0 para_dict = sess.run(model.para_dict) np.save(os.path.join(FLAG.save_dir, "para_dict.npy"), para_dict) print("save in %s" % os.path.join(FLAG.save_dir, "para_dict.npy")) else: patience_counter += 1 # shuffle Xtrain and Ytrain in the next epoch idx = np.random.permutation(Xtrain.shape[0]) Xtrain, Ytrain = Xtrain[idx, :, :, :], Ytrain[idx, :] # epoch end epoch_counter += 1 print( "Epoch %s (%s), %s sec >> train loss: %.4f, train accu: %.4f, val loss: %.4f, val accu at %s: %.4f, val accu at %s: %.4f" % (epoch_counter, patience_counter, round(time.time() - stime, 2), train_loss, train_accu, val_loss, tasks[0], val_accu1, tasks[-1], val_accu2)) history.append([train_loss, train_accu, val_loss, val_accu]) if epoch_counter % 10 == 0: import matplotlib.pyplot as plt df = pd.DataFrame(history) df.columns = [ 'train_loss', 'train_accu', 'val_loss', 'val_accu' ] df[['train_loss', 'val_loss']].plot() plt.savefig(os.path.join(FLAG.save_dir, 'loss.png')) plt.close() df[['train_accu', 'val_accu']].plot() plt.savefig(os.path.join(FLAG.save_dir, 'accu.png')) plt.close() saver.save(sess, checkpoint_path, global_step=epoch_counter) # extract features and calculate center output = [] for i in range(int(Xtrain.shape[0] / 200 + 1)): print(i, end="\r") st = i * 200 ed = min((i + 1) * 200, Xtrain.shape[0]) prob = sess.run(model.features, feed_dict={ model.x: Xtrain[st:ed, :], model.is_train: False, model.bn_train: False }) output.append(prob) for i in range(int(Xvalid.shape[0] / 200 + 1)): print(i, end="\r") st = i * 200 ed = min((i + 1) * 200, Xvalid.shape[0]) prob = sess.run(model.features, feed_dict={ model.x: Xvalid[st:ed, :], model.is_train: False, model.bn_train: False }) output.append(prob) EX = np.concatenate(output) print(EX.shape) EY = np.concatenate([ytrain, yvalid]) print(EY.shape) centers = np.zeros((len(y_dict), EX.shape[1])) for i in range(len(y_dict)): centers[i, :] = np.mean(EX[EY == i, :], axis=0) np.save(arr=centers, file=os.path.join(FLAG.save_dir, "centers.npy"))
def main(): augseq = iaa.Sequential( [iaa.Fliplr(0.5), iaa.CoarseDropout(p=0.1, size_percent=0.1)]) def func_images(images, random_state, parents, hooks): time.sleep(0.2) return images def func_heatmaps(heatmaps, random_state, parents, hooks): return heatmaps def func_keypoints(keypoints_on_images, random_state, parents, hooks): return keypoints_on_images augseq_slow = iaa.Sequential([ iaa.Fliplr(0.5), iaa.Lambda(func_images=func_images, func_heatmaps=func_heatmaps, func_keypoints=func_keypoints) ]) print("------------------") print("augseq.augment_batches(batches, background=True)") print("------------------") batches = list(load_images()) batches_aug = augseq.augment_batches(batches, background=True) images_aug = [] keypoints_aug = [] for batch_aug in batches_aug: images_aug.append(batch_aug.images_aug) keypoints_aug.append(batch_aug.keypoints_aug) ia.imshow(draw_grid(images_aug, keypoints_aug)) print("------------------") print("augseq.augment_batches(batches, background=True) -> only images") print("------------------") batches = list(load_images()) batches = [batch.images for batch in batches] batches_aug = augseq.augment_batches(batches, background=True) images_aug = [] keypoints_aug = None for batch_aug in batches_aug: images_aug.append(batch_aug) ia.imshow(draw_grid(images_aug, keypoints_aug)) print("------------------") print("BackgroundAugmenter") print("------------------") batch_loader = ia.BatchLoader(load_images) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq) images_aug = [] keypoints_aug = [] while True: print("Next batch...") batch = bg_augmenter.get_batch() if batch is None: print("Finished.") break images_aug.append(batch.images_aug) keypoints_aug.append(batch.keypoints_aug) ia.imshow(draw_grid(images_aug, keypoints_aug)) print("------------------") print("BackgroundAugmenter with generator in BL") print("------------------") batch_loader = ia.BatchLoader(load_images()) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq) images_aug = [] keypoints_aug = [] while True: print("Next batch...") batch = bg_augmenter.get_batch() if batch is None: print("Finished.") break images_aug.append(batch.images_aug) keypoints_aug.append(batch.keypoints_aug) ia.imshow(draw_grid(images_aug, keypoints_aug)) print("------------------") print("Long running BackgroundAugmenter at BL-queue_size=12") print("------------------") batch_loader = ia.BatchLoader(load_images(n_batches=1000), queue_size=12) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq) i = 0 while True: if i % 100 == 0: print("batch=%d..." % (i, )) batch = bg_augmenter.get_batch() if batch is None: print("Finished.") break i += 1 print("------------------") print("Long running BackgroundAugmenter at BL-queue_size=2") print("------------------") batch_loader = ia.BatchLoader(load_images(n_batches=1000), queue_size=2) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq) i = 0 while True: if i % 100 == 0: print("batch=%d..." % (i, )) batch = bg_augmenter.get_batch() if batch is None: print("Finished.") break i += 1 print("------------------") print("Long running BackgroundAugmenter (slow loading)") print("------------------") batch_loader = ia.BatchLoader(load_images(n_batches=100, sleep=0.2)) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq) i = 0 while True: if i % 10 == 0: print("batch=%d..." % (i, )) batch = bg_augmenter.get_batch() if batch is None: print("Finished.") break i += 1 print("------------------") print("Long running BackgroundAugmenter (slow aug) at BL-queue_size=12") print("------------------") batch_loader = ia.BatchLoader(load_images(n_batches=100), queue_size=12) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq_slow) i = 0 while True: if i % 10 == 0: print("batch=%d..." % (i, )) batch = bg_augmenter.get_batch() if batch is None: print("Finished.") break i += 1 print("------------------") print("Long running BackgroundAugmenter (slow aug) at BL-queue_size=2") print("------------------") batch_loader = ia.BatchLoader(load_images(n_batches=100), queue_size=2) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq_slow) i = 0 while True: if i % 10 == 0: print("batch=%d..." % (i, )) batch = bg_augmenter.get_batch() if batch is None: print("Finished.") break i += 1
def train(FLAG): print("Reading dataset...") if FLAG.dataset == 'CIFAR-10': train_data = CIFAR10(train=True) test_data = CIFAR10(train=False) vgg16 = VGG16(classes=10) elif FLAG.dataset == 'CIFAR-100': train_data = CIFAR100(train=True) test_data = CIFAR100(train=False) vgg16 = VGG16(classes=100) else: raise ValueError("dataset should be either CIFAR-10 or CIFAR-100.") print("Build VGG16 models for %s..." % FLAG.dataset) Xtrain, Ytrain = train_data.train_data, train_data.train_labels Xtest, Ytest = test_data.test_data, test_data.test_labels vgg16.build(vgg16_npy_path=FLAG.init_from, prof_type=FLAG.prof_type, conv_pre_training=True, fc_pre_training=False) vgg16.sparsity_train(l1_gamma=FLAG.lambda_s, l1_gamma_diff=FLAG.lambda_m, decay=FLAG.decay, keep_prob=FLAG.keep_prob) # define tasks tasks = ['var_dp'] print(tasks) # initial task cur_task = tasks[0] obj = vgg16.loss_dict[tasks[0]] saver = tf.train.Saver(tf.global_variables(), max_to_keep=len(tasks)) checkpoint_path = os.path.join(FLAG.save_dir, 'model.ckpt') tvars_trainable = tf.trainable_variables() #for rm in vgg16.gamma_var: # tvars_trainable.remove(rm) # print('%s is not trainable.'% rm) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) # hyper parameters batch_size = 64 epoch = 500 early_stop_patience = 50 min_delta = 0.0001 opt_type = 'adam' # recorder epoch_counter = 0 # optimizer global_step = tf.Variable(0, trainable=False) # Passing global_step to minimize() will increment it at each step. if opt_type is 'sgd': start_learning_rate = 1e-4 # adam # 4e-3 #sgd half_cycle = 20000 learning_rate = tf.train.exponential_decay(start_learning_rate, global_step, half_cycle, 0.5, staircase=True) opt = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=0.9, use_nesterov=True) else: start_learning_rate = 1e-4 # adam # 4e-3 #sgd half_cycle = 10000 learning_rate = tf.train.exponential_decay(start_learning_rate, global_step, half_cycle, 0.5, staircase=True) opt = tf.train.AdamOptimizer(learning_rate=learning_rate) train_op = opt.minimize(obj, global_step=global_step, var_list=tvars_trainable) # progress bar ptrain = IntProgress() pval = IntProgress() display(ptrain) display(pval) ptrain.max = int(Xtrain.shape[0] / batch_size) pval.max = int(Xtest.shape[0] / batch_size) spareness = vgg16.spareness(thresh=0.05) print("initial spareness: %s" % sess.run(spareness)) # re-initialize initialize_uninitialized(sess) # reset due to adding a new task patience_counter = 0 current_best_val_accu = 0 # optimize when the aggregated obj while (patience_counter < early_stop_patience and epoch_counter < epoch): def load_batches(): for i in range(int(Xtrain.shape[0] / batch_size)): st = i * batch_size ed = (i + 1) * batch_size batch = ia.Batch(images=Xtrain[st:ed, :, :, :], data=Ytrain[st:ed, :]) yield batch batch_loader = ia.BatchLoader(load_batches) bg_augmenter = ia.BackgroundAugmenter(batch_loader=batch_loader, augseq=transform, nb_workers=4) # start training stime = time.time() bar_train = Bar( 'Training', max=int(Xtrain.shape[0] / batch_size), suffix='%(index)d/%(max)d - %(percent).1f%% - %(eta)ds') bar_val = Bar( 'Validation', max=int(Xtest.shape[0] / batch_size), suffix='%(index)d/%(max)d - %(percent).1f%% - %(eta)ds') train_loss, train_accu = 0.0, 0.0 while True: batch = bg_augmenter.get_batch() if batch is None: print("Finished epoch.") break x_images_aug = batch.images_aug y_images = batch.data loss, accu, _ = sess.run( [obj, vgg16.accu_dict[cur_task], train_op], feed_dict={ vgg16.x: x_images_aug, vgg16.y: y_images, vgg16.is_train: True }) bar_train.next() train_loss += loss train_accu += accu ptrain.value += 1 ptrain.description = "Training %s/%s" % (ptrain.value, ptrain.max) train_loss = train_loss / ptrain.value train_accu = train_accu / ptrain.value batch_loader.terminate() bg_augmenter.terminate() # # training an epoch # for i in range(int(Xtrain.shape[0]/batch_size)): # st = i*batch_size # ed = (i+1)*batch_size # augX = transform.augment_images(Xtrain[st:ed,:,:,:]) # sess.run([train_op], feed_dict={vgg16.x: augX, # vgg16.y: Ytrain[st:ed,:], # vgg16.is_train: False}) # ptrain.value +=1 # ptrain.description = "Training %s/%s" % (i, ptrain.max) # bar_train.next() # validation val_loss = 0 val_accu = 0 for i in range(int(Xtest.shape[0] / 200)): st = i * 200 ed = (i + 1) * 200 loss, accu = sess.run( [obj, vgg16.accu_dict[cur_task]], feed_dict={ vgg16.x: Xtest[st:ed, :], vgg16.y: Ytest[st:ed, :], vgg16.is_train: False }) val_loss += loss val_accu += accu pval.value += 1 pval.description = "Testing %s/%s" % (pval.value, pval.value) val_loss = val_loss / pval.value val_accu = val_accu / pval.value print("\nspareness: %s" % sess.run(spareness)) # early stopping check if (val_accu - current_best_val_accu) > min_delta: current_best_val_accu = val_accu patience_counter = 0 para_dict = sess.run(vgg16.para_dict) np.save(os.path.join(FLAG.save_dir, "para_dict.npy"), para_dict) print("save in %s" % os.path.join(FLAG.save_dir, "para_dict.npy")) else: patience_counter += 1 # shuffle Xtrain and Ytrain in the next epoch idx = np.random.permutation(Xtrain.shape[0]) Xtrain, Ytrain = Xtrain[idx, :, :, :], Ytrain[idx, :] # epoch end # writer.add_summary(epoch_summary, epoch_counter) epoch_counter += 1 ptrain.value = 0 pval.value = 0 bar_train.finish() bar_val.finish() print( "Epoch %s (%s), %s sec >> train loss: %.4f, train accu: %.4f, val loss: %.4f, val accu at %s: %.4f" % (epoch_counter, patience_counter, round(time.time() - stime, 2), train_loss, train_accu, val_loss, cur_task, val_accu)) saver.save(sess, checkpoint_path, global_step=epoch_counter) sp, rcut = gammaSparsifyVGG16(para_dict, thresh=0.02) np.save(os.path.join(FLAG.save_dir, "sparse_dict.npy"), sp) print("sparsify %s in %s" % (np.round( 1 - rcut, 3), os.path.join(FLAG.save_dir, "sparse_dict.npy"))) #writer.close() arr_spareness.append(1 - rcut) np.save(os.path.join(FLAG.save_dir, "sprocess.npy"), arr_spareness) FLAG.optimizer = opt_type FLAG.lr = start_learning_rate FLAG.batch_size = batch_size FLAG.epoch_end = epoch_counter FLAG.val_accu = current_best_val_accu header = '' row = '' for key in sorted(vars(FLAG)): if header is '': header = key row = str(getattr(FLAG, key)) else: header += "," + key row += "," + str(getattr(FLAG, key)) row += "\n" header += "\n" if os.path.exists("/home/cmchang/new_CP_CNN/model.csv"): with open("/home/cmchang/new_CP_CNN/model.csv", "a") as myfile: myfile.write(row) else: with open("/home/cmchang/new_CP_CNN/model.csv", "w") as myfile: myfile.write(header) myfile.write(row)
def example_background_classes(): print("Example: Background Augmentation via Classes") import imgaug as ia from imgaug import augmenters as iaa import numpy as np from skimage import data # Example augmentation sequence to run in the background. augseq = iaa.Sequential( [iaa.Fliplr(0.5), iaa.CoarseDropout(p=0.1, size_percent=0.1)]) # A generator that loads batches from the hard drive. def load_batches(): # Here, load 10 batches of size 4 each. # You can also load an infinite amount of batches, if you don't train # in epochs. batch_size = 4 nb_batches = 10 # Here, for simplicity we just always use the same image. astronaut = data.astronaut() astronaut = ia.imresize_single_image(astronaut, (64, 64)) for i in range(nb_batches): # A list containing all images of the batch. batch_images = [] # A list containing IDs per image. This is not necessary for the # background augmentation and here just used to showcase that you # can transfer additional information. batch_data = [] # Add some images to the batch. for b in range(batch_size): batch_images.append(astronaut) batch_data.append((i, b)) # Create the batch object to send to the background processes. batch = ia.Batch(images=np.array(batch_images, dtype=np.uint8), data=batch_data) yield batch # background augmentation consists of two components: # (1) BatchLoader, which runs in a Thread and calls repeatedly a user-defined # function (here: load_batches) to load batches (optionally with keypoints # and additional information) and sends them to a queue of batches. # (2) BackgroundAugmenter, which runs several background processes (on other # CPU cores). Each process takes batches from the queue defined by (1), # augments images/keypoints and sends them to another queue. # The main process can then read augmented batches from the queue defined # by (2). batch_loader = ia.BatchLoader(load_batches) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq) # Run until load_batches() returns nothing anymore. This also allows infinite # training. while True: print("Next batch...") batch = bg_augmenter.get_batch() if batch is None: print("Finished epoch.") break images_aug = batch.images_aug print("Image IDs: ", batch.data) ia.imshow(np.hstack(list(images_aug))) batch_loader.terminate() bg_augmenter.terminate()
def _augment_small_4(): batch_loader = ia.BatchLoader(load_images(n_batches=10), queue_size=100) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq_i) batch = bg_augmenter.get_batch()
def main(): augseq = iaa.Sequential( [iaa.Fliplr(0.5), iaa.CoarseDropout(p=0.1, size_percent=0.1)]) def func_images(images, random_state, parents, hooks): time.sleep(0.2) return images def func_heatmaps(heatmaps, random_state, parents, hooks): return heatmaps def func_keypoints(keypoints_on_images, random_state, parents, hooks): return keypoints_on_images augseq_slow = iaa.Sequential([ iaa.Fliplr(0.5), iaa.Lambda(func_images=func_images, func_heatmaps=func_heatmaps, func_keypoints=func_keypoints) ]) print("------------------") print("augseq.augment_batches(batches, background=True)") print("------------------") batches = list(load_images()) batches_aug = augseq.augment_batches(batches, background=True) images_aug = [] keypoints_aug = [] for batch_aug in batches_aug: images_aug.append(batch_aug.images_aug) keypoints_aug.append(batch_aug.keypoints_aug) ia.imshow(draw_grid(images_aug, keypoints_aug)) print("------------------") print("augseq.augment_batches(batches, background=True) -> only images") print("------------------") batches = list(load_images()) batches = [batch.images for batch in batches] batches_aug = augseq.augment_batches(batches, background=True) images_aug = [] keypoints_aug = None for batch_aug in batches_aug: images_aug.append(batch_aug) ia.imshow(draw_grid(images_aug, keypoints_aug)) print("------------------") print("BackgroundAugmenter") print("------------------") batch_loader = ia.BatchLoader(load_images) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq) images_aug = [] keypoints_aug = [] while True: print("Next batch...") batch = bg_augmenter.get_batch() if batch is None: print("Finished.") break images_aug.append(batch.images_aug) keypoints_aug.append(batch.keypoints_aug) ia.imshow(draw_grid(images_aug, keypoints_aug)) print("------------------") print("BackgroundAugmenter with generator in BL") print("------------------") batch_loader = ia.BatchLoader(load_images()) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq) images_aug = [] keypoints_aug = [] while True: print("Next batch...") batch = bg_augmenter.get_batch() if batch is None: print("Finished.") break images_aug.append(batch.images_aug) keypoints_aug.append(batch.keypoints_aug) ia.imshow(draw_grid(images_aug, keypoints_aug)) print("------------------") print("Long running BackgroundAugmenter at BL-queue_size=12") print("------------------") batch_loader = ia.BatchLoader(load_images(n_batches=1000), queue_size=12) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq) i = 0 while True: if i % 100 == 0: print("batch=%d..." % (i, )) batch = bg_augmenter.get_batch() if batch is None: print("Finished.") break i += 1 print("------------------") print("Long running BackgroundAugmenter at BL-queue_size=2") print("------------------") batch_loader = ia.BatchLoader(load_images(n_batches=1000), queue_size=2) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq) i = 0 while True: if i % 100 == 0: print("batch=%d..." % (i, )) batch = bg_augmenter.get_batch() if batch is None: print("Finished.") break i += 1 print("------------------") print("Long running BackgroundAugmenter (slow loading)") print("------------------") batch_loader = ia.BatchLoader(load_images(n_batches=100, sleep=0.2)) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq) i = 0 while True: if i % 10 == 0: print("batch=%d..." % (i, )) batch = bg_augmenter.get_batch() if batch is None: print("Finished.") break i += 1 print("------------------") print("Long running BackgroundAugmenter (slow aug) at BL-queue_size=12") print("------------------") batch_loader = ia.BatchLoader(load_images(n_batches=100), queue_size=12) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq_slow) i = 0 while True: if i % 10 == 0: print("batch=%d..." % (i, )) batch = bg_augmenter.get_batch() if batch is None: print("Finished.") break i += 1 print("------------------") print("Long running BackgroundAugmenter (slow aug) at BL-queue_size=2") print("------------------") batch_loader = ia.BatchLoader(load_images(n_batches=100), queue_size=2) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq_slow) i = 0 while True: if i % 10 == 0: print("batch=%d..." % (i, )) batch = bg_augmenter.get_batch() if batch is None: print("Finished.") break i += 1 for augseq_i in [augseq, augseq_slow]: print("------------------") print("Many very small runs (batches=1)") print("------------------") for i in range(100): batch_loader = ia.BatchLoader(load_images(n_batches=1), queue_size=100) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq_i) while True: batch = bg_augmenter.get_batch() if batch is None: print("Finished (%d/%d)." % (i + 1, 100)) break print("------------------") print("Many very small runs (batches=2)") print("------------------") for i in range(100): batch_loader = ia.BatchLoader(load_images(n_batches=2), queue_size=100) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq_i) while True: batch = bg_augmenter.get_batch() if batch is None: print("Finished (%d/%d)." % (i + 1, 100)) break print("------------------") print("Many very small runs, separate function (batches=1)") print("------------------") def _augment_small_1(): batch_loader = ia.BatchLoader(load_images(n_batches=1), queue_size=100) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq_i) i = 0 while True: batch = bg_augmenter.get_batch() if batch is None: break i += 1 for i in range(100): _augment_small_1() print("Finished (%d/%d)." % (i + 1, 100)) print("------------------") print("Many very small runs, separate function (batches=2)") print("------------------") def _augment_small_2(): batch_loader = ia.BatchLoader(load_images(n_batches=2), queue_size=100) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq_i) i = 0 while True: batch = bg_augmenter.get_batch() if batch is None: break i += 1 for i in range(100): _augment_small_2() print("Finished (%d/%d)." % (i + 1, 100)) print("------------------") print( "Many very small runs, separate function, incomplete fetching (batches=2)" ) print("------------------") def _augment_small_3(): batch_loader = ia.BatchLoader(load_images(n_batches=2), queue_size=100) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq_i) batch = bg_augmenter.get_batch() for i in range(100): _augment_small_3() print("Finished (%d/%d)." % (i + 1, 100)) #for augseq_i in [augseq, augseq_slow]: print("------------------") print( "Many very small runs, separate function, incomplete fetching (batches=10)" ) print("------------------") def _augment_small_4(): batch_loader = ia.BatchLoader(load_images(n_batches=10), queue_size=100) bg_augmenter = ia.BackgroundAugmenter(batch_loader, augseq_i) batch = bg_augmenter.get_batch() #bg_augmenter.terminate() for i in range(100): _augment_small_4() print("Finished (%d/%d)." % (i + 1, 100))