Skip to content

Remote Sensing API

Auto-generated from source — see Remote Sensing for narrative descriptions.

UNet

keras_climate.remote_sensing.unet.UNet

UNet(input_shape=(256, 256, 3), num_classes=1, base_filters=64, depth=4, final_activation=None, name='unet')
Source code in keras_climate/remote_sensing/unet.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def UNet(
    input_shape=(256, 256, 3),
    num_classes=1,
    base_filters=64,
    depth=4,
    final_activation=None,
    name="unet",
):
    inputs = keras.Input(shape=input_shape, name="image")
    x = inputs

    skips = []
    filters = base_filters
    for i in range(depth):
        x = DoubleConv(filters, name=f"enc{i}")(x)
        skips.append(x)
        x = layers.MaxPooling2D(2, name=f"pool{i}")(x)
        filters *= 2

    x = DoubleConv(filters, name="bottleneck")(x)

    for i in reversed(range(depth)):
        filters //= 2
        x = layers.Conv2DTranspose(
            filters, 2, strides=2, padding="same", name=f"upconv{i}"
        )(x)
        skip = skips[i]
        x = layers.Concatenate(name=f"concat{i}")([skip, x])
        x = DoubleConv(filters, name=f"dec{i}")(x)

    outputs = layers.Conv2D(num_classes, 1, activation=final_activation, name="logits")(
        x
    )

    return keras.Model(inputs, outputs, name=name)

keras_climate.remote_sensing.unet.unet_config

unet_config(variant='base')
Source code in keras_climate/remote_sensing/unet.py
43
44
45
46
47
48
49
def unet_config(variant="base"):
    presets = {
        "small": dict(base_filters=32, depth=4),
        "base": dict(base_filters=64, depth=4),
        "large": dict(base_filters=64, depth=5),
    }
    return presets[variant]

DeepLabV3+

keras_climate.remote_sensing.deeplabv3plus.DeepLabV3Plus

DeepLabV3Plus(input_shape=(512, 512, 3), num_classes=1, backbone_layers=(3, 4, 23, 3), output_stride=16, aspp_filters=256, decoder_filters=48, final_activation=None, name='deeplabv3plus')
Source code in keras_climate/remote_sensing/deeplabv3plus.py
 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def DeepLabV3Plus(
    input_shape=(512, 512, 3),
    num_classes=1,
    backbone_layers=(3, 4, 23, 3),
    output_stride=16,
    aspp_filters=256,
    decoder_filters=48,
    final_activation=None,
    name="deeplabv3plus",
):
    inputs = keras.Input(shape=input_shape, name="image")

    low_level_feat, x = resnet_backbone(
        inputs, backbone_layers, output_stride, name="backbone"
    )

    x = ASPP(filters=aspp_filters, name="aspp")(x)

    x = layers.Lambda(
        lambda t: ops.image.resize(
            t[0], ops.shape(t[1])[1:3], interpolation="bilinear"
        ),
        name="upsample_to_low_level",
    )([x, low_level_feat])

    low_level_feat = ConvBNAct(decoder_filters, 1, name="low_level_project")(
        low_level_feat
    )

    x = layers.Concatenate(name="decoder_concat")([x, low_level_feat])
    x = ConvBNAct(256, 3, name="decoder_conv1")(x)
    x = ConvBNAct(256, 3, name="decoder_conv2")(x)

    x = layers.Lambda(
        lambda t: ops.image.resize(
            t, (input_shape[0], input_shape[1]), interpolation="bilinear"
        ),
        name="upsample_to_input",
    )(x)

    outputs = layers.Conv2D(num_classes, 1, activation=final_activation, name="logits")(
        x
    )

    return keras.Model(inputs, outputs, name=name)

keras_climate.remote_sensing.deeplabv3plus.deeplabv3plus_config

deeplabv3plus_config(variant='resnet50')
Source code in keras_climate/remote_sensing/deeplabv3plus.py
135
136
137
138
139
140
def deeplabv3plus_config(variant="resnet50"):
    presets = {
        "resnet50": dict(backbone_layers=(3, 4, 6, 3)),
        "resnet101": dict(backbone_layers=(3, 4, 23, 3)),
    }
    return presets[variant]

SegFormer

keras_climate.remote_sensing.segformer.SegFormer

