示例#1
0
    def test_camvid_dataset_with_transforms(self):
        from inferno.io.box.camvid import get_camvid_loaders
        from inferno.utils.io_utils import print_tensor

        train_loader, validate_loader, test_loader = get_camvid_loaders(
            self.get_camvid_root())
        train_dataset = train_loader.dataset
        image, label = train_dataset[0]
        # Make sure the shapes checkout
        self.assertSequenceEqual(image.size(), (3, 360, 480))
        self.assertSequenceEqual(label.size(), (360, 480))
        self.assertEqual(image.type(), 'torch.FloatTensor')
        self.assertEqual(label.type(), 'torch.LongTensor')
        # Print tensors to make sure they look legit
        if not exists(self.PLOT_DIRECTORY):
            os.mkdir(self.PLOT_DIRECTORY)
        else:
            assert isdir(self.PLOT_DIRECTORY)
        print_tensor(image.numpy()[None, ...],
                     prefix='IMG--',
                     directory=self.PLOT_DIRECTORY)
        print_tensor(label.numpy()[None, None, ...],
                     prefix='LAB--',
                     directory=self.PLOT_DIRECTORY)
        print("[+] Inspect images at {}".format(self.PLOT_DIRECTORY))
示例#2
0
    def test_master_affinities_multi_order(self):
        data_config = self.DATA_CONFIG.format('affinities_multi_order')
        from neurofire.datasets.cremi.loaders.master import CREMIDatasets
        from inferno.utils.io_utils import print_tensor

        dataset = CREMIDatasets.from_config(data_config)
        # Get from dataset
        batch = dataset[0]
        # Validate
        self.assertEqual(len(batch), 2)
        self.assertEqual(list(batch[0].size()), [1, 5, 512, 512])
        self.assertEqual(list(batch[1].size())[1:], [5, 512, 512])
        self.assertIn(batch[1].size(0),
                      [2 * 4, 3 * 4, (2 * 4) + 1, (3 * 4) + 1])
        # Print to file
        if os.path.exists(self.PLOT_DIRECTORY):
            assert os.path.isdir(self.PLOT_DIRECTORY)
        else:
            os.mkdir(self.PLOT_DIRECTORY)
        print_tensor(tensor=batch[0].numpy()[None, ...],
                     prefix='MOAFFRAW',
                     directory=self.PLOT_DIRECTORY)
        print_tensor(tensor=batch[1].numpy()[None, ...],
                     prefix='MOAFFTAR',
                     directory=self.PLOT_DIRECTORY)
        print("Plots printed to {}.".format(self.PLOT_DIRECTORY))
示例#3
0
    def _test_camvid_dataset_with_transforms(self):
        from inferno.io.box.camvid import CamVid
        from inferno.io.transform.base import Compose
        from inferno.io.transform.image import PILImage2NumPyArray, RandomSizedCrop, Scale
        from inferno.utils.io_utils import print_tensor

        camvid = CamVid(self.get_camvid_root(),
                        image_transform=Compose(),
                        label_transform=Compose(),
                        joint_transform=Compose())
        camvid.image_transform.add(PILImage2NumPyArray())
        camvid.label_transform.add(PILImage2NumPyArray())
        image, label = camvid[0]
        self.assertSequenceEqual(image.shape, (3, 360, 480))
        self.assertSequenceEqual(label.shape, (360, 480))
        # Add crop trafo
        camvid.joint_transform.add(RandomSizedCrop(ratio_between=(0.7, 1.0),
                                                   preserve_aspect_ratio=True))
        # We need 2 scale transforms, one with order 3 (image) and the other with order 0 (label)
        camvid.joint_transform.add(Scale(output_image_shape=(360, 480),
                                         interpolation_order=3, apply_to=[0]))
        camvid.joint_transform.add(Scale(output_image_shape=(360, 480),
                                         interpolation_order=0, apply_to=[1]))
        image, label = camvid[0]
        self.assertSequenceEqual(image.shape, (3, 360, 480))
        self.assertSequenceEqual(label.shape, (360, 480))
        self.assertLessEqual(len(np.unique(label)), 12)
        # Print tensors to make sure they look legit
        if not exists(self.PLOT_DIRECTORY):
            os.mkdir(self.PLOT_DIRECTORY)
        else:
            assert isdir(self.PLOT_DIRECTORY)
        print_tensor(image[None, ...], prefix='IMG--', directory=self.PLOT_DIRECTORY)
        print_tensor(label[None, None, ...], prefix='LAB--', directory=self.PLOT_DIRECTORY)
        print("[+] Inspect images at {}".format(self.PLOT_DIRECTORY))
