-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.py
241 lines (231 loc) · 7.57 KB
/
examples.py
1
2
3
4
5
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
41
42
43
44
45
46
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
from typing import cast
import pystac
import shapely
from dateutil.parser import parse as parse_dt
from pystac.extensions.eo import Band, EOExtension
from pystac.extensions.file import FileExtension
from stac_model.base import ProcessingExpression
from stac_model.input import InputStructure, ModelInput, ValueScalingObject
from stac_model.output import MLMClassification, ModelOutput, ModelResult
from stac_model.schema import ItemMLModelExtension, MLModelExtension, MLModelProperties
def eurosat_resnet() -> ItemMLModelExtension:
input_struct = InputStructure(
shape=[-1, 13, 64, 64],
dim_order=["batch", "channel", "height", "width"],
data_type="float32",
)
band_names = [
"B01",
"B02",
"B03",
"B04",
"B05",
"B06",
"B07",
"B08",
"B8A",
"B09",
"B10",
"B11",
"B12",
]
stats_mean = [
1354.40546513,
1118.24399958,
1042.92983953,
947.62620298,
1199.47283961,
1999.79090914,
2369.22292565,
2296.82608323,
732.08340178,
12.11327804,
1819.01027855,
1118.92391149,
2594.14080798,
]
stats_stddev = [
245.71762908,
333.00778264,
395.09249139,
593.75055589,
566.4170017,
861.18399006,
1086.63139075,
1117.98170791,
404.91978886,
4.77584468,
1002.58768311,
761.30323499,
1231.58581042,
]
value_scaling = [
cast(
ValueScalingObject,
dict(
type="z-score",
mean=mean,
stddev=stddev,
),
)
for mean, stddev in zip(stats_mean, stats_stddev, strict=False)
]
model_input = ModelInput(
name="13 Band Sentinel-2 Batch",
bands=band_names,
input=input_struct,
resize_type=None,
value_scaling=value_scaling,
pre_processing_function=ProcessingExpression(
format="python",
expression="torchgeo.datamodules.eurosat.EuroSATDataModule.collate_fn",
), # noqa: E501
)
result_struct = ModelResult(
shape=[-1, 10],
dim_order=["batch", "class"],
data_type="float32",
)
class_map = {
"Annual Crop": 0,
"Forest": 1,
"Herbaceous Vegetation": 2,
"Highway": 3,
"Industrial Buildings": 4,
"Pasture": 5,
"Permanent Crop": 6,
"Residential Buildings": 7,
"River": 8,
"SeaLake": 9,
}
class_objects = [
MLMClassification(
value=class_value,
name=class_name,
)
for class_name, class_value in class_map.items()
]
model_output = ModelOutput(
name="classification",
tasks={"classification"},
classes=class_objects,
result=result_struct,
post_processing_function=None,
)
assets = {
"model": pystac.Asset(
title="Pytorch weights checkpoint",
description=(
"A Resnet-18 classification model trained on normalized Sentinel-2 "
"imagery with Eurosat landcover labels with torchgeo."
),
href="https://huggingface.co/torchgeo/resnet18_sentinel2_all_moco/resolve/main/resnet18_sentinel2_all_moco-59bfdff9.pth",
media_type="application/octet-stream; application=pytorch",
roles=[
"mlm:model",
"mlm:weights",
"data",
],
extra_fields={"mlm:artifact_type": "torch.save"}
),
"source_code": pystac.Asset(
title="Model implementation.",
description="Source code to run the model.",
href="https://github.com/microsoft/torchgeo/blob/61efd2e2c4df7ebe3bd03002ebbaeaa3cfe9885a/torchgeo/models/resnet.py#L207",
media_type="text/x-python",
roles=[
"mlm:source_code",
"code",
],
),
}
ml_model_size = 43000000
ml_model_meta = MLModelProperties(
name="Resnet-18 Sentinel-2 ALL MOCO",
architecture="ResNet-18",
tasks={"classification"},
framework="pytorch",
framework_version="2.1.2+cu121",
accelerator="cuda",
accelerator_constrained=False,
accelerator_summary="Unknown",
file_size=ml_model_size,
memory_size=1,
pretrained=True,
pretrained_source="EuroSat Sentinel-2",
total_parameters=11_700_000,
input=[model_input],
output=[model_output],
)
# TODO, this can't be serialized but pystac.item calls for a datetime
# in docs. start_datetime=datetime.strptime("1900-01-01", "%Y-%m-%d")
# Is this a problem that we don't do date validation if we supply as str?
start_datetime_str = "1900-01-01"
end_datetime_str = "9999-01-01" # cannot be None, invalid against STAC Core!
start_datetime = parse_dt(start_datetime_str).isoformat() + "Z"
end_datetime = parse_dt(end_datetime_str).isoformat() + "Z"
bbox = [
-7.882190080512502,
37.13739173208318,
27.911651652899923,
58.21798141355221,
]
geometry = shapely.geometry.Polygon.from_bounds(*bbox).__geo_interface__
item_name = "item_basic"
col_name = "ml-model-examples"
item = pystac.Item(
id=item_name,
collection=col_name,
geometry=geometry,
bbox=bbox,
datetime=None,
properties={
"start_datetime": start_datetime,
"end_datetime": end_datetime,
"description": "Sourced from torchgeo python library, identifier is ResNet18_Weights.SENTINEL2_ALL_MOCO",
},
assets=assets,
)
# note: cannot use 'item.add_derived_from' since it expects a 'Item' object, but we refer to a 'Collection' here
# item.add_derived_from("https://earth-search.aws.element84.com/v1/collections/sentinel-2-l2a")
item.add_link(
pystac.Link(
target="https://earth-search.aws.element84.com/v1/collections/sentinel-2-l2a",
rel=pystac.RelType.DERIVED_FROM,
media_type=pystac.MediaType.JSON,
)
)
# define more link references
col = pystac.Collection(
id=col_name,
title="Machine Learning Model examples",
description="Collection of items contained in the Machine Learning Model examples.",
extent=pystac.Extent(
temporal=pystac.TemporalExtent([[parse_dt(start_datetime), parse_dt(end_datetime)]]),
spatial=pystac.SpatialExtent([bbox]),
),
)
col.set_self_href("./examples/collection.json")
col.add_item(item)
item.set_self_href(f"./examples/{item_name}.json")
model_asset = cast(
FileExtension[pystac.Asset],
FileExtension.ext(assets["model"], add_if_missing=True),
)
model_asset.apply(size=ml_model_size)
eo_model_asset = cast(
EOExtension[pystac.Asset],
EOExtension.ext(assets["model"], add_if_missing=True),
)
# NOTE:
# typically, it is recommended to add as much details as possible for the band description
# minimally, the names (which are well-known for sentinel-2) are sufficient
eo_bands = []
for name in band_names:
band = Band({})
band.apply(name=name)
eo_bands.append(band)
eo_model_asset.apply(bands=eo_bands)
item_mlm = MLModelExtension.ext(item, add_if_missing=True)
item_mlm.apply(ml_model_meta.model_dump(by_alias=True, exclude_unset=True, exclude_defaults=True))
return item_mlm