-
Notifications
You must be signed in to change notification settings - Fork 1
/
c941681a050d_add_initial_records.py
311 lines (289 loc) · 11.3 KB
/
c941681a050d_add_initial_records.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
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
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
"""Add initial records
Revision ID: c941681a050d
Revises: 39277f6278f4
Create Date: 2022-07-06 12:49:46.037868
"""
from datetime import datetime
from sqlalchemy import orm
import cerulean_cloud.database_schema as database_schema
from alembic import op
# revision identifiers, used by Alembic.
revision = "c941681a050d"
down_revision = "39277f6278f4"
branch_labels = None
depends_on = None
def upgrade() -> None:
"""add initial rows"""
bind = op.get_bind()
session = orm.Session(bind=bind)
# EditTheDatabase
with session.begin():
clses = [
database_schema.Cls(
short_name="BACKGROUND",
long_name="Background",
),
database_schema.Cls(
short_name="ANTHRO",
long_name="Anthropogenic",
),
database_schema.Cls(
short_name="NATURAL",
long_name="Natural",
),
database_schema.Cls(
short_name="INFRA",
long_name="Infrastructure",
supercls=2,
),
database_schema.Cls(
short_name="VESSEL",
long_name="Vessel",
supercls=2,
),
database_schema.Cls(
short_name="OLD_VESSEL",
long_name="Vessel, old",
supercls=5,
),
database_schema.Cls(
short_name="REC_VESSEL",
long_name="Vessel, recent",
supercls=5,
),
database_schema.Cls(
short_name="COIN_VESSEL",
long_name="Vessel, coincident",
supercls=7,
),
database_schema.Cls(
short_name="AMBIGUOUS",
long_name="Ambiguous",
),
]
session.add_all(clses)
models = [
database_schema.Model(
type="MASKRCNN",
file_path="experiments/2023_10_05_02_22_46_4cls_rnxt101_pr512_px1024_680min_maskrcnn_wd01/scripting_cpu_model.pt",
layers=["VV", "ALL_255", "VESSEL"],
cls_map={
0: "BACKGROUND",
1: "INFRA",
2: "NATURAL",
3: "VESSEL",
}, # inference_idx maps to class table
name="ResNext 101 hires56",
tile_width_m=40844,
tile_width_px=512,
epochs=122,
thresholds={
"poly_nms_thresh": 0.2,
"pixel_nms_thresh": 0.4,
"bbox_score_thresh": 0.3,
"poly_score_thresh": 0.1,
"pixel_score_thresh": 0.5,
"groundtruth_dice_thresh": 0.0,
},
backbone_size=101,
pixel_f1=0.461,
instance_f1=0.47,
),
database_schema.Model(
type="FASTAIUNET",
file_path="experiments/2024_09_04_21_34_24_4cls_resnet34_pr512_px1024_100epochs_unet/tracing_cpu_model.pt",
layers=["VV"],
cls_map={
0: "BACKGROUND",
1: "INFRA",
2: "NATURAL",
3: "VESSEL",
}, # inference_idx maps to class table
name="ResNet34 46.6%",
tile_width_m=40844, # Used to calculate zoom
tile_width_px=512, # Used to calculate scale
epochs=500,
thresholds={
"poly_nms_thresh": 0.2, # Minimum IoU between instances that will keep the higher scoring multipolygon
"pixel_nms_thresh": 0.0, # NOT USED IN UNETS
"bbox_score_thresh": 0.0001, # Smallest bridge value that will connect polygons into a multipolygon
"poly_score_thresh": 0.5, # Determines the size of the outline of any given polygon
"pixel_score_thresh": 0.9, # Minimum pixel score that will be required to keep a multipolygon
"groundtruth_dice_thresh": 0.0,
},
backbone_size=34,
pixel_f1=0.532,
# instance_f1=0.0, # TODO CALCULATE
),
]
session.add_all(models)
layers = [
database_schema.Layer(
short_name="VV",
long_name="S1 VV",
citation="Copernicus Sentinel data, processed by ESA, accessed via AWS Open Data Registry.",
source_url="https://registry.opendata.aws/sentinel-1/",
),
database_schema.Layer(
short_name="INFRA", # TODO Rename to something like INFRA_LAY, to avoid conflict with INFRA_PIXEL_CLASS
long_name="Infrastructure Distance",
citation="Generated by SkyTruth, using GFW's Infrastructure Dataset (pre-release)",
source_url="https://storage.googleapis.com/ceruleanml/aux_datasets/infra_locations_01_cogeo.tiff",
),
database_schema.Layer(
short_name="VESSEL", # TODO Rename to something like VESSEL_LAY, to avoid conflict with VESSEL_PIXEL_CLASS
long_name="Vessel Density",
citation="Global Maritime Traffic Density Service (GTMDS) retrieved from GlobalMaritimeTraffic.org, a service of MapLarge 2021",
source_url="https://gmtds.maplarge.com/public/ext/GMTDS/Main",
notes="Typically uses the previous month's density map. If unavailable will default to previous year.",
),
database_schema.Layer(
short_name="ALL_255", # TODO Rename to something liketo avoid conflict with PIXEL CLASS
long_name="All Pixels Value=255",
citation="",
source_url="",
notes="Can be used for ablation or to replace unwanted layers.",
),
database_schema.Layer(
short_name="ALL_ZEROS", # TODO Rename to something liketo avoid conflict with PIXEL CLASS
long_name="All Pixels Value=0",
citation="",
source_url="",
notes="Can be used for ablation or to replace unwanted layers.",
),
]
session.add_all(layers)
aoi_types = [
database_schema.AoiType(
table_name="aoi_eez",
long_name="Exclusive Economic Zone",
short_name="EEZ",
source_url="https://www.marineregions.org/eez.php",
citation="Flanders Marine Institute (2019). Maritime Boundaries Geodatabase, version 11. Available online at https://www.marineregions.org/. https://doi.org/10.14284/382.",
update_time=datetime.now(),
),
database_schema.AoiType(
table_name="aoi_iho",
long_name="IHO Sea Areas",
short_name="IHO",
source_url="https://www.marineregions.org/sources.php#iho",
citation="Flanders Marine Institute (2018). IHO Sea Areas, version 3. Available online at https://www.marineregions.org/. https://doi.org/10.14284/323.",
update_time=datetime.now(),
),
database_schema.AoiType(
table_name="aoi_mpa",
long_name="Marine Protected Area",
short_name="MPA",
source_url="https://www.protectedplanet.net/en/thematic-areas/marine-protected-areas",
citation="UNEP-WCMC and IUCN (2023), Protected Planet: The World Database on Protected Areas (WDPA) and World Database on Other Effective Area-based Conservation Measures (WD-OECM) [Online], July 2023, Cambridge, UK: UNEP-WCMC and IUCN. Available at: www.protectedplanet.net.",
update_time=datetime.now(),
),
database_schema.AoiType(
table_name="aoi_user",
long_name="User-generated",
short_name="USER",
update_time=datetime.now(),
),
]
session.add_all(aoi_types)
source_types = [
database_schema.SourceType(
table_name="source_vessel",
long_name="Vessel Source",
short_name="VESSEL",
citation="AIS from GFW",
),
database_schema.SourceType(
table_name="source_infra",
long_name="Infrastructure Source",
short_name="INFRA",
citation="SkyTruth",
),
]
session.add_all(source_types)
frequencies = [
database_schema.Frequency(
short_name="REALTIME",
long_name="Near real-time alerts",
),
database_schema.Frequency(
short_name="DAILY",
long_name="Daily digest",
),
database_schema.Frequency(
short_name="WEEKLY",
long_name="Weekly digest",
),
database_schema.Frequency(
short_name="MONTHLY",
long_name="Monthly digest",
),
]
session.add_all(frequencies)
def downgrade() -> None:
"""drop initial rows"""
bind = op.get_bind()
session = orm.Session(bind=bind)
with session.begin():
models = session.query(database_schema.Model).all()
for model in models:
session.delete(model)
layers = (
session.query(database_schema.Layer)
.filter(
database_schema.Layer.short_name.in_(
["VV", "INFRA", "VESSEL", "ALL_255", "ALL_ZEROS"]
)
)
.all()
)
for layer in layers:
session.delete(layer)
aoi_types = (
session.query(database_schema.AoiType)
.filter(
database_schema.AoiType.short_name.in_(["EEZ", "IHO", "MPA", "USER"])
)
.all()
)
for aoi_type in aoi_types:
session.delete(aoi_type)
source_types = (
session.query(database_schema.SourceType)
.filter(database_schema.SourceType.short_name.in_(["VESSEL", "INFRA"]))
.all()
)
for source_type in source_types:
session.delete(source_type)
frequencies = (
session.query(database_schema.Frequency)
.filter(
database_schema.Frequency.short_name.in_(
["REALTIME", "DAILY", "WEEKLY", "MONTHLY"]
)
)
.all()
)
for frequency in frequencies:
session.delete(frequency)
clses = (
session.query(database_schema.Cls)
.filter(
database_schema.Cls.short_name.in_(
[
"BACKGROUND",
"ANTHRO",
"NATURAL",
"INFRA",
"VESSEL",
"OLD_VESSEL",
"REC_VESSEL",
"COIN_VESSEL",
"AMBIGUOUS",
]
)
)
.all()
)
for clas in clses:
session.delete(clas)