Views
No views yet
(pixel - band_min_value) / (band_max_value - band_min_value)1# Install kelp-o-matic
2pip install git+https://github.com/HakaiInstitute/kelp-o-matic@dev
3
4# List available models
5kom list-models
6
7# Run kelp species segmentation on RGB+NIR drone imagery
8kom segment \
9 --model kelp-rgbi \
10 --input /path/to/rgbi_drone_image.tif \
11 --output /path/to/kelp_species_segmentation.tif \
12 --batch-size 6 \
13 --crop-size 2048 \
14 --blur-kernel 5 \
15 --morph-kernel 3 \
16 -b 1 \ # Specify -b flags to rearrange bands to Red, Green, Blue, NIR order
17 -b 2 \
18 -b 3 \
19 -b 4
20
21# Use specific model version
22kom segment \
23 --model kelp-rgbi \
24 --version 20231214 \
25 --input image.tif \
26 --output result.tif
27
28# For high-resolution multispectral imagery
29kom segment \
30 --model kelp-rgbi \
31 --input high_res_multispectral.tif \
32 --output result.tif \
33 --batch-size 4 \
34 --crop-size 1024 ]
35 -b 3 \ # BGRI -> RGBI
36 -b 2 \
37 -b 1 \
38 -b 41from kelp_o_matic import model_registry
2
3# Load the model (automatically downloads if needed)
4model = model_registry["kelp-rgbi"]
5
6# Process a large multispectral image with automatic tiling
7model.process(
8 input_path="path/to/your/rgbi_drone_image.tif",
9 output_path="path/to/output/kelp_species_segmentation.tif",
10 batch_size=6, # Moderate batch size for 4-band
11 crop_size=2048,
12 blur_kernel_size=5, # Post-processing median blur
13 morph_kernel_size=3, # Morphological operations
14 band_order=[1, 2, 3, 4], # Ensure RGBI order
15)
16
17# For more control, use the predict method directly
18import rasterio
19import numpy as np
20
21with rasterio.open("multispectral_image.tif") as src:
22 # Read a 2048x2048 tile (4 bands: RGBI)
23 tile = src.read(window=((0, 2048), (0, 2048))) # Shape: (4, 2048, 2048)
24 tile = np.transpose(tile, (1, 2, 0)) # Convert to HWC
25
26 # Add batch dimension and predict
27 batch = np.expand_dims(tile, axis=0) # Shape: (1, 2048, 2048, 4)
28 batch = np.transpose(batch, (0, 3, 1, 2)) # Convert to BCHW
29
30 # Run inference (preprocessing handled automatically)
31 predictions = model.predict(batch)
32
33 # Post-process to get final segmentation
34 segmentation = model.postprocess(predictions)
35 # Result: 0=background, 1=giant kelp, 2=bull kelp1import numpy as np
2import onnxruntime as ort
3from huggingface_hub import hf_hub_download
4
5# Download the model
6model_path = hf_hub_download(repo_id="HakaiInstitute/kelp-rgbi", filename="model.onnx")
7
8# Load the model
9session = ort.InferenceSession(model_path)
10
11# Preprocess your 4-band image
12def preprocess(image):
13 """
14 Preprocess 4-band RGBI image for model input
15 image: numpy array of shape [height, width, 4] with any pixel value range
16 """
17 # Normalize to 0-1 first
18 image = image.astype(np.float32) / 1.0
19
20 # Apply min-max normalization per image
21 img_min = image.min()
22 img_max = image.max()
23 image = (image - img_min) / (img_max - img_min + 1e-8)
24
25 # Reshape to model input format [batch, channels, height, width]
26 image = np.transpose(image, (2, 0, 1)) # HWC to CHW
27 image = np.expand_dims(image, axis=0) # Add batch dimension
28
29 return image
30
31# Run inference
32preprocessed = preprocess(your_4band_image)
33input_name = session.get_inputs()[0].name
34output = session.run(None, {input_name: preprocessed})
35
36# Postprocess to get class predictions
37logits = output[0] # Raw probabilities for each class
38prediction = np.argmax(logits, axis=1).squeeze(0).astype(np.uint8)
39# Result: 0=background, 1=giant kelp, 2=bull kelp1from huggingface_hub import hf_hub_download
2import onnxruntime as ort
3
4# Download and load model
5model_path = hf_hub_download(
6 repo_id="HakaiInstitute/kelp-rgbi",
7 filename="model.onnx",
8 cache_dir="./models"
9)
10
11session = ort.InferenceSession(model_path)
12# ... continue with preprocessing and inference as above1# Via pip
2pip install git+https://github.com/HakaiInstitute/kelp-o-matic@dev1pip install onnxruntime huggingface-hub numpy
2# For GPU support:
3pip install onnxruntime-gpu1@software{Denouden_Kelp-O-Matic,
2 author = {Denouden, Taylor and Reshitnyk, Luba},
3 doi = {10.5281/zenodo.7672166},
4 title = {{Kelp-O-Matic}},
5 url = {https://github.com/HakaiInstitute/kelp-o-matic}
6}