You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by cm...@apache.org on 2019/01/11 21:01:03 UTC

[incubator-mxnet] branch clojure-infer-predict-tweak created (now e8e55a9)

This is an automated email from the ASF dual-hosted git repository.

cmeier pushed a change to branch clojure-infer-predict-tweak
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git.


      at e8e55a9  change predictions to a map for image-classifiers

This branch includes the following new commits:

     new 488843e  change object detection prediction to be a map
     new e8e55a9  change predictions to a map for image-classifiers

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[incubator-mxnet] 02/02: change predictions to a map for image-classifiers

Posted by cm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cmeier pushed a commit to branch clojure-infer-predict-tweak
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git

commit e8e55a90b11686219f260b7ef62e0792b5e79c2c
Author: gigasquid <cm...@gigasquidsoftware.com>
AuthorDate: Fri Jan 11 16:00:20 2019 -0500

    change predictions to a map for image-classifiers
---
 .../src/org/apache/clojure_mxnet/infer.clj         | 35 +++++++++++++---------
 .../clojure_mxnet/infer/imageclassifier_test.clj   | 34 +++++++--------------
 2 files changed, 32 insertions(+), 37 deletions(-)

diff --git a/contrib/clojure-package/src/org/apache/clojure_mxnet/infer.clj b/contrib/clojure-package/src/org/apache/clojure_mxnet/infer.clj
index bc5090f..801c717 100644
--- a/contrib/clojure-package/src/org/apache/clojure_mxnet/infer.clj
+++ b/contrib/clojure-package/src/org/apache/clojure_mxnet/infer.clj
@@ -103,12 +103,15 @@
 
 (s/def ::nil-or-int (s/nilable int?))
 
-(defn- format-predictions [predictions]
+(defn- format-detection-predictions [predictions]
   (mapv (fn [[c p]]
           (let [[prob xmin ymin xmax ymax] (mapv float p)]
             {:class c :prob prob :x-min xmin :y-min ymin :x-max xmax :y-max ymax}))
         predictions))
 
+(defn- format-classification-predictions [predictions]
+  (mapv (fn [[c p]] {:class c :prob p}) predictions))
+
 (extend-protocol AClassifier
   WrappedClassifier
   (classify
@@ -181,11 +184,13 @@
      (util/validate! ::image image "Invalid image")
      (util/validate! ::nil-or-int topk "Invalid top-K")
      (util/validate! ::dtype dtype "Invalid dtype")
-     (util/coerce-return-recursive
-      (.classifyImage (:image-classifier wrapped-image-classifier)
-                      image
-                      (util/->int-option topk)
-                      dtype))))
+     (-> (.classifyImage (:image-classifier wrapped-image-classifier)
+                         image
+                         (util/->int-option topk)
+                         dtype)
+         (util/coerce-return-recursive)
+         (first)
+         (format-classification-predictions))))
   (classify-image-batch
     ([wrapped-image-classifier images]
      (classify-image-batch wrapped-image-classifier images nil dtype/FLOAT32))
@@ -196,11 +201,13 @@
                      "Invalid classifier")
      (util/validate! ::nil-or-int topk "Invalid top-K")
      (util/validate! ::dtype dtype "Invalid dtype")
-     (util/coerce-return-recursive
-      (.classifyImageBatch (:image-classifier wrapped-image-classifier)
-                           images
-                           (util/->int-option topk)
-                           dtype)))))
+     (-> (.classifyImageBatch (:image-classifier wrapped-image-classifier)
+                              images
+                              (util/->int-option topk)
+                              dtype)
+         (util/coerce-return-recursive)
+         (first)
+         (format-classification-predictions)))))
 
 (extend-protocol AObjectDetector
   WrappedObjectDetector
@@ -217,7 +224,7 @@
                               (util/->int-option topk))
           (util/coerce-return-recursive)
           (first)
-          (format-predictions))))
+          (format-detection-predictions))))
   (detect-objects-batch
     ([wrapped-detector images]
      (detect-objects-batch wrapped-detector images nil))
@@ -230,7 +237,7 @@
                                    (util/->int-option topk))
           (util/coerce-return-recursive)
           (first)
-          (format-predictions))))
+          (format-detection-predictions))))
   (detect-objects-with-ndarrays
     ([wrapped-detector input-arrays]
      (detect-objects-with-ndarrays wrapped-detector input-arrays nil))
@@ -245,7 +252,7 @@
                                     (util/->int-option topk))
           (util/coerce-return-recursive)
           (first)
-          (format-predictions)))))
+          (format-detection-predictions)))))
 
 (defprotocol AInferenceFactory
   (create-predictor [factory] [factory opts])
diff --git a/contrib/clojure-package/test/org/apache/clojure_mxnet/infer/imageclassifier_test.clj b/contrib/clojure-package/test/org/apache/clojure_mxnet/infer/imageclassifier_test.clj
index b459b06..448a52f 100644
--- a/contrib/clojure-package/test/org/apache/clojure_mxnet/infer/imageclassifier_test.clj
+++ b/contrib/clojure-package/test/org/apache/clojure_mxnet/infer/imageclassifier_test.clj
@@ -40,23 +40,15 @@
 (deftest test-single-classification
   (let [classifier (create-classifier)
         image (infer/load-image-from-file "test/test-images/kitten.jpg")
-        [predictions-all] (infer/classify-image classifier image)
-        [predictions-with-default-dtype] (infer/classify-image classifier image 10)
-        [predictions] (infer/classify-image classifier image 5 dtype/FLOAT32)]
+        predictions-all (infer/classify-image classifier image)
+        predictions-with-default-dtype (infer/classify-image classifier image 10)
+        predictions (infer/classify-image classifier image 5 dtype/FLOAT32)]
+    predictions
     (is (= 1000 (count predictions-all)))
     (is (= 10 (count predictions-with-default-dtype)))
-    (is (some? predictions))
     (is (= 5 (count predictions)))
-    (is (every? #(= 2 (count %)) predictions))
-    (is (every? #(string? (first %)) predictions))
-    (is (every? #(float? (second %)) predictions))
-    (is (every? #(< 0 (second %) 1) predictions))
-    (is (= ["n02123159 tiger cat"
-            "n02124075 Egyptian cat"
-            "n02123045 tabby, tabby cat"
-            "n02127052 lynx, catamount"
-            "n02128757 snow leopard, ounce, Panthera uncia"]
-           (map first predictions)))))
+    (is (= "n02123159 tiger cat" (:class (first predictions))))
+    (is (= (< 0 (:prob (first predictions)) 1)))))
 
 (deftest test-batch-classification
   (let [classifier (create-classifier)
@@ -64,13 +56,9 @@
                                              "test/test-images/Pug-Cookie.jpg"])
         batch-predictions-all (infer/classify-image-batch classifier image-batch)
         batch-predictions-with-default-dtype (infer/classify-image-batch classifier image-batch 10)
-        batch-predictions (infer/classify-image-batch classifier image-batch 5 dtype/FLOAT32)
-        predictions (first batch-predictions)]
-    (is (= 1000 (count (first batch-predictions-all))))
-    (is (= 10 (count (first batch-predictions-with-default-dtype))))
-    (is (some? batch-predictions))
+        predictions (infer/classify-image-batch classifier image-batch 5 dtype/FLOAT32)]
+    (is (= 1000 (count batch-predictions-all)))
+    (is (= 10 (count batch-predictions-with-default-dtype)))
     (is (= 5 (count predictions)))
-    (is (every? #(= 2 (count %)) predictions))
-    (is (every? #(string? (first %)) predictions))
-    (is (every? #(float? (second %)) predictions))
-    (is (every? #(< 0 (second %) 1) predictions))))
+    (is (= "n02123159 tiger cat" (:class (first predictions))))
+    (is (= (< 0 (:prob (first predictions)) 1)))))


[incubator-mxnet] 01/02: change object detection prediction to be a map

Posted by cm...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cmeier pushed a commit to branch clojure-infer-predict-tweak
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git

commit 488843ee575e02ebf752bdfdc8891e14490994ee
Author: gigasquid <cm...@gigasquidsoftware.com>
AuthorDate: Fri Jan 11 14:24:57 2019 -0500

    change object detection prediction to be a map
---
 .../src/org/apache/clojure_mxnet/infer.clj         | 36 ++++++++++------
 .../clojure_mxnet/infer/objectdetector_test.clj    | 49 +++++++++++++++-------
 2 files changed, 57 insertions(+), 28 deletions(-)

diff --git a/contrib/clojure-package/src/org/apache/clojure_mxnet/infer.clj b/contrib/clojure-package/src/org/apache/clojure_mxnet/infer.clj
index 224a392..bc5090f 100644
--- a/contrib/clojure-package/src/org/apache/clojure_mxnet/infer.clj
+++ b/contrib/clojure-package/src/org/apache/clojure_mxnet/infer.clj
@@ -103,6 +103,12 @@
 
 (s/def ::nil-or-int (s/nilable int?))
 
+(defn- format-predictions [predictions]
+  (mapv (fn [[c p]]
+          (let [[prob xmin ymin xmax ymax] (mapv float p)]
+            {:class c :prob prob :x-min xmin :y-min ymin :x-max xmax :y-max ymax}))
+        predictions))
+
 (extend-protocol AClassifier
   WrappedClassifier
   (classify
@@ -206,10 +212,12 @@
                     "Invalid object detector")
      (util/validate! ::image image "Invalid image")
      (util/validate! ::nil-or-int topk "Invalid top-K")
-     (util/coerce-return-recursive
-      (.imageObjectDetect (:object-detector wrapped-detector)
-                          image
-                          (util/->int-option topk)))))
+     (->> (.imageObjectDetect (:object-detector wrapped-detector)
+                              image
+                              (util/->int-option topk))
+          (util/coerce-return-recursive)
+          (first)
+          (format-predictions))))
   (detect-objects-batch
     ([wrapped-detector images]
      (detect-objects-batch wrapped-detector images nil))
@@ -217,10 +225,12 @@
      (util/validate! ::wrapped-detector wrapped-detector
                      "Invalid object detector")
      (util/validate! ::nil-or-int topk "Invalid top-K")
-     (util/coerce-return-recursive
-      (.imageBatchObjectDetect (:object-detector wrapped-detector)
-                               images
-                               (util/->int-option topk)))))
+     (->> (.imageBatchObjectDetect (:object-detector wrapped-detector)
+                                   images
+                                   (util/->int-option topk))
+          (util/coerce-return-recursive)
+          (first)
+          (format-predictions))))
   (detect-objects-with-ndarrays
     ([wrapped-detector input-arrays]
      (detect-objects-with-ndarrays wrapped-detector input-arrays nil))
@@ -230,10 +240,12 @@
      (util/validate! ::vec-of-ndarrays input-arrays
                      "Invalid inputs")
      (util/validate! ::nil-or-int topk "Invalid top-K")
-     (util/coerce-return-recursive
-      (.objectDetectWithNDArray (:object-detector wrapped-detector)
-                                (util/vec->indexed-seq input-arrays)
-                                (util/->int-option topk))))))
+     (->> (.objectDetectWithNDArray (:object-detector wrapped-detector)
+                                    (util/vec->indexed-seq input-arrays)
+                                    (util/->int-option topk))
+          (util/coerce-return-recursive)
+          (first)
+          (format-predictions)))))
 
 (defprotocol AInferenceFactory
   (create-predictor [factory] [factory opts])
diff --git a/contrib/clojure-package/test/org/apache/clojure_mxnet/infer/objectdetector_test.clj b/contrib/clojure-package/test/org/apache/clojure_mxnet/infer/objectdetector_test.clj
index 3a0e3d3..91d4f0e 100644
--- a/contrib/clojure-package/test/org/apache/clojure_mxnet/infer/objectdetector_test.clj
+++ b/contrib/clojure-package/test/org/apache/clojure_mxnet/infer/objectdetector_test.clj
@@ -17,11 +17,13 @@
 (ns org.apache.clojure-mxnet.infer.objectdetector-test
   (:require [org.apache.clojure-mxnet.context :as context]
             [org.apache.clojure-mxnet.dtype :as dtype]
+            [org.apache.clojure-mxnet.image :as image]
             [org.apache.clojure-mxnet.infer :as infer]
             [org.apache.clojure-mxnet.layout :as layout]
             [clojure.java.io :as io]
             [clojure.java.shell :refer [sh]]
-            [clojure.test :refer :all]))
+            [clojure.test :refer :all]
+            [org.apache.clojure-mxnet.ndarray :as ndarray]))
 
 (def model-dir "data/")
 (def model-path-prefix (str model-dir "resnet50_ssd/resnet50_ssd_model"))
