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 @@ -55,16 +55,16 @@
"Print image classifier predictions for the given input file"
[predictions]
(println (apply str (repeat 80 "=")))
(doseq [[label probability] predictions]
(println (format "Class: %s Probability=%.8f" label probability)))
(doseq [p predictions]
(println p))
(println (apply str (repeat 80 "="))))

(defn classify-single-image
"Classify a single image and print top-5 predictions"
[classifier input-image]
(let [image (infer/load-image-from-file input-image)
topk 5
[predictions] (infer/classify-image classifier image topk)]
predictions (infer/classify-image classifier image topk)]
predictions))

(defn classify-images-in-dir
Expand All @@ -78,12 +78,10 @@
(filter #(re-matches #".*\.jpg$" (.getPath %)))
(mapv #(.getPath %))
(partition-all batch-size))]
(apply
concat
(for [image-files image-file-batches]
(let [image-batch (infer/load-image-paths image-files)
topk 5]
(infer/classify-image-batch classifier image-batch topk))))))
(for [image-files image-file-batches]
(let [image-batch (infer/load-image-paths image-files)
topk 5]
(infer/classify-image-batch classifier image-batch topk)))))

(defn run-classifier
"Runs an image classifier based on options provided"
Expand All @@ -98,6 +96,7 @@
factory {:contexts [(context/default-context)]})]
(println "Classifying a single image")
(print-predictions (classify-single-image classifier input-image))
(println "\n")
(println "Classifying images in a directory")
(doseq [predictions (classify-images-in-dir classifier input-dir)]
(print-predictions predictions))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,13 @@
predictions (classify-single-image classifier image-file)]
(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)
batch-predictions (classify-images-in-dir classifier image-dir)
predictions (first batch-predictions)]
(is (some? batch-predictions))
predictions (first (classify-images-in-dir classifier image-dir))]
(is (some? predictions))
(is (= 5 (count predictions)))
gigasquid marked this conversation as resolved.
Show resolved Hide resolved
(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)))))
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@
"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 [[label prob-and-bounds] predictions]
(doseq [{:keys [class prob x-min y-min x-max y-max]} predictions]
(println (format
"Class: %s Prob=%.5f Coords=(%.3f, %.3f, %.3f, %.3f)"
label
(aget prob-and-bounds 0)
(* (aget prob-and-bounds 1) width)
(* (aget prob-and-bounds 2) height)
(* (aget prob-and-bounds 3) width)
(* (aget prob-and-bounds 4) height))))
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
"Detect objects in a single image and print top-5 predictions"
[detector input-image]
(let [image (infer/load-image-from-file input-image)
topk 5
[predictions] (infer/detect-objects detector image topk)]
predictions (infer/detect-objects detector image topk)]
predictions))

(defn detect-images-in-dir
Expand All @@ -84,12 +84,10 @@
(filter #(re-matches #".*\.jpg$" (.getPath %)))
(mapv #(.getPath %))
(partition-all batch-size))]
(apply
concat
(for [image-files image-file-batches]
(let [image-batch (infer/load-image-paths image-files)
topk 5]
(infer/detect-objects-batch detector image-batch topk))))))
(for [image-files image-file-batches]
(let [image-batch (infer/load-image-paths image-files)
topk 5]
(infer/detect-objects-batch detector image-batch topk)))))

(defn run-detector
"Runs an image detector based on options provided"
Expand All @@ -107,6 +105,7 @@
{:contexts [(context/default-context)]})]
(println "Object detection on a single image")
(print-predictions (detect-single-image detector input-image) width height)
(println "\n")
(println "Object detection on images in a directory")
(doseq [predictions (detect-images-in-dir detector input-dir)]
(print-predictions predictions width height))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@

(deftest test-single-detection
(let [detector (create-detector)
predictions (detect-single-image detector image-file)]
predictions (detect-single-image detector image-file)
{:keys [class prob x-min x-max y-min y-max] :as pred} (first predictions)]
gigasquid marked this conversation as resolved.
Show resolved Hide resolved
(is (some? predictions))
(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 (= ["car" "bicycle" "dog" "bicycle" "person"]
(map first predictions)))))
(is (string? class))
(is (< 0.8 prob))
(is (every? #(< 0 % 1) [x-min x-max y-min y-max]))
(is (= #{"dog" "person" "bicycle" "car"} (set (mapv :class predictions))))))

(deftest test-batch-detection
(let [detector (create-detector)
batch-predictions (detect-images-in-dir detector image-dir)
predictions (first batch-predictions)]
predictions (first batch-predictions)
{:keys [class prob x-min x-max y-min y-max] :as pred} (first predictions)]
(is (some? batch-predictions))
(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 (string? class))
(is (< 0.8 prob))
(every? #(< 0 % 1) [x-min x-max y-min y-max])
(is (= #{"dog" "person" "bicycle" "car"} (set (mapv :class predictions))))))
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
(defn do-inference
"Run inference using given predictor"
[predictor image]
(let [[predictions] (infer/predict-with-ndarray predictor [image])]
predictions))
(let [predictions (infer/predict-with-ndarray predictor [image])]
(first predictions)))

(defn postprocess
[model-path-prefix predictions]
Expand Down
Loading