def main(): parser = argparse.ArgumentParser( description="Trim Detection weights and save in PyTorch format.") parser.add_argument( "--input_dir", default="", help="Input directory which contains images and annotations", type=str, ) parser.add_argument( "--output_dir", default="", help= "Output directory which contains images and annotations after data augmentation", type=str, ) args = parser.parse_args() # create the augmentor object PROBLEM = "instance_segmentation" ANNOTATION_MODE = "coco" INPUT_PATH = args.input_dir GENERATION_MODE = "linear" OUTPUT_MODE = "coco" OUTPUT_PATH = args.output_dir + '/' augmentor = createAugmentor(PROBLEM, ANNOTATION_MODE, OUTPUT_MODE, GENERATION_MODE, INPUT_PATH, {"outputPath": OUTPUT_PATH}) # add the augmentation techniques transformer = transformerGenerator(PROBLEM) for angle in [0, 90, 180, 270]: rotate = createTechnique("rotate", {"angle": angle}) augmentor.addTransformer(transformer(rotate)) flip = createTechnique("flip", {"flip": 1}) augmentor.addTransformer(transformer(flip)) crop = createTechnique("crop", {"percentage": 0.7, "startFrom": "TOPLEFT"}) augmentor.addTransformer(transformer(crop)) crop = createTechnique("crop", { "percentage": 0.7, "startFrom": "BOTTOMRIGHT" }) augmentor.addTransformer(transformer(crop)) # apply the augmentation process augmentor.applyAugmentation() print('Done!')
def augment_data(train_data_text_file, augmented_path): #1. copy the images from training dataset #copy_for_augment(train_data_text_file, raw_path, augmented_path, last_frame) #2. initliaze augmentor augmentor, transformer = init_augment(augmented_path) vFlip = createTechnique("flip", {"flip": 0}) augmentor.addTransformer(transformer(vFlip)) hFlip = createTechnique("flip", {"flip": 1}) augmentor.addTransformer(transformer(hFlip)) hvFlip = createTechnique("flip", {"flip": -1}) augmentor.addTransformer(transformer(hvFlip)) rotate = createTechnique("rotate", {"angle": 90}) augmentor.addTransformer(transformer(rotate)) avgBlur = createTechnique("average_blurring", {"kernel": 5}) augmentor.addTransformer(transformer(avgBlur)) hue = createTechnique("raise_hue", {"power": 0.9}) augmentor.addTransformer(transformer(hue)) total_frames = constants.TOTAL_FRAMES frames = [] for fname in os.listdir(augmented_path): if fname.endswith(".jpg"): frames.append(fname) for fr in frames: img, boxes = load_boxes(augmented_path + "/" + fr, augmented_path + "/" + fr[:-3] + "txt") ver_img, ver_boxes = aug_operation(vFlip, transformer, img, boxes) save_augmentation(ver_img, ver_boxes, augmented_path, total_frames) total_frames += 1 hor_img, hor_boxes = aug_operation(hFlip, transformer, img, boxes) save_augmentation(hor_img, hor_boxes, augmented_path, total_frames) total_frames += 1 blur_img, blur_boxes = aug_operation(avgBlur, transformer, img, boxes) save_augmentation(blur_img, blur_boxes, augmented_path, total_frames) total_frames += 1 hv_img, hv_boxes = aug_operation(hvFlip, transformer, img, boxes) save_augmentation(hv_img, hv_boxes, augmented_path, total_frames) total_frames += 1 hue_img, hue_boxes = aug_operation(hue, transformer, img, boxes) save_augmentation(hue_img, hue_boxes, augmented_path, total_frames) total_frames += 1
def apply_eqhisto(input_path, output_path): PROBLEM = "semantic_segmentation" ANNOTATION_MODE = "folders" INPUT_PATH = input_path + "/" GENERATION_MODE = "linear" OUTPUT_MODE = "folders" OUTPUT_PATH= output_path + "/" LABELS_EXTENSION = ".png" augmentor = createAugmentor(PROBLEM,ANNOTATION_MODE,OUTPUT_MODE,GENERATION_MODE,INPUT_PATH,{"outputPath":OUTPUT_PATH,"labelsExtension":LABELS_EXTENSION}) transformer = transformerGenerator(PROBLEM) equalize = createTechnique("equalize_histogram",{}) augmentor.addTransformer(transformer(equalize)) augmentor.applyAugmentation() print("Equalize histogram results were saved given directory.")
def apply_shearing(input_path, output_path, a = 0.5): PROBLEM = "semantic_segmentation" ANNOTATION_MODE = "folders" INPUT_PATH = input_path + "/" GENERATION_MODE = "linear" OUTPUT_MODE = "folders" OUTPUT_PATH= output_path + "/" LABELS_EXTENSION = ".png" augmentor = createAugmentor(PROBLEM,ANNOTATION_MODE,OUTPUT_MODE,GENERATION_MODE,INPUT_PATH,{"outputPath":OUTPUT_PATH,"labelsExtension":LABELS_EXTENSION}) transformer = transformerGenerator(PROBLEM) rotate = createTechnique("shearing", {"a" : a}) augmentor.addTransformer(transformer(rotate)) augmentor.applyAugmentation() print("Shearing results were saved given directory.")
def aug_blurring(input_path, output_path, blurr:list): PROBLEM = "semantic_segmentation" ANNOTATION_MODE = "folders" INPUT_PATH = input_path + "/" GENERATION_MODE = "linear" OUTPUT_MODE = "folders" OUTPUT_PATH= output_path + "/" LABELS_EXTENSION = ".png" augmentor = createAugmentor(PROBLEM,ANNOTATION_MODE,OUTPUT_MODE,GENERATION_MODE,INPUT_PATH,{"outputPath":OUTPUT_PATH,"labelsExtension":LABELS_EXTENSION}) transformer = transformerGenerator(PROBLEM) for ker in blurr: blur = createTechnique("blurring", {"kernel" : ker}) augmentor.addTransformer(transformer(blur)) print("Blurring for kernel = {} is done".format(ker)) augmentor.applyAugmentation() print("Augmentation results were saved given directory.")
def apply_white_noise(input_path, output_path, sd): PROBLEM = "semantic_segmentation" ANNOTATION_MODE = "folders" INPUT_PATH = input_path + "/" GENERATION_MODE = "linear" OUTPUT_MODE = "folders" OUTPUT_PATH= output_path + "/" LABELS_EXTENSION = ".png" augmentor = createAugmentor(PROBLEM,ANNOTATION_MODE,OUTPUT_MODE,GENERATION_MODE,INPUT_PATH,{"outputPath":OUTPUT_PATH,"labelsExtension":LABELS_EXTENSION}) transformer = transformerGenerator(PROBLEM) for sigma in sd: white_noise = createTechnique("gaussian_noise", {"mean" : 0,"sigma":sigma}) augmentor.addTransformer(transformer(white_noise)) augmentor.applyAugmentation() print("White noise results were saved given directory.")
def apply_gamma_correction(input_path, output_path, gammas): PROBLEM = "semantic_segmentation" ANNOTATION_MODE = "folders" INPUT_PATH = input_path + "/" GENERATION_MODE = "linear" OUTPUT_MODE = "folders" OUTPUT_PATH= output_path + "/" LABELS_EXTENSION = ".png" augmentor = createAugmentor(PROBLEM,ANNOTATION_MODE,OUTPUT_MODE,GENERATION_MODE,INPUT_PATH,{"outputPath":OUTPUT_PATH,"labelsExtension":LABELS_EXTENSION}) transformer = transformerGenerator(PROBLEM) for gamma in gammas: gamma_t = createTechnique("gamma", {"gamma" : gamma}) augmentor.addTransformer(transformer(gamma_t)) augmentor.applyAugmentation() print("Gamma correction results were saved given directory.")
def apply_dropout(input_path, output_path, percentages): PROBLEM = "semantic_segmentation" ANNOTATION_MODE = "folders" INPUT_PATH = input_path + "/" GENERATION_MODE = "linear" OUTPUT_MODE = "folders" OUTPUT_PATH= output_path + "/" LABELS_EXTENSION = ".png" augmentor = createAugmentor(PROBLEM,ANNOTATION_MODE,OUTPUT_MODE,GENERATION_MODE,INPUT_PATH,{"outputPath":OUTPUT_PATH,"labelsExtension":LABELS_EXTENSION}) transformer = transformerGenerator(PROBLEM) for percentage in percentages: dropout = createTechnique("dropout", {"percentage" : percentage}) augmentor.addTransformer(transformer(dropout)) augmentor.applyAugmentation() print("Dropout results were saved given directory.")
img = cv2.imread(image_path) h, w = img.shape[:2] lines = [line.strip('\n') for line in open(label_path)] boxes = [] if lines != ['']: for line in lines: words = line.split(' ') id = words[0] x = int(float(words[1]) * w - float(words[3]) * w / 2) y = int(float(words[2]) * h - float(words[4]) * h / 2) he = int(float(words[4]) * h) wi = int(float(words[3]) * w) boxes.append((id, (x, y, wi, he))) return img, boxes augmentor = createAugmentor('detection', 'yolo', 'yolo', 'linear', 'dataset', {'outputPath': 'dataset/augmented'}) transformer = transformerGenerator('detection') v_flip = createTechnique('flip', {'flip': 0}) h_flip = createTechnique('flip', {'flip': 1}) hv_flip = createTechnique('flip', {'flip': -1}) avg_blur_3 = createTechnique('average_blurring', {'kernel': 3}) none = createTechnique('none', {}) augmentor.addTransformer(transformer(v_flip)) augmentor.addTransformer(transformer(h_flip)) augmentor.addTransformer(transformer(hv_flip)) augmentor.addTransformer(transformer(avg_blur_3)) augmentor.addTransformer(transformer(none)) augmentor.applyAugmentation()
# from keras.optimizers import SGD from clodsa.augmentors.augmentorFactory import createAugmentor from clodsa.transformers.transformerFactory import transformerGenerator from clodsa.techniques.techniqueFactory import createTechnique from clodsa.utils.minivgg import MiniVGGNet import cv2 import parseConfig ### config = parseConfig.parseConfig("config_augmentImages.json") augmentor = createAugmentor(config["problem"], config["annotationMode"], config["outputMode"], config["generationMode"], config["inputPath"], {"outputPath": config["outputPath"]}) transformer = transformerGenerator(config["problem"]) ### # Load the techniques and add them to the augmentor techniques = [ createTechnique(technique, param) for (technique, param) in config["augmentationTechniques"] ] i = 0 for technique in techniques: augmentor.addTransformer(transformer(technique)) i = i + 1 agm = augmentor.applyAugmentation()
from clodsa.augmentors.augmentorFactory import createAugmentor from clodsa.transformers.transformerFactory import transformerGenerator from clodsa.techniques.techniqueFactory import createTechnique from clodsa.utils.minivgg import MiniVGGNet import cv2 import parseConfig config = parseConfig.parseConfig("cats_dogs_folder_folder_linear.json") augmentor = createAugmentor(config["problem"],config["annotationMode"],config["outputMode"],config["generationMode"],config["inputPath"], {"outputPath":config["outputPath"]}) transformer = transformerGenerator(config["problem"]) # Load the techniques and add them to the augmentor techniques = [createTechnique(technique,param) for (technique,param) in config["augmentationTechniques"]] print("Number of images in input folder") !ls /home/alex/anaconda3/envs/DD2424-project/DD2424---Project-Covid-19/git/datasets/images | wc -l img = {} img["original"] = cv2.imread(config["inputPath"] + "images/cat_1.jpg") i = 0 plt.figure("original") # changing to the BGR format of OpenCV to RGB format for matplotlib plt.imshow(img["original"][:,:,::-1]) for technique in techniques: augmentor.addTransformer(transformer(technique)) img[config["augmentationTechniques"][i][0]] = technique.apply(img["original"])
PROBLEM = "instance_segmentation" ANNOTATION_MODE = "coco" INPUT_PATH = "/Users/sunbowen/Desktop/shellfish_detection/data/val_img" GENERATION_MODE = "linear" OUTPUT_MODE = "coco" OUTPUT_PATH = "/Users/sunbowen/Desktop/shellfish_detection/data/output/" augmentor = createAugmentor(PROBLEM, ANNOTATION_MODE, OUTPUT_MODE, GENERATION_MODE, INPUT_PATH, {"outputPath": OUTPUT_PATH}) transformer = transformerGenerator(PROBLEM) # Rotations for angle in [0, 90, 180]: rotate = createTechnique("rotate", {"angle": angle}) augmentor.addTransformer(transformer(rotate)) # Flip flip = createTechnique("flip", {"flip": 1}) augmentor.addTransformer(transformer(flip)) # Implementation augmentor.applyAugmentation() # # image_directory = OUTPUT_PATH # annotation_file = OUTPUT_PATH + 'annotation.json' # # example_coco = COCO(annotation_file) # # categories = example_coco.loadCats(example_coco.getCatIds())
GENERATION_MODE = "sequential" OUTPUT_MODE = "coco" OUTPUT_PATH = "output/" augmentor = createAugmentor(PROBLEM, ANNOTATION_MODE, OUTPUT_MODE, GENERATION_MODE, INPUT_PATH, {"outputPath": OUTPUT_PATH}) transformer = transformerGenerator(PROBLEM) #for angle in [90,180]: def sample(): return (50, 50, 256, 256) t = createTechnique("patchify", {"patchify": sample}) #t = createTechnique("random_object_non_occlusion", # {"random_images_dir":"/home/fer/repos/CLoDSA/random_instances/", # "rate":0.5, # "resize_factor":0.4}) #augmentor.addTransformer(transformer(t)) #t = createTechnique("resize", {"x":20,"y":20}) #augmentor.addTransformer(transformer(t)) #rotate = createTechnique("random_rotate", {"range":[-90,90]}) #augmentor.addTransformer(transformer(rotate)) #flip0 = createTechnique("random_object_occlusion", # {"random_images_dir":"/home/fer/repos/CLoDSA/random_instances/color"})