Skip to content

Commit

Permalink
Merge pull request #112 from IGNF/fix-output-epsg
Browse files Browse the repository at this point in the history
Pass input file parameters to las writer
  • Loading branch information
leavauchier authored Feb 14, 2024
2 parents d7b38bc + 80850f8 commit 11bbad5
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 27 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# CHANGELOG
### 3.8.1
- fix: propagate input las format to output las (in particular epsg which comes either from input or config)

### 3.8.0
## 3.8.0
- dev: log confusion matrices to Comet after each epoch.
- fix: do not mix the two way to log IoUs to avoid known lightning [Common Pitfalls](https://lightning.ai/docs/torchmetrics/stable/pages/lightning.html#common-pitfalls).

Expand All @@ -9,7 +11,7 @@

## 3.7.0
- Update all versions of Pytorch, Pytorch Lightning, and Pytorch Geometric.
Changes are retrocompatible for models trained with older versions (with adjustment to the configuration file).
Changes are retrocompatible for models trained with older versions (with adjustment to the configuration file).
- Refactor logging of single-class IoUs to go from num_classes+1 torchmetrics instances to only 1.

### 3.6.1
Expand Down Expand Up @@ -64,4 +66,4 @@
- Fix dataset description for pacasam: there was an unwanted int-to-int mapping in classification_dict

## 3.4.0
- Allow inference for the smallest possible patches (num_nodes=1) to have consistent inference behavior
- Allow inference for the smallest possible patches (num_nodes=1) to have consistent inference behavior
10 changes: 6 additions & 4 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ dependencies:
- numpy
- h5py
# --------- geo --------- #
- pdal
- pdal==2.6.*
- python-pdal
- pyproj
# --------- Visualization --------- #
- conda-forge:gdal
- conda_forge:pyproj
# --------- Visualization --------- #
- pandas
- matplotlib
# --------- loggers --------- #
Expand Down Expand Up @@ -68,4 +69,5 @@ dependencies:
# --------- Documentation --------- #
- myst_parser==0.17.*
- sphinxnotes-mock==1.0.0b0 # still a beta
- sphinx_paramlinks==0.5.*
- sphinx_paramlinks==0.5.*
- ign-pdal-tools>=1.5.2
16 changes: 10 additions & 6 deletions myria3d/models/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from torch.distributions import Categorical
from torch_scatter import scatter_sum

from pdaltools import las_info

from myria3d.pctl.dataset.utils import get_pdal_info_metadata, get_pdal_reader

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -55,7 +57,7 @@ def __init__(
self.logits: List[torch.Tensor] = []
self.idx_in_full_cloud_list: List[np.ndarray] = []

def load_full_las_for_update(self, src_las: str, epsg: str) -> np.ndarray:
def load_full_las_for_update(self, src_las: str, epsg: str) -> Tuple[np.ndarray, Dict]:
"""Loads a LAS and adds necessary extradim.
Args:
Expand Down Expand Up @@ -83,7 +85,10 @@ def load_full_las_for_update(self, src_las: str, epsg: str) -> np.ndarray:
pipeline |= pdal.Filter.assign(value=f"{self.entropy_channel}=0")

pipeline.execute()
return pipeline.arrays[0]
writer_params = las_info.get_writer_parameters_from_reader_metadata(
pipeline.metadata, a_srs=f"EPSG:{epsg}" if str(epsg).isdigit() else epsg
)
return pipeline.arrays[0], writer_params

def store_predictions(self, logits, idx_in_original_cloud) -> None:
"""Keep a list of predictions made so far."""
Expand Down Expand Up @@ -143,7 +148,7 @@ def reduce_predictions_and_save(self, raw_path: str, output_dir: str, epsg: str)
del logits

# Read las after fetching all information to write into it
las = self.load_full_las_for_update(raw_path, epsg)
las, writer_params = self.load_full_las_for_update(raw_path, epsg)

for idx, class_name in enumerate(self.classification_dict.values()):
if class_name in self.probas_to_save:
Expand Down Expand Up @@ -173,9 +178,8 @@ def reduce_predictions_and_save(self, raw_path: str, output_dir: str, epsg: str)
out_f = os.path.abspath(out_f)
log.info(f"Updated LAS ({basename}) will be saved to: \n {output_dir}\n")
log.info("Saving...")
pipeline = pdal.Writer.las(
filename=out_f, extra_dims="all", minor_version=4, dataformat_id=8
).pipeline(las)
writer_params["extra_dims"] = "all"
pipeline = pdal.Writer.las(filename=out_f, **writer_params).pipeline(las)
pipeline.execute()
log.info("Saved.")

Expand Down
19 changes: 6 additions & 13 deletions myria3d/pctl/dataset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,12 @@ def get_pdal_reader(las_path: str, epsg: str) -> pdal.Reader.las:

if epsg :
# if an epsg in provided, force pdal to read the lidar file with it
try : # epsg can be added as a number like "2154" or as a string like "EPSG:2154"
int(epsg)
return pdal.Reader.las(
filename=las_path,
nosrs=True,
override_srs=f"EPSG:{epsg}",
)
except ValueError:
return pdal.Reader.las(
filename=las_path,
nosrs=True,
override_srs=epsg,
)
# epsg can be added as a number like "2154" or as a string like "EPSG:2154"
return pdal.Reader.las(
filename=las_path,
nosrs=True,
override_srs=f"EPSG:{epsg}" if str(epsg).isdigit() else epsg,
)

try :
if get_metadata(las_path)['metadata']['readers.las']['srs']['compoundwkt']:
Expand Down
2 changes: 1 addition & 1 deletion package_metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__: "3.8.0"
__version__: "3.8.1"
__name__: "myria3d"
__url__: "https://github.com/IGNF/myria3d"
__description__: "Deep Learning for the Semantic Segmentation of Aerial Lidar Point Clouds"
Expand Down
6 changes: 6 additions & 0 deletions tests/myria3d/test_train_and_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import numpy as np
import pytest
from lightning.pytorch.accelerators import find_usable_cuda_devices
from pathlib import Path
from pdaltools import las_info


from myria3d.pctl.dataset.toy_dataset import TOY_LAS_DATA
Expand Down Expand Up @@ -94,6 +96,10 @@ def test_predict_as_command(one_epoch_trained_RandLaNet_checkpoint, tmpdir):
"task.task_name=predict",
]
run_hydra_decorated_command(command)
output_path = Path(tmpdir) / Path(abs_path_to_toy_LAS).name
metadata = las_info.las_info_metadata(output_path)
out_pesg = las_info.get_epsg_from_header_info(metadata)
assert out_pesg == DEFAULT_EPSG


def test_command_without_epsg(one_epoch_trained_RandLaNet_checkpoint, tmpdir):
Expand Down

0 comments on commit 11bbad5

Please sign in to comment.