kokolegorille

kokolegorille

Transform Nx tensor image to web image

Hello everyone,

I am testing stable diffusion with Bumblebee. Everything is fine, but I would like to display the generated images on the web.

I receive these kind of results…

%{
  results: [
    %{
      image: #Nx.Tensor<
        u8[512][512][3]
        EXLA.Backend<host:0, 0.3536702673.2258239536.2772>
        [
          [
            [75, 103, 11],
            [68, 107, 0],
            [64, 109, 0],
            [63, 110, 0],
            [62, 107, 0],
            [61, 110, 0],
            [61, 110, 0],
            [60, 110, 0],
            [60, 110, 0],
            [60, 110, 0],
            [61, 109, 0],
            [61, 109, 0],
            [61, 109, 0],
            [60, 108, 0],
            [60, 109, 0],
            [60, 109, ...],
            ...
          ],
          ...
        ]
      >,
      is_safe: true
    },
...

It is a list of #Nx.Tensor<u8[512][512]…>

In the demo code these images are transformed into Kino.Image, to be displayed into Livebook.

Does anyone knows how to transform those images into something more usual for the web, like jpg, or png?

Thanks for taking time

Most Liked

kokolegorille

kokolegorille

Sorry for the noise…

I added Image, and used the from_nx to transform resulting image to Vix.Vips.Image

From there it’s possible to dump a jpg.

Here is the sample with the default parameters (“numbat, forest, high quality, detailed, digital art”, “darkness, rainy, foggy”)

Have a nice weekend

yurko

yurko

I ended up using stb_image for that, then it gets pretty straighforward:

  @spec tensor_image_to_base64(Nx.Tensor.t()) :: String.t()
  defp tensor_image_to_base64(tensor) when is_struct(tensor, Nx.Tensor) do
    "data:image/png;base64,#{tensor |> StbImage.from_nx() |> StbImage.to_binary(:png) |> Base.encode64()}"
  end

Bumblebee is really cool though, I got to play with it and was really impressed, here’s the result sample app with few models: GitHub - bcat-eu/jimmy: Your friendly robot · GitHub

kip

kip

ex_cldr Core Team

I’ve just now finished up the first integration between Image and Bumblebee which is image classification. I haven’t looked at StableDiffusion integration - will take a look and see what that might look like (but maybe not til early new year unless someone would like to dive in and contribute). I’m also looking forward to the work @seanmor5 is considering for object detection and image segmentation.

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

We're in Beta

About us Mission Statement