Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Resnet18 image classification

demo_ResNet18/Resnet18.livemd

Resnet18 image classification

File.cd!(__DIR__)
# for windows JP
# System.shell("chcp 65001")
System.put_env("NNCOMPILED", "YES")

Mix.install([
  {:tfl_interp, path: ".."},
  {:cimg, "~> 0.1.14"},
  {:nx, "~> 0.4.0"},
  {:kino, "~> 0.7.0"}
])

0.Original work

torchvision.models.resnet18 - pre-trained model included in Pytorch.

Thanks a lot!!!


Implementation with TflInterp in Elixir

1.Defining the inference module: ResNet18

defmodule Resnet18 do
  @width 224
  @height 224

  alias TflInterp, as: NNInterp

  use NNInterp,
    model: "./model/resnet18.tflite",
    url: "https://github.com/shoz-f/tfl_interp/releases/download/0.0.1/resnet18.tflite",
    inputs: [f4: {1, 3, @height, @width}],
    outputs: [f4: {1, 1000}]

  @imagenet1000 (for item <- File.stream!("./imagenet1000.label") do
                   String.trim_trailing(item)
                 end)
                |> Enum.with_index(&amp;{&amp;2, &amp;1})
                |> Enum.into(%{})

  def apply(img, top \\ 1) do
    # preprocess
    input0 =
      CImg.builder(img)
      |> CImg.resize({@width, @height})
      |> CImg.to_binary([{:gauss, {{123.7, 58.4}, {116.3, 57.1}, {103.5, 57.4}}}, :nchw])

    # prediction
    output0 =
      session()
      |> NNInterp.set_input_tensor(0, input0)
      |> NNInterp.invoke()
      |> NNInterp.get_output_tensor(0)
      |> Nx.from_binary(:f32)
      |> Nx.reshape({1000})

    # postprocess
    # softmax
    then(Nx.exp(output0), fn exp -> Nx.divide(exp, Nx.sum(exp)) end)
    |> Nx.argsort(direction: :desc)
    |> Nx.slice([0], [top])
    |> Nx.to_flat_list()
    |> Enum.map(&amp;@imagenet1000[&amp;1])
  end
end

Launch Resnet18.

# TflInterp.stop(Resnet18)
Resnet18.start_link([])

Display the properties of the Resnet18 model.

TflInterp.info(Resnet18)

2.Let’s try it

Load a photo and apply Resnet18 to it.

img = CImg.load("lion.jpg")
Kino.render(CImg.display_kino(img, :jpeg))

Resnet18.apply(img, 3)