示例#4
0
    def test_cityscapes_dataset_with_transforms_unzipped(self):
        from inferno.io.box.cityscapes import get_cityscapes_loaders
        from inferno.utils.io_utils import print_tensor

        train_loader, validate_loader = get_cityscapes_loaders(
            join(self.get_cityscapes_root(), 'extracted'),
            include_coarse_dataset=self.INCLUDE_COARSE,
            read_from_zip_archive=False)
        train_dataset = train_loader.dataset
        tic = time.time()
        image, label = train_dataset[0]
        toc = time.time()
        print("[+] Loaded sample in {} seconds.".format(toc - tic))
        # Make sure the shapes checkout
        self.assertSequenceEqual(image.size(), (3, 1024, 2048))
        self.assertSequenceEqual(label.size(), (1024, 2048))
        self.assertEqual(image.type(), 'torch.FloatTensor')
        self.assertEqual(label.type(), 'torch.LongTensor')
        # Print tensors to make sure they look legit
        if not exists(self.PLOT_DIRECTORY):
            os.mkdir(self.PLOT_DIRECTORY)
        else:
            assert isdir(self.PLOT_DIRECTORY)
        print_tensor(image.numpy()[None, ...],
                     prefix='IMG--',
                     directory=self.PLOT_DIRECTORY)
        for class_id in np.unique(label.numpy()):
            print_tensor((label.numpy()[None, None,
                                        ...] == class_id).astype('float32'),
                         prefix='LAB-{}--'.format(class_id),
                         directory=self.PLOT_DIRECTORY)
        print_tensor(label.numpy()[None, None, ...],
                     prefix='LAB--',
                     directory=self.PLOT_DIRECTORY)
        print("[+] Inspect images at {}".format(self.PLOT_DIRECTORY))
示例#5
0
    def _test_master_membranes_or_affinities(self, data_config):
        from neurofire.datasets.cremi.loaders.master import CREMIDatasets
        from inferno.utils.io_utils import print_tensor

        dataset = CREMIDatasets.from_config(data_config)
        # Get from dataset
        batch = dataset[0]
        # Validate
        self.assertEqual(len(batch), 2)
        for _batch in batch:
            self.assertEqual(list(_batch.size()), [1, 3, 512, 512])
        # Print to file
        if os.path.exists(self.PLOT_DIRECTORY):
            assert os.path.isdir(self.PLOT_DIRECTORY)
        else:
            os.mkdir(self.PLOT_DIRECTORY)
        print_tensor(tensor=batch[0].numpy()[None, ...],
                     prefix='RAW',
                     directory=self.PLOT_DIRECTORY)
        print_tensor(tensor=batch[1].numpy()[None, ...],
                     prefix='TAR',
                     directory=self.PLOT_DIRECTORY)
        print("Plots printed to {}.".format(self.PLOT_DIRECTORY))
示例#6
0
    def test_master(self):
        from neurofire.datasets.isbi2012.loaders.master import ISBI2012Dataset
        from inferno.utils.io_utils import print_tensor

        dataset = ISBI2012Dataset.from_config(self.DATA_CONFIG)
        # Get from dataset
        batch = dataset[0]
        # Validate
        self.assertEqual(len(batch), 2)
        for _batch in batch:
            self.assertEqual(list(_batch.size()),
                             [1, 576, 576])  # batch axis added by loader
        # Print to file
        if os.path.exists(self.PLOT_DIRECTORY):
            assert os.path.isdir(self.PLOT_DIRECTORY)
        else:
            os.mkdir(self.PLOT_DIRECTORY)
        print_tensor(tensor=batch[0].numpy()[None, ...],
                     prefix='RAW',
                     directory=self.PLOT_DIRECTORY)
        print_tensor(tensor=batch[1].numpy()[None, ...],
                     prefix='MEM',
                     directory=self.PLOT_DIRECTORY)
        print("Plots printed to {}.".format(self.PLOT_DIRECTORY))