-
Notifications
You must be signed in to change notification settings - Fork 99
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: Jonatan Kłosko <[email protected]>
- Loading branch information
1 parent
9f62d99
commit f23faee
Showing
3 changed files
with
203 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
defmodule Bumblebee.Vision.ImageEmbedding do | ||
@moduledoc false | ||
|
||
alias Bumblebee.Shared | ||
|
||
def image_embedding(model_info, featurizer, opts \\ []) do | ||
%{model: model, params: params, spec: spec} = model_info | ||
|
||
opts = | ||
Keyword.validate!(opts, [ | ||
:compile, | ||
output_attribute: :pooled_state, | ||
embedding_processor: nil, | ||
defn_options: [] | ||
]) | ||
|
||
output_attribute = opts[:output_attribute] | ||
embedding_processor = opts[:embedding_processor] | ||
defn_options = opts[:defn_options] | ||
|
||
compile = | ||
if compile = opts[:compile] do | ||
compile | ||
|> Keyword.validate!([:batch_size]) | ||
|> Shared.require_options!([:batch_size]) | ||
end | ||
|
||
batch_size = compile[:batch_size] | ||
|
||
{_init_fun, encoder} = Axon.build(model) | ||
|
||
embedding_fun = fn params, inputs -> | ||
output = encoder.(params, inputs) | ||
|
||
output = | ||
if is_map(output) do | ||
output[output_attribute] | ||
else | ||
output | ||
end | ||
|
||
output = | ||
case embedding_processor do | ||
nil -> | ||
output | ||
|
||
:l2_norm -> | ||
Bumblebee.Utils.Nx.normalize(output) | ||
|
||
other -> | ||
raise ArgumentError, | ||
"expected :embedding_processor to be one of nil or :l2_norm, got: #{inspect(other)}" | ||
end | ||
|
||
output | ||
end | ||
|
||
Nx.Serving.new( | ||
fn defn_options -> | ||
embedding_fun = | ||
Shared.compile_or_jit(embedding_fun, defn_options, compile != nil, fn -> | ||
inputs = %{ | ||
"pixel_values" => Shared.input_template(spec, "pixel_values", [batch_size]) | ||
} | ||
|
||
[params, inputs] | ||
end) | ||
|
||
fn inputs -> | ||
inputs = Shared.maybe_pad(inputs, batch_size) | ||
embedding_fun.(params, inputs) | ||
end | ||
end, | ||
defn_options | ||
) | ||
|> Nx.Serving.process_options(batch_size: batch_size) | ||
|> Nx.Serving.client_preprocessing(fn input -> | ||
{images, multi?} = Shared.validate_serving_input!(input, &Shared.validate_image/1) | ||
|
||
inputs = Bumblebee.apply_featurizer(featurizer, images) | ||
|
||
{Nx.Batch.concatenate([inputs]), multi?} | ||
end) | ||
|> Nx.Serving.client_postprocessing(fn {embeddings, _metadata}, multi? -> | ||
for embedding <- Bumblebee.Utils.Nx.batch_to_list(embeddings) do | ||
%{embedding: embedding} | ||
end | ||
|> Shared.normalize_output(multi?) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
defmodule Bumblebee.Vision.ImageEmbeddingTest do | ||
use ExUnit.Case, async: false | ||
|
||
import Bumblebee.TestHelpers | ||
|
||
@moduletag model_test_tags() | ||
@images_dir Path.expand("../../fixtures/images", __DIR__) | ||
|
||
describe "integration" do | ||
test "returns CLIP Vision embedding (without projection head) for an image" do | ||
{:ok, model_info} = | ||
Bumblebee.load_model({:hf, "openai/clip-vit-base-patch32"}, | ||
module: Bumblebee.Vision.ClipVision | ||
) | ||
|
||
{:ok, featurizer} = Bumblebee.load_featurizer({:hf, "openai/clip-vit-base-patch32"}) | ||
|
||
serving = Bumblebee.Vision.ImageEmbedding.image_embedding(model_info, featurizer) | ||
image = StbImage.read_file!(Path.join(@images_dir, "coco/39769.jpeg")) | ||
|
||
assert %{embedding: %Nx.Tensor{} = embedding} = Nx.Serving.run(serving, image) | ||
assert Nx.shape(embedding) == {768} | ||
|
||
assert_all_close( | ||
embedding[1..3], | ||
Nx.tensor([0.0978, -0.7233, -0.7707]), | ||
atol: 1.0e-4 | ||
) | ||
end | ||
|
||
test "returns normalized CLIP Vision embedding (without projection head) for an image" do | ||
{:ok, model_info} = | ||
Bumblebee.load_model({:hf, "openai/clip-vit-base-patch32"}, | ||
module: Bumblebee.Vision.ClipVision | ||
) | ||
|
||
{:ok, featurizer} = Bumblebee.load_featurizer({:hf, "openai/clip-vit-base-patch32"}) | ||
|
||
options = [ | ||
embedding_processor: :l2_norm | ||
] | ||
|
||
serving = Bumblebee.Vision.ImageEmbedding.image_embedding(model_info, featurizer, options) | ||
image = StbImage.read_file!(Path.join(@images_dir, "coco/39769.jpeg")) | ||
|
||
assert %{embedding: %Nx.Tensor{} = embedding} = Nx.Serving.run(serving, image) | ||
assert Nx.shape(embedding) == {768} | ||
|
||
assert_all_close( | ||
embedding[1..3], | ||
Nx.tensor([0.0036, -0.0269, -0.0286]), | ||
atol: 1.0e-4 | ||
) | ||
end | ||
end | ||
end |