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

Conversation

gigasquid
Copy link
Member

Description

Makes tweaks to the infer package to make it more friendly by using Clojure data structures for inputs and outputs to functions. Also add tests to cover all the infer functions.

Checklist

Essentials

Please feel free to remove inapplicable items for your PR.

  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage:
  • Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
  • Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
  • Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
  • Code is well-documented:
  • For user-facing API changes, API doc string has been updated.
  • For new C++ functions in header files, their functionalities and arguments are documented.
  • For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
  • Check the API doc at http:https://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Changes

  • Change inputs and outputs of the infer functions to be Clojure data structures where possible
  • Add unit tests to cover all the infer functions

Comments

Object detection now returns a map structure ex:

{:class car, :prob 0.99847263, :x-min 0.6097917, :y-min 0.1406818, :x-max 0.8906532, :y-max 0.29426125}

Image classification also returns a map structure

{:class n02123159 tiger cat, :prob 0.30337515}

@gigasquid
Copy link
Member Author

@kedarbellare Can you please take a look 😸

(map first predictions)))))
(is (string? class))
(is (< 0.8 prob))
(every? #(< 0 % 1) [x-min x-max y-min y-max])
Copy link
Contributor

Choose a reason for hiding this comment

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

(is (...))?

Copy link
Member Author

Choose a reason for hiding this comment

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

nice catch :)


(s/def ::wrapped-predictor (s/keys :req-un [::predictor]))
(s/def ::wrapped-classifier (s/keys :req-un [::classifier]))
(s/def ::wrapped-image-classifier (s/keys :req-un [::image-classifier]))
(s/def ::wrapped-detector (s/keys :req-un [::object-detector]))

(defn- format-detection-predictions [predictions]
Copy link
Contributor

Choose a reason for hiding this comment

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

so much cleaner! thanks for refactoring 😃

([image input-shape-vec dtype]
(util/validate! ::image image "Invalid image")
(util/validate! (s/coll-of int?) input-shape-vec "Invalid shape vector")
(ImageClassifier/bufferedImageToPixels image (shape/->shape input-shape-vec) dtype/FLOAT32)))
Copy link
Contributor

Choose a reason for hiding this comment

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

use dtype here instead of dtype/FLOAT32?

Copy link
Member Author

Choose a reason for hiding this comment

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

nice catch again - thanks :)

Copy link
Contributor

@kedarbellare kedarbellare left a comment

Choose a reason for hiding this comment

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

looks good! only few minor comments

@gigasquid
Copy link
Member Author

gigasquid commented Jan 12, 2019

Thanks so much @kedarbellare for your review. Great feedback 😄
Also thanks so much for bringing the infer package to life.

Copy link
Contributor

@kedarbellare kedarbellare left a comment

Choose a reason for hiding this comment

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

another thing to check is the captcha example in which i used the infer package. there might be a slight tweak needed there (i.e. removing first): https://github.com/apache/incubator-mxnet/blob/master/contrib/clojure-package/examples/captcha/src/captcha/infer_ocr.clj#L53. unfortunately i couldn't add a unit test for the captcha inference part since it requires a model.

lgtm. please check the captcha inference before merging. and thanks for the changes! 👍

(* (aget prob-and-bounds 3) width)
(* (aget prob-and-bounds 4) height))))
(doseq [p predictions]
(println p))
Copy link
Contributor

Choose a reason for hiding this comment

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

minor: one thing this does change is that the bounds (e.g. x-min, y-min) are not scaled by the width and height of the image. is that fine? maybe this should be added in the docstring of the detection method?

Copy link
Member Author

Choose a reason for hiding this comment

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

oh right - that was a miss on my part - I'll add that back it (without the aget)

@@ -54,23 +54,16 @@
"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

@gigasquid
Copy link
Member Author

Oh good point about the captcha - will do 👍

(->> (.predict (:predictor wrapped-predictor)
(util/vec->indexed-seq [(float-array (first inputs))]))
(util/coerce-return-recursive)
(first)
Copy link
Contributor

Choose a reason for hiding this comment

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

oh! i just remembered that this is changing semantics of predict API. instead of taking a vector of [image1 image2 ...] and returning [predictions1 predictions2 ...] this is just returning predictions for the first image. is that ok? a workaround may be to just take one input or input-array instead of a vector of those.

Copy link
Member Author

Choose a reason for hiding this comment

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

good point. I'm thinking maybe that should be map float array on the inputs instead

I'll add a test for it to be sure. Thanks for catching that :)

(* 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.

👍

@gigasquid
Copy link
Member Author

@kedarbellare I removed the first on all functions in the infer so it will match the scala function signature

Copy link
Contributor

@kedarbellare kedarbellare left a comment

Choose a reason for hiding this comment

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

looks great!

@gigasquid gigasquid merged commit c2110ad into apache:master Jan 13, 2019
@gigasquid gigasquid deleted the clojure-infer-predict-tweak branch January 13, 2019 00:01
piyushghai added a commit to piyushghai/incubator-mxnet that referenced this pull request Jan 22, 2019
haohuanw pushed a commit to haohuanw/incubator-mxnet that referenced this pull request Jun 23, 2019
* change object detection prediction to be a map

* change predictions to a map for image-classifiers

* change return types of the classifiers to be a map
- add tests for base classifier and with-ndarray as well

* tweak return types and inputs for predict
- add test for plain predict

* updated infer-classify examples

* adjust the infer/object detections tests

* tweak predictor test

* Feedback from @kedarbellare review

* put scaling back in

* put back predict so it can handle multiple inputs

* restore original functions signatures (remove first)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants