Skip to content

Instantly share code, notes, and snippets.

@jzstark
Last active May 5, 2018 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jzstark/f5409c44d6444921a8ceec00e33c42c4 to your computer and use it in GitHub Desktop.
Save jzstark/f5409c44d6444921a8ceec00e33c42c4 to your computer and use it in GitHub Desktop.

VGG16

VGG16 is one of the DNN models for image classification. The original paper of this network is: Very Deep Convolutional Networks for Large-Scale Image Recognition.

Usage

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

#zoo "f5409c44d6444921a8ceec00e33c42c4"

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

Note that you need to download the weight file "vgg16_owl.network" from here to current directory before running the code.

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
#!/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 make_network img_size =
input [|img_size;img_size;3|]
(* block 1 *)
|> conv2d [|3;3;3;64|] [|1;1|] ~act_typ:Activation.Relu ~padding:SAME
|> conv2d [|3;3;64;64|] [|1;1|] ~act_typ:Activation.Relu ~padding:SAME
|> max_pool2d [|2;2|] [|2;2|] ~padding:VALID
(* block 2 *)
|> conv2d [|3;3;64;128|] [|1;1|] ~act_typ:Activation.Relu ~padding:SAME
|> conv2d [|3;3;128;128|] [|1;1|] ~act_typ:Activation.Relu ~padding:SAME
|> max_pool2d [|2;2|] [|2;2|] ~padding:VALID
(* block 3 *)
|> conv2d [|3;3;128;256|] [|1;1|] ~act_typ:Activation.Relu ~padding:SAME
|> conv2d [|3;3;256;256|] [|1;1|] ~act_typ:Activation.Relu ~padding:SAME
|> conv2d [|3;3;256;256|] [|1;1|] ~act_typ:Activation.Relu ~padding:SAME
|> max_pool2d [|2;2|] [|2;2|] ~padding:VALID
(* block 4 *)
|> conv2d [|3;3;256;512|] [|1;1|] ~act_typ:Activation.Relu ~padding:SAME
|> conv2d [|3;3;512;512|] [|1;1|] ~act_typ:Activation.Relu ~padding:SAME
|> conv2d [|3;3;512;512|] [|1;1|] ~act_typ:Activation.Relu ~padding:SAME
|> max_pool2d [|2;2|] [|2;2|] ~padding:VALID
(* block 5 *)
|> conv2d [|3;3;512;512|] [|1;1|] ~act_typ:Activation.Relu ~padding:SAME
|> conv2d [|3;3;512;512|] [|1;1|] ~act_typ:Activation.Relu ~padding:SAME
|> conv2d [|3;3;512;512|] [|1;1|] ~act_typ:Activation.Relu ~padding:SAME
|> max_pool2d [|2;2|] [|2;2|] ~padding:VALID
(* classification block *)
|> flatten
|> fully_connected ~act_typ:Activation.Relu 4096
|> fully_connected ~act_typ:Activation.Relu 4096
|> fully_connected ~act_typ:Activation.(Softmax 1) 1000
|> get_network
let weight_file = Owl_zoo_path.extend_zoo_path "vgg16_owl.network"
let infer img_name =
let nn = Graph.load weight_file in
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
let _ = Sys.command ("convert -resize 224x224\\! " ^ img_name ^ " " ^ tmp_img) in
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"
Please download the weight file "vgg16_owl.network" at: https://drive.google.com/file/d/1tMK6gZsjdD4oT0blWtsZiicliWpploOB/view?usp=sharing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment