-
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
1 parent
a960f0b
commit b3d2e79
Showing
23 changed files
with
250 additions
and
102 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
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule ExVision.Detection.FasterRCNN_ResNet50_FPN do | ||
@moduledoc """ | ||
FasterRCNN object detector with ResNet50 backbone and FPN detection head, exported from torchvision. | ||
""" | ||
use ExVision.Model.Definition.Ortex, base_dir: "detection/fasterrcnn_resnet50_fpn" | ||
use ExVision.Detection.GenericDetector | ||
|
||
require Logger | ||
|
||
@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 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
defmodule ExVision.Detection.GenericDetector do | ||
@moduledoc false | ||
|
||
# Contains a default implementation of pre and post processing for TorchVision detectors | ||
# To use: `use ExVision.Detection.GenericDetector` | ||
|
||
require Logger | ||
|
||
alias ExVision.Types.{BBox, ImageMetadata} | ||
|
||
@typep output_t() :: [BBox.t()] | ||
|
||
@spec preprocessing(Nx.Tensor.t(), ImageMetadata.t()) :: Nx.Tensor.t() | ||
def preprocessing(img, _metadata) do | ||
ExVision.Utils.resize(img, {224, 224}) | ||
end | ||
|
||
@spec postprocessing({Nx.Tensor.t(), Nx.Tensor.t(), Nx.Tensor.t()}, ImageMetadata.t(), [atom()]) :: | ||
output_t() | ||
def postprocessing( | ||
%{"boxes" => bboxes, "scores" => scores, "labels" => labels}, | ||
metadata, | ||
categories | ||
) do | ||
{h, w} = metadata.original_size | ||
scale_x = w / 224 | ||
scale_y = h / 224 | ||
|
||
bboxes = | ||
bboxes | ||
|> Nx.multiply(Nx.tensor([scale_x, scale_y, scale_x, scale_y])) | ||
|> Nx.round() | ||
|> Nx.as_type(:s64) | ||
|> Nx.to_list() | ||
|
||
scores = scores |> Nx.to_list() | ||
labels = labels |> Nx.to_list() | ||
|
||
[bboxes, scores, labels] | ||
|> Enum.zip() | ||
|> Enum.filter(fn {_bbox, score, _label} -> score > 0.1 end) | ||
|> Enum.map(fn {[x1, y1, x2, y2], score, label} -> | ||
%BBox{ | ||
x1: x1, | ||
x2: x2, | ||
y1: y1, | ||
y2: y2, | ||
score: score, | ||
label: Enum.at(categories, label) | ||
} | ||
end) | ||
end | ||
|
||
defmacro __using__(_opts) do | ||
quote do | ||
@typedoc """ | ||
A type describing output of `run/2` as a list of a bounding boxes. | ||
Each bounding box describes the location of the object indicated by the `label`. | ||
It also provides the `score` field marking the probability of the prediction. | ||
Bounding boxes with very low scores should most likely be ignored. | ||
""" | ||
@type output_t() :: [BBox.t()] | ||
|
||
@impl true | ||
defdelegate preprocessing(image, metadata), to: ExVision.Detection.GenericDetector | ||
|
||
@impl true | ||
@spec postprocessing(tuple(), ExVision.Types.ImageMetadata.t()) :: output_t() | ||
def postprocessing(output, metadata) do | ||
ExVision.Detection.GenericDetector.postprocessing(output, metadata, categories()) | ||
end | ||
|
||
defoverridable preprocessing: 2, postprocessing: 2 | ||
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 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 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
Oops, something went wrong.