-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mateusz Kopcinski
authored and
Mateusz Kopcinski
committed
Aug 14, 2024
1 parent
65be519
commit 57d8d39
Showing
7 changed files
with
39 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 29 additions & 47 deletions
76
lib/ex_vision/semantic_segmentation/deep_lab_v3_mobilenet_v3.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,32 @@ | ||
defmodule ExVision.SemanticSegmentation.DeepLabV3_MobileNetV3 do | ||
@moduledoc """ | ||
An instance segmentation model with a ResNet-50-FPN backbone. Exported from torchvision. | ||
""" | ||
use ExVision.Model.Definition.Ortex, | ||
# model: "udnie.onnx", | ||
model: "udnie.onnx", | ||
categories: "priv/categories/coco_categories.json" | ||
|
||
import ExVision.Utils | ||
|
||
require Logger | ||
|
||
alias ExVision.Types.BBoxWithMask | ||
|
||
@type output_t() :: [BBoxWithMask.t()] | ||
|
||
@impl true | ||
def load(options \\ []) do | ||
if Keyword.has_key?(options, :batch_size) do | ||
Logger.warning( | ||
"`:max_batch_size` was given, but this model can only process batch of size 1. Overriding" | ||
) | ||
end | ||
|
||
options | ||
|> Keyword.put(:batch_size, 1) | ||
|> default_model_load() | ||
end | ||
|
||
@impl true | ||
def preprocessing(img, _metdata) do | ||
ExVision.Utils.resize(img, {640, 480}) |> Nx.divide(255.0) | ||
end | ||
|
||
@impl true | ||
def postprocessing( | ||
stylized_frame, | ||
metadata | ||
) do | ||
categories = categories() | ||
|
||
{h, w} = metadata.original_size | ||
scale_x = w / 640 | ||
scale_y = h / 480 | ||
|
||
stylized_frame | ||
end | ||
@moduledoc """ | ||
A semantic segmentation model for MobileNetV3 Backbone. Exported from torchvision. | ||
""" | ||
use ExVision.Model.Definition.Ortex, | ||
model: "deeplab_v3_mobilenetv3_segmentation.onnx", | ||
categories: "priv/categories/coco_with_voc_labels_categories.json" | ||
|
||
@type output_t() :: %{category_t() => Nx.Tensor.t()} | ||
|
||
@impl true | ||
def preprocessing(img, _metdata) do | ||
ExVision.Utils.resize(img, {224, 224}) | ||
end | ||
|
||
@impl true | ||
def postprocessing(%{"output" => out}, metadata) do | ||
cls_per_pixel = | ||
out | ||
|> Nx.backend_transfer() | ||
|> NxImage.resize(metadata.original_size, channels: :first) | ||
|> Nx.squeeze() | ||
|> Axon.Activations.softmax(axis: [0]) | ||
|> Nx.argmax(axis: 0) | ||
|
||
categories() | ||
|> Enum.with_index() | ||
|> Map.new(fn {category, i} -> | ||
{category, cls_per_pixel |> Nx.equal(i)} | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters