Skip to content

Commit

Permalink
New mussel detection model (#104)
Browse files Browse the repository at this point in the history
* Update mussel model

Update post processing for mussels function

* Fix shortcut logic for mussel model

Sigmoid activation requires some different logic for handly the shortcut to skip classifying all black regions

* Update mussels dataset statistics

* Linter fixes with Ruff
  • Loading branch information
tayden authored May 3, 2024
1 parent 8ef8ad4 commit 87537e1
Show file tree
Hide file tree
Showing 9 changed files with 417 additions and 450 deletions.
9 changes: 3 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ repos:
hooks:
- id: poetry-check
- id: poetry-lock
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.265
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.2
hooks:
- id: ruff
args: [ --fix ]
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
- id: ruff-format
41 changes: 24 additions & 17 deletions docs/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,21 @@ flowchart TD

**Kelp (species)**

| Split | Scenes | Tiles | Pixels~macro~ | Pixels~nereo~ | Pixels~total~ | Area (km^2^) | Res~max~ (m) | Res~min~ (m) | Res~$\mu$~ (m) | Res~$\sigma$~ (m) |
|:------|-------:|-------:|--------------:|--------------:|--------------:|-------------:|-------------:|-------------:|---------------:|------------------:|
| Train | 17 | 336740 | 605462674 | 1158650042 | 88008624978 | 4034.11 | 0.1040 | 0.0230 | 0.0488 | 0.0266 |
| Val. | 4 | 15805 | 127410722 | 20244320 | 4110229732 | 123.91 | 0.0420 | 0.0230 | 0.0296 | 0.0085 |
| Test | 6 | 46304 | 143277498 | 176569508 | 12046802177 | 409.72 | 0.0680 | 0.0230 | 0.0385 | 0.0161 |
| *Sum* | 27 | 398849 | 876150894 | 1355463870 | 104165656887 | 4567.75 | | | | |
| Split | Scenes | Tiles | Pixels~macro~ | Pixels~nereo~ | Pixels~total~ | Area (km^2^) | Res~max~ (m) | Res~min~ (m) | Res~$\mu$~ (m) | Res~$\sigma$~ (m) |
|:-----------|-------:|-------:|--------------:|--------------:|--------------:|-------------:|-------------:|-------------:|---------------:|------------------:|
| Train | 17 | 336740 | 605462674 | 1158650042 | 88008624978 | 4034.11 | 0.1040 | 0.0230 | 0.0488 | 0.0266 |
| Validation | 4 | 15805 | 127410722 | 20244320 | 4110229732 | 123.91 | 0.0420 | 0.0230 | 0.0296 | 0.0085 |
| Test | 6 | 46304 | 143277498 | 176569508 | 12046802177 | 409.72 | 0.0680 | 0.0230 | 0.0385 | 0.0161 |
| *Sum* | 27 | 398849 | 876150894 | 1355463870 | 104165656887 | 4567.75 | | | | |

**Mussels (presence/absence)**

!!! todo

The mussel model is a work in progress. This section will be available in the future.
| Split | Scenes | Tiles | Pixels~mussels~ | Pixels~total~ | Area (km^2^) | Res~max~ (m) | Res~min~ (m) | Res~$\mu$~ (m) | Res~$\sigma$~ (m) |
|:-----------|-------:|------:|----------------:|--------------:|-------------:|-------------:|-------------:|---------------:|------------------:|
| Train | 39 | 4834 | 933287123 | 5068816384 | 23.8147 | 0.027 | 0.00231 | 0.0082244 | 0.00492054 |
| Validation | 8 | 1277 | 223598444 | 1339031552 | 5.74255 | 0.00518591 | 0.00330667 | 0.00421985 | 0.000545617 |
| Test | 8 | 1110 | 175226412 | 1163919360 | 4.64052 | 0.00578278 | 0.003671 | 0.00435929 | 0.000665025 |
| *Sum* | 55 | 7221 | 1332111979 | 7571767296 | 34.197799 | 0.037969 | 0.009288 | 0.016804 | 0.006131 |

## Model Training

Expand Down Expand Up @@ -333,22 +336,26 @@ $$

**Mussels (presence/absence)**

!!! todo
=== "Test split"

The mussel model is a work in progress. This section will be updated in the future.
| Class | IoU | Precision | Recall | Accuracy |
|:------------|-------:|----------:|-------:|---------:|
| Mussels | 0.7360 | 0.8314 | 0.8262 | - |
| Not Mussels | 0.9824 | 0.9880 | 0.9942 | - |
| *Mean* | 0.8592 | 0.9097 | 0.9102 | 0.9847 |

=== "Validation split"

| Class | IoU | Precision | Recall | Accuracy |
|:------------|-------:|----------:|-------:|---------:|
| Mussels | 0.7684 | 0.8594 | 0.8777 | - |
| Not Mussels | 0.9805 | 0.9917 | 0.9886 | - |
| *Mean* | 0.8745 | 0.9721 | 0.9721 | 0.9331 |
| Mussels | 0.7863 | 0.9232 | 0.8433 | - |
| Not Mussels | 0.9822 | 0.9887 | 0.9933 | - |
| *Mean* | 0.8843 | 0.9560 | 0.9183 | 0.9846 |

=== "Train split"

| Class | IoU | Precision | Recall | Accuracy |
|:------------|-------:|----------:|-------:|---------:|
| Mussels | 0.8344 | 0.8636 | 0.9595 | - |
| Not Mussels | 0.9770 | 0.9892 | 0.9874 | - |
| *Mean* | 0.9057 | 0.9264 | 0.9735 | 0.9062 |
| Mussels | 0.7499 | 0.8698 | 0.8425 | - |
| Not Mussels | 0.9783 | 0.9889 | 0.9891 | - |
| *Mean* | 0.8641 | 0.9294 | 0.9158 | 0.9806 |
1 change: 1 addition & 0 deletions kelp_o_matic/geotiff_io/geotiff_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Date: 2021-02-22
Description: A Pytorch Dataset for geotiff images that dynamically crops the image.
"""

import itertools
from pathlib import Path
from typing import List, Optional, Union
Expand Down
1 change: 1 addition & 0 deletions kelp_o_matic/geotiff_io/geotiff_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Date: 2021-02-22
Description: A Pytorch Dataset for geotiff images that dynamically crops the image.
"""

from pathlib import Path
from typing import Union

Expand Down
12 changes: 6 additions & 6 deletions kelp_o_matic/hann.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,21 @@ def step(self, new_logits: torch.Tensor, img_window: Window):
logits_bd = logits_abcd[:, :, self.hws :]

# write c0
self.register[
:, :, img_window.col_off : img_window.col_off + self.hws
] = logits_c0
self.register[:, :, img_window.col_off : img_window.col_off + self.hws] = (
logits_c0
)

# write bd
col_off_bd = img_window.col_off + self.hws
self.register[:, :, col_off_bd : col_off_bd + (self.ws - self.hws)] = logits_bd

# Return the information-complete predictions
preds_win = Window(
logits_win = Window(
col_off=img_window.col_off,
row_off=img_window.row_off,
height=min(self.hws, img_window.height),
width=min(self.hws, img_window.width),
)
preds = logits_a[:, : img_window.height, : img_window.width].softmax(axis=0)
logits = logits_a[:, : img_window.height, : img_window.width]

return preds, preds_win
return logits, logits_win
22 changes: 17 additions & 5 deletions kelp_o_matic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

class _Model(ABC):
register_depth = 2
all_black_val = 1

@staticmethod
def transform(x: Union[np.ndarray, Image]) -> torch.Tensor:
Expand Down Expand Up @@ -53,7 +54,7 @@ def shortcut(self, crop_size: int):
logits = torch.zeros(
(self.register_depth, crop_size, crop_size), device=self.device
)
logits[0] = 1
logits[0] = self.all_black_val
return logits


Expand Down Expand Up @@ -97,10 +98,21 @@ class KelpRGBSpeciesSegmentationModel(_SpeciesSegmentationModel):


class MusselRGBPresenceSegmentationModel(_Model):
torchscript_path = "LRASPP_MobileNetV3_mussel_presence_rgb_jit_miou=0.8745.pt"
register_depth = 1
all_black_val = -1

torchscript_path = (
"UNetPlusPlus_EfficientNetB4_mussel_presence_rgb_jit_dice=0.9269.pt"
)

def post_process(self, x: "torch.Tensor") -> "np.ndarray":
with torch.no_grad():
label = (torch.sigmoid(x) > 0.5).to(torch.uint8)[0]

return label.detach().cpu().numpy()


def _unet_efficientnet_b4_transform(x: Union[np.ndarray, Image]) -> torch.Tensor:
def _rgbi_kelp_transform(x: Union[np.ndarray, Image]) -> torch.Tensor:
# to float
x = f.to_tensor(x)[:4, :, :].to(torch.float)
# min-max scale
Expand All @@ -119,7 +131,7 @@ class KelpRGBIPresenceSegmentationModel(_Model):

@staticmethod
def transform(x: Union[np.ndarray, Image]) -> torch.Tensor:
return _unet_efficientnet_b4_transform(x)
return _rgbi_kelp_transform(x)


class KelpRGBISpeciesSegmentationModel(_SpeciesSegmentationModel):
Expand All @@ -130,4 +142,4 @@ class KelpRGBISpeciesSegmentationModel(_SpeciesSegmentationModel):

@staticmethod
def transform(x: Union[np.ndarray, Image]) -> torch.Tensor:
return _unet_efficientnet_b4_transform(x)
return _rgbi_kelp_transform(x)
Loading

0 comments on commit 87537e1

Please sign in to comment.