Commit 04ebef9d authored by Carlos Ungil's avatar Carlos Ungil

.

parent 2e521f5a
```
(in-package :cl-user)
(ql:quickload :mxnet-predict)
(ql:quickload :opticl)
;;; cd ~
;;; wget http://data.dmlc.ml/mxnet/models/imagenet/inception-bn.tar.gz
;;; tar xvzf inception-bn.tar.gz
;;; ls ~/model
(defvar *data-dir* "~/model")
(defun file-as-string (path)
(with-open-file (s path)
(let* ((len (file-length s))
(data (make-string len)))
(values data (read-sequence data s)))))
(defun file-as-bytes (path)
(with-open-file (s path :element-type 'unsigned-byte)
(let* ((len (file-length s))
(data (make-array len :element-type 'unsigned-byte)))
(values data (read-sequence data s)))))
(defvar *labels*
(with-open-file (in (format nil "~A/synset.txt" *data-dir*))
(loop for line = (read-line in nil nil) while line collect line)))
(defvar *json*
(file-as-string (format nil "~A/Inception_BN-symbol.json" *data-dir*)))
(defvar *params*
(file-as-bytes (format nil "~A/Inception_BN-0039.params" *data-dir*)))
(defvar *predictor*
(mxpred:predictor *json* *params* '(("data" 1 3 224 224)) :dev-type :cpu))
(defun identify-image (png-file &optional (top 5))
(let* ((image (opticl:coerce-image
(opticl:resize-image (opticl:read-png-file png-file) 224 224)
'opticl:rgb-image))
(data (make-array (* 1 3 224 224) :element-type 'single-float
:initial-contents (loop for c from 0 below 3
append (loop for i from c by 3 below (array-total-size image)
collect (- (row-major-aref image i) 128.0))))))
(mxpred:forward *predictor* `(("data" ,data)))
(subseq (sort (loop for prob in (mxpred:get-output *predictor*)
for label in *labels*
collect (cons prob label))
#'> :key #'car)
0 top)))
;;; wget http://pngimg.com/upload/cup_PNG2000.png
;; (identify-image "~/cup_PNG2000.png")
;; ((0.44806847 "n07930864 cup")
;; (0.2880092 "n02823750 beer glass")
;; (0.09610717 "n07920052 espresso")
;; (0.036979508 "n02948072 candle, taper, wax light")
;; (0.023760669 "n07584110 consomme"))
;;; wget http://pngimg.com/upload/small/car_wheel_PNG1079.png
;; (identify-image "~/car_wheel_PNG1079.png")
;; ((0.99627846 "n02974003 car wheel")
;; (0.0010841467 "n03208938 disk brake, disc brake")
;; (4.2866074e-4 "n04548280 wall clock")
;; (3.3683694e-4 "n03777568 Model T")
;; (1.7127325e-4 "n03065424 coil, spiral, volute, whorl, helix"))
;;; wget http://orig04.deviantart.net/758b/f/2014/068/6/c/elephant_5_png_by_clipartcotttage-d79kjov.png
;; (identify-image "~/elephant_5_png_by_clipartcotttage-d79kjov.png")
;; ((0.31204274 "n02504013 Indian elephant, Elephas maximus")
;; (0.29726356 "n02504458 African elephant, Loxodonta africana")
;; (0.11172767 "n01704323 triceratops")
;; (0.1089943 "n01871265 tusker")
;; (0.041385192 "n03146219 cuirass"))
% sbcl --load examples/prediction.lisp
This is SBCL 1.3.16, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.
SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses. See the CREDITS and COPYING files in the
distribution for more information.
To load "mxnet-predict":
Load 1 ASDF system:
mxnet-predict
; Loading "mxnet-predict"
....
To load "opticl":
Load 1 ASDF system:
opticl
; Loading "opticl"
To load "drakma":
Load 1 ASDF system:
drakma
; Loading "drakma"
.
WARNING: file synset.txt not found, downloading...
WARNING: file .json not found, downloading...
WARNING: file .params not found, downloading...
[15:45:16] src/nnvm/legacy_json_util.cc:190: Loading symbol saved by previous version v0.8.0. Attempting to upgrade...
[15:45:16] src/nnvm/legacy_json_util.cc:198: Symbol successfully upgraded!
https://upload.wikimedia.org/wikipedia/commons/thumb/b/b8/Blue_cup_of_coffee.svg/500px-Blue_cup_of_coffee.svg.png
(0.36638436 . n07930864 cup)
(0.24669063 . n03063599 coffee mug)
(0.07620737 . n04398044 teapot)
(0.07242939 . n03532672 hook, claw)
(0.062123947 . n02948072 candle, taper, wax light)
https://upload.wikimedia.org/wikipedia/commons/thumb/9/92/Sport_car_rim.svg/500px-Sport_car_rim.svg.png
(0.34170008 . n04254680 soccer ball)
(0.26591486 . n03271574 electric fan, blower)
(0.233061 . n02974003 car wheel)
(0.0210932 . n04192698 shield, buckler)
(0.020947622 . n03944341 pinwheel)
https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/PEO-car.svg/500px-PEO-car.svg.png
(0.71059537 . n04285008 sports car, sport car)
(0.10266427 . n04037443 racer, race car, racing car)
(0.07309721
. n02814533 beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon)
(0.0341434 . n03459775 grille, radiator grille)
(0.0135731995 . n03670208 limousine, limo)
https://upload.wikimedia.org/wikipedia/commons/thumb/9/9f/Perseus1Hfx.png/480px-Perseus1Hfx.png
(0.95933306 . n03216828 dock, dockage, docking facility)
(0.017900886 . n04483307 trimaran)
(0.0058579743 . n02981792 catamaran)
(0.0053269146 . n04147183 schooner)
(0.0029011746 . n04347754 submarine, pigboat, sub, U-boat)
https://upload.wikimedia.org/wikipedia/commons/7/77/Avatar_cat.png
(0.93543136 . n02123045 tabby, tabby cat)
(0.035738677 . n02123159 tiger cat)
(0.02567893 . n02124075 Egyptian cat)
(0.0017329287 . n02123597 Siamese cat, Siamese)
(7.2913914e-4 . n02127052 lynx, catamount)
```
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment