|
| 1 | +from torch.utils.data import Dataset, DataLoader, WeightedRandomSampler |
| 2 | +import glob |
| 3 | +import os |
| 4 | +import torch |
| 5 | +import numpy as np |
| 6 | +import pandas as pd |
| 7 | +import pdb |
| 8 | +from torch.utils.data.dataloader import default_collate |
| 9 | +import sys |
| 10 | + |
| 11 | +means = { |
| 12 | + 'ai4forest_camera': (10782.3223, 3304.7444, 1999.6086, 7276.4209, 1186.4460, 1884.6165, |
| 13 | + 2645.6113, 3128.2588, 3806.2808, 4134.6855, 4113.4883, 4259.1885, |
| 14 | + 4683.5879, 3838.2222), # Not the true values, change for your dataset |
| 15 | +} |
| 16 | + |
| 17 | +stds = { |
| 18 | + 'ai4forest_camera': (907.7484, 472.1412, 423.8558, 1086.0916, 175.0936, 226.6303, |
| 19 | + 299.4834, 313.0911, 388.1186, 434.4579, 455.7314, 455.0303, |
| 20 | + 388.5127, 374.1260), # Not the true values, change for your dataset |
| 21 | +} |
| 22 | + |
| 23 | +percentiles = { |
| 24 | + 'ai4forest_camera': { |
| 25 | + 1: (-7542.0, -8126.0, -16659.0, -14187.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), |
| 26 | + 2: (-6834.0, -7255.0, -14468.0, -13537.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), |
| 27 | + 5: (-5694.0, -5963.0, -12383.0, -12601.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), |
| 28 | + 95: (24995.0, 24556.0, 22124.0, 20120.0, 15016.0, 15116.0, 15212.0, 15181.0, 14946.0, 14406.0, 14660.0, 13810.0, 12082.0, 13041.0), |
| 29 | + 98: (25969.0, 26078.0, 23632.0, 21934.0, 15648.0, 15608.0, 15487.0, 15449.0, 15296.0, 15155.0, 15264.0, 14943.0, 13171.0, 14064.0), |
| 30 | + 99: (27044.0, 27349.0, 24868.0, 23266.0, 15970.0, 15680.0, 15548.0, 15494.0, 15432.0, 15368.0, 15385.0, 15219.0, 13590.0, 14657.0), |
| 31 | + } # Not the true values, change for your dataset |
| 32 | +} |
| 33 | + |
| 34 | +class FixValDataset(Dataset): |
| 35 | + """ |
| 36 | + Dataset class to load the fixval dataset. |
| 37 | + """ |
| 38 | + def __init__(self, data_path, dataframe, image_transforms=None): |
| 39 | + self.data_path = data_path |
| 40 | + self.df = pd.read_csv(dataframe, index_col=False) |
| 41 | + self.files = list(self.df["paths"].apply(lambda x: os.path.join(data_path, x))) |
| 42 | + self.image_transforms = image_transforms |
| 43 | + |
| 44 | + def __len__(self): |
| 45 | + return len(self.files) |
| 46 | + |
| 47 | + def __getitem__(self, index): |
| 48 | + file = self.files[index].replace(r"'", "") |
| 49 | + fileName = file[file.rfind('data_')+5: file.rfind('.npz')] |
| 50 | + data = np.load(file) |
| 51 | + |
| 52 | + image = data["data"].astype(np.float32) |
| 53 | + # Move the channel axis to the last position (required for torchvision transforms) |
| 54 | + image = np.moveaxis(image, 0, -1) |
| 55 | + if self.image_transforms: |
| 56 | + image = self.image_transforms(image) |
| 57 | + |
| 58 | + return image, fileName |
| 59 | + |
| 60 | +class PreprocessedSatelliteDataset(Dataset): |
| 61 | + """ |
| 62 | + Dataset class for preprocessed satellite imagery. |
| 63 | + """ |
| 64 | + |
| 65 | + def __init__(self, data_path, dataframe=None, image_transforms=None, label_transforms=None, joint_transforms=None, use_weighted_sampler=False, |
| 66 | + use_weighting_quantile=None, use_memmap=False, remove_corrupt=True, load_labels=True, patch_size=512): |
| 67 | + self.use_memmap = use_memmap |
| 68 | + self.patch_size = patch_size |
| 69 | + self.load_labels = load_labels # If False, we only load the images and not the labels |
| 70 | + df = pd.read_csv(dataframe) |
| 71 | + |
| 72 | + if remove_corrupt: |
| 73 | + old_len = len(df) |
| 74 | + #df = df[df["missing_s2_flag"] == False] # Use only the rows that are not corrupt, i.e. those where df["missing_s2_flag"] == False |
| 75 | + |
| 76 | + # Use only the rows that are not corrupt, i.e. those where df["has_corrupt_s2_channel_flag"] == False |
| 77 | + df = df[df["has_corrupt_s2_channel_flag"] == False] |
| 78 | + sys.stdout.write(f"Removed {old_len - len(df)} corrupt rows.\n") |
| 79 | + |
| 80 | + self.files = list(df["paths"].apply(lambda x: os.path.join(data_path, x))) |
| 81 | + |
| 82 | + if use_weighted_sampler not in [False, None]: |
| 83 | + assert use_weighted_sampler in ['g5', 'g10', 'g15', 'g20', 'g25', 'g30'] |
| 84 | + weighting_quantile = use_weighting_quantile |
| 85 | + assert weighting_quantile in [None, 'None'] or int(weighting_quantile) == weighting_quantile, "weighting_quantile must be an integer." |
| 86 | + if weighting_quantile in [None, 'None']: |
| 87 | + self.weights = (df[use_weighted_sampler] / df["totals"]).values.clip(0., 1.) |
| 88 | + else: |
| 89 | + # We do not clip between 0 and 1, but rather between the weighting_quantile and 1. |
| 90 | + weighting_quantile = float(weighting_quantile) |
| 91 | + self.weights = (df[use_weighted_sampler] / df["totals"]).values |
| 92 | + |
| 93 | + # Compute the quantiles, ignoring nan values and zero values |
| 94 | + tmp_weights = self.weights.copy() |
| 95 | + tmp_weights[np.isnan(tmp_weights)] = 0. |
| 96 | + tmp_weights = tmp_weights[tmp_weights > 0.] |
| 97 | + |
| 98 | + quantile_min = np.nanquantile(tmp_weights, weighting_quantile / 100) |
| 99 | + sys.stdout.write(f"Computed weighting {weighting_quantile}-quantile-lower bound: {quantile_min}.\n") |
| 100 | + |
| 101 | + # Clip the weights |
| 102 | + self.weights = self.weights.clip(quantile_min, 1.0) |
| 103 | + |
| 104 | + # Set the nan values to 0. |
| 105 | + self.weights[np.isnan(self.weights)] = 0. |
| 106 | + |
| 107 | + else: |
| 108 | + self.weights = None |
| 109 | + self.image_transforms, self.label_transforms, self.joint_transforms = image_transforms, label_transforms, joint_transforms |
| 110 | + |
| 111 | + def __len__(self): |
| 112 | + return len(self.files) |
| 113 | + |
| 114 | + def __getitem__(self, index): |
| 115 | + if self.use_memmap: |
| 116 | + item = self.getitem_memmap(index) |
| 117 | + else: |
| 118 | + item = self.getitem_classic(index) |
| 119 | + |
| 120 | + return item |
| 121 | + |
| 122 | + def getitem_memmap(self, index): |
| 123 | + file = self.files[index] |
| 124 | + with np.load(file, mmap_mode='r') as npz_file: |
| 125 | + image = npz_file['data'].astype(np.float32) |
| 126 | + # Move the channel axis to the last position (required for torchvision transforms) |
| 127 | + image = np.moveaxis(image, 0, -1) |
| 128 | + if self.image_transforms: |
| 129 | + image = self.image_transforms(image) |
| 130 | + if self.load_labels: |
| 131 | + label = npz_file['labels'].astype(np.float32) |
| 132 | + |
| 133 | + # Process label |
| 134 | + label = label[:3] # Everything after index/granule 3 is irrelevant |
| 135 | + label = label / 100 # Convert from cm to m |
| 136 | + label = np.moveaxis(label, 0, -1) |
| 137 | + |
| 138 | + if self.label_transforms: |
| 139 | + label = self.label_transforms(label) |
| 140 | + if self.joint_transforms: |
| 141 | + image, label = self.joint_transforms(image, label) |
| 142 | + return image, label |
| 143 | + |
| 144 | + return image |
| 145 | + |
| 146 | + def getitem_classic(self, index): |
| 147 | + file = self.files[index] |
| 148 | + data = np.load(file) |
| 149 | + |
| 150 | + image = data["data"].astype(np.float32) |
| 151 | + # Move the channel axis to the last position (required for torchvision transforms) |
| 152 | + image = np.moveaxis(image, 0, -1)[:self.patch_size,:self.patch_size] |
| 153 | + if self.image_transforms: |
| 154 | + image = self.image_transforms(image) |
| 155 | + if self.load_labels: |
| 156 | + label = data["labels"].astype(np.float32) |
| 157 | + |
| 158 | + # Process label |
| 159 | + label = label[:3] # Everything after index 3 is irrelevant |
| 160 | + label = label[:,:self.patch_size, :self.patch_size] |
| 161 | + label = label / 100 # Convert from cm to m |
| 162 | + label = np.moveaxis(label, 0, -1) |
| 163 | + |
| 164 | + if self.label_transforms: |
| 165 | + label = self.label_transforms(label) |
| 166 | + if self.joint_transforms: |
| 167 | + image, label = self.joint_transforms(image, label) |
| 168 | + return image, label |
| 169 | + |
| 170 | + return image |
0 commit comments