@@ -40,28 +42,43 @@
 (deftest test-single-detection
   (let [detector (create-detector)
         image (infer/load-image-from-file "test/test-images/kitten.jpg")
-        [predictions-all] (infer/detect-objects detector image)
-        [predictions] (infer/detect-objects detector image 5)]
+        predictions-all (infer/detect-objects detector image)
+        predictions (infer/detect-objects detector image 5)
+        {:keys [class prob x-min x-max y-min y-max] :as pred} (first predictions)]
     (is (some? predictions))
     (is (= 5 (count predictions)))
     (is (= 13 (count predictions-all)))
-    (is (every? #(= 2 (count %)) predictions))
-    (is (every? #(string? (first %)) predictions))
-    (is (every? #(= 5 (count (second %))) predictions))
-    (is (every? #(< 0 (first (second %)) 1) predictions))
-    (is (= "cat" (first (first predictions))))))
+    (is (= "cat" class))
+    (is (< 0.8 prob))
+    (every? #(< 0 % 1) [x-min x-max y-min y-max])))
 
 (deftest test-batch-detection
   (let [detector (create-detector)
         image-batch (infer/load-image-paths ["test/test-images/kitten.jpg"
                                              "test/test-images/Pug-Cookie.jpg"])
         batch-predictions-all (infer/detect-objects-batch detector image-batch)
-        batch-predictions (infer/detect-objects-batch detector image-batch 5)
-        predictions (first batch-predictions)]
-    (is (some? batch-predictions))
-    (is (= 13 (count (first batch-predictions-all))))
+        predictions (infer/detect-objects-batch detector image-batch 5)
+        {:keys [class prob x-min x-max y-min y-max] :as pred} (first predictions)]
+    (is (some? predictions))
+    (is (= 13 (count batch-predictions-all)))
     (is (= 5 (count predictions)))
-    (is (every? #(= 2 (count %)) predictions))
-    (is (every? #(string? (first %)) predictions))
-    (is (every? #(= 5 (count (second %))) predictions))
-    (is (every? #(< 0 (first (second %)) 1) predictions))))
+    (is (= "cat" class))
+    (is (< 0.8 prob))
+    (every? #(< 0 % 1) [x-min x-max y-min y-max])))
+
+(deftest test-detection-with-ndarrays
+  (let [detector (create-detector)
+        image     (-> (image/read-image "test/test-images/kitten.jpg" {:to-rbg true})
+                      (image/resize-image 512 512)
+                      (ndarray/transpose)
+                      (ndarray/expand-dims 0)
+                      (ndarray/cast dtype/FLOAT32))
+        predictions-all (infer/detect-objects-with-ndarrays detector [image])
+        predictions (infer/detect-objects-with-ndarrays detector [image] 1)
+        {:keys [class prob x-min x-max y-min y-max] :as pred} (first predictions)]
+        (is (some? predictions-all))
+        (is (= 1 (count predictions)))
+        (is (= "cat" class))
+        (is (< 0.8 prob))
+        (every? #(< 0 % 1) [x-min x-max y-min y-max])))
+