def get_dataset_loaders(model, dataset, workers): target_size = (model["common"]["image_size"],) * 2 batch_size = model["common"]["batch_size"] path = dataset["common"]["dataset"] mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225] transform = JointCompose( [ JointTransform(ConvertImageMode("RGB"), ConvertImageMode("P")), JointTransform(Resize(target_size, Image.BILINEAR), Resize(target_size, Image.NEAREST)), JointTransform(CenterCrop(target_size), CenterCrop(target_size)), JointRandomHorizontalFlip(0.5), JointRandomRotation(0.5, 90), JointRandomRotation(0.5, 90), JointRandomRotation(0.5, 90), JointTransform(ImageToTensor(), MaskToTensor()), JointTransform(Normalize(mean=mean, std=std), None), ] ) train_dataset = SlippyMapTilesConcatenation( [os.path.join(path, "training", "images")], os.path.join(path, "training", "labels"), transform ) val_dataset = SlippyMapTilesConcatenation( [os.path.join(path, "validation", "images")], os.path.join(path, "validation", "labels"), transform ) train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, drop_last=True, num_workers=workers) val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False, drop_last=True, num_workers=workers) return train_loader, val_loader
def test_len(self): path = "tests/fixtures" target = "tests/fixtures/labels/" channels = [{"sub": "images", "bands": [1, 2, 3]}] transform = JointCompose( [JointTransform(ImageToTensor(), MaskToTensor())]) dataset = SlippyMapTilesConcatenation(path, channels, target, transform) self.assertEqual(len(dataset), 3)
def test_getitem(self): path = "tests/fixtures" target = "tests/fixtures/labels/" channels = [{"sub": "images", "bands": [1, 2, 3]}] transform = JointCompose( [JointTransform(ImageToTensor(), MaskToTensor())]) dataset = SlippyMapTilesConcatenation(path, channels, target, transform) images, mask, tiles = dataset[0] self.assertEqual(tiles, mercantile.Tile(69105, 105093, 18)) self.assertEqual(type(images), torch.Tensor) self.assertEqual(type(mask), torch.Tensor)
def get_dataset_loaders(path, config, workers): # Values computed on ImageNet DataSet mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225] transform = JointCompose([ JointResize(config["model"]["image_size"]), JointRandomFlipOrRotate(config["model"]["data_augmentation"]), JointTransform(ImageToTensor(), MaskToTensor()), JointTransform(Normalize(mean=mean, std=std), None), ]) train_dataset = SlippyMapTilesConcatenation( os.path.join(path, "training"), config["channels"], os.path.join(path, "training", "labels"), joint_transform=transform, ) val_dataset = SlippyMapTilesConcatenation( os.path.join(path, "validation"), config["channels"], os.path.join(path, "validation", "labels"), joint_transform=transform, ) batch_size = config["model"]["batch_size"] train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, drop_last=True, num_workers=workers) val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False, drop_last=True, num_workers=workers) return train_loader, val_loader