Skip to content

Commit

Permalink
Merge branch 'main' into docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tayden authored Jan 24, 2025
2 parents c76046e + 0dfebe3 commit 4555302
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 59 deletions.
2 changes: 0 additions & 2 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ $ kom --help
Options
--version -v
--install-completion [bash|zsh|fish|powershell|pwsh] Install completion for the specified shell. [default: None]
--show-completion [bash|zsh|fish|powershell|pwsh] Show completion for the specified shell, to copy it or customize the installation. [default: None]
--help -h Show this message and exit.
Commands
Expand Down
191 changes: 134 additions & 57 deletions kelp_o_matic/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path
from typing import Optional
from typing import Annotated, Optional

import torch
import typer

from kelp_o_matic import (
Expand All @@ -9,42 +10,71 @@
find_mussels as find_mussels_,
)

cli = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]})
cli = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]}, add_completion=False)


@cli.command()
def find_kelp(
source: Path = typer.Argument(..., help="Input image with Byte data type."),
dest: Path = typer.Argument(..., help="File path location to save output to."),
species: bool = typer.Option(
False,
"--species/--presence",
help="Segment to species or presence/absence level.",
),
crop_size: int = typer.Option(
1024,
help="The data window size to run through the segmentation model.",
),
use_nir: bool = typer.Option(
False,
"--rgbi/--rgb",
help="Use RGB and NIR bands for classification. Assumes RGBI ordering.",
),
band_order: Optional[list[int]] = typer.Option(
None,
"-b",
help="GDAL-style band re-ordering flag. Defaults to RGB or RGBI order. "
"To e.g., reorder a BGRI image at runtime, pass flags `-b 3 -b 2 -b 1 -b 4`.",
),
use_gpu: bool = typer.Option(
True, "--gpu/--no-gpu", help="Enable or disable GPU, if available."
),
use_tta: bool = typer.Option(
False,
"--tta/--no-tta",
help="Use test time augmentation to improve accuracy at the cost of "
"processing time.",
),
source: Annotated[
Path,
typer.Argument(
exists=True,
dir_okay=False,
file_okay=True,
readable=True,
help="Input image with Byte data type.",
),
],
dest: Annotated[
Path,
typer.Argument(
exists=True,
dir_okay=False,
file_okay=True,
writable=True,
help="File path location to save output to.",
),
],
species: Annotated[
bool,
typer.Option(
"--species/--presence",
help="Segment to species or presence/absence level.",
),
] = False,
crop_size: Annotated[
int,
typer.Option(
help="The data window size to run through the segmentation model.",
),
] = 1024,
use_nir: Annotated[
bool,
typer.Option(
"--rgbi/--rgb",
help="Use RGB and NIR bands for classification. Assumes RGBI ordering.",
),
] = False,
band_order: Annotated[
Optional[list[int]],
typer.Option(
"-b",
help="GDAL-style band re-ordering flag. Defaults to RGB or RGBI order. "
"To e.g., reorder a BGRI image at runtime, pass flags `-b 3 -b 2 -b 1 -b 4`.",
),
] = None,
use_gpu: Annotated[
bool,
typer.Option("--gpu/--no-gpu", help="Enable or disable GPU, if available."),
] = True,
use_tta: Annotated[
bool,
typer.Option(
"--tta/--no-tta",
help="Use test time augmentation to improve accuracy at the cost of "
"processing time.",
),
] = False,
):
"""
Detect kelp in image at path SOURCE and output the resulting classification raster
Expand All @@ -55,27 +85,52 @@ def find_kelp(

@cli.command()
def find_mussels(
source: Path = typer.Argument(..., help="Input image with Byte data type."),
dest: Path = typer.Argument(..., help="File path location to save output to."),
crop_size: int = typer.Option(
1024,
help="The data window size to run through the segmentation model.",
),
band_order: Optional[list[int]] = typer.Option(
None,
"-b",
help="GDAL-style band re-ordering flag. Defaults to RGB order. "
"To e.g., reorder a BGR image at runtime, pass flags `-b 3 -b 2 -b 1`.",
),
use_gpu: bool = typer.Option(
True, "--gpu/--no-gpu", help="Enable or disable GPU, if available."
),
use_tta: bool = typer.Option(
False,
"--tta/--no-tta",
help="Use test time augmentation to improve accuracy at the cost of "
"processing time.",
),
source: Annotated[
Path,
typer.Argument(
exists=True,
dir_okay=False,
file_okay=True,
readable=True,
help="Input image with Byte data type.",
),
],
dest: Annotated[
Path,
typer.Argument(
exists=True,
dir_okay=False,
file_okay=True,
writable=True,
help="File path location to save output to.",
),
],
crop_size: Annotated[
int,
typer.Option(
help="The data window size to run through the segmentation model.",
),
] = 1024,
band_order: Annotated[
Optional[list[int]],
typer.Option(
"-b",
help="GDAL-style band re-ordering flag. Defaults to RGB or RGBI order. "
"To e.g., reorder a BGRI image at runtime, pass flags `-b 3 -b 2 -b 1 -b 4`.",
),
] = None,
use_gpu: Annotated[
bool,
typer.Option("--gpu/--no-gpu", help="Enable or disable GPU, if available."),
] = True,
use_tta: Annotated[
bool,
typer.Option(
"--tta/--no-tta",
help="Use test time augmentation to improve accuracy at the cost of "
"processing time.",
),
] = False,
):
"""
Detect mussels in image at path SOURCE and output the resulting classification
Expand All @@ -90,11 +145,33 @@ def version_callback(value: bool) -> None:
raise typer.Exit()


def gpu_callback(value: bool) -> None:
if value:
typer.echo(f"GPU detected: {torch.cuda.is_available()}")
raise typer.Exit()


@cli.callback()
def main(
version: bool = typer.Option(
None, "--version", "-v", callback=version_callback, is_eager=True
),
version: Annotated[
bool,
typer.Option(
"--version",
"-v",
callback=version_callback,
is_eager=True,
help="Show version and exit.",
),
] = False,
gpu_test: Annotated[
bool,
typer.Option(
"--gpu-test",
callback=gpu_callback,
is_eager=True,
help="Test if GPU is detected and exit.",
),
] = False,
):
return

Expand Down

0 comments on commit 4555302

Please sign in to comment.