def test_p_replace_stochastic_parameter_n_segments_2(self): aug = iaa.Superpixels( p_replace=iap.Binomial(iap.Choice([0.0, 1.0])), n_segments=2) observed = aug.augment_image(self.base_img) assert ( np.allclose(observed, self.base_img) or self._array_equals_tolerant( observed, self.base_img_superpixels, 2) )
def chapter_parameters_discrete(): ia.seed(1) # ----------------------- # Binomial # ----------------------- from imgaug import parameters as iap params = [iap.Binomial(0.5), iap.Binomial(0.9)] gridarr = draw_distributions_grid(params, rows=1) save("parameters", "continuous_binomial.jpg", gridarr) # ----------------------- # DiscreteUniform # ----------------------- from imgaug import parameters as iap params = [ iap.DiscreteUniform(0, 10), iap.DiscreteUniform(-10, 10), iap.DiscreteUniform([-10, -9, -8, -7], 10), iap.DiscreteUniform((-10, -7), 10) ] gridarr = draw_distributions_grid(params) save("parameters", "continuous_discreteuniform.jpg", gridarr) # ----------------------- # Poisson # ----------------------- from imgaug import parameters as iap params = [ iap.Poisson(1), iap.Poisson(2.5), iap.Poisson((1, 2.5)), iap.RandomSign(iap.Poisson(2.5)) ] gridarr = draw_distributions_grid(params) save("parameters", "continuous_poisson.jpg", gridarr)
def __init__(self): self.aug = iaa.Sequential([ iaa.Scale((224, 224)), iaa.Sometimes(0.30, iaa.GaussianBlur(sigma=(0, 3.0))), iaa.Sometimes(0.25, iaa.Multiply((0.5, 1.5), per_channel=0.5)), iaa.Sometimes(0.20, iaa.Invert(0.25, per_channel=0.5)), iaa.Sometimes(0.25, iaa.ReplaceElementwise( iap.FromLowerResolution(iap.Binomial(0.1), size_px=8), iap.Normal(128, 0.4*128), per_channel=0.5) ), iaa.Sometimes(0.30, iaa.AdditivePoissonNoise(40)), iaa.Fliplr(0.5), iaa.Affine(rotate=(-20, 20), mode='symmetric'), iaa.Sometimes(0.30, iaa.OneOf([iaa.Dropout(p=(0, 0.1)), iaa.CoarseDropout(0.1, size_percent=0.5)])), iaa.AddToHueAndSaturation(value=(-10, 10), per_channel=True) ])
def aug_data(x_data, y_data, percent, aug_percent): rand_vector = np.linspace(0, np.shape(x_data)[0], num=int(percent*np.shape(x_data)[0]/100), endpoint=False, dtype = int) for count in range(0, len(rand_vector)): ppl = Image.fromarray(x_data[rand_vector[count],:,:,:].astype('uint8')) ppl = np.array(ppl) seq1 = iaa.Sequential([ iaa.ReplaceElementwise( iap.FromLowerResolution(iap.Binomial(aug_percent), size_px=4), iap.Normal(128, 0.4*128), per_channel=1) ]) aug_image = seq1(images=ppl) x_data[rand_vector[count],:,:,:] = np.copy(aug_image) return x_data, y_data
def chapter_augmenters_replaceelementwise(): aug_cls = iaa.ReplaceElementwise fn_start = "arithmetic/replaceelementwise" aug = aug_cls(0.05, [0, 255]) run_and_save_augseq(fn_start + ".jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2, quality=95, seed=2) aug = aug_cls(0.05, [0, 255], per_channel=0.5) run_and_save_augseq(fn_start + "_per_channel_050.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2, quality=95, seed=2) aug = aug_cls(0.1, iap.Normal(128, 0.4 * 128), per_channel=0.5) run_and_save_augseq(fn_start + "_gaussian_noise.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2, quality=95, seed=2) aug = aug_cls(iap.FromLowerResolution(iap.Binomial(0.1), size_px=8), iap.Normal(128, 0.4 * 128), per_channel=0.5) run_and_save_augseq(fn_start + "_gaussian_noise_coarse.jpg", aug, [ia.quokka(size=(128, 128)) for _ in range(8)], cols=4, rows=2, quality=95, seed=2)
def main(): """Function that initializes the training (e.g. models) and runs the batches.""" parser = argparse.ArgumentParser(description="Train steering wheel tracker") parser.add_argument('--nocontinue', default=False, action="store_true", help="Whether to NOT continue the previous experiment", required=False) args = parser.parse_args() if os.path.isfile("steering_wheel.tar") and not args.nocontinue: checkpoint = torch.load("steering_wheel.tar") else: checkpoint = None if checkpoint is not None: history = plotting.History.from_string(checkpoint["history"]) else: history = plotting.History() history.add_group("loss", ["train", "val"], increasing=False) history.add_group("acc", ["train", "val"], increasing=True) loss_plotter = plotting.LossPlotter( history.get_group_names(), history.get_groups_increasing(), save_to_fp="train_plot.jpg" ) loss_plotter.start_batch_idx = 100 tracker_cnn = models.SteeringWheelTrackerCNNModel() tracker_cnn.train() optimizer = optim.Adam(tracker_cnn.parameters()) criterion = nn.CrossEntropyLoss() #criterion = nn.BCELoss() if checkpoint is not None: tracker_cnn.load_state_dict(checkpoint["tracker_cnn_state_dict"]) if Config.GPU >= 0: tracker_cnn.cuda(Config.GPU) criterion.cuda(Config.GPU) # initialize image augmentation cascade rarely = lambda aug: iaa.Sometimes(0.1, aug) sometimes = lambda aug: iaa.Sometimes(0.2, aug) often = lambda aug: iaa.Sometimes(0.4, aug) augseq = iaa.Sequential([ sometimes(iaa.Crop(percent=(0, 0.025))), rarely(iaa.GaussianBlur((0, 1.0))), # blur images with a sigma between 0 and 3.0 rarely(iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.02*255), per_channel=0.5)), # add gaussian noise to images often(iaa.Dropout( iap.FromLowerResolution( other_param=iap.Binomial(1 - 0.2), size_px=(2, 16) ), per_channel=0.2 )), often(iaa.Add((-20, 20), per_channel=0.5)), # change brightness of images (by -10 to 10 of original value) often(iaa.Multiply((0.8, 1.2), per_channel=0.25)), # change brightness of images (50-150% of original value) often(iaa.ContrastNormalization((0.8, 1.2), per_channel=0.5)), # improve or worsen the contrast often(iaa.Affine( scale={"x": (0.8, 1.3), "y": (0.8, 1.3)}, translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, rotate=(-0, 0), shear=(-0, 0), order=[0, 1], cval=(0, 255), mode=["constant", "edge"] )), rarely(iaa.Grayscale(alpha=(0.0, 1.0))) ], random_order=True # do all of the above in random order ) #memory = replay_memory.ReplayMemory.get_instance_supervised() batch_loader_train = BatchLoader(val=False, augseq=augseq, queue_size=15, nb_workers=4) batch_loader_val = BatchLoader(val=True, augseq=iaa.Noop(), queue_size=NB_VAL_BATCHES, nb_workers=2) start_batch_idx = 0 if checkpoint is None else checkpoint["batch_idx"] + 1 for batch_idx in xrange(start_batch_idx, NB_BATCHES): run_batch(batch_idx, False, batch_loader_train, tracker_cnn, criterion, optimizer, history, (batch_idx % 20) == 0) if (batch_idx+1) % VAL_EVERY == 0: for i in xrange(NB_VAL_BATCHES): run_batch(batch_idx, True, batch_loader_val, tracker_cnn, criterion, optimizer, history, i == 0) if (batch_idx+1) % PLOT_EVERY == 0: loss_plotter.plot(history) # every N batches, save a checkpoint if (batch_idx+1) % SAVE_EVERY == 0: torch.save({ "batch_idx": batch_idx, "history": history.to_string(), "tracker_cnn_state_dict": tracker_cnn.state_dict() }, "steering_wheel.tar")
def test_Superpixels(): reseed() def _array_equals_tolerant(a, b, tolerance): diff = np.abs(a.astype(np.int32) - b.astype(np.int32)) return np.all(diff <= tolerance) base_img = [[255, 255, 255, 0, 0, 0], [255, 235, 255, 0, 20, 0], [250, 250, 250, 5, 5, 5]] base_img = np.tile( np.array(base_img, dtype=np.uint8)[..., np.newaxis], (1, 1, 3)) base_img_superpixels = [[251, 251, 251, 4, 4, 4], [251, 251, 251, 4, 4, 4], [251, 251, 251, 4, 4, 4]] base_img_superpixels = np.tile( np.array(base_img_superpixels, dtype=np.uint8)[..., np.newaxis], (1, 1, 3)) base_img_superpixels_left = np.copy(base_img_superpixels) base_img_superpixels_left[:, 3:, :] = base_img[:, 3:, :] base_img_superpixels_right = np.copy(base_img_superpixels) base_img_superpixels_right[:, :3, :] = base_img[:, :3, :] aug = iaa.Superpixels(p_replace=0, n_segments=2) observed = aug.augment_image(base_img) expected = base_img assert np.allclose(observed, expected) aug = iaa.Superpixels(p_replace=1.0, n_segments=2) observed = aug.augment_image(base_img) expected = base_img_superpixels assert _array_equals_tolerant(observed, expected, 2) aug = iaa.Superpixels(p_replace=1.0, n_segments=iap.Deterministic(2)) observed = aug.augment_image(base_img) expected = base_img_superpixels assert _array_equals_tolerant(observed, expected, 2) aug = iaa.Superpixels(p_replace=iap.Binomial(iap.Choice([0.0, 1.0])), n_segments=2) observed = aug.augment_image(base_img) assert np.allclose(observed, base_img) or _array_equals_tolerant( observed, base_img_superpixels, 2) aug = iaa.Superpixels(p_replace=0.5, n_segments=2) seen = {"none": False, "left": False, "right": False, "both": False} for _ in sm.xrange(100): observed = aug.augment_image(base_img) if _array_equals_tolerant(observed, base_img, 2): seen["none"] = True elif _array_equals_tolerant(observed, base_img_superpixels_left, 2): seen["left"] = True elif _array_equals_tolerant(observed, base_img_superpixels_right, 2): seen["right"] = True elif _array_equals_tolerant(observed, base_img_superpixels, 2): seen["both"] = True else: raise Exception( "Generated superpixels image does not match any expected image." ) if all(seen.values()): break assert all(seen.values()) # test exceptions for wrong parameter types got_exception = False try: _ = iaa.Superpixels(p_replace="test", n_segments=100) except Exception: got_exception = True assert got_exception got_exception = False try: _ = iaa.Superpixels(p_replace=1, n_segments="test") except Exception: got_exception = True assert got_exception # test get_parameters() aug = iaa.Superpixels(p_replace=0.5, n_segments=2, max_size=100, interpolation="nearest") params = aug.get_parameters() assert isinstance(params[0], iap.Binomial) assert isinstance(params[0].p, iap.Deterministic) assert isinstance(params[1], iap.Deterministic) assert 0.5 - 1e-4 < params[0].p.value < 0.5 + 1e-4 assert params[1].value == 2 assert params[2] == 100 assert params[3] == "nearest" ################### # test other dtypes ################### # bool aug = iaa.Superpixels(p_replace=1.0, n_segments=2) img = np.array([[False, False, True, True], [False, False, True, True]], dtype=bool) img_aug = aug.augment_image(img) assert img_aug.dtype == img.dtype assert np.all(img_aug == img) aug = iaa.Superpixels(p_replace=1.0, n_segments=1) img = np.array([[True, True, True, True], [False, True, True, True]], dtype=bool) img_aug = aug.augment_image(img) assert img_aug.dtype == img.dtype assert np.all(img_aug) for dtype in [np.uint8, np.uint16, np.uint32, np.int8, np.int16, np.int32]: min_value, center_value, max_value = iadt.get_value_range_of_dtype( dtype) if np.dtype(dtype).kind == "i": values = [ int(center_value), int(0.1 * max_value), int(0.2 * max_value), int(0.5 * max_value), max_value - 100 ] values = [((-1) * value, value) for value in values] else: values = [(0, int(center_value)), (10, int(0.1 * max_value)), (10, int(0.2 * max_value)), (10, int(0.5 * max_value)), (0, max_value), (int(center_value), max_value)] for v1, v2 in values: aug = iaa.Superpixels(p_replace=1.0, n_segments=2) img = np.array([[v1, v1, v2, v2], [v1, v1, v2, v2]], dtype=dtype) img_aug = aug.augment_image(img) assert img_aug.dtype == np.dtype(dtype) assert np.array_equal(img_aug, img) aug = iaa.Superpixels(p_replace=1.0, n_segments=1) img = np.array([[v2, v2, v2, v2], [v1, v2, v2, v2]], dtype=dtype) img_aug = aug.augment_image(img) assert img_aug.dtype == np.dtype(dtype) assert np.all(img_aug == int(np.round((7 / 8) * v2 + (1 / 8) * v1))) for dtype in []: def _allclose(a, b): atol = 1e-4 if dtype == np.float16 else 1e-8 return np.allclose(a, b, atol=atol, rtol=0) isize = np.dtype(dtype).itemsize for value in [0, 1.0, 10.0, 1000**(isize - 1)]: v1 = (-1) * value v2 = value aug = iaa.Superpixels(p_replace=1.0, n_segments=2) img = np.array([[v1, v1, v2, v2], [v1, v1, v2, v2]], dtype=dtype) img_aug = aug.augment_image(img) assert img_aug.dtype == np.dtype(dtype) assert _allclose(img_aug, img) aug = iaa.Superpixels(p_replace=1.0, n_segments=1) img = np.array([[v2, v2, v2, v2], [v1, v2, v2, v2]], dtype=dtype) img_aug = aug.augment_image(img) assert img_aug.dtype == np.dtype(dtype) assert _allclose(img_aug, (7 / 8) * v2 + (1 / 8) * v1)
def test_Superpixels(): reseed() def _array_equals_tolerant(a, b, tolerance): diff = np.abs(a.astype(np.int32) - b.astype(np.int32)) return np.all(diff <= tolerance) base_img = [[255, 255, 255, 0, 0, 0], [255, 235, 255, 0, 20, 0], [250, 250, 250, 5, 5, 5]] base_img = np.tile( np.array(base_img, dtype=np.uint8)[..., np.newaxis], (1, 1, 3)) base_img_superpixels = [[251, 251, 251, 4, 4, 4], [251, 251, 251, 4, 4, 4], [251, 251, 251, 4, 4, 4]] base_img_superpixels = np.tile( np.array(base_img_superpixels, dtype=np.uint8)[..., np.newaxis], (1, 1, 3)) base_img_superpixels_left = np.copy(base_img_superpixels) base_img_superpixels_left[:, 3:, :] = base_img[:, 3:, :] base_img_superpixels_right = np.copy(base_img_superpixels) base_img_superpixels_right[:, :3, :] = base_img[:, :3, :] aug = iaa.Superpixels(p_replace=0, n_segments=2) observed = aug.augment_image(base_img) expected = base_img assert np.allclose(observed, expected) aug = iaa.Superpixels(p_replace=1.0, n_segments=2) observed = aug.augment_image(base_img) expected = base_img_superpixels assert _array_equals_tolerant(observed, expected, 2) aug = iaa.Superpixels(p_replace=1.0, n_segments=iap.Deterministic(2)) observed = aug.augment_image(base_img) expected = base_img_superpixels assert _array_equals_tolerant(observed, expected, 2) aug = iaa.Superpixels(p_replace=iap.Binomial(iap.Choice([0.0, 1.0])), n_segments=2) observed = aug.augment_image(base_img) assert np.allclose(observed, base_img) or _array_equals_tolerant( observed, base_img_superpixels, 2) aug = iaa.Superpixels(p_replace=0.5, n_segments=2) seen = {"none": False, "left": False, "right": False, "both": False} for _ in sm.xrange(100): observed = aug.augment_image(base_img) if _array_equals_tolerant(observed, base_img, 2): seen["none"] = True elif _array_equals_tolerant(observed, base_img_superpixels_left, 2): seen["left"] = True elif _array_equals_tolerant(observed, base_img_superpixels_right, 2): seen["right"] = True elif _array_equals_tolerant(observed, base_img_superpixels, 2): seen["both"] = True else: raise Exception( "Generated superpixels image does not match any expected image." ) if all(seen.values()): break assert all(seen.values()) # test exceptions for wrong parameter types got_exception = False try: _ = iaa.Superpixels(p_replace="test", n_segments=100) except Exception: got_exception = True assert got_exception got_exception = False try: _ = iaa.Superpixels(p_replace=1, n_segments="test") except Exception: got_exception = True assert got_exception # test get_parameters() aug = iaa.Superpixels(p_replace=0.5, n_segments=2, max_size=100, interpolation="nearest") params = aug.get_parameters() assert isinstance(params[0], iap.Binomial) assert isinstance(params[0].p, iap.Deterministic) assert isinstance(params[1], iap.Deterministic) assert 0.5 - 1e-4 < params[0].p.value < 0.5 + 1e-4 assert params[1].value == 2 assert params[2] == 100 assert params[3] == "nearest"