def run_n_aug(x, y, n_aug, num_classes): # data augmentation # x = augmentation.horizontal_flip(x) # x = augmentation.vertical_flip(x) # x = augmentation.random_crop(x) # x = augmentation.random_transfer(x) # x = augmentation.random_rotation(x) # x, y = augmentation.mixup(image=x, label=y, num_classes=num_classes) # x = augmentation.cutout(x) # x = augmentation.random_erasing(x) # x, y = augmentation.ricap(image_batch=x, label_batch=y, num_classes=num_classes) if n_aug == 1: x = augmentation.horizontal_flip(x) # x = augmentation.vertical_flip(x) elif n_aug == 2: x = augmentation.random_crop(x) elif n_aug == 3: x = augmentation.random_transfer(x) elif n_aug == 4: x = augmentation.random_rotation(x) elif n_aug == 5: x, y = augmentation.mixup(image=x, label=y, num_classes=num_classes) elif n_aug == 6: x = augmentation.cutout(x) elif n_aug == 7: x = augmentation.random_erasing(x) elif n_aug == 8: x, y = augmentation.ricap(image_batch=x, label_batch=y, num_classes=num_classes) x = to_var(x) y = to_var(y) elif n_aug == 12: x = augmentation.horizontal_flip(x) x = augmentation.random_crop(x) elif n_aug == 17: x = augmentation.horizontal_flip(x) x = augmentation.random_erasing(x) elif n_aug == 34: x = augmentation.random_transfer(x) x = augmentation.random_rotation(x) return x, y
def augmentation(self, X, Y): print('Augmentation model...') total = len(X) x_train, y_train = [], [] for i in xrange(total): x, y = X[i], Y[i] #standart x_train.append(x) y_train.append(y) # for _ in xrange(1): # _x, _y = elastic_transform(x[0], y[0], 100, 20) # x_train.append(_x.reshape((1,) + _x.shape)) # y_train.append(_y.reshape((1,) + _y.shape)) #flip x x_train.append(flip_axis(x, 2)) y_train.append(flip_axis(y, 2)) #flip y x_train.append(flip_axis(x, 1)) y_train.append(flip_axis(y, 1)) #continue #zoom for _ in xrange(1): _x, _y = random_zoom(x, y, (0.9, 1.1)) x_train.append(_x) y_train.append(_y) for _ in xrange(0): _x, _y = random_rotation(x, y, 5) x_train.append(_x) y_train.append(_y) #intentsity for _ in xrange(1): _x = random_channel_shift(x, 5.0) x_train.append(_x) y_train.append(y) x_train = np.array(x_train) y_train = np.array(y_train) print('x_trian: ', x_train.shape) return x_train, y_train
def aug_train(split_dir, mode): train_dir = split_dir + os.sep + "train" for subdir, dirs, files in os.walk(train_dir): for f in files: file_path = subdir + os.sep + f if (is_image(f)): print(file_path) name, ext = os.path.splitext(f) img = cv2.imread(file_path) for i in range(1, 4): rot_dir = (subdir + os.sep + name + "_aug_" + str(i + 1) + ext) if (mode == 'random'): cv2.imwrite(rot_dir, aug.random_rotation(img)) elif (mode == 'strict'): cv2.imwrite(rot_dir, aug.strict_rotation(img, i)) else: print("The mode should be either random | strict") sys.exit(1)