orthoseg 0.8.0.dev0__tar.gz → 0.8.0.dev1__tar.gz
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.
- {orthoseg-0.8.0.dev0/orthoseg.egg-info → orthoseg-0.8.0.dev1}/PKG-INFO +1 -1
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/helpers/config_helper.py +108 -70
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/lib/predicter.py +29 -10
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/lib/trainer.py +39 -21
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/load_images.py +2 -3
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/load_sampleprojects.py +43 -23
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/model/model_factory.py +164 -53
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/model/model_helper.py +37 -15
- orthoseg-0.8.0.dev1/orthoseg/model/model_weights_helper.py +121 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/postprocess.py +2 -3
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/predict.py +2 -3
- orthoseg-0.8.0.dev1/orthoseg/project_defaults.ini +841 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/train.py +5 -4
- orthoseg-0.8.0.dev1/orthoseg/util/_processing_util.py +218 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/util/config_util.py +7 -2
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/util/general_util.py +0 -66
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/util/image_util.py +114 -13
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/util/log_util.py +5 -2
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/validate.py +2 -3
- orthoseg-0.8.0.dev1/orthoseg/version.txt +1 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1/orthoseg.egg-info}/PKG-INFO +1 -1
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg.egg-info/SOURCES.txt +6 -2
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/pyproject.toml +3 -2
- orthoseg-0.8.0.dev0/tests/test_config.py → orthoseg-0.8.0.dev1/tests/test_config_helper.py +85 -2
- orthoseg-0.8.0.dev1/tests/test_config_util.py +31 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_end2end.py +66 -13
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_helper.py +2 -2
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_image_util.py +116 -24
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_load_images.py +3 -3
- orthoseg-0.8.0.dev1/tests/test_load_sampleprojects.py +163 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_model_factory.py +84 -13
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_model_helper.py +27 -3
- orthoseg-0.8.0.dev1/tests/test_model_weights_helper.py +58 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_postprocess.py +1 -1
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_predict.py +2 -2
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_train.py +2 -5
- orthoseg-0.8.0.dev1/tests/test_trainer.py +37 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_validate.py +8 -15
- orthoseg-0.8.0.dev0/orthoseg/project_defaults.ini +0 -524
- orthoseg-0.8.0.dev0/orthoseg/util/git_downloader.py +0 -150
- orthoseg-0.8.0.dev0/orthoseg/version.txt +0 -1
- orthoseg-0.8.0.dev0/tests/test_load_sampleprojects.py +0 -74
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/LICENSE.MD +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/MANIFEST.in +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/README.md +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/__init__.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/_compat.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/helpers/__init__.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/helpers/email_helper.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/helpers/vectorfile_helper.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/lib/__init__.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/lib/cleanup.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/lib/postprocess_predictions.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/lib/prepare_traindatasets.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/model/__init__.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/py.typed +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/scriptrunner.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/scriptrunner_defaults.ini +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/util/__init__.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/util/data.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/util/progress_util.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg/util/vector_util.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg.egg-info/dependency_links.txt +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg.egg-info/entry_points.txt +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg.egg-info/requires.txt +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/orthoseg.egg-info/top_level.txt +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/setup.cfg +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/setup.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/__init__.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_cleanup.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_data.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_postprocess_predictions.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_predicter.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_prepare_traindata.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_vector_util.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_vectorfile_helper.py +0 -0
- {orthoseg-0.8.0.dev0 → orthoseg-0.8.0.dev1}/tests/test_version.py +0 -0
|
@@ -10,6 +10,7 @@ import pprint
|
|
|
10
10
|
import re
|
|
11
11
|
import shutil
|
|
12
12
|
import tempfile
|
|
13
|
+
import warnings
|
|
13
14
|
from pathlib import Path
|
|
14
15
|
from typing import Any
|
|
15
16
|
|
|
@@ -19,8 +20,15 @@ from osgeo import gdal
|
|
|
19
20
|
|
|
20
21
|
from orthoseg._compat import KERAS_GTE_3
|
|
21
22
|
from orthoseg.lib.prepare_traindatasets import LabelInfo
|
|
23
|
+
from orthoseg.model.model_weights_helper import get_weights_types_for_architecture
|
|
22
24
|
from orthoseg.util import config_util
|
|
23
|
-
from orthoseg.util.image_util import
|
|
25
|
+
from orthoseg.util.image_util import (
|
|
26
|
+
FileLayerSource,
|
|
27
|
+
WMSLayerSource,
|
|
28
|
+
create_roi_for_dir,
|
|
29
|
+
create_vrt_for_dir,
|
|
30
|
+
has_switched_axes,
|
|
31
|
+
)
|
|
24
32
|
|
|
25
33
|
# Get a logger...
|
|
26
34
|
logger = logging.getLogger(__name__)
|
|
@@ -75,12 +83,14 @@ def read_orthoseg_config(config_path: Path, overrules: list[str] | None = None):
|
|
|
75
83
|
config_path (Path): path to the configuration file to read.
|
|
76
84
|
overrules (list[str], optional): list of config options that will overrule other
|
|
77
85
|
ways to supply configuration. They should be specified as a list of
|
|
78
|
-
"<section>.<
|
|
86
|
+
"<section>.<key>=<value>" strings. Defaults to None.
|
|
79
87
|
"""
|
|
80
88
|
# Set the temporary directory
|
|
81
89
|
_set_tmp_dir()
|
|
82
90
|
|
|
83
91
|
# Determine list of config files that should be loaded
|
|
92
|
+
# Support using ~ in the config path by expanding it
|
|
93
|
+
config_path = config_path.expanduser()
|
|
84
94
|
config_paths = config_util.get_config_files(config_path)
|
|
85
95
|
|
|
86
96
|
# If there are overrules, write them to a temporary configuration file.
|
|
@@ -149,7 +159,7 @@ def read_orthoseg_config(config_path: Path, overrules: list[str] | None = None):
|
|
|
149
159
|
segment_subject = general.get("segment_subject")
|
|
150
160
|
if segment_subject is None or segment_subject == "MUST_OVERRIDE":
|
|
151
161
|
raise ValueError(
|
|
152
|
-
"Projectconfig parameter general.segment_subject needs to be overruled to"
|
|
162
|
+
"Projectconfig parameter general.segment_subject needs to be overruled to "
|
|
153
163
|
"a proper name in a specific project config file, \nwith config_filepaths "
|
|
154
164
|
f"{config_paths}"
|
|
155
165
|
)
|
|
@@ -172,7 +182,7 @@ def read_orthoseg_config(config_path: Path, overrules: list[str] | None = None):
|
|
|
172
182
|
)
|
|
173
183
|
dirs["projects_dir"] = projects_dir_absolute.as_posix()
|
|
174
184
|
|
|
175
|
-
#
|
|
185
|
+
# Some version-specific defaults
|
|
176
186
|
if train.get("save_format") is None:
|
|
177
187
|
train["save_format"] = "keras" if KERAS_GTE_3 else "h5"
|
|
178
188
|
elif train.get("save_format") == "keras" and not KERAS_GTE_3:
|
|
@@ -186,6 +196,29 @@ def read_orthoseg_config(config_path: Path, overrules: list[str] | None = None):
|
|
|
186
196
|
"Valid options are 'keras', 'h5', or 'tf'."
|
|
187
197
|
)
|
|
188
198
|
|
|
199
|
+
if train.get("optimizer") is None:
|
|
200
|
+
if KERAS_GTE_3:
|
|
201
|
+
train["optimizer"] = "AdamW"
|
|
202
|
+
else:
|
|
203
|
+
# On keras 2, AdamW gives an error when training starts.
|
|
204
|
+
train["optimizer"] = "Adam"
|
|
205
|
+
|
|
206
|
+
# Some defaults that need some logic.
|
|
207
|
+
if train.get("weights_type") == "aerial_if_available":
|
|
208
|
+
# If aerial weights are available for the architecture, use them, otherwise use
|
|
209
|
+
# imagenet weights.
|
|
210
|
+
architecture = model.get("architecture")
|
|
211
|
+
weight_types = get_weights_types_for_architecture(architecture)
|
|
212
|
+
if len(weight_types) > 0 and "aerial" in weight_types:
|
|
213
|
+
train["weights_type"] = "aerial"
|
|
214
|
+
else:
|
|
215
|
+
warnings.warn(
|
|
216
|
+
f"No aerial pretrained weights available for {architecture=}, so "
|
|
217
|
+
f"falling back to imagenet pretrained weights.",
|
|
218
|
+
stacklevel=2,
|
|
219
|
+
)
|
|
220
|
+
train["weights_type"] = "imagenet"
|
|
221
|
+
|
|
189
222
|
# Read the layer config
|
|
190
223
|
layer_config_filepath = files.getpath("image_layers_config_filepath")
|
|
191
224
|
global layer_config_filepath_used
|
|
@@ -329,44 +362,64 @@ def _read_layer_config(layer_config_filepath: Path) -> dict:
|
|
|
329
362
|
switch_axes = has_switched_axes(crs)
|
|
330
363
|
image_layers[image_layer]["switch_axes"] = switch_axes
|
|
331
364
|
|
|
365
|
+
# Read nb_concurrent calls param
|
|
366
|
+
image_layers[image_layer]["nb_concurrent_calls"] = layer_config[
|
|
367
|
+
image_layer
|
|
368
|
+
].getint("nb_concurrent_calls", fallback=1)
|
|
369
|
+
|
|
370
|
+
# Check if a region of interest is specified as file or bbox
|
|
371
|
+
image_layers[image_layer]["roi_filepath"] = layer_config[image_layer].getpath(
|
|
372
|
+
"roi_filepath", fallback=None
|
|
373
|
+
)
|
|
374
|
+
bbox_tuple = None
|
|
375
|
+
if layer_config.has_option(image_layer, "bbox"):
|
|
376
|
+
bbox_list = layer_config[image_layer].getlist("bbox")
|
|
377
|
+
bbox_tuple = (
|
|
378
|
+
float(bbox_list[0]),
|
|
379
|
+
float(bbox_list[1]),
|
|
380
|
+
float(bbox_list[2]),
|
|
381
|
+
float(bbox_list[3]),
|
|
382
|
+
)
|
|
383
|
+
image_layers[image_layer]["bbox"] = bbox_tuple
|
|
384
|
+
|
|
385
|
+
# Check if the grid xmin and xmax are specified
|
|
386
|
+
image_layers[image_layer]["grid_xmin"] = layer_config[image_layer].getfloat(
|
|
387
|
+
"grid_xmin", fallback=0
|
|
388
|
+
)
|
|
389
|
+
image_layers[image_layer]["grid_ymin"] = layer_config[image_layer].getfloat(
|
|
390
|
+
"grid_ymin", fallback=0
|
|
391
|
+
)
|
|
392
|
+
|
|
393
|
+
# Check if a image_pixels_ignore_border is specified
|
|
394
|
+
image_layers[image_layer]["image_pixels_ignore_border"] = layer_config[
|
|
395
|
+
image_layer
|
|
396
|
+
].getint("image_pixels_ignore_border", fallback=0)
|
|
397
|
+
|
|
398
|
+
# Convert pixel_x_size and pixel_y_size to float
|
|
399
|
+
image_layers[image_layer]["pixel_x_size"] = layer_config[image_layer].getfloat(
|
|
400
|
+
"pixel_x_size"
|
|
401
|
+
)
|
|
402
|
+
image_layers[image_layer]["pixel_y_size"] = layer_config[image_layer].getfloat(
|
|
403
|
+
"pixel_y_size"
|
|
404
|
+
)
|
|
405
|
+
|
|
332
406
|
# If the layer source(s) are specified in a json parameter, parse it
|
|
333
407
|
if "layersources" in image_layers[image_layer]:
|
|
334
408
|
image_layers[image_layer]["layersources"] = layer_config[
|
|
335
409
|
image_layer
|
|
336
410
|
].getdict("layersources")
|
|
337
411
|
else:
|
|
338
|
-
# If not, the layersource
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
"wms_server_url",
|
|
342
|
-
"wms_version",
|
|
343
|
-
"wms_layernames",
|
|
344
|
-
"wms_layerstyles",
|
|
345
|
-
"bands",
|
|
346
|
-
"wms_username",
|
|
347
|
-
"wms_password",
|
|
348
|
-
"random_sleep",
|
|
349
|
-
"wms_ignore_capabilities_url",
|
|
350
|
-
"path",
|
|
351
|
-
"layername",
|
|
352
|
-
"wmts_server_url",
|
|
353
|
-
"wmts_version",
|
|
354
|
-
"wmts_layernames",
|
|
355
|
-
"wmts_layerstyles",
|
|
356
|
-
"wmts_tile_matrix_set",
|
|
357
|
-
"wmts_xyz",
|
|
412
|
+
# If not, the layersource should be specified in the top-level parameters
|
|
413
|
+
image_layers[image_layer]["layersources"] = [
|
|
414
|
+
dict(layer_config[image_layer])
|
|
358
415
|
]
|
|
359
|
-
for key in layersource_keys:
|
|
360
|
-
if key in layer_config[image_layer]:
|
|
361
|
-
layersource[key] = layer_config[image_layer][key]
|
|
362
|
-
image_layers[image_layer]["layersources"] = [layersource]
|
|
363
416
|
|
|
364
417
|
# Convert the layersource dicts to layersource objects
|
|
365
418
|
layersource_objects = []
|
|
366
419
|
for layersource in image_layers[image_layer]["layersources"]:
|
|
367
420
|
layersource_object: WMSLayerSource | FileLayerSource
|
|
368
421
|
try:
|
|
369
|
-
# If not, the layersource should be specified in
|
|
422
|
+
# If not, the layersource should be specified in separate parameters
|
|
370
423
|
if "wms_server_url" in layersource:
|
|
371
424
|
layersource_object = WMSLayerSource(
|
|
372
425
|
wms_server_url=layersource["wms_server_url"],
|
|
@@ -394,6 +447,32 @@ def _read_layer_config(layer_config_filepath: Path) -> dict:
|
|
|
394
447
|
# Resolve relative path based on layer_config_filepath.parent
|
|
395
448
|
path = layer_config_filepath.parent / layersource["path"]
|
|
396
449
|
path = path.resolve()
|
|
450
|
+
|
|
451
|
+
# The path is a directory, so we need to create a VRT file for it.
|
|
452
|
+
if path.is_dir():
|
|
453
|
+
file_patterns = _str2list(layersource.get("file_patterns"))
|
|
454
|
+
if file_patterns is None:
|
|
455
|
+
raise ValueError(
|
|
456
|
+
f"file_patterns should be specified if path points to "
|
|
457
|
+
f"a directory {path}"
|
|
458
|
+
)
|
|
459
|
+
path = create_vrt_for_dir(
|
|
460
|
+
path, file_patterns, crs=layersource.get("projection")
|
|
461
|
+
)
|
|
462
|
+
|
|
463
|
+
# If no roi is specified, create one based on the dir contents.
|
|
464
|
+
if (
|
|
465
|
+
image_layers[image_layer].get("roi_filepath") is None
|
|
466
|
+
and image_layers[image_layer].get("bbox") is None
|
|
467
|
+
):
|
|
468
|
+
image_layers[image_layer]["roi_filepath"] = (
|
|
469
|
+
create_roi_for_dir(
|
|
470
|
+
path.parent,
|
|
471
|
+
file_patterns,
|
|
472
|
+
crs=layersource.get("projection"),
|
|
473
|
+
)
|
|
474
|
+
)
|
|
475
|
+
|
|
397
476
|
layersource_object = FileLayerSource(
|
|
398
477
|
path=path,
|
|
399
478
|
layernames=_str2list(layersource["layername"]),
|
|
@@ -411,47 +490,6 @@ def _read_layer_config(layer_config_filepath: Path) -> dict:
|
|
|
411
490
|
layersource_objects.append(layersource_object)
|
|
412
491
|
image_layers[image_layer]["layersources"] = layersource_objects
|
|
413
492
|
|
|
414
|
-
# Read nb_concurrent calls param
|
|
415
|
-
image_layers[image_layer]["nb_concurrent_calls"] = layer_config[
|
|
416
|
-
image_layer
|
|
417
|
-
].getint("nb_concurrent_calls", fallback=1)
|
|
418
|
-
|
|
419
|
-
# Check if a region of interest is specified as file or bbox
|
|
420
|
-
image_layers[image_layer]["roi_filepath"] = layer_config[image_layer].getpath(
|
|
421
|
-
"roi_filepath", fallback=None
|
|
422
|
-
)
|
|
423
|
-
bbox_tuple = None
|
|
424
|
-
if layer_config.has_option(image_layer, "bbox"):
|
|
425
|
-
bbox_list = layer_config[image_layer].getlist("bbox")
|
|
426
|
-
bbox_tuple = (
|
|
427
|
-
float(bbox_list[0]),
|
|
428
|
-
float(bbox_list[1]),
|
|
429
|
-
float(bbox_list[2]),
|
|
430
|
-
float(bbox_list[3]),
|
|
431
|
-
)
|
|
432
|
-
image_layers[image_layer]["bbox"] = bbox_tuple
|
|
433
|
-
|
|
434
|
-
# Check if the grid xmin and xmax are specified
|
|
435
|
-
image_layers[image_layer]["grid_xmin"] = layer_config[image_layer].getfloat(
|
|
436
|
-
"grid_xmin", fallback=0
|
|
437
|
-
)
|
|
438
|
-
image_layers[image_layer]["grid_ymin"] = layer_config[image_layer].getfloat(
|
|
439
|
-
"grid_ymin", fallback=0
|
|
440
|
-
)
|
|
441
|
-
|
|
442
|
-
# Check if a image_pixels_ignore_border is specified
|
|
443
|
-
image_layers[image_layer]["image_pixels_ignore_border"] = layer_config[
|
|
444
|
-
image_layer
|
|
445
|
-
].getint("image_pixels_ignore_border", fallback=0)
|
|
446
|
-
|
|
447
|
-
# Convert pixel_x_size and pixel_y_size to float
|
|
448
|
-
image_layers[image_layer]["pixel_x_size"] = layer_config[image_layer].getfloat(
|
|
449
|
-
"pixel_x_size"
|
|
450
|
-
)
|
|
451
|
-
image_layers[image_layer]["pixel_y_size"] = layer_config[image_layer].getfloat(
|
|
452
|
-
"pixel_y_size"
|
|
453
|
-
)
|
|
454
|
-
|
|
455
493
|
return image_layers
|
|
456
494
|
|
|
457
495
|
|
|
@@ -25,7 +25,7 @@ import rasterio.plot as rio_plot
|
|
|
25
25
|
import tensorflow as tf
|
|
26
26
|
|
|
27
27
|
import orthoseg.lib.postprocess_predictions as postp
|
|
28
|
-
from orthoseg.util import
|
|
28
|
+
from orthoseg.util import _processing_util, image_util
|
|
29
29
|
from orthoseg.util.progress_util import ProgressLogger
|
|
30
30
|
|
|
31
31
|
# Get a logger...
|
|
@@ -456,7 +456,7 @@ def _predict_layer(
|
|
|
456
456
|
# We don't want the postprocess workers to block the entire system,
|
|
457
457
|
# so make them a bit nicer
|
|
458
458
|
def init_postprocess_worker():
|
|
459
|
-
|
|
459
|
+
_processing_util.setprocessnice(15)
|
|
460
460
|
|
|
461
461
|
with (
|
|
462
462
|
futures.ThreadPoolExecutor(nb_parallel_read) as read_pool,
|
|
@@ -575,6 +575,11 @@ def _predict_layer(
|
|
|
575
575
|
}
|
|
576
576
|
)
|
|
577
577
|
|
|
578
|
+
except Exception as ex: # pragma: no cover
|
|
579
|
+
nb_errors += 1
|
|
580
|
+
image_path = read_queue[future]
|
|
581
|
+
_handle_error(image_path, ex, images_error_log_filepath)
|
|
582
|
+
|
|
578
583
|
finally:
|
|
579
584
|
# Remove from queue...
|
|
580
585
|
del read_queue[future]
|
|
@@ -695,11 +700,7 @@ def _predict_layer(
|
|
|
695
700
|
|
|
696
701
|
if output_vector_path is None:
|
|
697
702
|
# No vector output, so we are ready with this image
|
|
698
|
-
|
|
699
|
-
"a+"
|
|
700
|
-
) as image_donelog_file:
|
|
701
|
-
image_donelog_file.write(f"{image_path.name}\n")
|
|
702
|
-
|
|
703
|
+
_write_to_done_log(image_path, images_done_log_filepath)
|
|
703
704
|
nb_done += 1
|
|
704
705
|
else:
|
|
705
706
|
# Schedule `_write_vector_result` to move the result of the
|
|
@@ -844,8 +845,26 @@ def _write_vector_result(
|
|
|
844
845
|
gfo.remove(partial_vector_path)
|
|
845
846
|
|
|
846
847
|
# Write filepath to file with files that are done
|
|
847
|
-
|
|
848
|
-
|
|
848
|
+
_write_to_done_log(image_path, images_done_log_filepath)
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
def _write_to_done_log(image_path: Path, images_done_log_filepath: Path):
|
|
852
|
+
nb_tries = 0
|
|
853
|
+
while True:
|
|
854
|
+
try:
|
|
855
|
+
with images_done_log_filepath.open("a+") as image_donelog_file:
|
|
856
|
+
image_donelog_file.write(f"{image_path.name}\n")
|
|
857
|
+
break
|
|
858
|
+
|
|
859
|
+
except Exception as ex: # pragma: no cover
|
|
860
|
+
nb_tries += 1
|
|
861
|
+
time.sleep(0.1)
|
|
862
|
+
|
|
863
|
+
if nb_tries >= 5:
|
|
864
|
+
message = (
|
|
865
|
+
f"Write to done log failed {nb_tries} times for {image_path}: {ex}"
|
|
866
|
+
)
|
|
867
|
+
raise RuntimeError(message) from ex
|
|
849
868
|
|
|
850
869
|
|
|
851
870
|
def _handle_error(image_path: Path, ex: Exception, log_path: Path):
|
|
@@ -905,7 +924,7 @@ def read_image(
|
|
|
905
924
|
|
|
906
925
|
# Read worked, so jump out of the loop...
|
|
907
926
|
break
|
|
908
|
-
except
|
|
927
|
+
except Exception as ex: # pragma: no cover
|
|
909
928
|
retry_count += 1
|
|
910
929
|
logger.warning(f"Read failed, retry nb {retry_count} for {image_path}")
|
|
911
930
|
if retry_count >= 3:
|
|
@@ -26,6 +26,7 @@ def train(
|
|
|
26
26
|
segment_subject: str,
|
|
27
27
|
traindata_id: int,
|
|
28
28
|
hyperparams: mh.HyperParams,
|
|
29
|
+
weights_dir: Path | None = None,
|
|
29
30
|
model_preload_filepath: Path | None = None,
|
|
30
31
|
image_width: int = 512,
|
|
31
32
|
image_height: int = 512,
|
|
@@ -53,6 +54,7 @@ def train(
|
|
|
53
54
|
segment_subject (str): segment subject
|
|
54
55
|
traindata_id (int): train data version
|
|
55
56
|
hyperparams (mh.HyperParams): the hyper parameters to use for the model
|
|
57
|
+
weights_dir: directory where pretrained weights are cached and read from
|
|
56
58
|
model_preload_filepath: filepath to the model to continue training on,
|
|
57
59
|
or None if you want to start from scratch
|
|
58
60
|
image_width: width the input images will be rescaled to for training
|
|
@@ -64,25 +66,38 @@ def train(
|
|
|
64
66
|
save_augmented_subdir: (str, optional): the subdirectory to save the augmented
|
|
65
67
|
images to. If None, the images aren't saved. Defaults to None.
|
|
66
68
|
"""
|
|
67
|
-
#
|
|
68
|
-
|
|
69
|
+
# First if there exists a model already for the traindata_id,...
|
|
70
|
+
curr_best_model = mh.get_best_model(
|
|
71
|
+
model_dir=model_save_dir,
|
|
72
|
+
segment_subject=segment_subject,
|
|
73
|
+
traindata_id=traindata_id,
|
|
74
|
+
architecture_id=hyperparams.architecture.architecture_id,
|
|
75
|
+
trainparams_id=hyperparams.train.trainparams_id,
|
|
76
|
+
)
|
|
69
77
|
model_save_base_filename = mh.format_model_basefilename(
|
|
70
78
|
segment_subject=segment_subject,
|
|
71
79
|
traindata_id=traindata_id,
|
|
72
80
|
architecture_id=hyperparams.architecture.architecture_id,
|
|
73
81
|
trainparams_id=hyperparams.train.trainparams_id,
|
|
74
82
|
)
|
|
75
|
-
|
|
76
|
-
|
|
83
|
+
|
|
84
|
+
# If there is an existing model and we want to continue training on that, recuperate
|
|
85
|
+
# the epoch.
|
|
86
|
+
start_epoch = 0
|
|
87
|
+
csv_log_path = model_save_dir / f"{model_save_base_filename}_train_log.csv"
|
|
88
|
+
model_json_path = model_save_dir / f"{model_save_base_filename}_model.json"
|
|
89
|
+
hyperparams_path = model_save_dir / f"{model_save_base_filename}_hyperparams.json"
|
|
90
|
+
|
|
91
|
+
if curr_best_model is not None:
|
|
77
92
|
if not model_preload_filepath:
|
|
78
93
|
message = (
|
|
79
|
-
"
|
|
80
|
-
f"{
|
|
94
|
+
"Model exists but preload model file not specified: "
|
|
95
|
+
f"{curr_best_model['model_filepath']}"
|
|
81
96
|
)
|
|
82
97
|
logger.critical(message)
|
|
83
98
|
raise ValueError(message)
|
|
84
99
|
|
|
85
|
-
train_log_df = pd.read_csv(
|
|
100
|
+
train_log_df = pd.read_csv(csv_log_path, sep=";", usecols=["epoch", "lr"])
|
|
86
101
|
assert isinstance(train_log_df, pd.DataFrame)
|
|
87
102
|
logger.debug(f"train_log csv contents:\n{train_log_df}")
|
|
88
103
|
start_epoch = train_log_df["epoch"].max()
|
|
@@ -94,6 +109,12 @@ def train(
|
|
|
94
109
|
f"{hyperparams.train.optimizer_params['learning_rate']}"
|
|
95
110
|
)
|
|
96
111
|
|
|
112
|
+
if start_epoch == 0:
|
|
113
|
+
# No existing model, so clean any existing files that might exist
|
|
114
|
+
csv_log_path.unlink(missing_ok=True)
|
|
115
|
+
model_json_path.unlink(missing_ok=True)
|
|
116
|
+
hyperparams_path.unlink(missing_ok=True)
|
|
117
|
+
|
|
97
118
|
# If no existing model provided, create it from scratch
|
|
98
119
|
if not model_preload_filepath:
|
|
99
120
|
# If the encode should be frozen for some epochs...
|
|
@@ -108,16 +129,16 @@ def train(
|
|
|
108
129
|
nb_channels=hyperparams.architecture.nb_channels,
|
|
109
130
|
nb_classes=len(hyperparams.architecture.classes),
|
|
110
131
|
activation=hyperparams.architecture.activation_function,
|
|
132
|
+
weights_type=hyperparams.train.weights_type,
|
|
133
|
+
weights_dir=weights_dir,
|
|
111
134
|
freeze=freeze,
|
|
112
135
|
)
|
|
113
136
|
|
|
114
137
|
# Save the model architecture to json
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
with model_json_filepath.open("w") as dst:
|
|
120
|
-
dst.write(str(model.to_json()))
|
|
138
|
+
model_save_dir.mkdir(parents=True, exist_ok=True)
|
|
139
|
+
with model_json_path.open("w") as dst:
|
|
140
|
+
dst.write(str(model.to_json()))
|
|
141
|
+
|
|
121
142
|
else:
|
|
122
143
|
# If a preload model is provided, load that if it exists...
|
|
123
144
|
if not model_preload_filepath.exists():
|
|
@@ -258,7 +279,7 @@ def train(
|
|
|
258
279
|
train_callbacks.append(tensorboard_logger)
|
|
259
280
|
if hyperparams.train.log_csv is True:
|
|
260
281
|
csv_logger = keras.callbacks.CSVLogger(
|
|
261
|
-
str(
|
|
282
|
+
str(csv_log_path), append=True, separator=";"
|
|
262
283
|
)
|
|
263
284
|
train_callbacks.append(csv_logger)
|
|
264
285
|
|
|
@@ -305,14 +326,11 @@ def train(
|
|
|
305
326
|
)
|
|
306
327
|
|
|
307
328
|
logger.info(f"{hyperparams.toJSON()}")
|
|
308
|
-
|
|
309
|
-
model_save_dir / f"{model_save_base_filename}_hyperparams.json"
|
|
310
|
-
)
|
|
311
|
-
hyperparams_filepath.write_text(hyperparams.toJSON())
|
|
329
|
+
hyperparams_path.write_text(hyperparams.toJSON())
|
|
312
330
|
|
|
313
331
|
try:
|
|
314
|
-
# If the encoder should be frozen for the first epochs, do so
|
|
315
332
|
if hyperparams.train.nb_epoch_with_freeze > 0:
|
|
333
|
+
# First train only the top layers for a few epochs.
|
|
316
334
|
logger.info(
|
|
317
335
|
f"First train for {hyperparams.train.nb_epoch_with_freeze} epochs with "
|
|
318
336
|
"frozen layers"
|
|
@@ -352,7 +370,7 @@ def train(
|
|
|
352
370
|
|
|
353
371
|
# Write some reporting
|
|
354
372
|
train_report_path = model_save_dir / (model_save_base_filename + "_report.pdf")
|
|
355
|
-
train_log_df = pd.read_csv(
|
|
373
|
+
train_log_df = pd.read_csv(csv_log_path, sep=";")
|
|
356
374
|
assert isinstance(train_log_df, pd.DataFrame)
|
|
357
375
|
columns_to_keep = []
|
|
358
376
|
for column in train_log_df.columns:
|
|
@@ -362,7 +380,7 @@ def train(
|
|
|
362
380
|
train_log_vis_df = train_log_df[columns_to_keep]
|
|
363
381
|
fig = train_log_vis_df.plot().get_figure()
|
|
364
382
|
if fig is not None:
|
|
365
|
-
fig.savefig(str(train_report_path))
|
|
383
|
+
fig.savefig(str(train_report_path)) # type: ignore[union-attr]
|
|
366
384
|
|
|
367
385
|
finally:
|
|
368
386
|
# Release the memory from the GPU...
|
|
@@ -40,8 +40,7 @@ def _load_images_args(args) -> argparse.Namespace:
|
|
|
40
40
|
"config_overrules",
|
|
41
41
|
nargs="*",
|
|
42
42
|
help=(
|
|
43
|
-
"Supply any number of config overrules like this: "
|
|
44
|
-
"<section>.<parameter>=<value>"
|
|
43
|
+
"Supply any number of config overrules like this: <section>.<key>=<value>"
|
|
45
44
|
),
|
|
46
45
|
)
|
|
47
46
|
|
|
@@ -61,7 +60,7 @@ def load_images(
|
|
|
61
60
|
images. Defaults to False.
|
|
62
61
|
config_overrules (list[str], optional): list of config options that will
|
|
63
62
|
overrule other ways to supply configuration. They should be specified in the
|
|
64
|
-
form of "<section>.<
|
|
63
|
+
form of "<section>.<key>=<value>". Defaults to None.
|
|
65
64
|
"""
|
|
66
65
|
# Init
|
|
67
66
|
# Load the config and save in a bunch of global variables zo it
|
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
import argparse
|
|
4
4
|
import logging
|
|
5
|
+
import shutil
|
|
5
6
|
import sys
|
|
7
|
+
import tempfile
|
|
8
|
+
import urllib.request
|
|
9
|
+
import zipfile
|
|
6
10
|
from pathlib import Path
|
|
7
11
|
|
|
8
12
|
import gdown
|
|
9
13
|
|
|
10
|
-
from orthoseg.util import git_downloader
|
|
11
|
-
|
|
12
14
|
# Get a logger...
|
|
13
15
|
logger = logging.getLogger(__name__)
|
|
14
16
|
|
|
@@ -59,43 +61,61 @@ def load_sampleprojects(dest_dir: Path, ssl_verify: bool | str = True):
|
|
|
59
61
|
this will be used. In corporate networks using a proxy server this is often
|
|
60
62
|
needed to avoid CERTIFICATE_VERIFY_FAILED errors. Defaults to True.
|
|
61
63
|
"""
|
|
64
|
+
dest_dir = dest_dir.expanduser()
|
|
62
65
|
dest_dir_full = dest_dir / "sample_projects"
|
|
63
66
|
if dest_dir_full.exists():
|
|
64
67
|
raise ValueError(f"Destination directory already exists: {dest_dir_full}")
|
|
65
68
|
|
|
66
69
|
# Download
|
|
67
70
|
print(f"Start download of sample projects to {dest_dir_full!s}")
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp_dir:
|
|
72
|
+
# Download the sample projects to a temporary directory first
|
|
73
|
+
url = "https://github.com/orthoseg/orthoseg_sample_projects/archive/refs/tags/v0.1.0.zip"
|
|
74
|
+
tmp_zip_path = Path(tmp_dir) / "sample_projects.zip"
|
|
75
|
+
urllib.request.urlretrieve(url, tmp_zip_path)
|
|
76
|
+
|
|
77
|
+
# Unzip the downloaded file
|
|
78
|
+
tmp_proj_dir = Path(tmp_dir) / "sample_projects_tmp"
|
|
79
|
+
with zipfile.ZipFile(tmp_zip_path, "r") as zip_file:
|
|
80
|
+
zip_file.extractall(tmp_proj_dir)
|
|
81
|
+
# Get the single subdirectory in the unzipped folder
|
|
82
|
+
subdirs = [d for d in tmp_proj_dir.iterdir() if d.is_dir()]
|
|
83
|
+
if len(subdirs) != 1:
|
|
84
|
+
raise ValueError(
|
|
85
|
+
f"Expected exactly one subdirectory, but found {len(subdirs)}"
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
# Move the unzipped sample projects to the destination directory
|
|
89
|
+
dest_dir.mkdir(parents=True, exist_ok=True)
|
|
90
|
+
shutil.move(subdirs[0] / "sample_projects", dest_dir)
|
|
91
|
+
|
|
73
92
|
print("Download finished")
|
|
93
|
+
|
|
74
94
|
print("Start download of footballfields pretrained neural net")
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
95
|
+
verify = True if ssl_verify is None else ssl_verify
|
|
96
|
+
model_dir = dest_dir_full / "footballfields/models"
|
|
97
|
+
model_dir.mkdir(parents=True, exist_ok=True)
|
|
98
|
+
|
|
99
|
+
model_hdf5_path = model_dir / "footballfields_01_0.97392_201.hdf5"
|
|
100
|
+
if not model_hdf5_path.exists():
|
|
79
101
|
gdown.download(
|
|
80
|
-
id="
|
|
102
|
+
id="1UlNorZ74ADCr3pL4MCJ_tnKRNoeZX79g",
|
|
81
103
|
output=str(model_hdf5_path),
|
|
82
|
-
verify=
|
|
104
|
+
verify=verify,
|
|
83
105
|
)
|
|
84
|
-
model_hyperparams_path =
|
|
85
|
-
|
|
86
|
-
)
|
|
87
|
-
if model_hyperparams_path.exists() is False:
|
|
106
|
+
model_hyperparams_path = model_dir / "footballfields_01_hyperparams.json"
|
|
107
|
+
if not model_hyperparams_path.exists():
|
|
88
108
|
gdown.download(
|
|
89
|
-
id="
|
|
109
|
+
id="1NwrVVjx9IsjvaioQ4-bkPMrq7S6HeWIo",
|
|
90
110
|
output=str(model_hyperparams_path),
|
|
91
|
-
verify=
|
|
111
|
+
verify=verify,
|
|
92
112
|
)
|
|
93
|
-
model_modeljson_path =
|
|
94
|
-
if model_modeljson_path.exists()
|
|
113
|
+
model_modeljson_path = model_dir / "footballfields_01_model.json"
|
|
114
|
+
if not model_modeljson_path.exists():
|
|
95
115
|
gdown.download(
|
|
96
|
-
id="
|
|
116
|
+
id="1LNPLypM5in3aZngBKK_U4Si47Oe97ZWN",
|
|
97
117
|
output=str(model_modeljson_path),
|
|
98
|
-
verify=
|
|
118
|
+
verify=verify,
|
|
99
119
|
)
|
|
100
120
|
print("Download finished")
|
|
101
121
|
|