Skip to content

Commit

Permalink
added style transfer models
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Kopcinski authored and Mateusz Kopcinski committed Aug 14, 2024
1 parent 62a4f06 commit 97a0659
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 57 deletions.
57 changes: 0 additions & 57 deletions lib/ex_vision/style_transfer/candy.ex

This file was deleted.

53 changes: 53 additions & 0 deletions lib/ex_vision/style_transfer/style_transfer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
defmodule Configuration do
@low_resolution {400,300}
@high_resolution {400,300}
def configuration do
%{
ExVision.StyleTransfer.Candy => [model: "candy.onnx", resolution: @high_resolution, categories: "priv/categories/coco_categories.json"],
ExVision.StyleTransfer.CandyFast => [model: "candy_fast.onnx", resolution: @low_resolution, categories: "priv/categories/coco_categories.json"],
ExVision.StyleTransfer.Princess => [model: "princess.onnx", resolution: @high_resolution, categories: "priv/categories/coco_categories.json"],
ExVision.StyleTransfer.PrincessFast => [model: "princess_fast.onnx", resolution: @low_resolution, categories: "priv/categories/coco_categories.json"],
ExVision.StyleTransfer.Udnie => [model: "udnie.onnx", resolution: @high_resolution, categories: "priv/categories/coco_categories.json"],
ExVision.StyleTransfer.UdnieFast => [model: "udnie_fast.onnx", resolution: @low_resolution, categories: "priv/categories/coco_categories.json"],
ExVision.StyleTransfer.Mosaic => [model: "mosaic.onnx", resolution: @high_resolution, categories: "priv/categories/coco_categories.json"],
ExVision.StyleTransfer.MosaicFast => [model: "mosaic_fast.onnx", resolution: @low_resolution, categories: "priv/categories/coco_categories.json"],
}
end
end

for {module, opts} <- Configuration.configuration() do
defmodule module do
require Logger
@type output_t() :: [Nx.Tensor.t()]

use ExVision.Model.Definition.Ortex, model: unquote(opts[:model]), categories: "priv/categories/coco_categories.json"

@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, unquote(opts[:resolution])) |> Nx.divide(255.0)
end

@impl true
def postprocessing(
stylized_frame,
metadata
) do

stylized_frame = stylized_frame["55"]
NxImage.resize(stylized_frame, metadata.original_size, channels: :first)
end
end
end

0 comments on commit 97a0659

Please sign in to comment.