SegFormer(input_shape=(512, 512, 3), num_classes=1, variant='b0', final_activation=None, name='segformer')
Source code in keras_climate/remote_sensing/segformer.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
def SegFormer(
    input_shape=(512, 512, 3),
    num_classes=1,
    variant="b0",
    final_activation=None,
    name="segformer",
):
    cfg = MIT_CONFIGS[variant]
    inputs = keras.Input(shape=input_shape, name="image")

    features = mit_encoder(inputs, cfg, name=f"{name}_backbone")
    x = segformer_decode_head(
        features,
        cfg["decoder_dim"],
        num_classes,
        target_hw=(input_shape[0], input_shape[1]),
        name=f"{name}_decode_head",
    )

    if final_activation:
        x = layers.Activation(final_activation, name="logits_act")(x)

    return keras.Model(inputs, x, name=f"{name}_{variant}")

keras_climate.remote_sensing.segformer.MIT_CONFIGS module-attribute

MIT_CONFIGS = {'b0': dict(embed_dims=[32, 64, 160, 256], depths=[2, 2, 2, 2], num_heads=[1, 2, 5, 8], sr_ratios=[8, 4, 2, 1], decoder_dim=256), 'b1': dict(embed_dims=[64, 128, 320, 512], depths=[2, 2, 2, 2], num_heads=[1, 2, 5, 8], sr_ratios=[8, 4, 2, 1], decoder_dim=256), 'b2': dict(embed_dims=[64, 128, 320, 512], depths=[3, 4, 6, 3], num_heads=[1, 2, 5, 8], sr_ratios=[8, 4, 2, 1], decoder_dim=768), 'b3': dict(embed_dims=[64, 128, 320, 512], depths=[3, 4, 18, 3], num_heads=[1, 2, 5, 8], sr_ratios=[8, 4, 2, 1], decoder_dim=768), 'b4': dict(embed_dims=[64, 128, 320, 512], depths=[3, 8, 27, 3], num_heads=[1, 2, 5, 8], sr_ratios=[8, 4, 2, 1], decoder_dim=768), 'b5': dict(embed_dims=[64, 128, 320, 512], depths=[3, 6, 40, 3], num_heads=[1, 2, 5, 8], sr_ratios=[8, 4, 2, 1], decoder_dim=768)}

SatMAE

keras_climate.remote_sensing.satmae.SatMAE

