Skip to content

Instantly share code, notes, and snippets.

@jzstark jzstark/#readme.md
Last active May 15, 2018

Embed
What would you like to do?

SqueezeNet

Squeezenet is one of the recent models to do image recognition, with focus on model size reduction. It can achieve AlexNet-level accuracy on ImageNet with 50x fewer parameters. The original paper of this network is here.

Usage

This gist implements an Squeezenet service in Owl, and provides simple interfaces to use. Here is an example:

#zoo "c424e1d1454d58cfb9b0284ba1925a48"

let img = "/path/to/your/image.png";;
let labels = Squeezenet.infer img;;
let labels_json   = Squeezenet.to_json ~top:5 labels;;
let labels_tuples = Squeezenet.to_tuples labels;;

The infer function takes image path as input. The image chould be of any popular formats: jpeg, png, etc. This gist contains an exemplar image for you to use, but feel free to use your own.

The output of this function is a 1x1000 vector. The user can further get human-readable classification results by passing this vector to_json or to_tuples. The output of former function is the top-N inference result as a json string, and the latter's is a list, each element in the form of [class: string; propability: float]. The probability is in range [0, 1].

The top parameter specifies how many top-N results are shown. It is default to be 5.

Prerequisite

This application relies on the tool ImageMagick to manipulate image format conversion and resizing. Please make sure it is installed. For example, on Ubuntu or Debian, you can use command:

sudo apt-get install imagemagick
{ "Squeezenet.infer": "string -> byte",
"Squeezenet.to_json": "int -> byte -> string" }
#!/usr/bin/env owl
open Owl
open Owl_types
open Neural
open Neural.S
open Neural.S.Graph
#zoo "51eaf74c65fa14c8c466ecfab2351bbd" (* Imagenet_cls *)
#zoo "86a1748bbc898f2e42538839edba00e1" (* ImageUtils *)
let fire_module in_shape squeeze expand nn =
let root = conv2d ~padding:VALID [|1;1; in_shape; squeeze|] [|1;1|] nn
|> activation Activation.Relu in
let left = conv2d ~padding:VALID [|1;1; squeeze; expand |] [|1;1|] root
|> activation Activation.Relu in
let right = conv2d ~padding:SAME [|3;3; squeeze; expand |] [|1;1|] root
|> activation Activation.Relu in
concatenate 3 [|left; right|]
let make_network img_size =
input [|img_size;img_size;3|]
|> conv2d ~padding:VALID [|3;3;3;64|] [|2;2|]
|> max_pool2d [|3;3|] [|2;2|] ~padding:VALID
(* block 1 *)
|> fire_module 64 16 64
|> fire_module 128 16 64
|> max_pool2d [|3;3|] [|2;2|] ~padding:VALID
(* block 2 *)
|> fire_module 128 32 128
|> fire_module 256 32 128
|> max_pool2d [|3;3|] [|2;2|] ~padding:VALID
(* block 3 *)
|> fire_module 256 48 192
|> fire_module 384 48 192
|> fire_module 384 64 256
|> fire_module 512 64 256
(* include top *)
|> dropout 0.5
|> conv2d ~padding:VALID [|1;1;512;1000|] [|1;1|]
|> activation Activation.Relu
|> global_avg_pool2d
|> activation Activation.(Softmax 1)
|> get_network
let weight_file = Owl_zoo_path.extend_zoo_path "squeezenet_owl.weight"
let infer img_name =
let nn = make_network 227 in
Graph.load_weights nn weight_file;
let filename = String.split_on_char '/' img_name |> List.rev |> List.hd in
let prefix = Filename.remove_extension filename in
let tmp_img = Filename.temp_file prefix ".ppm" in
Sys.command ("convert -resize 227x227\\! " ^ img_name ^ " " ^ tmp_img) |> ignore;
let img_ppm = ImageUtils.(load_ppm tmp_img |> extend_dim |> preprocess) in
Graph.model nn img_ppm
let to_tuples ?(top=5) label =
Imagenet_cls.to_tuples ~top label
let to_json?(top=5) label =
Imagenet_cls.to_json ~top label
let test () =
let example = Owl_zoo_path.extend_zoo_path "panda.png" in
infer example |> to_json |> Printf.printf "%s\n"
This file has been truncated, but you can view the full file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.