-
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.
Co-authored-by: Mateusz Kopcinski <[email protected]>
- Loading branch information
Showing
15 changed files
with
187 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
defmodule Configuration do | ||
@moduledoc false | ||
|
||
@low_resolution {400, 300} | ||
@high_resolution {640, 480} | ||
|
||
@spec configuration() :: %{} | ||
def configuration do | ||
%{ | ||
ExVision.StyleTransfer.Candy => [model: "candy.onnx", resolution: @high_resolution], | ||
ExVision.StyleTransfer.CandyFast => [model: "candy_fast.onnx", resolution: @low_resolution], | ||
ExVision.StyleTransfer.Princess => [model: "princess.onnx", resolution: @high_resolution], | ||
ExVision.StyleTransfer.PrincessFast => [ | ||
model: "princess_fast.onnx", | ||
resolution: @low_resolution | ||
], | ||
ExVision.StyleTransfer.Udnie => [model: "udnie.onnx", resolution: @high_resolution], | ||
ExVision.StyleTransfer.UdnieFast => [model: "udnie_fast.onnx", resolution: @low_resolution], | ||
ExVision.StyleTransfer.Mosaic => [model: "mosaic.onnx", resolution: @high_resolution], | ||
ExVision.StyleTransfer.MosaicFast => [ | ||
model: "mosaic_fast.onnx", | ||
resolution: @low_resolution | ||
] | ||
} | ||
end | ||
end | ||
|
||
for {module, opts} <- Configuration.configuration() do | ||
defmodule module do | ||
@moduledoc """ | ||
#{module} is a custom style transfer model optimised for devices with low computational capabilities and CPU inference. | ||
""" | ||
use ExVision.Model.Definition.Ortex, model: unquote(opts[:model]) | ||
|
||
require Logger | ||
|
||
@typedoc """ | ||
A type consisting of output tesnor (stylized image tensor) from style transfer models of shape {#{Enum.join(Tuple.to_list(opts[:resolution]) ++ [3], ", ")}}. | ||
""" | ||
@type output_t() :: Nx.Tensor.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 | ||
img |> ExVision.Utils.resize(unquote(opts[:resolution])) |> Nx.divide(255.0) | ||
end | ||
|
||
@impl true | ||
def postprocessing( | ||
stylized_frame, | ||
metadata | ||
) do | ||
{h, w} = unquote(opts[:resolution]) | ||
|
||
stylized_frame["55"] | ||
|> Nx.reshape({3, h, w}, names: [:channel, :height, :width]) | ||
|> NxImage.resize(metadata.original_size, channels: :first, method: :bilinear) | ||
|> Nx.clip(0.0, 255.0) | ||
|> Nx.as_type(:u8) | ||
|> Nx.transpose(axes: [1, 2, 0]) | ||
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,56 @@ | ||
defmodule TestConfiguration do | ||
@spec configuration() :: %{} | ||
def configuration do | ||
%{ | ||
ExVision.StyleTransfer.CandyTest => [ | ||
module: ExVision.StyleTransfer.Candy, | ||
gt_file: "cat_candy.gt" | ||
], | ||
ExVision.StyleTransfer.CandyFastTest => [ | ||
module: ExVision.StyleTransfer.CandyFast, | ||
gt_file: "cat_candy_fast.gt" | ||
], | ||
ExVision.StyleTransfer.PrincessTest => [ | ||
module: ExVision.StyleTransfer.Princess, | ||
gt_file: "cat_princess.gt" | ||
], | ||
ExVision.StyleTransfer.PrincessFastTest => [ | ||
module: ExVision.StyleTransfer.PrincessFast, | ||
gt_file: "cat_princess_fast.gt" | ||
], | ||
ExVision.StyleTransfer.UdnieTest => [ | ||
module: ExVision.StyleTransfer.Udnie, | ||
gt_file: "cat_udnie.gt" | ||
], | ||
ExVision.StyleTransfer.UdnieFastTest => [ | ||
module: ExVision.StyleTransfer.UdnieFast, | ||
gt_file: "cat_udnie_fast.gt" | ||
], | ||
ExVision.StyleTransfer.MosaicTest => [ | ||
module: ExVision.StyleTransfer.Mosaic, | ||
gt_file: "cat_mosaic.gt" | ||
], | ||
ExVision.StyleTransfer.MosaicFastTest => [ | ||
module: ExVision.StyleTransfer.MosaicFast, | ||
gt_file: "cat_mosaic_fast.gt" | ||
] | ||
} | ||
end | ||
end | ||
|
||
for {module, opts} <- TestConfiguration.configuration() do | ||
defmodule module do | ||
use ExVision.Model.Case, module: unquote(opts[:module]) | ||
use ExVision.TestUtils | ||
|
||
@impl true | ||
def test_inference_result(result) do | ||
expected_result = | ||
"test/assets/results/style_transfer/#{unquote(opts[:gt_file])}" | ||
|> File.read!() | ||
|> Nx.deserialize() | ||
|
||
assert_tensors_equal(result, expected_result, 5, 0.05) | ||
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