Skip to content

Commit

Permalink
Merge pull request #163 from PUTvision/fix/shifted_channels
Browse files Browse the repository at this point in the history
Fix shifted values, updated test for them
  • Loading branch information
bartoszptak authored Apr 14, 2024
2 parents a8ef061 + 0722bd8 commit d939c95
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/deepness/metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
name=Deepness: Deep Neural Remote Sensing
qgisMinimumVersion=3.22
description=Inference of deep neural network models (ONNX) for segmentation, detection and regression
version=0.6.2
version=0.6.3
author=PUT Vision
[email protected]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ def _create_result_message(self, result_img: np.ndarray) -> str:
total_area = number_of_pixels_in_processing_area * self.params.resolution_m_per_px**2

for channel_id in range(layer_sizes):
# See note in the class description why are we adding/subtracting 1 here
pixels_count = counts_map.get(channel_id, 0)
pixels_count = counts_map.get(channel_id + 1, 0) # we add 1 to avoid 0 values, find the MADD1 code for explanation
area = pixels_count * self.params.resolution_m_per_px**2

if total_area > 0 and not np.isnan(total_area) and not np.isinf(total_area):
Expand All @@ -110,8 +109,7 @@ def _create_vlayer_from_mask_for_base_extent(self, mask_img) -> Callable:
for output_id, layer_sizes in enumerate(self._get_indexes_of_model_output_channels_to_create()):
output_vlayers = []
for channel_id in range(layer_sizes):
# See note in the class description why are we adding/subtracting 1 here
local_mask_img = np.uint8(mask_img[output_id] == channel_id)
local_mask_img = np.uint8(mask_img[output_id] == (channel_id + 1)) # we add 1 to avoid 0 values, find the MADD1 code for explanation

contours, hierarchy = cv2.findContours(local_mask_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = processing_utils.transform_contours_yx_pixels_to_target_crs(
Expand Down Expand Up @@ -177,10 +175,10 @@ def _process_tile(self, tile_img_batched: np.ndarray) -> np.ndarray:
result = np.expand_dims(result, axis=1)

if (result.shape[1] == 1):
result = (result != 0).astype(int)
result = (result != 0).astype(int) + 1 # we add 1 to avoid 0 values, find the MADD1 code for explanation
else:
shape = result.shape
result = np.argmax(result, axis=1).reshape(shape[0], 1, shape[2], shape[3]) + 1
result = np.argmax(result, axis=1).reshape(shape[0], 1, shape[2], shape[3]) + 1 # we add 1 to avoid 0 values, find the MADD1 code for explanation

assert len(result.shape) == 4
assert result.shape[1] == 1
Expand Down
16 changes: 8 additions & 8 deletions test/test_map_processor_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ def test_generic_processing_test__specified_extent_from_vlayer_one_channel():

# just check a few pixels
assert all(result_img.ravel()[[365, 41234, 59876, 234353, 111222, 134534, 223423, 65463, 156451]] ==
np.asarray([0, 1, 1, 1, 1, 0, 0, 1, 0]))
np.asarray([0, 2, 2, 2, 2, 0, 0, 2, 0]))

# and counts of different values
np.testing.assert_allclose(np.unique(result_img, return_counts=True)[1], np.array([178063, 206029]), atol=3)
np.testing.assert_allclose(np.unique(result_img, return_counts=True)[1], np.array([166903, 11159, 206030]), atol=3)

def test_generic_processing_test__specified_extent_from_vlayer_two_channels():
qgs = init_qgis()
Expand Down Expand Up @@ -188,10 +188,10 @@ def test_generic_processing_test__specified_extent_from_vlayer_two_channels():

# just check a few pixels
assert all(result_img.ravel()[[365, 41234, 59876, 234353, 111222, 134534, 223423, 65463, 156451]] ==
np.asarray([0, 1, 1, 1, 1, 0, 0, 1, 0]))
np.asarray([0, 2, 2, 2, 2, 0, 0, 2, 0]))

# and counts of different values
np.testing.assert_allclose(np.unique(result_img, return_counts=True)[1], np.array([356126, 412058]), atol=3)
np.testing.assert_allclose(np.unique(result_img, return_counts=True)[1], np.array([333806, 22318, 412060]), atol=3)


def test_generic_processing_test__specified_extent_from_vlayer_crs3857_one_channel():
Expand Down Expand Up @@ -233,9 +233,9 @@ def test_generic_processing_test__specified_extent_from_vlayer_crs3857_one_chann

# just check a few pixels
assert all(result_img.ravel()[[365, 41234, 59876, 234353, 111222, 134534, 223423, 65463, 156451]] ==
np.asarray([0, 0, 1, 1, 1, 0, 0, 1, 0]))
np.asarray([0, 0, 2, 2, 2, 0, 0, 2, 0]))
# and counts of different values
np.testing.assert_allclose(np.unique(result_img, return_counts=True)[1], np.array([193853, 203797]), atol=3)
np.testing.assert_allclose(np.unique(result_img, return_counts=True)[1], np.array([182693, 11159, 203798]), atol=3)

def test_generic_processing_test__specified_extent_from_vlayer_crs3857_two_channels():
qgs = init_qgis()
Expand Down Expand Up @@ -279,9 +279,9 @@ def test_generic_processing_test__specified_extent_from_vlayer_crs3857_two_chann

# just check a few pixels
assert all(result_img.ravel()[[365, 41234, 59876, 234353, 111222, 134534, 223423, 65463, 156451]] ==
np.asarray([0, 0, 1, 1, 1, 0, 0, 1, 0]))
np.asarray([0, 0, 2, 2, 2, 0, 0, 2, 0]))

np.testing.assert_allclose(np.unique(result_img, return_counts=True)[1], np.array([387706, 407594]), atol=3)
np.testing.assert_allclose(np.unique(result_img, return_counts=True)[1], np.array([365386, 22318, 407596]), atol=3)


def test_generic_processing_test__specified_extent_from_active_map_extent_one_channel():
Expand Down
7 changes: 4 additions & 3 deletions test/test_map_processor_segmentation_sigmoid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from test.test_utils import (create_default_input_channels_mapping_for_rgba_bands, create_rlayer_from_file,
create_vlayer_from_file, get_dummy_fotomap_area_crs3857_path, get_dummy_fotomap_area_path,
get_dummy_fotomap_small_path, get_dummy_segmentation_model_path, get_dummy_sigmoid_model_path, init_qgis)
get_dummy_fotomap_small_path, get_dummy_segmentation_model_path,
get_dummy_sigmoid_model_path, init_qgis)
from unittest.mock import MagicMock

import matplotlib.pyplot as plt
Expand Down Expand Up @@ -56,8 +57,8 @@ def test_sigmoid_model_processing__entire_file():
result_img = map_processor.get_result_img()

assert result_img.shape == (1, 561, 829)
non_zero_pixels = np.count_nonzero(result_img)
assert abs(non_zero_pixels - 25002) < 50 # number of RED pixels in the image
pixels = np.unique(result_img, return_counts=True)
assert abs(pixels[1][1] - 25002) < 50 # number of RED pixels in the image

# you should see only the part of RASTER_FILE_PATH that is pure red pixels. Use snippet below for debugging
# from matplotlib import pyplot as plt
Expand Down

0 comments on commit d939c95

Please sign in to comment.