rslearn 0.0.11__py3-none-any.whl → 0.0.12__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,228 +0,0 @@
1
- """Copernicus FM model."""
2
-
3
- import logging
4
- import math
5
- from enum import Enum
6
- from pathlib import Path
7
-
8
- import torch
9
- import torch.nn.functional as F
10
- from einops import rearrange
11
- from huggingface_hub import hf_hub_download
12
-
13
- from .copernicusfm_src.model_vit import vit_base_patch16
14
-
15
- logger = logging.getLogger(__name__)
16
-
17
-
18
- class CopernicusFMModality(Enum):
19
- """Modality for Copernicus FM."""
20
-
21
- SENTINEL2_L2A = "sentinel2_l2a"
22
- SENTINEL1 = "sentinel1"
23
-
24
-
25
- MODALITY_TO_WAVELENGTH_BANDWIDTHS: dict[str, dict[str, list]] = {
26
- # https://github.com/zhu-xlab/Copernicus-FM/blob/main/Copernicus-Bench/src/configs/dataset/cobench_eurosat_s2.yaml
27
- CopernicusFMModality.SENTINEL2_L2A.value: {
28
- "band_names": [
29
- "B01",
30
- "B02",
31
- "B03",
32
- "B04",
33
- "B05",
34
- "B06",
35
- "B07",
36
- "B08",
37
- "B8A",
38
- "B09",
39
- "B10",
40
- "B11",
41
- "B12",
42
- ],
43
- "band_wavelengths": [
44
- 440,
45
- 490,
46
- 560,
47
- 665,
48
- 705,
49
- 740,
50
- 783,
51
- 842,
52
- 860,
53
- 940,
54
- 1370,
55
- 1610,
56
- 2190,
57
- ],
58
- "band_bandwidths": [20, 65, 35, 30, 15, 15, 20, 115, 20, 20, 30, 90, 180],
59
- },
60
- # https://github.com/zhu-xlab/Copernicus-FM/blob/main/Copernicus-Bench/src/configs/dataset/cobench_eurosat_s1.yaml
61
- CopernicusFMModality.SENTINEL1.value: {
62
- "band_names": ["vv", "vh"],
63
- "band_wavelengths": [50000000, 50000000],
64
- "band_bandwidths": [1e9, 1e9],
65
- },
66
- }
67
-
68
- HF_REPO_ID = "wangyi111/Copernicus-FM"
69
- HF_REPO_REVISION = "e1db406d517a122c8373802e1c130c5fc4789f84"
70
- HF_FILENAME = "CopernicusFM_ViT_base_varlang_e100.pth"
71
-
72
-
73
- class CopernicusFM(torch.nn.Module):
74
- """Wrapper for Copernicus FM to ingest Masked Helios Sample."""
75
-
76
- image_resolution = 224
77
- patch_size = 16
78
- input_mode = "spectral"
79
- # Don't need this as band order is provided
80
- supported_modalities = [
81
- CopernicusFMModality.SENTINEL2_L2A.value,
82
- CopernicusFMModality.SENTINEL1.value,
83
- ]
84
-
85
- def __init__(
86
- self,
87
- band_order: dict[str, list[str]],
88
- cache_dir: str | Path | None = None,
89
- ) -> None:
90
- """Initialize the Copernicus FM wrapper.
91
-
92
- Args:
93
- band_order: The band order for each modality that will be used. The bands
94
- can be provided in any order, and any subset can be used.
95
- cache_dir: The directory to cache the weights. If None, a default directory
96
- managed by huggingface_hub is used. The weights are downloaded from
97
- Hugging Face (https://huggingface.co/wangyi111/Copernicus-FM).
98
- """
99
- super().__init__()
100
-
101
- # Make sure all keys in band_order are in supported_modalities.
102
- for modality_name in band_order.keys():
103
- if modality_name in self.supported_modalities:
104
- continue
105
- raise ValueError(
106
- f"band_order contains unsupported modality {modality_name}"
107
- )
108
-
109
- # global_pool=True so that we initialize the fc_norm layer
110
- self.model = vit_base_patch16(num_classes=10, global_pool=True)
111
-
112
- # Load weights, downloading if needed.
113
- local_fname = hf_hub_download(
114
- repo_id=HF_REPO_ID,
115
- revision=HF_REPO_REVISION,
116
- filename=HF_FILENAME,
117
- local_dir=cache_dir,
118
- ) # nosec
119
- state_dict = torch.load(local_fname, weights_only=True)
120
- self.model.load_state_dict(state_dict, strict=False)
121
-
122
- # take MODALITY_TO_WAVELENGTH_BANDWIDTHS and rearrange it so that it has the same
123
- # ordering as the user-provided band order.
124
- self.modality_to_wavelength_bandwidths = {}
125
- for modality in self.supported_modalities:
126
- if modality not in band_order:
127
- continue
128
-
129
- wavelength_bandwidths = MODALITY_TO_WAVELENGTH_BANDWIDTHS[modality]
130
- wavelengths = []
131
- bandwidths = []
132
- for b in band_order[modality]:
133
- cfm_idx = wavelength_bandwidths["band_names"].index(b)
134
- wavelengths.append(wavelength_bandwidths["band_wavelengths"][cfm_idx])
135
- bandwidths.append(wavelength_bandwidths["band_bandwidths"][cfm_idx])
136
- self.modality_to_wavelength_bandwidths[modality] = {
137
- "band_bandwidths": bandwidths,
138
- "band_wavelengths": wavelengths,
139
- }
140
-
141
- def _resize_data(self, data: torch.Tensor) -> torch.Tensor:
142
- """Process individual modality data.
143
-
144
- Args:
145
- data: Input tensor of shape [B, C, H, W]
146
-
147
- Returns:
148
- list of tensors of shape [B, C, H, W]
149
- """
150
- # Get original dimensions
151
- original_height = data.shape[2]
152
- new_height = self.patch_size if original_height == 1 else self.image_resolution
153
- data = F.interpolate(
154
- data,
155
- size=(new_height, new_height),
156
- mode="bilinear",
157
- align_corners=False,
158
- )
159
- return data
160
-
161
- def prepare_input(
162
- self,
163
- inputs: dict[str, torch.Tensor],
164
- ) -> tuple[torch.Tensor, list[int], list[int]]:
165
- """Prepare input for the CopernicusFM model from MaskedHeliosSample."""
166
- wavelengths: list[int] = []
167
- bandwidths: list[int] = []
168
- all_processed_data: list[list[torch.Tensor]] = []
169
- for modality in inputs.keys():
170
- if modality not in self.supported_modalities:
171
- logger.debug(
172
- f"Skipping modality {modality} as it is not in the supported "
173
- f"modalities list {self.supported_modalities}"
174
- )
175
- continue
176
-
177
- data = inputs[modality]
178
-
179
- if data is None:
180
- continue
181
-
182
- all_processed_data.append(self._resize_data(data))
183
- wavelengths.extend(
184
- self.modality_to_wavelength_bandwidths[modality]["band_wavelengths"]
185
- )
186
- bandwidths.extend(
187
- self.modality_to_wavelength_bandwidths[modality]["band_bandwidths"]
188
- )
189
-
190
- concatenated_processed_data = torch.cat(all_processed_data, dim=1)
191
- return concatenated_processed_data, wavelengths, bandwidths
192
-
193
- def forward(
194
- self,
195
- inputs: list[dict[str, torch.Tensor]],
196
- ) -> torch.Tensor:
197
- """Forward pass through CopernicusFM model."""
198
- batch_inputs = {
199
- key: torch.stack([inp[key] for inp in inputs], dim=0)
200
- for key in inputs[0].keys()
201
- }
202
- # Prepare input
203
- data, wavelengths, bandwidths = self.prepare_input(batch_inputs)
204
- meta = torch.full(
205
- (1, 4), float("nan"), device=data.device
206
- ) # [lon, lat, delta_time, patch_token_area], assume unknown
207
- # "The embed tensor contains the encoded image features, which can be used for downstream tasks."
208
- _, timestep_output = self.model(
209
- data,
210
- meta,
211
- wavelengths,
212
- bandwidths,
213
- None,
214
- self.input_mode,
215
- self.patch_size,
216
- )
217
- # no norm, following
218
- # https://github.com/zhu-xlab/Copernicus-FM/blob/main/Copernicus-Bench/src/foundation_models/CopernicusFM/models_dwv_seg.py
219
- side = math.isqrt(timestep_output.shape[1])
220
- output_features = rearrange(
221
- timestep_output, "b (h w) c -> b c h w ", h=side, w=side
222
- )
223
- return [output_features]
224
-
225
- def get_backbone_channels(self) -> list[tuple[int, int]]:
226
- """Returns the output channels of this model when used as a backbone."""
227
- # TODO: load this from a constant depending on the model size
228
- return [(self.patch_size, 768)]
@@ -1 +0,0 @@
1
- # mypy: ignore-errors
@@ -1,50 +0,0 @@
1
- """Copyright (c) Microsoft Corporation. Licensed under the MIT license."""
2
-
3
- import torch
4
-
5
- __all__ = ["area", "radius_earth"]
6
-
7
-
8
- # float: Radius of the earth in kilometers.
9
- radius_earth = 6378137 / 1000
10
-
11
-
12
- def area(polygon: torch.Tensor) -> torch.Tensor:
13
- """Compute the area of a polygon specified by latitudes and longitudes in degrees.
14
-
15
- This function is a PyTorch port of the PyPI package `area`. In particular, it is heavily
16
- inspired by the following file:
17
-
18
- https://github.com/scisco/area/blob/9d9549d6ebffcbe4bffe11b71efa2d406d1c9fe9/area/__init__.py
19
-
20
- Args:
21
- polygon (:class:`torch.Tensor`): Polygon of the shape `(*b, n, 2)` where `b` is an optional
22
- multidimensional batch size, `n` is the number of points of the polygon, and 2
23
- concatenates first latitudes and then longitudes. The polygon does not have be closed.
24
-
25
- Returns:
26
- :class:`torch.Tensor`: Area in square kilometers.
27
- """
28
- # Be sure to close the loop.
29
- polygon = torch.cat((polygon, polygon[..., -1:, :]), axis=-2)
30
-
31
- area = torch.zeros(polygon.shape[:-2], dtype=polygon.dtype, device=polygon.device)
32
- n = polygon.shape[-2] # Number of points of the polygon
33
-
34
- rad = torch.deg2rad # Convert degrees to radians.
35
-
36
- if n > 2:
37
- for i in range(n):
38
- i_lower = i
39
- i_middle = (i + 1) % n
40
- i_upper = (i + 2) % n
41
-
42
- lon_lower = polygon[..., i_lower, 1]
43
- lat_middle = polygon[..., i_middle, 0]
44
- lon_upper = polygon[..., i_upper, 1]
45
-
46
- area = area + (rad(lon_upper) - rad(lon_lower)) * torch.sin(rad(lat_middle))
47
-
48
- area = area * radius_earth * radius_earth / 2
49
-
50
- return torch.abs(area)
@@ -1,134 +0,0 @@
1
- # type: ignore
2
- """Copyright (c) Microsoft Corporation. Licensed under the MIT license."""
3
-
4
- import math
5
-
6
- import numpy as np
7
- import torch
8
- import torch.nn as nn
9
-
10
- from .area import area, radius_earth
11
-
12
- __all__ = [
13
- "FourierExpansion",
14
- "pos_expansion",
15
- "scale_expansion",
16
- "lead_time_expansion",
17
- "levels_expansion",
18
- "absolute_time_expansion",
19
- ]
20
-
21
-
22
- class FourierExpansion(nn.Module):
23
- """A Fourier series-style expansion into a high-dimensional space.
24
-
25
- Attributes:
26
- lower (float): Lower wavelength.
27
- upper (float): Upper wavelength.
28
- assert_range (bool): Assert that the encoded tensor is within the specified wavelength
29
- range.
30
- """
31
-
32
- def __init__(self, lower: float, upper: float, assert_range: bool = True) -> None:
33
- """Initialise.
34
-
35
- Args:
36
- lower (float): Lower wavelength.
37
- upper (float): Upper wavelength.
38
- assert_range (bool, optional): Assert that the encoded tensor is within the specified
39
- wavelength range. Defaults to `True`.
40
- """
41
- super().__init__()
42
- self.lower = lower
43
- self.upper = upper
44
- self.assert_range = assert_range
45
-
46
- def forward(self, x: torch.Tensor, d: int) -> torch.Tensor:
47
- """Perform the expansion.
48
-
49
- Adds a dimension of length `d` to the end of the shape of `x`.
50
-
51
- Args:
52
- x (:class:`torch.Tensor`): Input to expand of shape `(..., n)`. All elements of `x` must
53
- lie within `[self.lower, self.upper]` if `self.assert_range` is `True`.
54
- d (int): Dimensionality. Must be a multiple of two.
55
-
56
- Raises:
57
- AssertionError: If `self.assert_range` is `True` and not all elements of `x` are not
58
- within `[self.lower, self.upper]`.
59
- ValueError: If `d` is not a multiple of two.
60
-
61
- Returns:
62
- torch.Tensor: Fourier series-style expansion of `x` of shape `(..., n, d)`.
63
- """
64
- # If the input is not within the configured range, the embedding might be ambiguous!
65
- in_range = torch.logical_and(
66
- self.lower <= x.abs(), torch.all(x.abs() <= self.upper)
67
- )
68
- in_range_or_zero = torch.all(
69
- torch.logical_or(in_range, x == 0)
70
- ) # Allow zeros to pass through.
71
- if self.assert_range and not in_range_or_zero:
72
- raise AssertionError(
73
- f"The input tensor is not within the configured range"
74
- f" `[{self.lower}, {self.upper}]`."
75
- )
76
-
77
- # We will use half of the dimensionality for `sin` and the other half for `cos`.
78
- if not (d % 2 == 0):
79
- raise ValueError("The dimensionality must be a multiple of two.")
80
-
81
- # Always perform the expansion with `float64`s to avoid numerical accuracy shenanigans.
82
- x = x.double()
83
-
84
- wavelengths = torch.logspace(
85
- math.log10(self.lower),
86
- math.log10(self.upper),
87
- d // 2,
88
- base=10,
89
- device=x.device,
90
- dtype=x.dtype,
91
- )
92
- prod = torch.einsum("...i,j->...ij", x, 2 * np.pi / wavelengths)
93
- encoding = torch.cat((torch.sin(prod), torch.cos(prod)), dim=-1)
94
-
95
- return encoding.float() # Cast to `float32` to avoid incompatibilities.
96
-
97
-
98
- # Determine a reasonable smallest value for the scale embedding by assuming a smallest delta in
99
- # latitudes and longitudes.
100
- _delta = 0.01 # Reasonable smallest delta in latitude and longitude
101
- _min_patch_area: float = area(
102
- torch.tensor(
103
- [
104
- # The smallest patches will be at the poles. Just use the north pole.
105
- [90, 0],
106
- [90, _delta],
107
- [90 - _delta, _delta],
108
- [90 - _delta, 0],
109
- ],
110
- dtype=torch.float64,
111
- )
112
- ).item()
113
- _area_earth = 4 * np.pi * radius_earth * radius_earth
114
-
115
- pos_expansion = FourierExpansion(_delta, 720)
116
-
117
-
118
- scale_expansion = FourierExpansion(_min_patch_area, _area_earth)
119
-
120
-
121
- lead_time_expansion = FourierExpansion(1 / 60, 24 * 7 * 3)
122
-
123
- levels_expansion = FourierExpansion(0.01, 1e5)
124
-
125
- absolute_time_expansion = FourierExpansion(1, 24 * 365.25, assert_range=False)
126
-
127
- ### new for SSL4EO-S ###
128
- # min wavelength: ultraviolet light (100 nm)
129
- # max wavelength: radio waves (1 m)
130
- spectrum_central_expansion = FourierExpansion(1e-7, 1)
131
-
132
- # min bandwidth: 10nm
133
- # max bandwidth: 1m
134
- spectrum_width_expansion = FourierExpansion(1e-7, 1)