def testDropoutFlipUD(self): orig_events, sensor_size = create_random_input() flip_probability = 1 drop_probability = 0.5 transform = transforms.Compose([ transforms.DropEvent(p=drop_probability), transforms.RandomFlipUD(sensor_size=sensor_size, p=flip_probability), ]) events = transform(orig_events) drop_events = np.isclose(events.shape[0], (1 - drop_probability) * orig_events.shape[0]) assert drop_events, ( "Event dropout should result in drop_probability*len(original) events" " dropped out.") temporal_order = np.isclose( np.sum((events["t"] - np.sort(events["t"]))**2), 0) assert temporal_order, "Temporal order should be maintained." first_dropped_index = np.where( events["t"][0] == orig_events["t"])[0][0] flipped_events = ( sensor_size[1] - 1 - orig_events["y"][first_dropped_index] == events["y"][0]) assert flipped_events, ( "When flipping up and down y must map to the opposite pixel, i.e. y' =" " sensor width - y") assert events is not orig_events
def testTimeSkewFlipPolarityFlipLR(self): orig_events, sensor_size = create_random_input() coefficient = 1.5 offset = 0 flip_probability_pol = 1 flip_probability_lr = 1 transform = transforms.Compose([ transforms.TimeSkew(coefficient=coefficient, offset=offset), transforms.RandomFlipPolarity(p=flip_probability_pol), transforms.RandomFlipLR(sensor_size=sensor_size, p=flip_probability_lr), ]) events = transform(orig_events) assert len(events) == len(orig_events) assert (events["t"] >= orig_events["t"]).all() assert np.min(events["t"]) >= 0 assert (events["p"] == orig_events["p"] * (-1)).all(), "Polarities should be flipped." same_pixel = np.isclose((sensor_size[0] - 1) - events["x"][0], orig_events["x"][0]) assert same_pixel, ( "When flipping left and right x must map to the opposite pixel, i.e. x' =" " sensor width - x") assert events is not orig_events
def test_caching_transforms(): sensor_size = datasets.POKERDVS.sensor_size preprocess = transforms.Compose( [transforms.Downsample(time_factor=1, spatial_factor=1)]) augmentation = transforms.Compose( [transforms.Downsample(time_factor=1, spatial_factor=1)]) dataset = datasets.POKERDVS(save_to="./data", train=True, transform=preprocess) dataset_cached = CachedDataset(dataset, cache_path="./cache/test2", transform=augmentation, num_copies=4) for (data, label), (data2, label2) in zip(dataset, dataset_cached): assert (data == data2).all() assert label == label2
def plot_events(events, sensor_size, ordering, frame_time=25000, repeat=False): from .utils import plot_frames transform = transforms.Compose([ transforms.Denoise(time_filter=20000), transforms.ToRatecodedFrame(frame_time=frame_time, merge_polarities=True), ]) frames = transform(events, sensor_size=sensor_size, ordering=ordering) plot_frames(frames, frame_time)
def testToTimesurfaces(self): events = self.random_xytp[0].copy() surf_dims = (7, 7) transform = transforms.Compose([ transforms.ToTimesurface(surface_dimensions=surf_dims, tau=5e3, decay="lin") ]) surfaces = transform(events=events, sensor_size=self.random_xytp[2], ordering=self.ordering) self.assertEqual(surfaces.shape[0], len(self.original_events)) self.assertEqual(surfaces.shape[1], 2) self.assertEqual(surfaces.shape[2:], surf_dims)
def testTimeJitter(self): events = self.random_xytp[0].copy() images = None variance = max(self.random_xytp[0][:, 2]) / 10 transform = transforms.Compose( [transforms.TimeJitter(variance=variance, clip_negative=False)]) transform(events=events, sensor_size=self.random_xytp[2], ordering=self.ordering) self.assertTrue(len(events) == len(self.original_events)) self.assertTrue((events[:, 0] == self.original_events[:, 0]).all()) self.assertTrue((events[:, 1] == self.original_events[:, 1]).all()) self.assertFalse((events[:, 2] == self.original_events[:, 2]).all()) self.assertTrue((events[:, 3] == self.original_events[:, 3]).all()) self.assertTrue( np.isclose(events[:, 2].all(), self.original_events[:, 2].all(), atol=variance))
def testDropoutFlipUD(self): events = self.random_xytp[0].copy() images = self.random_xytp[1].copy() multi_image = self.random_xytp[3] flip_probability = 1 drop_probability = 0.5 transform = transforms.Compose([ transforms.DropEvents(drop_probability=drop_probability), transforms.FlipUD(flip_probability=flip_probability), ]) events, images = transform( events=events, images=images, sensor_size=self.random_xytp[2], ordering=self.ordering, multi_image=multi_image, ) drop_events = np.isclose(events.shape[0], (1 - drop_probability) * self.original_events.shape[0]) self.assertTrue( drop_events, "Event dropout should result in drop_probability*len(original) events" " dropped out.", ) temporal_order = np.isclose( np.sum((events[:, 2] - np.sort(events[:, 2]))**2), 0) self.assertTrue(temporal_order, "Temporal order should be maintained.") first_dropped_index = np.where( events[0, 2] == self.original_events[:, 2])[0][0] flipped_events = ( self.random_xytp[2][1] - 1 - self.original_events[first_dropped_index, 1] == events[0, 1]) self.assertTrue( flipped_events, "When flipping up and down y must map to the opposite pixel, i.e. y' =" " sensor width - y", )
def testTimeReversalSpatialJitter(self): orig_events, sensor_size = create_random_input() flip_probability = 1 variance_x = 3 variance_y = 3 sigma_x_y = 0 transform = transforms.Compose([ transforms.RandomTimeReversal(p=flip_probability), transforms.SpatialJitter( sensor_size=sensor_size, variance_x=variance_x, variance_y=variance_y, sigma_x_y=sigma_x_y, clip_outliers=False, ), ]) events = transform(orig_events) assert len(events) == len( orig_events), "Number of events should be the same." spatial_var_x = np.isclose(events["x"].all(), orig_events["x"].all(), atol=variance_x) assert spatial_var_x, "Spatial jitter should be within chosen variance x." assert (events["x"] != orig_events["x"]).any(), "X coordinates should be different." spatial_var_y = np.isclose(events["y"].all(), orig_events["y"].all(), atol=variance_y) assert spatial_var_y, "Spatial jitter should be within chosen variance y." assert (events["y"] != orig_events["y"]).any(), "Y coordinates should be different." assert (events["p"] == orig_events["p"] * (-1)).all(), "Polarities should be flipped." time_reversal = (events["t"] == np.max(orig_events["t"]) - orig_events["t"]).all() assert ( time_reversal ), "Condition of time reversal t_i' = max(t) - t_i has to be fullfilled" assert events is not orig_events
def testTimeSkewFlipPolarityFlipLR(self): events = self.random_xytp[0].copy() images = self.random_xytp[1].copy() multi_image = self.random_xytp[3] coefficient = 1.5 offset = 0 flip_probability_pol = 1 flip_probability_lr = 1 transform = transforms.Compose([ transforms.TimeSkew(coefficient=coefficient, offset=offset), transforms.FlipPolarity(flip_probability=flip_probability_pol), transforms.FlipLR(flip_probability=flip_probability_lr), ]) events, images = transform( events=events, images=images, sensor_size=self.random_xytp[2], ordering=self.ordering, multi_image=multi_image, ) self.assertTrue(len(events) == len(self.original_events)) self.assertTrue((events[:, 2] >= self.original_events[:, 2]).all()) self.assertTrue(np.min(events[:, 2]) >= 0) self.assertTrue( (events[:, 3] == self.original_events[:, 3] * (-1)).all(), "Polarities should be flipped.", ) same_pixel = np.isclose((self.random_xytp[2][0] - 1) - events[0, 0], self.original_events[0, 0]) self.assertTrue( same_pixel, "When flipping left and right x must map to the opposite pixel, i.e. x' =" " sensor width - x", )
def test_pytorch_batch_collation_dense_tensor(): import torch events1, sensor_size = create_random_input() events2, sensor_size = create_random_input() time_window = 1000 transform = transforms.Compose( [transforms.ToFrame(sensor_size=sensor_size, time_window=time_window)]) dataset = DummyDataset( (events1[:5000], events2), transform) # simulate recordings of different length batch_size = 2 dataloader = torch.utils.data.DataLoader( dataset, collate_fn=tonic.collation.PadTensors(), batch_size=batch_size) batch, label = next(iter(dataloader)) max_time = int(events2["t"][-1]) assert batch.shape[0] == max_time // time_window assert batch.shape[1] == batch_size assert batch.shape[2] == sensor_size[2]
def testTimeReversalSpatialJitter(self): events = self.random_xytp[0].copy() images = self.random_xytp[1].copy() flip_probability = 1 multi_image = self.random_xytp[3] variance_x = 1 variance_y = 1 sigma_x_y = 0 transform = transforms.Compose([ transforms.TimeReversal(flip_probability=flip_probability), transforms.SpatialJitter( variance_x=variance_x, variance_y=variance_y, sigma_x_y=sigma_x_y, clip_outliers=False, ), ]) events, images = transform( events=events, images=images, sensor_size=self.random_xytp[2], ordering=self.ordering, multi_image=multi_image, ) self.assertTrue( len(events) == len(self.original_events), "Number of events should be the same.", ) spatial_var_x = np.isclose(events[:, 0].all(), self.original_events[:, 0].all(), atol=variance_x) self.assertTrue(spatial_var_x, "Spatial jitter should be within chosen variance x.") self.assertFalse( (events[:, 0] == self.original_events[:, 0]).all(), "X coordinates should be different.", ) spatial_var_y = np.isclose(events[:, 1].all(), self.original_events[:, 1].all(), atol=variance_y) self.assertTrue(spatial_var_y, "Spatial jitter should be within chosen variance y.") self.assertFalse( (events[:, 1] == self.original_events[:, 1]).all(), "Y coordinates should be different.", ) self.assertTrue( (events[:, 3] == self.original_events[:, 3] * (-1)).all(), "Polarities should be flipped.", ) time_reversal = (events[:, 2] == np.max(self.original_events[:, 2]) - self.original_events[:, 2]).all() self.assertTrue( time_reversal, "Condition of time reversal t_i' = max(t) - t_i has to be fullfilled", ) self.assertTrue( (images[::-1] == self.original_images).all(), "Images should be in reversed order.", )