Git Product home page Git Product logo

stb_image's Introduction

StbImage

Hex.pm

A tiny image reader/writer library using stb_image as the backend. See the documentation.

There is an alternative library, image_rs, which uses image_rs as the backend. That backend is implemented in Rust, so you will need a working Rust compiler. But the number of supported image formats is greater than stb_image.

Installation

Add stb_image to your list of dependencies in mix.exs:

def deps do
  [
    {:stb_image, "~> 0.6"}
  ]
end

This library also provides some precompiled binaries for the following platforms:

  • macOS
    • x86_64-apple-darwin
    • aarch64-apple-darwin
  • Linux
    • x86_64-linux-gnu
    • x86_64-linux-musl
    • aarch64-linux-gnu
    • aarch64-linux-musl
    • riscv64-linux-gnu
    • riscv64-linux-musl
  • Windows
    • x86_64-windows-msvc

License

Copyright 2022 Cocoa Xu

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

stb_image's People

Contributors

cocoa-xu avatar jonatanklosko avatar josevalim avatar kianmeng avatar msluszniak avatar seanmor5 avatar shinohara-rin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

stb_image's Issues

Return a struct on from_file/from_memory

Instead of:

  {:ok, img, shape, type, channels} = StbImage.from_file("/path/to/image", channels: 4)
  {h, w, c} = shape

We want:

{:ok, struct} = StbImage.from_file("/path/to/image", channels: 4)

Where struct is an %StbImage{} struct with the foillowing fields:

  • :data
  • :shape
  • :type
  • :color_mode (the channels value)

For gifs, it should be:

{:ok, frames, delays} 

Where frames is a list of StbImage structs.

Plan for releases

Hi @cocoa-xu!

I noticed you added another backend as two separate folder but I am worried that doing so will complicate shipping the packages. I am not sure what is the best solution but I can think of:

  1. Have two distinct libraries
  2. Keep a single library and use config or env vars to choose the backend

Do you have any thoughts? ๐Ÿค”

Crash when loading a 4 channel image when channel option is forced to 3 in StbImage.read_file

Hello,
I'm reading the awesome book Machine learning with elixir and
on my M1 macbook with erlang 26.2.2 and elixir 1.16.1,
the following script crashes (from chapter 7 of the book, the dataset comes from kaggle : https://www.kaggle.com/datasets/shaunthesheep/microsoft-catsvsdogs-dataset) :

Mix.install([{:axon, "~> 0.6"}, {:nx, path: "./nx/nx", override: true}, {:exla, path: "./nx/exla", override: true}, {:stb_image, "0.5.2"}, {:kino, "~> 0.8"}], system_env: %{"DEBUG" => 1})
Process.sleep(1000)
Nx.tensor(1, backend: EXLA.Backend)


defmodule CatsAndDogs do
  def pipeline(paths, batch_size, target_height, target_width) do
    paths
    |> Enum.shuffle()
    |> Task.async_stream(&parse_image/1)
    |> Stream.filter(fn
       {:ok, {%StbImage{shape: {_, _, 3}}, _}} -> true
       _ -> false end)
    |> Stream.map(&to_tensors(&1, target_height, target_width))
    |> Stream.chunk_every(batch_size, batch_size, :discard)
    |> Stream.map(fn chunks ->
      {img_chunk, label_chunk} = Enum.unzip(chunks)
      {Nx.stack(img_chunk), Nx.stack(label_chunk)}
    end)
  end

  defp parse_image(path) do
   label = if String.contains?(path, "Cat"), do: 0, else: 1
   case StbImage.read_file(path, channels: 3) do
      {:ok, img} -> {img, label}
      _error -> :error
    end
  end

  defp to_tensors({:ok, {img, label}}, target_height, target_width) do img_tensor =
    img
    |> StbImage.resize(target_height, target_width)
    |> StbImage.to_nx()
    |> Nx.divide(255)
    label_tensor = Nx.tensor([label])
    {img_tensor, label_tensor}
  end

end


{test_paths, train_paths} =(Path.wildcard("/Users/christophebelpaire/perso/machine-learning-in-elixir/PetImages/Cat/*.jpg")
++ Path.wildcard("/Users/christophebelpaire/perso/machine-learning-in-elixir/PetImages/Dog/*.jpg"))
|> Enum.shuffle()
|> Enum.split(1000)


target_height = 96
target_width = 96
batch_size = 32

train_pipeline = CatsAndDogs.pipeline(
  train_paths, batch_size, target_height, target_width
)
test_pipeline = CatsAndDogs.pipeline(
  test_paths, batch_size, target_height, target_width
)

mlp_model =
  Axon.input("images", shape: {nil, target_height, target_width, 3}) |> Axon.flatten()
  |> Axon.dense(256, activation: :relu)
  |> Axon.dense(128, activation: :relu)
  |> Axon.dense(1, activation: :sigmoid)


  IO.gets("Press enter to continue - #{System.pid()}")

  _mlp_trained_model_state =
  mlp_model
  |> Axon.Loop.trainer(:binary_cross_entropy, :adam)
  |> Axon.Loop.metric(:accuracy)
  |> Axon.Loop.run(train_pipeline, %{}, epochs: 5, compiler: EXLA)

Here is the debug message from lldb :

Architecture set to: arm64-apple-macosx-.
(lldb) continue
Process 13117 resuming
Process 13117 stopped
* thread #25, name = 'erts_dios_1', stop reason = EXC_BAD_ACCESS (code=1, address=0x14a394000)
    frame #0: 0x0000000189b496b0 libsystem_platform.dylib`_platform_memmove + 96
libsystem_platform.dylib`:
->  0x189b496b0 <+96>:  ldnp   q0, q1, [x1]
    0x189b496b4 <+100>: add    x1, x1, #0x20
    0x189b496b8 <+104>: subs   x2, x2, #0x20
    0x189b496bc <+108>: b.hi   0x189b496a8               ; <+88>
Target 0: (beam.smp) stopped.
(lldb) bt
* thread #25, name = 'erts_dios_1', stop reason = EXC_BAD_ACCESS (code=1, address=0x14a394000)
  * frame #0: 0x0000000189b496b0 libsystem_platform.dylib`_platform_memmove + 96
    frame #1: 0x0000000148127134 stb_image_nif.so`pack_data + 96
    frame #2: 0x0000000148125c08 stb_image_nif.so`read_file + 812
    frame #3: 0x0000000102d746c8 beam.smp`erts_call_dirty_nif(esdp=0x00000001439c1c80, c_p=0x0000000145eb12b8, I=0x00000001502b7278, reg=0x000000010308c5c0) at erl_nif.c:466:18 [opt]
    frame #4: 0x0000000102c4e0e0 beam.smp`erts_dirty_process_main(esdp=0x00000001439c1c80) at beam_common.c:280:23 [opt]
    frame #5: 0x0000000102bc68e8 beam.smp`sched_dirty_io_thread_func(vesdp=0x00000001439c1c80) at erl_process.c:8768:5 [opt]
    frame #6: 0x0000000102e13cbc beam.smp`thr_wrapper(vtwd=0x000000016d242560) at ethread.c:116:25 [opt]
    frame #7: 0x0000000189b1a034 libsystem_pthread.dylib`_pthread_start + 136

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.