SatMAE(img_size=224, patch_size=16, in_chans=3, embed_dim=768, depth=12, num_heads=12, decoder_embed_dim=512, decoder_depth=8, decoder_num_heads=16, mode='single', num_groups=3, num_frames=3, mask_ratio=0.75, name='satmae')
Source code in keras_climate/remote_sensing/satmae.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
def SatMAE(
    img_size=224,
    patch_size=16,
    in_chans=3,
    embed_dim=768,
    depth=12,
    num_heads=12,
    decoder_embed_dim=512,
    decoder_depth=8,
    decoder_num_heads=16,
    mode="single",
    num_groups=3,
    num_frames=3,
    mask_ratio=0.75,
    name="satmae",
):
    if mode == "single":
        inp_shape = (img_size, img_size, in_chans)
    elif mode == "multispectral":
        inp_shape = (num_groups, img_size, img_size, in_chans)
    else:
        inp_shape = (num_frames, img_size, img_size, in_chans)

    inputs = keras.Input(shape=inp_shape, name="image")
    encoder = SatMAEEncoder(
        img_size,
        patch_size,
        in_chans,
        embed_dim,
        depth,
        num_heads,
        mode=mode,
        num_groups=num_groups,
        num_frames=num_frames,
        name=f"{name}_encoder",
    )
    tokens, mask, ids_restore = encoder(
        inputs, apply_masking=True, mask_ratio=mask_ratio
    )

    num_patches = (img_size // patch_size) ** 2
    num_repeats = 1
    if mode == "multispectral":
        num_repeats = num_groups
    elif mode == "temporal":
        num_repeats = num_frames
    num_patches *= num_repeats

    decoder = SatMAEDecoder(
        num_patches,
        patch_size,
        in_chans,
        decoder_embed_dim,
        decoder_depth,
        decoder_num_heads,
        embed_dim,
        num_repeats=num_repeats,
        name=f"{name}_decoder",
    )
    pred = decoder(tokens, ids_restore)

    return keras.Model(inputs, [pred, mask], name=name)

keras_climate.remote_sensing.satmae.SatMAEEncoder

SatMAEEncoder(img_size=224, patch_size=16, in_chans=3, embed_dim=768, depth=12, num_heads=12, mlp_ratio=4.0, mode='single', num_groups=3, num_frames=3, name='satmae_encoder', **kwargs)

Bases: Model

Source code in keras_climate/remote_sensing/satmae.py
 47
 48
 49
 50
 51
 52
 53
 54
 55
 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
def __init__(
    self,
    img_size=224,
    patch_size=16,
    in_chans=3,
    embed_dim=768,
    depth=12,
    num_heads=12,
    mlp_ratio=4.0,
    mode="single",
    num_groups=3,
    num_frames=3,
    name="satmae_encoder",
    **kwargs,
):
    super().__init__(name=name, **kwargs)
    self.img_size = img_size
    self.patch_size = patch_size
    self.embed_dim = embed_dim
    self.mode = mode
    self.num_groups = num_groups
    self.num_frames = num_frames

    self.patch_embed = PatchEmbed2D(
        patch_size, embed_dim, norm=False, name="patch_embed"
    )
    self.cls_token = self.add_weight(
        shape=(1, 1, embed_dim),
        initializer="zeros",
        trainable=True,
        name="cls_token",
    )

    num_patches = (img_size // patch_size) ** 2
    pos = sincos_position_embedding_2d(
        img_size // patch_size, img_size // patch_size, embed_dim
    )
    pos = np.concatenate([np.zeros((1, embed_dim), dtype=np.float32), pos], axis=0)
    self.pos_embed = self.add_weight(
        shape=pos.shape,
        initializer=keras.initializers.Constant(pos),
        trainable=False,
        name="pos_embed",
    )

    if mode == "multispectral":
        self.group_embed = self.add_weight(
            shape=(num_groups, 1, embed_dim),
            initializer="zeros",
            trainable=True,
            name="group_embed",
        )
    elif mode == "temporal":
        self.temporal_embed = self.add_weight(
            shape=(num_frames, 1, embed_dim),
            initializer="zeros",
            trainable=True,
            name="temporal_embed",
        )

    self.blocks = [
        TransformerEncoderBlock(embed_dim, num_heads, mlp_ratio, name=f"block{i}")
        for i in range(depth)
    ]
    self.norm = layers.LayerNormalization(epsilon=1e-6, name="norm")

keras_climate.remote_sensing.satmae.SatMAEDecoder

SatMAEDecoder(num_patches, patch_size, in_chans, decoder_embed_dim=512, decoder_depth=8, decoder_num_heads=16, encoder_embed_dim=768, num_repeats=1, name='satmae_decoder', **kwargs)

Bases: Model

Source code in keras_climate/remote_sensing/satmae.py
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
def __init__(
    self,
    num_patches,
    patch_size,
    in_chans,
    decoder_embed_dim=512,
    decoder_depth=8,
    decoder_num_heads=16,
    encoder_embed_dim=768,
    num_repeats=1,
    name="satmae_decoder",
    **kwargs,
):
    super().__init__(name=name, **kwargs)
    self.decoder_embed = layers.Dense(decoder_embed_dim, name="decoder_embed")
    self.mask_token = self.add_weight(
        shape=(1, 1, decoder_embed_dim),
        initializer="zeros",
        trainable=True,
        name="mask_token",
    )
    base_patches = num_patches // num_repeats
    grid_size = int(round(base_patches**0.5))
    if grid_size * grid_size != base_patches:
        raise ValueError(
            f"num_patches // num_repeats ({base_patches}) is not a perfect square; "
            "cannot build a 2D sin-cos grid position embedding."
        )
    base_pos = sincos_position_embedding_2d(grid_size, grid_size, decoder_embed_dim)
    pos = np.tile(base_pos, (num_repeats, 1))
    pos = np.concatenate(
        [np.zeros((1, decoder_embed_dim), dtype=np.float32), pos], axis=0
    )
    self.decoder_pos_embed = self.add_weight(
        shape=pos.shape,
        initializer=keras.initializers.Constant(pos),
        trainable=False,
        name="decoder_pos_embed",
    )
    self.blocks = [
        TransformerEncoderBlock(
            decoder_embed_dim, decoder_num_heads, name=f"block{i}"
        )
        for i in range(decoder_depth)
    ]
    self.norm = layers.LayerNormalization(epsilon=1e-6, name="norm")
    self.pred = layers.Dense(patch_size * patch_size * in_chans, name="pred")
    self.num_patches = num_patches

Scale-MAE

keras_climate.remote_sensing.scalemae.ScaleMAE

ScaleMAE(img_size=224, patch_size=16, in_chans=3, embed_dim=1024, depth=24, num_heads=16, mlp_ratio=4.0, name='scalemae')
Source code in keras_climate/remote_sensing/scalemae.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def ScaleMAE(
    img_size=224,
    patch_size=16,
    in_chans=3,
    embed_dim=1024,
    depth=24,
    num_heads=16,
    mlp_ratio=4.0,
    name="scalemae",
):
    image_in = keras.Input(shape=(img_size, img_size, in_chans), name="image")
    res_in = keras.Input(shape=(), name="res")
    encoder = ScaleMAEEncoder(
        img_size,
        patch_size,
        in_chans,
        embed_dim,
        depth,
        num_heads,
        mlp_ratio,
        name=f"{name}_encoder",
    )
    tokens = encoder([image_in, res_in])
    return keras.Model([image_in, res_in], tokens, name=name)

keras_climate.remote_sensing.scalemae.ScaleMAEEncoder

ScaleMAEEncoder(img_size=224, patch_size=16, in_chans=3, embed_dim=1024, depth=24, num_heads=16, mlp_ratio=4.0, name='scalemae_encoder', **kwargs)

Bases: Model

Source code in keras_climate/remote_sensing/scalemae.py
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
def __init__(
    self,
    img_size=224,
    patch_size=16,
    in_chans=3,
    embed_dim=1024,
    depth=24,
    num_heads=16,
    mlp_ratio=4.0,
    name="scalemae_encoder",
    **kwargs,
):
    super().__init__(name=name, **kwargs)
    self.img_size = img_size
    self.patch_size = patch_size
    self.embed_dim = embed_dim
    grid_size = img_size // patch_size

    self.patch_embed = PatchEmbed2D(
        patch_size, embed_dim, norm=False, name="patch_embed"
    )
    self.cls_token = self.add_weight(
        shape=(1, 1, embed_dim),
        initializer="zeros",
        trainable=True,
        name="cls_token",
    )
    self.pos_embed = GSDPositionalEmbedding(grid_size, embed_dim, name="pos_embed")

    self.blocks = [
        TransformerEncoderBlock(embed_dim, num_heads, mlp_ratio, name=f"block{i}")
        for i in range(depth)
    ]
    self.norm = layers.LayerNormalization(epsilon=1e-6, name="norm")

keras_climate.remote_sensing.scalemae.GSDPositionalEmbedding

GSDPositionalEmbedding(grid_size, dim, **kwargs)

Bases: Layer

Source code in keras_climate/remote_sensing/scalemae.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def __init__(self, grid_size, dim, **kwargs):
    super().__init__(**kwargs)
    self.grid_size = grid_size
    self.dim = dim
    grid_h, grid_w = np.meshgrid(
        np.arange(grid_size, dtype=np.float32),
        np.arange(grid_size, dtype=np.float32),
        indexing="ij",
    )
    self._grid_h_np = grid_h.reshape(-1)
    self._grid_w_np = grid_w.reshape(-1)
    self._omega_np = 1.0 / (
        10000 ** (np.arange(dim // 4, dtype=np.float32) / (dim / 4.0))
    )

RingMo

keras_climate.remote_sensing.ringmo.RingMo

RingMo(img_size=192, in_chans=3, embed_dim=128, depths=(2, 2, 18, 2), num_heads=(4, 8, 16, 32), window_size=6, mlp_ratio=4.0, mask_patch_size=32, mask_ratio=0.6, inside_ratio=0.6, name='ringmo')
Source code in keras_climate/remote_sensing/ringmo.py
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
def RingMo(
    img_size=192,
    in_chans=3,
    embed_dim=128,
    depths=(2, 2, 18, 2),
    num_heads=(4, 8, 16, 32),
    window_size=6,
    mlp_ratio=4.0,
    mask_patch_size=32,
    mask_ratio=0.6,
    inside_ratio=0.6,
    name="ringmo",
):
    inputs = keras.Input(shape=(img_size, img_size, in_chans), name="image")
    masked, mask = PIMask(mask_patch_size, mask_ratio, inside_ratio, name="pi_mask")(
        inputs
    )

    encoder = RingMoEncoder(
        img_size,
        embed_dim,
        depths,
        num_heads,
        window_size,
        mlp_ratio,
        name=f"{name}_encoder",
    )
    tokens = encoder(masked)

    feat = layers.Reshape(
        (encoder.final_grid, encoder.final_grid, encoder.final_dim),
        name="reshape_to_grid",
    )(tokens)

    encoder_stride = 4 * (2 ** (len(depths) - 1))
    pred = SimMIMDecoder(encoder_stride, in_chans, name=f"{name}_decoder")(feat)

    return keras.Model(inputs, [pred, mask], name=name)

keras_climate.remote_sensing.ringmo.RingMoEncoder

RingMoEncoder(img_size=192, embed_dim=128, depths=(2, 2, 18, 2), num_heads=(4, 8, 16, 32), window_size=6, mlp_ratio=4.0, name='ringmo_encoder', **kwargs)

Bases: Model

Source code in keras_climate/remote_sensing/ringmo.py
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
def __init__(
    self,
    img_size=192,
    embed_dim=128,
    depths=(2, 2, 18, 2),
    num_heads=(4, 8, 16, 32),
    window_size=6,
    mlp_ratio=4.0,
    name="ringmo_encoder",
    **kwargs,
):
    super().__init__(name=name, **kwargs)
    self.patch_embed = RingMoPatchEmbed(embed_dim, name="patch_embed")
    grid = img_size // 4
    dim, resolution = embed_dim, grid
    self.stages = []
    for i, (depth, heads) in enumerate(zip(depths, num_heads)):
        downsample = i < len(depths) - 1
        self.stages.append(
            SwinStage(
                dim,
                (resolution, resolution),
                depth,
                heads,
                window_size,
                mlp_ratio,
                downsample=downsample,
                name=f"stage{i}",
            )
        )
        if downsample:
            dim *= 2
            resolution //= 2
    self.norm = layers.LayerNormalization(epsilon=1e-5, name="norm")
    self.final_dim = dim
    self.final_grid = resolution

SSL4EO-S12

keras_climate.remote_sensing.ssl4eo.SSL4EOResNet50

SSL4EOResNet50(input_shape=(224, 224, 13), name='ssl4eo_resnet50')
Source code in keras_climate/remote_sensing/ssl4eo.py
 5
 6
 7
 8
 9
10
def SSL4EOResNet50(input_shape=(224, 224, 13), name="ssl4eo_resnet50"):
    inputs = keras.Input(shape=input_shape, name="image")
    _, features = resnet_backbone(
        inputs, layer_counts=(3, 4, 6, 3), output_stride=32, name="backbone"
    )
    return keras.Model(inputs, features, name=name)

SatCLIP

keras_climate.remote_sensing.satclip.SatCLIPLocationEncoder

SatCLIPLocationEncoder(legendre_polys=10, dim_hidden=512, num_hidden_layers=2, embed_dim=256, w0_initial=30.0, w0=1.0, name='satclip_location_encoder')
Source code in keras_climate/remote_sensing/satclip.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def SatCLIPLocationEncoder(
    legendre_polys=10,
    dim_hidden=512,
    num_hidden_layers=2,
    embed_dim=256,
    w0_initial=30.0,
    w0=1.0,
    name="satclip_location_encoder",
):
    lonlat_in = keras.Input(shape=(2,), name="lonlat")
    x = SphericalHarmonics(legendre_polys, name="posenc")(lonlat_in)

    for i in range(num_hidden_layers):
        x = SirenLayer(
            dim_hidden,
            w0=(w0_initial if i == 0 else w0),
            use_sine=True,
            name=f"layers{i}",
        )(x)
    out = SirenLayer(embed_dim, w0=w0, use_sine=False, name="last_layer")(x)

    return keras.Model(lonlat_in, out, name=name)

keras_climate.remote_sensing.satclip.SphericalHarmonics

SphericalHarmonics(legendre_polys=10, **kwargs)

Bases: Layer

Source code in keras_climate/remote_sensing/satclip.py
 8
 9
10
11
def __init__(self, legendre_polys=10, **kwargs):
    super().__init__(**kwargs)
    self.L = legendre_polys
    self.embedding_dim = self.L * self.L

keras_climate.remote_sensing.satclip.SirenLayer

SirenLayer(units, w0=1.0, use_sine=True, **kwargs)

Bases: Layer

Source code in keras_climate/remote_sensing/satclip.py
73
74
75
76
77
def __init__(self, units, w0=1.0, use_sine=True, **kwargs):
    super().__init__(**kwargs)
    self.units = units
    self.w0 = w0
    self.use_sine = use_sine