seq = iaa.Sequential([ sometimes( iaa.CropAndPad( percent=(-0.05, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255))), iaa.SomeOf((0, 5), [ sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 7)), iaa.MedianBlur(k=(3, 11)), ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), iaa.SimplexNoiseAlpha( iaa.OneOf([ iaa.EdgeDetect(alpha=(0.5, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), iaa.OneOf([ iaa.Dropout((0.01, 0.1), per_channel=0.5), iaa.CoarseDropout( (0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2), ]), iaa.Add((-10, 10), per_channel=0.5), iaa.OneOf([ iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.FrequencyNoiseAlpha(exponent=(-4, 0), first=iaa.Multiply( (0.5, 1.5), per_channel=True), second=iaa.ContrastNormalization( (0.5, 2.0))) ]), iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), ], random_order=True) ], random_order=True)
def crop_seq(crop_size): seq = iaa.Sequential([fast_seq, RandomCropFixedSize(px=crop_size)], random_order=False) return seq
def load_images_from_folder(folder): images = [] for filename in os.listdir(folder): img = cv2.imread(os.path.join(folder, filename)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) if img is not None: images.append(img) return images if __name__ == '__main__': images = load_images_from_folder( "/home/yang/PycharmProjects/carND/video_iamge/") seq = iaa.Sequential( [ # Small gaussian blur with random sigma between 0 and 0.5. # But we only blur about 50% of all images. iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.5))), # Strengthen or weaken the contrast in each image. iaa.ContrastNormalization((0.75, 1.5)), # Make some images brighter and some darker. # In 20% of all cases, we sample the multiplier once per channel, # which can end up changing the color of the images. iaa.Multiply((0.8, 1.2), per_channel=0.2), ], random_order=True) # apply augmenters in random order images_aug = seq.augment_images(images) seq.show_grid(images[0], 4, 4)
class no:3 label 0:5893, label 0:65 ''' csv_train = Path(__file__).parent.parent.absolute().joinpath('datafiles', data_version, 'train.csv') csv_valid = Path(__file__).parent.parent.absolute().joinpath('datafiles', data_version, 'valid.csv') csv_test = Path(__file__).parent.parent.absolute().joinpath('datafiles', data_version, 'test.csv') iaa = iaa.Sequential([ # iaa.CropAndPad(percent=(-0.04, 0.04)), iaa.Fliplr(0.5), iaa.Flipud(0.25), iaa.GaussianBlur(sigma=(0.0, 0.3)), iaa.MultiplyBrightness(mul=(0.7, 1.3)), iaa.contrast.LinearContrast((0.7, 1.3)), iaa.Sometimes(0.9, iaa.Add((-8, 8))), iaa.Sometimes(0.9, iaa.Affine( scale=(0.98, 1.02), translate_percent={"x": (-0.06, 0.06), "y": (-0.06, 0.06)}, rotate=(-15, 15), )), ]) batch_size_train, batch_size_valid = 32, 64 num_workers = 4 # when debugging it should be set to 0. ''' do not do resampling in this project import pandas as pd import numpy as np
import os from PIL import Image from config import imshape, n_classes, labels, model_name import imgaug as ia from imgaug import augmenters as iaa import cv2 import json import tensorflow as tf ia.seed(1) seq = iaa.Sequential( [ iaa.Fliplr(0.5), iaa.Multiply((1.2, 1.5)), iaa.Affine( #scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, rotate=(-90, 90)), iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 8))) ], random_order=True) class DataGenerator(tf.keras.utils.Sequence): # Generates data for Keras def __init__(self, image_paths, annot_paths, batch_size=32, shuffle=True, augment=False): self.image_paths = image_paths
def render(self, visibility=0.5, aug=None, display=False, debug=False): """ Display the current state of the generator. :param visibility: portion of the card's image that must not be overlapped by other cards for the card to be considered as visible :param aug: image augmentator to apply during rendering :param display: flag for displaying the rendering result :param debug: flag for debug :return: none """ self.check_visibility(visibility=visibility) img_result = np.zeros((self.height, self.width, 3), dtype=np.uint8) card_mask = cv2.imread(Config.card_mask_path) for card in self.cards: card_x = int(card.x + 0.5) card_y = int(card.y + 0.5) # Scale & rotate card image img_card = cv2.resize(card.img, (int(len(card.img[0]) * card.scale), int(len(card.img) * card.scale))) # Add a random glaring on individual card - it happens frequently in real life as MTG cards can reflect # the lights very well. if aug is not None: seq = iaa.Sequential([ iaa.SimplexNoiseAlpha(first=iaa.Add(random.randrange(128)), size_px_max=[1, 3], upscale_method="cubic"), # Lighting ]) img_card = seq.augment_image(img_card) mask_scale = cv2.resize(card_mask, (int(len(card_mask[0]) * card.scale), int(len(card_mask) * card.scale))) img_mask = cv2.bitwise_and(img_card, mask_scale) img_rotate = imutils.rotate_bound(img_mask, card.theta / math.pi * 180) # Calculate the position of the card image in relation to the background # Crop the card image if it's out of boundary card_w = len(img_rotate[0]) card_h = len(img_rotate) card_crop_x1 = max(0, card_w // 2 - card_x) card_crop_x2 = min(card_w, card_w // 2 + len(img_result[0]) - card_x) card_crop_y1 = max(0, card_h // 2 - card_y) card_crop_y2 = min(card_h, card_h // 2 + len(img_result) - card_y) img_card_crop = img_rotate[card_crop_y1:card_crop_y2, card_crop_x1:card_crop_x2] # Calculate the position of the corresponding area in the background bg_crop_x1 = max(0, card_x - (card_w // 2)) bg_crop_x2 = min(len(img_result[0]), int(card_x + (card_w / 2) + 0.5)) bg_crop_y1 = max(0, card_y - (card_h // 2)) bg_crop_y2 = min(len(img_result), int(card_y + (card_h / 2) + 0.5)) img_result_crop = img_result[bg_crop_y1:bg_crop_y2, bg_crop_x1:bg_crop_x2] # Override the background with the current card img_result_crop = np.where(img_card_crop, img_card_crop, img_result_crop) img_result[bg_crop_y1:bg_crop_y2, bg_crop_x1:bg_crop_x2] = img_result_crop if debug: for ext_obj in card.objects: if ext_obj.visible: for pt in ext_obj.key_pts: cv2.circle( img_result, card.coordinate_in_generator(pt[0], pt[1]), 2, (1, 1, 255), 10) bounding_box = card.bb_in_generator(ext_obj.key_pts) cv2.rectangle(img_result, bounding_box[0], bounding_box[2], (1, 255, 1), 5) img_result = cv2.GaussianBlur(img_result, (5, 5), 0) # Skew the cards if it's provided if self.M is not None: img_result = cv2.warpPerspective(img_result, self.M, (self.width, self.height)) if debug: for card in self.cards: for ext_obj in card.objects: if ext_obj.visible: new_pts = np.array([[ list(card.coordinate_in_generator( pt[0], pt[1])) ] for pt in ext_obj.key_pts], dtype=np.float32) new_pts = cv2.perspectiveTransform(new_pts, self.M) for pt in new_pts: cv2.circle(img_result, (pt[0][0], pt[0][1]), 2, (255, 1, 1), 10) img_bg = cv2.resize(self.img_bg, (self.width, self.height)) img_result = np.where(img_result, img_result, img_bg) # Apply image augmentation if aug is not None: img_result = aug.augment_image(img_result) if display or debug: cv2.imshow('Result', img_result) cv2.waitKey(0) self.img_result = img_result pass
import numpy as np import re from imgaug import augmenters as iaa from skimage import io from scipy.misc import imsave seq = iaa.Sequential([ iaa.Crop( px=(0, 16)), # crop images from each side by 0 to 16px (randomly chosen) iaa.Affine(rotate=(-25, 25), shear=(-8, 8)), ]) def imread_convert(f): return io.imread(f).astype(np.uint8) def load_data_from_folder(dir): # read all images into an image collection ic = io.ImageCollection(dir + "*.bmp", load_func=imread_convert) #create one large array of image data data = io.concatenate_images(ic) #extract labels from image names labels = np.array(ic.files) for i, f in enumerate(labels): m = re.search("_", f) labels[i] = f[len(dir):m.start()]
def create_list_of_augmenters(flip_vertical=True, flip_horizontal=True, random_rotations=True): '''Creates a list of image augmenters. Description : Creates a list of image augmenters that can possibly flip an image vertically, horizontally and perform clockwise rotations in increments of 90 degrees out of [90,180,270,360]. Arguments : flip_vertical -- Bool. The augmenters created will have the ability to flip images vertically. flip_horizontal -- Bool. The augmenters created will have the ability to flip images horizontally. random_rotations -- Bool. The augmenters created will have the ability to rotate images by 90,180,270 and 360 degrees. Returns : ans -- List. The list contains a number of image augmenters that have the capability to perform flips and rotations as decided by the input parameters "flip_vertical", "flip_horizontal" and "random_rotations". If all three parameters are "false" then "None" is returned. ''' if flip_vertical and flip_horizontal and random_rotations: ans = {} # flip up down flip_ud = 1.0 # flip left right flip_lr = 1.0 # Add 4 augmenters with different rotation capabilities. for i in range(4): string = str(i) ans[string] = iaa.Sequential([ iaa.Flipud(flip_ud), iaa.Fliplr(flip_lr), iaa.Affine(rotate=i * 90, cval=0), ]) return ans elif flip_vertical and flip_horizontal and (not random_rotations): ans = {} # flip up down flip_ud = 1.0 # flip left right flip_lr = 1.0 ans["0"] = iaa.Sequential([ iaa.Flipud(flip_ud), iaa.Fliplr(flip_lr), ]) return ans elif flip_vertical and (not flip_horizontal) and (not random_rotations): ans = {} # flip up down flip_ud = 1.0 ans["0"] = iaa.Sequential([ iaa.Flipud(flip_ud), ]) return ans elif flip_vertical and (not flip_horizontal) and random_rotations: ans = {} # flip up down flip_ud = 1.0 # Add 4 augmenters with different rotation capabilities. for i in range(4): string = str(i) ans[string] = iaa.Sequential([ iaa.Flipud(flip_ud), iaa.Affine(rotate=i * 90, cval=0), ]) return ans elif (not flip_vertical) and flip_horizontal and (not random_rotations): ans = {} # flip left right flip_lr = 1.0 ans["0"] = iaa.Sequential([ iaa.Fliplr(1.0), ]) return ans elif (not flip_vertical) and flip_horizontal and random_rotations: ans = {} # flip left right flip_lr = 1.0 # Add 4 augmenters with different rotation capabilities. for i in range(4): string = str(i) ans[string] = iaa.Sequential([ iaa.Fliplr(1.0), iaa.Affine(rotate=i * 90, cval=0), ]) return ans elif (not flip_vertical) and (not flip_horizontal) and random_rotations: ans = {} # Add 4 augmenters with different rotation capabilities. for i in range(4): string = str(i) ans[string] = iaa.Sequential([ iaa.Affine(rotate=i * 90, cval=0), ]) return ans else: return None
def main(cfg): device = torch.device('cuda' if cfg.cuda else 'cpu') autoenc = DeepLabv3Plus() model = Siamese(autoenc, in_channels=3, n_edges=cfg.n_edges, sp_pool_use_max=cfg.sp_pooling_max) if (cfg.checkpoint_autoenc is not None): print('loading checkpoint {}'.format(cfg.checkpoint_autoenc)) state_dict = torch.load(cfg.checkpoint_autoenc, map_location=lambda storage, loc: storage) autoenc.load_state_dict(state_dict) elif (cfg.checkpoint_siam is not None): print('loading checkpoint {}'.format(cfg.checkpoint_siam)) state_dict = torch.load(cfg.checkpoint_siam, map_location=lambda storage, loc: storage) model.load_state_dict(state_dict) autoenc.to(device) model.to(device) transf = iaa.Sequential([ iaa.Invert(0.5) if 'Dataset1' in 'Dataset' + cfg.train_dir else iaa.Noop(), iaa.SomeOf(3, [ iaa.Affine(scale={ "x": (1 - cfg.aug_scale, 1 + cfg.aug_scale), "y": (1 - cfg.aug_scale, 1 + cfg.aug_scale) }, rotate=(-cfg.aug_rotate, cfg.aug_rotate), shear=(-cfg.aug_shear, cfg.aug_shear)), iaa.SomeOf(1, [ iaa.AdditiveGaussianNoise(scale=cfg.aug_noise * 255), iaa.GaussianBlur(sigma=(0., cfg.aug_blur)), iaa.GammaContrast((0., cfg.aug_gamma)) ]), iaa.Fliplr(p=0.5), iaa.Flipud(p=0.5) ]), rescale_augmenter ]) transf_normal = Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) dl_train = Loader(pjoin(cfg.in_root, 'Dataset' + cfg.train_dir), augmentation=transf, n_segments=cfg.n_segments_train, delta_segments=cfg.delta_segments_train, normalization=transf_normal) dl_test = torch.utils.data.ConcatDataset([ Loader(pjoin(cfg.in_root, 'Dataset' + d), augmentation=transf, n_segments=cfg.n_segments_test, delta_segments=cfg.delta_segments_test, normalization=transf_normal) for d in cfg.test_dirs ]) dataloader_train = DataLoader(dl_train, batch_size=cfg.batch_size, sampler=SubsetRandomSampler( cfg.n_frames_epoch * cfg.train_frames), collate_fn=dl_train.collate_fn, drop_last=True, num_workers=cfg.n_workers) dataloader_test = DataLoader(dl_test, batch_size=cfg.batch_size, collate_fn=dl_train.collate_fn, sampler=torch.utils.data.RandomSampler( dl_test, replacement=True, num_samples=cfg.batch_size), num_workers=cfg.n_workers) dataloaders = {'train': dataloader_train, 'test': dataloader_test} d = datetime.datetime.now() ds_dir = os.path.split('Dataset' + cfg.train_dir)[-1] run_dir = pjoin(cfg.out_dir, '{}_{:%Y-%m-%d_%H-%M}_{}'.format(ds_dir, d, cfg.exp_name)) if (not os.path.exists(run_dir)): os.makedirs(run_dir) # Save cfg with open(pjoin(run_dir, 'cfg.yml'), 'w') as outfile: yaml.dump(cfg.__dict__, stream=outfile, default_flow_style=False) # convert batch to device batch_to_device = lambda batch: { k: v.to(device) if (isinstance(v, torch.Tensor)) else v for k, v in batch.items() } optimizer = optim.SGD(params=[{ 'params': model.autoenc.encoder.parameters(), 'lr': cfg.lr_autoenc }, { 'params': model.autoenc.aspp.parameters(), 'lr': cfg.lr_autoenc }, { 'params': model.autoenc.decoder.parameters(), 'lr': cfg.lr_siam }, { 'params': model.linear1.parameters(), 'lr': cfg.lr_siam }, { 'params': model.linear2.parameters(), 'lr': cfg.lr_siam }], momentum=cfg.momentum, weight_decay=cfg.decay) utls.setup_logging(run_dir) logger = logging.getLogger('siam') logger.info('run_dir: {}'.format(run_dir)) train(cfg, model, dataloaders, run_dir, batch_to_device, optimizer, logger) logger.info('training siam')
# Define our sequence of augmentation steps that will be applied to every image seq = iaa.Sequential( [ # apply the following augmenters to most images # iaa.Fliplr(0.5), # horizontally flip 50% of all images # iaa.Flipud(0.2), # vertically flip 20% of all images # crop images by -5% to 10% of their height/width sometimes(iaa.CropAndPad( percent=(-0.05, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255) )), sometimes(iaa.Affine( # scale images to 80-120% of their size, individually per axis scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # translate by -20 to +20 percent (per axis) translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees # use nearest neighbour or bilinear interpolation (fast) order=[0, 1], cval=(0, 255), # if mode is constant, use a cval between 0 and 255 # use any of scikit-image's warping modes (see 2nd image from the top for examples) mode=ia.ALL )) ], random_order=True )
def draw_single_sequential_images(): ia.seed(44) #image = misc.imresize(ndimage.imread("quokka.jpg")[0:643, 0:643], (128, 128)) image = ia.quokka_square(size=(128, 128)) sometimes = lambda aug: iaa.Sometimes(0.5, aug) seq = iaa.Sequential( [ # apply the following augmenters to most images iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.Flipud(0.2), # vertically flip 20% of all images # crop images by -5% to 10% of their height/width sometimes(iaa.CropAndPad( percent=(-0.05, 0.1), pad_mode=ia.ALL, pad_cval=(0, 255) )), sometimes(iaa.Affine( scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis) rotate=(-45, 45), # rotate by -45 to +45 degrees shear=(-16, 16), # shear by -16 to +16 degrees order=[0, 1], # use nearest neighbour or bilinear interpolation (fast) cval=(0, 255), # if mode is constant, use a cval between 0 and 255 mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples) )), # execute 0 to 5 of the following (less important) augmenters per image # don't execute all of them, as that would often be way too strong iaa.SomeOf((0, 5), [ sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0 iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7 iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7 ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images # search either for all edges or for directed edges, # blend the result with the original image using a blobby mask iaa.SimplexNoiseAlpha(iaa.OneOf([ iaa.EdgeDetect(alpha=(0.5, 1.0)), iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)), ])), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images iaa.OneOf([ iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2), ]), iaa.Invert(0.05, per_channel=True), # invert color channels iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value) iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation # either change the brightness of the whole image (sometimes # per channel) or change the brightness of subareas iaa.OneOf([ iaa.Multiply((0.5, 1.5), per_channel=0.5), iaa.FrequencyNoiseAlpha( exponent=(-4, 0), first=iaa.Multiply((0.5, 1.5), per_channel=True), second=iaa.ContrastNormalization((0.5, 2.0)) ) ]), iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast iaa.Grayscale(alpha=(0.0, 1.0)), sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths) sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), # sometimes move parts of the image around sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1))) ], random_order=True ) ], random_order=True ) grid = seq.draw_grid(image, cols=8, rows=8) misc.imsave("examples_grid.jpg", grid)
sys.path.append('./') import utils.losses as losses import utils.detectors as detectors import utils.metrics as metrics import utils.optimizer as optim from models.model_builder import getModel from datasets.data_loader import getDataLoader import imgaug.augmenters as iaa from config import cfg global global_cfg global_cfg = dict() seq0 = iaa.Sequential([ #iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=True), iaa.SigmoidContrast(gain=(3, 14), cutoff=(0.4, 0.6)), iaa.PerspectiveTransform(scale=(0.01, 0.15)), ]) def summary_write(summary, writer): for key in summary.keys(): writer.add_scalar(key, summary[key], summary['epoch']) def train_epoch_wo_outlier(model, optimizer, in_loader, loss_func, cur_epoch, op_cfg, writer): global global_cfg model.train() avg_loss = 0 correct = 0
#endregion model_file_transfer_base = 'transfer_vessel_seg_patch-012-0.968_0.68_0.81.hdf5' # model_file_transfer_base = 'transfer_vessel_seg_patch-014-0.970-014-0.682_0.810.hdf5' # model_file_transfer_base = 'transfer_014-0.969-014-0.669_0.801.hdf5' # a little bad performance # region data generator image_shape = (PATCH_SIZE, PATCH_SIZE, 3) from imgaug import augmenters as iaa sometimes = lambda aug: iaa.Sometimes(0.96, aug) imgaug_train_seq = iaa.Sequential([ # iaa.CropAndPad(percent=(-0.04, 0.04)), iaa.Fliplr(0.5), # horizontally flip 50% of the images iaa.Flipud(0.2), # horizontally flip 50% of the images sometimes(iaa.ContrastNormalization((0.9, 1.1))), iaa.Sometimes(0.9, iaa.Add((-6, 6))), ]) my_gen_train = my_Generator_seg(train_image_files, train_mask_files, image_shape=image_shape, batch_size=BATCH_SIZE_TRAIN, imgaug_seq=imgaug_train_seq, single_channel_no=1) my_gen_valid = my_Generator_seg(valid_image_files, valid_mask_files, image_shape=image_shape, batch_size=BATCH_SIZE_VALID,
def main(): parser = argparse.ArgumentParser(description="Check augmenters visually.") parser.add_argument( "--only", default=None, help= "If this is set, then only the results of an augmenter with this name will be shown. " "Optionally, comma-separated list.", required=False) args = parser.parse_args() images = [ ia.quokka_square(size=(128, 128)), ia.imresize_single_image(data.astronaut(), (128, 128)) ] keypoints = [ ia.KeypointsOnImage([ ia.Keypoint(x=50, y=40), ia.Keypoint(x=70, y=38), ia.Keypoint(x=62, y=52) ], shape=images[0].shape), ia.KeypointsOnImage([ ia.Keypoint(x=55, y=32), ia.Keypoint(x=42, y=95), ia.Keypoint(x=75, y=89) ], shape=images[1].shape) ] bounding_boxes = [ ia.BoundingBoxesOnImage([ ia.BoundingBox(x1=10, y1=10, x2=20, y2=20), ia.BoundingBox(x1=40, y1=50, x2=70, y2=60) ], shape=images[0].shape), ia.BoundingBoxesOnImage([ ia.BoundingBox(x1=10, y1=10, x2=20, y2=20), ia.BoundingBox(x1=40, y1=50, x2=70, y2=60) ], shape=images[1].shape) ] augmenters = [ iaa.Sequential([ iaa.CoarseDropout(p=0.5, size_percent=0.05), iaa.AdditiveGaussianNoise(scale=0.1 * 255), iaa.Crop(percent=0.1) ], name="Sequential"), iaa.SomeOf(2, children=[ iaa.CoarseDropout(p=0.5, size_percent=0.05), iaa.AdditiveGaussianNoise(scale=0.1 * 255), iaa.Crop(percent=0.1) ], name="SomeOf"), iaa.OneOf(children=[ iaa.CoarseDropout(p=0.5, size_percent=0.05), iaa.AdditiveGaussianNoise(scale=0.1 * 255), iaa.Crop(percent=0.1) ], name="OneOf"), iaa.Sometimes(0.5, iaa.AdditiveGaussianNoise(scale=0.1 * 255), name="Sometimes"), iaa.WithColorspace("HSV", children=[iaa.Add(20)], name="WithColorspace"), iaa.WithChannels([0], children=[iaa.Add(20)], name="WithChannels"), iaa.AddToHueAndSaturation((-20, 20), per_channel=True, name="AddToHueAndSaturation"), iaa.Noop(name="Noop"), iaa.Scale({ "width": 64, "height": 64 }, name="Scale"), iaa.CropAndPad(px=(-8, 8), name="CropAndPad-px"), iaa.Pad(px=(0, 8), name="Pad-px"), iaa.Crop(px=(0, 8), name="Crop-px"), iaa.Crop(percent=(0, 0.1), name="Crop-percent"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Superpixels(p_replace=0.75, n_segments=50, name="Superpixels"), iaa.Grayscale(0.5, name="Grayscale0.5"), iaa.Grayscale(1.0, name="Grayscale1.0"), iaa.GaussianBlur((0, 3.0), name="GaussianBlur"), iaa.AverageBlur(k=(3, 11), name="AverageBlur"), iaa.MedianBlur(k=(3, 11), name="MedianBlur"), iaa.BilateralBlur(d=10, name="BilateralBlur"), iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0, 2.0), name="Sharpen"), iaa.Emboss(alpha=(0.1, 1.0), strength=(0, 2.0), name="Emboss"), iaa.EdgeDetect(alpha=(0.1, 1.0), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0, 1.0), name="DirectedEdgeDetect"), iaa.Add((-50, 50), name="Add"), iaa.Add((-50, 50), per_channel=True, name="AddPerChannel"), iaa.AddElementwise((-50, 50), name="AddElementwise"), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.1 * 255), name="AdditiveGaussianNoise"), iaa.Multiply((0.5, 1.5), name="Multiply"), iaa.Multiply((0.5, 1.5), per_channel=True, name="MultiplyPerChannel"), iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"), iaa.Dropout((0.0, 0.1), name="Dropout"), iaa.CoarseDropout(p=0.05, size_percent=(0.05, 0.5), name="CoarseDropout"), iaa.Invert(p=0.5, name="Invert"), iaa.Invert(p=0.5, per_channel=True, name="InvertPerChannel"), iaa.ContrastNormalization(alpha=(0.5, 2.0), name="ContrastNormalization"), iaa.SaltAndPepper(p=0.05, name="SaltAndPepper"), iaa.Salt(p=0.05, name="Salt"), iaa.Pepper(p=0.05, name="Pepper"), iaa.CoarseSaltAndPepper(p=0.05, size_percent=(0.01, 0.1), name="CoarseSaltAndPepper"), iaa.CoarseSalt(p=0.05, size_percent=(0.01, 0.1), name="CoarseSalt"), iaa.CoarsePepper(p=0.05, size_percent=(0.01, 0.1), name="CoarsePepper"), iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_px={ "x": (-16, 16), "y": (-16, 16) }, rotate=(-45, 45), shear=(-16, 16), order=ia.ALL, cval=(0, 255), mode=ia.ALL, name="Affine"), iaa.PiecewiseAffine(scale=0.03, nb_rows=(2, 6), nb_cols=(2, 6), name="PiecewiseAffine"), iaa.PerspectiveTransform(scale=0.1, name="PerspectiveTransform"), iaa.ElasticTransformation(alpha=(0.5, 8.0), sigma=1.0, name="ElasticTransformation"), iaa.Alpha(factor=(0.0, 1.0), first=iaa.Add(100), second=iaa.Dropout(0.5), per_channel=False, name="Alpha"), iaa.Alpha(factor=(0.0, 1.0), first=iaa.Add(100), second=iaa.Dropout(0.5), per_channel=True, name="AlphaPerChannel"), iaa.Alpha(factor=(0.0, 1.0), first=iaa.Affine(rotate=(-45, 45)), per_channel=True, name="AlphaAffine"), iaa.AlphaElementwise(factor=(0.0, 1.0), first=iaa.Add(50), second=iaa.ContrastNormalization(2.0), per_channel=False, name="AlphaElementwise"), iaa.AlphaElementwise(factor=(0.0, 1.0), first=iaa.Add(50), second=iaa.ContrastNormalization(2.0), per_channel=True, name="AlphaElementwisePerChannel"), iaa.AlphaElementwise(factor=(0.0, 1.0), first=iaa.Affine(rotate=(-45, 45)), per_channel=True, name="AlphaElementwiseAffine"), iaa.SimplexNoiseAlpha(first=iaa.EdgeDetect(1.0), per_channel=False, name="SimplexNoiseAlpha"), iaa.FrequencyNoiseAlpha(first=iaa.EdgeDetect(1.0), per_channel=False, name="FrequencyNoiseAlpha") ] augmenters.append( iaa.Sequential([iaa.Sometimes(0.2, aug.copy()) for aug in augmenters], name="Sequential")) augmenters.append( iaa.Sometimes(0.5, [aug.copy() for aug in augmenters], name="Sometimes")) for augmenter in augmenters: if args.only is None or augmenter.name in [ v.strip() for v in args.only.split(",") ]: print("Augmenter: %s" % (augmenter.name, )) grid = [] for image, kps, bbs in zip(images, keypoints, bounding_boxes): aug_det = augmenter.to_deterministic() imgs_aug = aug_det.augment_images( np.tile(image[np.newaxis, ...], (16, 1, 1, 1))) kps_aug = aug_det.augment_keypoints([kps] * 16) bbs_aug = aug_det.augment_bounding_boxes([bbs] * 16) imgs_aug_drawn = [ kps_aug_one.draw_on_image(img_aug) for img_aug, kps_aug_one in zip(imgs_aug, kps_aug) ] imgs_aug_drawn = [ bbs_aug_one.draw_on_image(img_aug) for img_aug, bbs_aug_one in zip(imgs_aug_drawn, bbs_aug) ] grid.append(np.hstack(imgs_aug_drawn)) ia.imshow(np.vstack(grid))
def __init__(self, prob, scale): self.prob = prob self.seq = iaa.Sequential([iaa.AdditiveGaussianNoise(scale)])
Returns: A Tensor. Has the same type as input. Has the shape of tensor.shape * repeats """ expanded_tensor = tf.expand_dims(tensor, -1) multiples = [1] + repeats tiled_tensor = tf.tile(expanded_tensor, multiples=multiples) repeated_tesnor = tf.reshape(tiled_tensor, tf.shape(tensor) * repeats) return repeated_tesnor # data aug seq = iaa.Sequential( [ iaa.Fliplr(1.0), # horizontal flips iaa.Sometimes(0.1, iaa.GaussianBlur(sigma=(0, 0.5))) ], random_order=True) # apply augmenters in random order # class class CNN(): def __init__(self, k, inc, out): self.w = tf.Variable(tf.truncated_normal([k, k, inc, out], stddev=0.05)) self.m, self.v = tf.Variable(tf.zeros_like(self.w)), tf.Variable( tf.zeros_like(self.w)) def getw(self): return self.w
def main(): images = [ misc.imresize( ndimage.imread("../quokka.jpg")[0:643, 0:643], (128, 128)), misc.imresize(data.astronaut(), (128, 128)) ] augmenters = [ iaa.Noop(name="Noop"), iaa.Crop(px=(0, 8), name="Crop-px"), iaa.Crop(percent=(0, 0.1), name="Crop-percent"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Superpixels(p_replace=0.75, n_segments=50, name="Superpixels"), iaa.Grayscale(0.5, name="Grayscale0.5"), iaa.Grayscale(1.0, name="Grayscale1.0"), iaa.GaussianBlur((0, 3.0), name="GaussianBlur"), iaa.Sharpen(alpha=(0.1, 1.0), strength=(0, 2.0), name="Sharpen"), iaa.Emboss(alpha=(0.1, 1.0), strength=(0, 2.0), name="Emboss"), iaa.EdgeDetect(alpha=(0.1, 1.0), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0, 1.0), name="DirectedEdgeDetect"), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.1 * 255), name="AdditiveGaussianNoise"), iaa.Dropout((0.0, 0.1), name="Dropout"), iaa.Invert(p=0.5, name="Invert"), iaa.Invert(p=0.5, per_channel=True, name="InvertPerChannel"), iaa.Add((-50, 50), name="Add"), iaa.Add((-50, 50), per_channel=True, name="AddPerChannel"), iaa.AddElementwise((-50, 50), name="AddElementwise"), iaa.Multiply((0.5, 1.5), name="Multiply"), iaa.Multiply((0.5, 1.5), per_channel=True, name="MultiplyPerChannel"), iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"), iaa.ContrastNormalization(alpha=(0.5, 2.0), name="ContrastNormalization"), iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_px={ "x": (-16, 16), "y": (-16, 16) }, rotate=(-45, 45), shear=(-16, 16), order=ia.ALL, cval=(0, 1.0), mode=ia.ALL, name="Affine"), iaa.ElasticTransformation(alpha=(0.5, 8.0), sigma=1.0, name="ElasticTransformation") ] #for i, aug in enumerate(augmenters): #print(i) #aug.deepcopy() #import copy #copy.deepcopy(aug) seq = iaa.Sequential([aug.copy() for aug in augmenters], name="Sequential") st = iaa.Sometimes(0.5, seq.copy(), name="Sometimes") augmenters.append(seq) augmenters.append(st) for augmenter in augmenters: print("Augmenter: %s" % (augmenter.name, )) grid = augmenter.draw_grid(images, rows=1, cols=16) misc.imshow(grid)
def __init__(self): configMain.__init__(self) st = lambda aug: iaa.Sometimes(0.2, aug) oc = lambda aug: iaa.Sometimes(0.1, aug) rl = lambda aug: iaa.Sometimes(0.05, aug) self.augment = [ iaa.Sequential( [ rl(iaa.GaussianBlur( (0, 1.3))), # blur images with a sigma between 0 and 1.5 rl( iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05), per_channel=0.5)), # add gaussian noise to images rl(iaa.Dropout((0.0, 0.10), per_channel=0.5) ), # randomly remove up to X% of the pixels oc( iaa.Add((-20, 20), per_channel=0.5) ), # change brightness of images (by -X to Y of original value) st(iaa.Multiply( (0.25, 2.5), per_channel=0.2 )), # change brightness of images (X-Y% of original value) rl(iaa.ContrastNormalization( (0.5, 1.5), per_channel=0.5)), # improve or worsen the contrast rl(iaa.Grayscale((0.0, 1))), # put grayscale ], random_order=True # do all of the above in random order ) ] * 3 all_files = [] self.val_db_path = [] ids = [ "steer103_v5_way_v2", 'steer103_v5_way_v2_town02', "nonoise_town03_way", "nonoise_town02_way", "nonoise_town04_way" ] for id in ids: all_files += glob.glob( "/data/yang/code/aws/scratch/carla_collect/" + id + "/*/data_*.h5") for valid in range(1, 15, 3): self.val_db_path += glob.glob( "/data/yang/code/aws/scratch/carla_collect/" + id + "/*WeatherId=" + str(valid).zfill(2) + "/data_*.h5") self.train_db_path = list(set(all_files) - set(self.val_db_path)) self.speed_factor = 40.0 # In KM/H # The division is made by three diferent data kinds # in every mini-batch there will be equal number of samples with labels from each group # e.g. for [[0,1],[2]] there will be 50% samples with labels 0 and 1, and 50% samples with label 2 self.labels_per_division = [[0, 2, 5], [3], [4]] self.dataset_names = ['targets'] self.queue_capacity = 5 # now measured in how many batches
def main(args): random.seed() ia.seed(random.randrange(10000)) bg_images = generate_data.load_dtd(dtd_dir='%s/dtd/images' % Config.data_dir, dump_it=False) background = generate_data.Backgrounds(images=bg_images) card_pool = pd.DataFrame() for set_name in Config.all_set_list: df = fetch_data.load_all_cards_text('%s/csv/%s.csv' % (Config.data_dir, set_name)) card_pool = card_pool.append(df) class_ids = {} with open('%s/obj.names' % Config.data_dir) as names_file: class_name_list = names_file.read().splitlines() for i in range(len(class_name_list)): class_ids[class_name_list[i]] = i for i in range(args.num_gen): # Arbitrarily select top left and right corners for perspective transformation # Since the training image are generated with random rotation, don't need to skew all four sides skew = [[random.uniform(0, 0.25), 0], [0, 1], [1, 1], [random.uniform(0.75, 1), 0]] generator = ImageGenerator(background.get_random(), class_ids, args.width, args.height, skew=skew) out_name = '' # Use 2 to 5 cards per generator for _, card_info in card_pool.sample(random.randint(2, 5)).iterrows(): img_name = '%s/card_img/png/%s/%s_%s.png' % ( Config.data_dir, card_info['set'], card_info['collector_number'], fetch_data.get_valid_filename(card_info['name'])) out_name += '%s%s_' % (card_info['set'], card_info['collector_number']) card_img = cv2.imread(img_name) if card_img is None: fetch_data.fetch_card_image( card_info, out_dir='%s/card_img/png/%s' % (Config.data_dir, card_info['set'])) card_img = cv2.imread(img_name) if card_img is None: print('WARNING: card %s is not found!' % img_name) detected_object_list = generate_data.apply_bounding_box( card_img, card_info) card = Card(card_img, card_info, detected_object_list) generator.add_card(card) for j in range(args.num_iter): seq = iaa.Sequential([ iaa.Multiply((0.8, 1.2)), # darken / brighten the whole image iaa.SimplexNoiseAlpha(first=iaa.Add(random.randrange(64)), per_channel=0.1, size_px_max=[3, 6], upscale_method="cubic"), # Lighting iaa.AdditiveGaussianNoise(scale=random.uniform(0, 0.05) * 255, per_channel=0.1), # Noises iaa.Dropout(p=[0, 0.05], per_channel=0.1) # Dropout ]) if i % 3 == 0: generator.generate_non_obstructive() generator.export_training_data( visibility=0.0, out_name='%s/train/non_obstructive_update/%s%d' % (Config.data_dir, out_name, j), aug=seq) elif i % 3 == 1: generator.generate_horizontal_span( theta=random.uniform(-math.pi, math.pi)) generator.export_training_data( visibility=0.0, out_name='%s/train/horizontal_span_update/%s%d' % (Config.data_dir, out_name, j), aug=seq) else: generator.generate_vertical_span( theta=random.uniform(-math.pi, math.pi)) generator.export_training_data( visibility=0.0, out_name='%s/train/vertical_span_update/%s%d' % (Config.data_dir, out_name, j), aug=seq) #generator.generate_horizontal_span(theta=random.uniform(-math.pi, math.pi)) #generator.render(display=True, aug=seq, debug=True) print('Generated %s%d' % (out_name, j)) generator.img_bg = background.get_random() pass
img = np.array(img) bndbox = read_xml_annotation(cmd, str(image_id) + '.xml') bbs = ia.BoundingBoxesOnImage( [ ia.BoundingBox( x1=bndbox[0], y1=bndbox[1], x2=bndbox[2], y2=bndbox[3]) #改这里! ], shape=img.shape) seq = iaa.Sequential([ iaa.Flipud(0.5), # vertically flip 20% of all images iaa.Multiply((1.2, 1.5)), # change brightness, doesn't affect BBs iaa.Affine( translate_px={ "x": 10, "y": 10 }, scale=(0.8, 0.95), rotate=(-10, 10) ) # translate by 40/60px on x/y axis, and scale to 50-70%, affects BBs ]) seq_det = seq.to_deterministic() # 保持坐标和图像同步改变,而不是随机 image_aug = seq_det.augment_images([img])[0] bbs_aug = seq_det.augment_bounding_boxes([bbs])[0] before = bbs.bounding_boxes[0] after = bbs_aug.bounding_boxes[0] print("BB : (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)" % (before.x1, before.y1, before.x2, before.y2, after.x1, after.y1, after.x2, after.y2))
ia.seed(seed) seq = iaa.Sequential([ # iaa.AddToHueAndSaturation((-1, 1),per_channel=True) # iaa.MultiplyHueAndSaturation((0.5, 1.5), per_channel=True) iaa.MultiplySaturation((0.5, 1.5)), # iaa.Crop(px=(0, 16)), # crop images from each side by 0 to 16px (randomly chosen) # iaa.Fliplr(0.5), # horizontally flip 50% of the images iaa.GaussianBlur(sigma=(0, 3.0)), # blur images with a sigma of 0 to 3.0 iaa.LinearContrast((0.75, 1.5)), # Strengthen or weaken the contrast in each image. iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # Add gaussian noise. iaa.Multiply((0.8, 1.2), per_channel=0.2), # Make some images brighter and some darker. # In 20% of all cases, we sample the multiplier once per channel, # which can end up changing the color of the images. ], random_order=True) def augment(img): img=np.array(img,dtype=np.uint8)
def __init__(self, path='/'): configMain.__init__(self) st = lambda aug: iaa.Sometimes(0.2, aug) oc = lambda aug: iaa.Sometimes(0.1, aug) rl = lambda aug: iaa.Sometimes(0.05, aug) self.augment = [ iaa.Sequential( [ rl(iaa.GaussianBlur( (0, 1.3))), # blur images with a sigma between 0 and 1.5 rl( iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05), per_channel=0.5)), # add gaussian noise to images rl(iaa.Dropout((0.0, 0.10), per_channel=0.5) ), # randomly remove up to X% of the pixels oc( iaa.Add((-20, 20), per_channel=0.5) ), # change brightness of images (by -X to Y of original value) st(iaa.Multiply( (0.25, 2.5), per_channel=0.2 )), # change brightness of images (X-Y% of original value) rl(iaa.ContrastNormalization( (0.5, 1.5), per_channel=0.5)), # improve or worsen the contrast rl(iaa.Grayscale((0.0, 1))), # put grayscale ], random_order=True # do all of the above in random order ), iaa.Sequential( [ # AUGMENTATION Labels rl(iaa.Dropout( (0.0, 0.03))), # randomly remove up to X% of the pixels rl(iaa.CoarseDropout((0.0, 0.03), size_percent=( 0.2, 0.3))), # randomly remove up to X% of the pixels ], random_order=True # do all of the above in random order ) ] # self.augment = [None] # there are files with data, 200 images each, and here we select which ones to use self.dataset_name = 'Carla' train_path = os.path.join(path, 'RC28_wpz_M') val_path = os.path.join(path, 'RC025Val') print(train_path) self.train_db_path = [ os.path.join(train_path, f) for f in glob.glob1(train_path, "data_*.h5") ] self.val_db_path = [ os.path.join(val_path, f) for f in glob.glob1(val_path, "data_*.h5") ] # Speed Divide Factor # TODO: FOr now is hardcooded, but eventually we should be able to calculate this from data at the loading time. self.speed_factor = 40.0 # In KM/H FOR GTA it should be maximun 30.0 # The division is made by three diferent data kinds # in every mini-batch there will be equal number of samples with labels from each group # e.g. for [[0,1],[2]] there will be 50% samples with labels 0 and 1, and 50% samples with label 2 self.labels_per_division = [[0, 2, 5], [3], [4]] self.dataset_names = ['targets'] self.queue_capacity = 20 * self.batch_size
def calc_sp_feats(self, cfg): """ Computes UNet features in Autoencoder-mode Train/forward-propagate Unet and save features/weights """ df_fname = 'sp_desc_{}.p' cp_fname = 'checkpoint_{}.pth.tar' bm_fname = 'best_model_{}.pth.tar' df_path = os.path.join(self.desc_path, df_fname) bm_path = os.path.join(self.desc_path, bm_fname) cp_path = os.path.join(self.desc_path, cp_fname) from ksptrack.models.deeplab import DeepLabv3Plus transf = iaa.Sequential([ iaa.SomeOf(cfg.feat_data_someof, [ iaa.Affine( scale={ "x": (1 - cfg.feat_data_width_shift, 1 + cfg.feat_data_width_shift), "y": (1 - cfg.feat_data_height_shift, 1 + cfg.feat_data_height_shift) }, rotate=(-cfg.feat_data_rot_range, cfg.feat_data_rot_range), shear=(-cfg.feat_data_shear_range, cfg.feat_data_shear_range)), iaa.AdditiveGaussianNoise( scale=cfg.feat_data_gaussian_noise_std * 255), iaa.Fliplr(p=0.5), iaa.Flipud(p=0.5) ]), rescale_augmenter, Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) dl = LocPriorDataset(root_path=self.root_path, sig_prior=cfg.feat_locs_gaussian_std, augmentations=transf) if (self.feats_mode == 'autoenc'): sampler = None shuffle = True else: idx_refine = np.repeat(np.arange(len(dl)), 120 // len(dl)) sampler = SubsetRandomSampler(idx_refine) shuffle = False dataloader = DataLoader(dl, batch_size=cfg.batch_size, shuffle=shuffle, sampler=sampler, collate_fn=dl.collate_fn, drop_last=True, num_workers=cfg.feat_n_workers) model = DeepLabv3Plus(pretrained=False) train_model(model, cfg, dataloader, cp_path.format('autoenc'), self.desc_path, cp_fname, bm_fname, self.feats_mode) # Do forward pass on images and retrieve features if (not os.path.exists(df_path.format(self.feats_mode))): self.logger.info("Computing features on superpixels") transf = iaa.Sequential([ rescale_augmenter, Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) dl.augmentations = transf dataloader = DataLoader(dl, batch_size=1, collate_fn=dl.collate_fn) model = DeepLabv3Plus() feats_sp = get_features(model, cfg, dataloader, cp_path.format(self.feats_mode), self.feats_mode) feats_df = self.centroids_loc.assign(desc=feats_sp) self.logger.info('Saving features to {}.'.format( df_path.format(self.feats_mode))) feats_df.to_pickle(os.path.join(df_path.format(self.feats_mode)))
def test_rotation(opt): result_dir = os.path.join(args.path, args.code_name, "result+" + "-".join(opt.model_prefix_list)) if not os.path.exists(result_dir): os.makedirs(result_dir) # Load assert len(opt.model_prefix_list) <= torch.cuda.device_count(), \ "number of models should not exceed the device numbers" nets = [] for _, prefix in enumerate(opt.model_prefix_list): net = model.SSD(cfg, connect_loc_to_conf=True, fix_size=False, incep_conf=True, incep_loc=True) device_id = opt.device_id if len(opt.model_prefix_list) == 1 else _ net = net.to("cuda:%d" % (device_id)) net_dict = net.state_dict() weight_dict = util.load_latest_model(args, net, prefix=prefix, return_state_dict=True, nth=opt.nth_best_model) loading_fail_signal = False for i, key in enumerate(net_dict.keys()): if "module." + key not in weight_dict: net_dict[key] = torch.zeros(net_dict[key].shape) for key in weight_dict.keys(): if key[7:] in net_dict: if net_dict[key[7:]].shape == weight_dict[key].shape: net_dict[key[7:]] = weight_dict[key] else: print( "Key: %s from disk has shape %s copy to the model with shape %s" % (key[7:], str(weight_dict[key].shape), str(net_dict[key[7:]].shape))) loading_fail_signal = True else: print("Key: %s does not exist in net_dict" % (key[7:])) if loading_fail_signal: raise RuntimeError( 'Shape Error happens, remove "%s" from your -mpl settings.' % (prefix)) net.load_state_dict(net_dict) net.eval() nets.append(net) print("Above model loaded with out a problem") detector = model.Detect(num_classes=2, bkg_label=0, top_k=opt.detector_top_k, conf_thresh=opt.detector_conf_threshold, nms_thresh=opt.detector_nms_threshold) # Enumerate test folder root_path = os.path.expanduser(opt.test_dataset_root) if not os.path.exists(root_path): raise FileNotFoundError( "%s does not exists, please check your -tdr/--test_dataset_root settings" % (root_path)) img_list = glob.glob(root_path + "/*.%s" % (opt.extension)) precisions, recalls = [], [] for i, img_file in enumerate(sorted(img_list)): start = time.time() name = img_file[img_file.rfind("/") + 1:-4] img = cv2.imread(img_file) height_ori, width_ori = img.shape[0], img.shape[1] # detect rotation for returning the image back img, transform_det = estimate_angle(img, args, None, None, None) transform_det["rotation"] = 0 if transform_det["rotation"] != 0: rot_aug = augmenters.Affine(rotate=transform_det["rotation"], cval=args.aug_bg_color) else: rot_aug = None # Perform Augmentation if rot_aug: rot_aug = augmenters.Sequential( augmenters.Affine(rotate=transform_det["rotation"], cval=args.aug_bg_color)) image = rot_aug.augment_image(img) else: image = img # Resize the longer side to a certain length if height_ori >= width_ori: resize_aug = augmenters.Sequential([ augmenters.Resize(size={ "height": square, "width": "keep-aspect-ratio" }) ]) else: resize_aug = augmenters.Sequential([ augmenters.Resize(size={ "height": "keep-aspect-ratio", "width": square }) ]) resize_aug = resize_aug.to_deterministic() image = resize_aug.augment_image(image) h_re, w_re = image.shape[0], image.shape[1] # Pad the image into a square image pad_aug = augmenters.Sequential( augmenters.PadToFixedSize(width=square, height=square, pad_cval=255, position="center")) pad_aug = pad_aug.to_deterministic() image = pad_aug.augment_image(image) h_final, w_final = image.shape[0], image.shape[1] # Prepare image tensor and test image_t = torch.Tensor(util.normalize_image(args, image)).unsqueeze(0) image_t = image_t.permute(0, 3, 1, 2) #visualize_bbox(args, cfg, image, [torch.Tensor(rot_coord).cuda()], net.prior, height_final/width_final) text_boxes = [] for _, net in enumerate(nets): device_id = opt.device_id if len(nets) == 1 else _ image_t = image_t.to("cuda:%d" % (device_id)) out = net(image_t, is_train=False) loc_data, conf_data, prior_data = out prior_data = prior_data.to("cuda:%d" % (device_id)) det_result = detector(loc_data, conf_data, prior_data) # Extract the predicted bboxes idx = det_result.data[0, 1, :, 0] >= 0.1 text_boxes.append(det_result.data[0, 1, idx, 1:]) text_boxes = torch.cat(text_boxes, dim=0) text_boxes = combine_boxes(text_boxes, img=image_t) pred = [[float(coor) for coor in area] for area in text_boxes] BBox = [ imgaug.imgaug.BoundingBox(box[0] * w_final, box[1] * h_final, box[2] * w_final, box[3] * h_final) for box in pred ] BBoxes = imgaug.imgaug.BoundingBoxesOnImage(BBox, shape=image.shape) return_aug = augment_back(transform_det, height_ori, width_ori, (h_final - h_re) / 2, (w_final - w_re) / 2) return_aug = return_aug.to_deterministic() img_ori = return_aug.augment_image(image) bbox = return_aug.augment_bounding_boxes([BBoxes])[0] #print_box(blue_boxes=pred, idx=i, img=vb.plot_tensor(args, image_t, margin=0), #save_dir=args.val_log) f = open(os.path.join(result_dir, name + ".txt"), "w") import researches.ocr.textbox.tb_data as tb_data gt_box_file = os.path.join(opt.test_dataset_root, name + "." + opt.ground_truth_extension) coords = tb_data.parse_file(os.path.expanduser(gt_box_file)) gt_coords = [] for coord in coords: x1, x2 = min(coord[::2]), max(coord[::2]) y1, y2 = min(coord[1::2]), max(coord[1::2]) gt_coords.append([x1, y1, x2, y2]) pred_final = [] for box in bbox.bounding_boxes: x1, y1, x2, y2 = int(round(box.x1)), int(round(box.y1)), int( round(box.x2)), int(round(box.y2)) pred_final.append([x1, y1, x2, y2]) #box_tensors.append(torch.tensor([x1, y1, x2, y2])) # 4-point to 8-point: x1, y1, x2, y1, x2, y2, x1, y2 f.write("%d,%d,%d,%d,%d,%d,%d,%d\n" % (x1, y1, x2, y1, x2, y2, x1, y2)) cv2.rectangle(img, (x1, y1), (x2, y2), (255, 105, 65), 2) accu, precision, recall = measure(torch.Tensor(pred_final).cuda(), torch.Tensor(gt_coords).cuda(), width=img.shape[1], height=img.shape[0]) precisions.append(precision) recalls.append(recall) img_save_directory = os.path.join( args.path, args.code_name, "val+" + "-".join(opt.model_prefix_list)) if not os.path.exists(img_save_directory): os.mkdir(img_save_directory) cv2.imwrite(os.path.join(img_save_directory, name + ".jpg"), img) f.close() print("%d th image cost %.2f seconds" % (i, time.time() - start)) print("Precision: %.2f, Recall: %.2f" % (avg(precisions), avg(recalls))) os.chdir( os.path.join(args.path, args.code_name, "result+" + "-".join(opt.model_prefix_list))) os.system( "zip result_%s.zip ~/Pictures/dataset/ocr/_text_detection/result+%s/*.txt" % ("val+" + "-".join(opt.model_prefix_list), "-".join( opt.model_prefix_list)))
def main(): parser = argparse.ArgumentParser(description="Check augmenters visually.") parser.add_argument( '--only', default=None, help= "If this is set, then only the results of an augmenter with this name will be shown.", required=False) args = parser.parse_args() images = [ misc.imresize( ndimage.imread("../quokka.jpg")[0:643, 0:643], (128, 128)), misc.imresize(data.astronaut(), (128, 128)) ] augmenters = [ iaa.Noop(name="Noop"), iaa.OneOf(children=[ iaa.CoarseDropout(p=0.5, size_percent=0.05), iaa.AdditiveGaussianNoise(scale=0.1 * 255), iaa.Crop(percent=0.1) ], name="OneOf"), iaa.Crop(px=(0, 8), name="Crop-px"), iaa.Crop(percent=(0, 0.1), name="Crop-percent"), iaa.Fliplr(0.5, name="Fliplr"), iaa.Flipud(0.5, name="Flipud"), iaa.Superpixels(p_replace=0.75, n_segments=50, name="Superpixels"), iaa.Grayscale(0.5, name="Grayscale0.5"), iaa.Grayscale(1.0, name="Grayscale1.0"), iaa.AverageBlur(k=(3, 11), name="AverageBlur"), iaa.GaussianBlur((0, 3.0), name="GaussianBlur"), iaa.MedianBlur(k=(3, 11), name="MedianBlur"), iaa.Sharpen(alpha=(0.1, 1.0), lightness=(0, 2.0), name="Sharpen"), iaa.Emboss(alpha=(0.1, 1.0), strength=(0, 2.0), name="Emboss"), iaa.EdgeDetect(alpha=(0.1, 1.0), name="EdgeDetect"), iaa.DirectedEdgeDetect(alpha=(0.1, 1.0), direction=(0, 1.0), name="DirectedEdgeDetect"), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.1 * 255), name="AdditiveGaussianNoise"), iaa.Dropout((0.0, 0.1), name="Dropout"), iaa.CoarseDropout(p=0.05, size_percent=(0.05, 0.5), name="CoarseDropout"), iaa.Invert(p=0.5, name="Invert"), iaa.Invert(p=0.5, per_channel=True, name="InvertPerChannel"), iaa.Add((-50, 50), name="Add"), iaa.Add((-50, 50), per_channel=True, name="AddPerChannel"), iaa.AddElementwise((-50, 50), name="AddElementwise"), iaa.Multiply((0.5, 1.5), name="Multiply"), iaa.Multiply((0.5, 1.5), per_channel=True, name="MultiplyPerChannel"), iaa.MultiplyElementwise((0.5, 1.5), name="MultiplyElementwise"), iaa.ContrastNormalization(alpha=(0.5, 2.0), name="ContrastNormalization"), iaa.Affine(scale={ "x": (0.8, 1.2), "y": (0.8, 1.2) }, translate_px={ "x": (-16, 16), "y": (-16, 16) }, rotate=(-45, 45), shear=(-16, 16), order=ia.ALL, cval=(0, 255), mode=ia.ALL, name="Affine"), iaa.PiecewiseAffine(scale=0.03, nb_rows=(2, 6), nb_cols=(2, 6), name="PiecewiseAffine"), iaa.ElasticTransformation(alpha=(0.5, 8.0), sigma=1.0, name="ElasticTransformation") ] augmenters.append( iaa.Sequential([iaa.Sometimes(0.2, aug.copy()) for aug in augmenters], name="Sequential")) augmenters.append( iaa.Sometimes(0.5, [aug.copy() for aug in augmenters], name="Sometimes")) for augmenter in augmenters: if args.only is None or augmenter.name == args.only: print("Augmenter: %s" % (augmenter.name, )) grid = augmenter.draw_grid(images, rows=1, cols=16) misc.imshow(grid)
def train_deploy(dataset_location='classification/sample_data/imgs_recycling', dataset_name='recycling', do_augmentations=False, deployment_name="testdeployment", azureml_rscgroup=None, azureml_cluster_name=None): """ a sample pipeline for deploying themodel that is trained on a dataset. loads a dataset, optionally does some augmentations, creates and trains a model using transfer learning based on ResNet18, deploys the trained model on the specified Azure ML cluster or picks up the one set using the CLI. and returns the Scoring URL. Args: dataset_location: path to a dataset. there should be a top level folder containing folders for each class. see the sample recycling dataset for an example of the format dataset_name: the of the dataset. will be used in the dataset management functionality do_augmentations: boolean. specifies whether augmentations should be applied to the test set deployment_name: the deployment of the deployment. Will be used in deployment management facility azureml_rscgroup: Azure ML resource group name of the model management account. If not set, default value will be picked up if set from CLI azureml_cluster_name: Azure ML cluster name where the model is deployed. If not set, default value will be picked up if set from CLI. Returns: the scoring API URL of the deployment """ # if we're not running inside AML WB, set up the share directory if 'AZUREML_NATIVE_SHARE_DIRECTORY' not in os.environ: os.environ['AZUREML_NATIVE_SHARE_DIRECTORY'] = './share' context = Context.get_global_context() # create a dataset from a directory with folders representing different classes dataset = ClassificationDataset.create_from_dir(dataset_name, dataset_location) # print out some info about the dataset dataset.print_info() # optionally augment images by cropping and rotating if do_augmentations: # here we create two pipelines for doing augmentations. the first # will rotate each image by between -45 and 45 degrees (the angle is # chosen at random). then the rotated images will be flipped from left # to right with probability .5. the second pipeline will randomly crop # images by between 0 and 10 percent. each pipeline will be applied to # the original dataset. the resulting dataset will three times as many # images as the original - the original dataset, the dataset after # augmentation by the rotate_and_flip pipeline, and the dataset # after augmentation by the crop pipeline rotate_and_flip = augmenters.Sequential([ augmenters.Affine(rotate=(-45, 45)), augmenters.Fliplr(.5)]) crop = augmenters.Sequential([augmenters.Crop(percent=(0, .1))]) train_set = augment_dataset(dataset, [rotate_and_flip, crop]) else: train_set = dataset # now create the model base_model_name = 'ResNet18_ImageNet_CNTK' model = CNTKTLModel(train_set.labels, base_model_name = base_model_name, output_path='.') # train the model using cntk model.train(train_set) print("Model state:", model.model_state) # check if the deployment exists, if yes remove it first AMLDeployment.delete_if_service_exist(deployment_name) #deploy the trained model deploy_obj = AMLDeployment( deployment_name=deployment_name, associated_DNNModel=model, aml_env = "cluster", replicas=1) deploy_obj.deploy() return deploy_obj.service_url
def padding_seq(pad_size, pad_method): seq = iaa.Sequential([PadFixed(pad=pad_size, pad_method=pad_method), ]).to_deterministic() return seq
img=plt.imread('bird.jpg') seq = iaa.Sequential([ iaa.Fliplr(p=0),# basically this is original one iaa.Crop(px=(22, 45),keep_size=False), # crop images from each side by 0 to 16px (randomly chosen) iaa.Fliplr(1), # horizontally flip 50% of the images iaa.GaussianBlur(sigma=(5, 7.0)), # blur images with a sigma of 0 to 3.0 iaa.ImpulseNoise(p=(0.6,1)), iaa.EdgeDetect(alpha=(0.9,1)), #iaa.AddToBrightness(add=(100,124)), iaa.Canny(alpha=(0.8,0.9)), iaa.Grayscale(alpha=1.00), iaa.ChannelShuffle(p=1), iaa.geometric.Affine( scale=2,rotate=22, backend='cv2'), iaa.Cartoon(blur_ksize=(11,13)), iaa.CenterCropToAspectRatio(1), iaa.CenterCropToFixedSize(100,100), iaa.ChangeColorTemperature(kelvin=(2222,3333)), #iaa.segmentation(), iaa.CLAHE(clip_limit=(4,8)), iaa.Rotate(rotate=(-30,90)) ]) plt.figure(figsize=(12,12)) for idx,Augmentor in enumerate(seq): # print(1) ax=plt.subplot(4,4,idx+1)
def __init__(self, images, config, shuffle=True, jitter=True, norm=None): self.generator = None self.images = images self.config = config self.shuffle = shuffle self.jitter = jitter self.norm = norm self.anchors = [ BoundBox(0, 0, config['ANCHORS'][2 * i], config['ANCHORS'][2 * i + 1]) for i in range(int(len(config['ANCHORS']) // 2)) ] ### augmentors by https://github.com/aleju/imgaug sometimes = lambda aug: iaa.Sometimes(0.1, aug) # Define our sequence of augmentation steps that will be applied to every image # All augmenters with per_channel=0.5 will sample one value _per image_ # in 50% of all cases. In all other cases they will sample new values # _per channel_. self.aug_pipe = iaa.Sequential( [ #sometimes(iaa.Add((-50, 10))), # change brightness of images (by -10 to 10 of original value) sometimes( iaa.Multiply((0.3, 0.8)) ), # change brightness of images (50-150% of original value) sometimes(iaa.ContrastNormalization((0.3, 1.0))), #sometimes( # iaa.Affine( # scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis) # rotate=(-5, 5), # rotate by -45 to +45 degrees # shear=(-5, 5), # shear by -16 to +16 degrees # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast) # cval=(0, 255), # if mode is constant, use a cval between 0 and 255 # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples) #)), # execute 0 to 5 of the following (less important) augmenters per image # don't execute all of them, as that would often be way too strong iaa.SomeOf( (0, 5), [ iaa.OneOf([ #iaa.Flipud(0.5), # vertically flip 20% of all images #iaa.Fliplr(0.5), # horizontally flip 50% of all images iaa.GaussianBlur( (0, 3.0) ), # blur images with a sigma between 0 and 3.0 iaa.MedianBlur( k=(3, 11) ), # blur image using local medians with kernel sizes between 2 and 7 #iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images #iaa.Superpixels(p_replace=(0, 1.0), n_segments=(75, 200)), # convert images into their superpixel representation ]), #iaa.Invert(0.05, per_channel=True), # invert color channels #iaa.Add((-10, 10), per_channel=0.5), #iaa.Multiply((0.5, 1.5), per_channel=0.5), # change brightness of images (50-150% of original value) #iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast #iaa.Grayscale(alpha=(0.0, 1.0)), #sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths) #sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))) # sometimes move parts of the image around ], random_order=True) ], random_order=True) if shuffle: np.random.shuffle(self.images)
def __init__(self, data_root, image_shape=None, min_shape=None, val_mode=None, plus1=True, isTrain=True): ''' We assume that image_shape is always an even number. With plus1 = True, we'll modify image_shape to odd. ''' self.data_root = data_root if isTrain: assert image_shape is not None self.image_shape = image_shape else: assert min_shape is not None self.image_shape = (min_shape, min_shape) self.isTrain = isTrain self.val_mode = val_mode if plus1: self.image_shape = (image_shape[0] + 1, image_shape[1] + 1) if isTrain: bg_set_fn = [ i.strip() for i in open(os.path.join(data_root, 'bg_train_set.txt'), 'r') ] fg_set = [ i.strip() for i in open( os.path.join(data_root, 'fg_train_set_old.txt'), 'r') ] fg_set_fn = [None] * len(bg_set_fn) for i in range(len(bg_set_fn)): fg_set_fn[i] = fg_set[i // 100] else: bg_set_fn = [ i.strip() for i in open(os.path.join(data_root, 'bg_val_set.txt'), 'r') ] #[60:] fg_set_fn = [ i.strip() for i in open(os.path.join(data_root, 'fg_val_set.txt'), 'r') ] #[60:] self.dataset_length = len(bg_set_fn) assert len(fg_set_fn) == self.dataset_length #ratio = self.dataset_length // len(fg_set_fn) self.sample_fn = [None] * self.dataset_length for i in range(self.dataset_length): _fn = fg_set_fn[i].split(' ') self.sample_fn[i] = (_fn[0], _fn[1], bg_set_fn[i]) # apply to fg, bg self.flip_and_color_aug = iaa.Sequential([ iaa.MultiplyHueAndSaturation(mul=iap.TruncatedNormal( 1.0, 0.2, 0.5, 1.5)), # mean, std, low, high iaa.GammaContrast(gamma=iap.TruncatedNormal(1.0, 0.2, 0.5, 1.5)), iaa.AddToHue(value=iap.TruncatedNormal(0.0, 0.1 * 100, -0.2 * 255, 0.2 * 255)) ]) if isTrain: self.min_shape = min_shape self.preshape_aug = iaa.Sequential([ iaa.CropToFixedSize(width=self.min_shape, height=self.min_shape, \ position='uniform' if isTrain else 'center'), ]) else: assert self.val_mode in ['gca', 'dim', 'origin', 'resize'] if self.val_mode == 'resize': assert min_shape is not None self.min_shape = min_shape elif self.val_mode == 'origin': print('Warning: val_mode == origin, change min_shape to 2112') self.min_shape = 2112 self.image_shape = (2112, 2112) self.shape_aug = iaa.Sequential([ iaa.Fliplr(0.5), iaa.OneOf([ # iaa.CropToFixedSize(width=384, height=384), # iaa.CropToFixedSize(width=448, height=448), iaa.CropToFixedSize(width=512, height=512), iaa.CropToFixedSize(width=576, height=576), iaa.CropToFixedSize(width=640, height=640), iaa.CropToFixedSize(width=704, height=704), iaa.CropToFixedSize(width=768, height=768) ]), ]) if isTrain else \ iaa.Sequential([ iaa.CropToFixedSize(width=self.image_shape[1], \ height=self.image_shape[0], \ position='uniform' if isTrain else 'center'), ])