Skip to content

ALOSDataset

ALOSDataset(
    volpath: str = None,
    transform=None,
    crop_coordinates: tuple = None,
    patch_size: tuple = (128, 128),
    patch_stride: tuple = None,
)

Bases: Dataset

ALOSDataset

The format is described in https://www.eorc.jaxa.jp/ALOS/en/alos-2/pdf/product_format_description/PALSAR-2_xx_Format_CEOS_E_g.pdf

The dataset is constructed from the volume file. If leader and trailer files are colocated, they are loaded as well.

Important, this code has been developed for working with L1.1 HBQ-R Quad Pol datafiles. It is not expected to work out of the box for other levels and for less than 4 polarizations.

Parameters:

  • volpath (str, default: None ) –

    the path to the VOLUME file

  • transform

    the transform applied the cropped image. It applies on a dictionnary of patches {'HH': np.array, 'HV': np.array}

  • crop_coordinates (tuple, default: None ) –

    the subpart of the image to consider as ((row_i, col_i), (row_j, col_j)) defining the corner coordinates

  • patch_size (tuple, default: (128, 128) ) –

    the dimensions of the patches to consider (rows, cols)

  • patch_stride (tuple, default: None ) –

    the shift between two consecutive patches, default:patch_size

Source code in src/torchcvnn/datasets/alos2/dataset.py
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def __init__(
    self,
    volpath: str = None,
    transform=None,
    crop_coordinates: tuple = None,
    patch_size: tuple = (128, 128),
    patch_stride: tuple = None,
):
    super().__init__()

    self.transform = transform

    self.patch_size = patch_size
    self.patch_stride = patch_stride
    if patch_stride is None:
        self.patch_stride = patch_size

    self.volFile = VolFile(volpath)

    leader_filepath = volpath.parents[0] / volpath.name.replace("VOL-", "LED-")
    self.leaderFile = None
    if leader_filepath.exists():
        self.leaderFile = LeaderFile(leader_filepath)

    trailer_filepath = volpath.parents[0] / volpath.name.replace("VOL-", "TRL-")
    self.trailerFile = None
    if trailer_filepath.exists():
        self.trailerFile = TrailerFile(trailer_filepath)

    self.crop_coordinates = None
    if crop_coordinates is not None:
        self.crop_coordinates = crop_coordinates

    self.images = {}
    for pol in ["HH", "HV", "VH", "VV"]:
        filepath = volpath.parents[0] / volpath.name.replace("VOL-", f"IMG-{pol}-")
        if not filepath.exists():
            continue
        self.images[pol] = SARImage(filepath)
        if self.crop_coordinates is None:
            self.crop_coordinates = (
                (0, 0),
                (self.images[pol].num_rows, self.images[pol].num_cols),
            )

    if len(self.images) != self.volFile.num_polarizations:
        raise RuntimeError(
            f"I was expecting {self.volFile.num_polarizations} data file but I found {len(self.images)} data file"
        )

    # Precompute the dimension of the grid of patches
    nrows = self.crop_coordinates[1][0] - self.crop_coordinates[0][0]
    ncols = self.crop_coordinates[1][1] - self.crop_coordinates[0][1]

    nrows_patch, ncols_patch = self.patch_size
    row_stride, col_stride = self.patch_stride

    self.nsamples_per_rows = (nrows - nrows_patch) // row_stride + 1
    self.nsamples_per_cols = (ncols - ncols_patch) // col_stride + 1

__getitem__

__getitem__(idx: int)

Access and returns the subpatch specified by the index

Parameters:

  • idx (int) –

    the index of the patch to access

Source code in src/torchcvnn/datasets/alos2/dataset.py
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
def __getitem__(self, idx: int):
    """
    Access and returns the subpatch specified by the index

    Arguments:
        idx: the index of the patch to access
    """
    row_stride, col_stride = self.patch_stride
    start_row = (
        self.crop_coordinates[0][0] + (idx // self.nsamples_per_cols) * row_stride
    )
    start_col = (
        self.crop_coordinates[0][1] + (idx % self.nsamples_per_cols) * col_stride
    )
    num_rows, num_cols = self.patch_size
    patches = {
        pol: im.read_patch(start_row, num_rows, start_col, num_cols)
        * self.leaderFile.calibration_factor
        for pol, im in self.images.items()
    }

    if self.transform is not None:
        patches = self.transform(patches)
    else:
        patches = np.stack([patchi for _, patchi in patches.items()])

    return patches

__len__

__len__() -> int

Returns the length of the dataset according to the patch size, stride and image size

Returns:

  • int ( int ) –

    the total number of available patches

Source code in src/torchcvnn/datasets/alos2/dataset.py
137
138
139
140
141
142
143
144
145
146
def __len__(self) -> int:
    """
    Returns the length of the dataset according to the patch size, stride
    and image size

    Returns:
        int: the total number of available patches
    """

    return self.nsamples_per_rows * self.nsamples_per_cols