Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[Clojure] package infer tweaks #13864

Merged
merged 11 commits into from
Jan 13, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,15 @@
"Print image detector predictions for the given input file"
[predictions width height]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

width and height aren't used anymore. remove?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I add the w and h back into the print method it should be good and resolve it

(println (apply str (repeat 80 "=")))
(doseq [p predictions]
(println p))
(doseq [{:keys [class prob x-min y-min x-max y-max]} predictions]
(println (format
"Class: %s Prob=%.5f Coords=(%.3f, %.3f, %.3f, %.3f)"
class
prob
(* x-min width)
(* y-min height)
(* x-max width)
(* y-max height))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

(println (apply str (repeat 80 "="))))

(defn detect-single-image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"Run inference using given predictor"
[predictor image]
(let [predictions (infer/predict-with-ndarray predictor [image])]
predictions))
(first predictions)))

(defn postprocess
[model-path-prefix predictions]
Expand Down
10 changes: 4 additions & 6 deletions contrib/clojure-package/src/org/apache/clojure_mxnet/infer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,17 @@
(util/validate! ::vvec-of-numbers inputs
"Invalid inputs")
(->> (.predict (:predictor wrapped-predictor)
(util/vec->indexed-seq [(float-array (first inputs))]))
(util/coerce-return-recursive)
(first)
(mapv float)))
(util/vec->indexed-seq (mapv float-array inputs)))
(util/coerce-return-recursive)
(mapv #(mapv float %))))
(predict-with-ndarray [wrapped-predictor input-arrays]
(util/validate! ::wrapped-predictor wrapped-predictor
"Invalid predictor")
(util/validate! ::vec-of-ndarrays input-arrays
"Invalid input arrays")
(-> (.predictWithNDArray (:predictor wrapped-predictor)
(util/vec->indexed-seq input-arrays))
(util/coerce-return-recursive)
(first))))
(util/coerce-return-recursive))))

(s/def ::nil-or-int (s/nilable int?))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
[clojure.java.io :as io]
[clojure.java.shell :refer [sh]]
[clojure.string :refer [split]]
[clojure.test :refer :all]))
[clojure.test :refer :all]
[org.apache.clojure-mxnet.util :as util]))

(def model-dir "data/")
(def model-path-prefix (str model-dir "resnet-18/resnet-18"))
Expand All @@ -45,16 +46,16 @@
(deftest predictor-test-with-ndarray
(let [predictor (create-predictor)
image-ndarray (-> "test/test-images/kitten.jpg"
infer/load-image-from-file
(infer/reshape-image width height)
(infer/buffered-image-to-pixels [3 width height])
(ndarray/expand-dims 0))
infer/load-image-from-file
(infer/reshape-image width height)
(infer/buffered-image-to-pixels [3 width height])
(ndarray/expand-dims 0))
predictions (infer/predict-with-ndarray predictor [image-ndarray])
synset-file (-> (io/file model-path-prefix)
(.getParent)
(io/file "synset.txt"))
synset-names (split (slurp synset-file) #"\n")
[best-index] (ndarray/->int-vec (ndarray/argmax predictions 1))
[best-index] (ndarray/->int-vec (ndarray/argmax (first predictions) 1))
best-prediction (synset-names best-index)]
(is (= "n02123159 tiger cat" best-prediction))))

Expand All @@ -70,7 +71,7 @@
(.getParent)
(io/file "synset.txt"))
synset-names (split (slurp synset-file) #"\n")
ndarray-preds (ndarray/array predictions [1 1000])
ndarray-preds (ndarray/array (first predictions) [1 1000])
[best-index] (ndarray/->int-vec (ndarray/argmax ndarray-preds 1))
best-prediction (synset-names best-index)]
(is (= "n02123159 tiger cat" best-prediction))))