Skip to content

Commit

Permalink
Clipping values in normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaspadilha committed Jul 30, 2024
1 parent fb6a04b commit 6ec49e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ops/segment_anything/automatic_segmentation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ description:
parameters:
model_type: SAM's image encoder backbone architecture, among 'vit_h', 'vit_l', or 'vit_b'. Before running the workflow, make sure the desired model has been exported to the cluster by running `scripts/export_sam_models.py`. For more information, refer to the FarmVibes.AI troubleshooting page in the documentation.
band_names: Name of raster bands that should be selected to compose the 3-channel images expected by SAM. If not provided, will try to use ["R", "G", "B"]. If only a single band name is provided, will replicate it through all three channels.
band_scaling: A list of floats to scale each band by to the range of [0.0, 1.0] or [0.0, 255.0]. If not provided, will default to the raster scaling parameter. If a list with a single value is provided, will use it for all three bands.
band_scaling: A list of floats to scale each band by to the range of [0.0, 1.0]. If not provided, will default to the raster scaling parameter. If a list with a single value is provided, will use it for all three bands.
band_offset: A list of floats to offset each band by. If not provided, will default to the raster offset value. If a list with a single value is provided, will use it for all three bands.
spatial_overlap: Percentage of spatial overlap between chips in the range of [0.0, 1.0).
points_per_side: The number of points to be sampled along one side of the chip to be prompts. The total number of points is points_per_side**2.
Expand Down
18 changes: 13 additions & 5 deletions src/vibe_lib/vibe_lib/segment_anything.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
from geopandas import GeoDataFrame
from numpy.typing import NDArray
from rasterio import Affine
from rasterio.windows import Window
from shapely.geometry.base import BaseGeometry
from torchvision.transforms.functional import resize

from vibe_core.data import GeometryCollection, Raster
from vibe_core.data.core_types import BBox, Point
from vibe_lib.spaceeye.chip import ChipDataset, Dims, Window
from vibe_lib.spaceeye.chip import ChipDataset, Dims

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -473,7 +474,7 @@ def build_chip_preprocessing_operation(
elif len(band_scaling) != len(band_names):
raise ValueError(f"Expected one or three scaling parameters. Got {band_scaling}")
else:
band_scaling = [raster.scale] * 3
band_scaling = [float(raster.scale)] * 3
scale = np.array(band_scaling).reshape(1, 3, 1, 1)

if band_offset:
Expand All @@ -483,13 +484,20 @@ def build_chip_preprocessing_operation(
elif len(band_offset) != len(band_names):
raise ValueError(f"Expected one or three offset parameters. Got {band_offset}")
else:
band_offset = [raster.offset] * 3
band_offset = [float(raster.offset)] * 3
offset = np.array(band_offset).reshape(1, 3, 1, 1)

def preprocessing_operation(chip: NDArray[Any]) -> NDArray[Any]:
normalized_chip = chip[:, band_idx, :, :] * scale + offset
if np.min(normalized_chip) >= 0 and np.max(normalized_chip) <= 1:
normalized_chip = normalized_chip * 255.0
if np.min(normalized_chip) < 0 or np.max(normalized_chip) > 1:
LOGGER.warning(
"Chip values are outside the expected range [0, 1] after scaling and offset. "
f"Found max of {np.max(normalized_chip)} and min of {np.min(normalized_chip)}."
"Will clip to [0, 1] and normalize to [0, 255]. Please, verify the band_scaling "
"and band_offset parameters of the workflow."
)
normalized_chip = np.clip(normalized_chip, 0, 1)
normalized_chip = normalized_chip * 255.0
return normalized_chip.astype(np.float32)

return preprocessing_operation
Expand Down

0 comments on commit 6ec49e4

Please sign in to comment.