Object Detection With Clojure MXNet
Object detection just landed in MXNet thanks to the work of contributors Kedar Bellare and Nicolas Modrzyk. Kedar ported over the
infer package to Clojure, making inference and prediction much easier for users and Nicolas integrated in his Origami OpenCV library into the the examples to make the visualizations happen.
We’ll walk through the main steps to use the
infer object detection which include creating the detector with a model and then loading the image and running the inference on it.
Creating the Detector
To create the detector you need to define a couple of things:
- How big is your image?
- What model are you going to be using for object detection?
In the code below, we are going to be giving it an color image of size 512 x 512.
1 2 3 4 5 6 7
- The shape is going to be
[1 3 512 512].
1is for the batch size which in our case is a single image.
3is for the channels in the image which for a RGB image is
512is for the image height and width.
layoutspecifies that the shape given is in terms of
NCHWwhich is batch size, channel size, height, and width.
dtypeis the image data type which will be the standard
model-path-prefixpoints to the place where the trained model we are using for object detection lives.
How to Load an Image and Run the Detector
Now that we have a model and a detector, we can load an image up and run the object detection.
To load the image use
load-image which will load the image from the path.
Then run the detection using
infer/detect-objects which will give you the top five predictions by default.
It will give an output something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
which you can then use to draw bounding boxes on the image.
Try Running the Example
One of the best ways to explore using it is with the object detection example in the MXNet repo. It will be coming out officially in the
1.5.0 release, but you can get an early peek at it by building the project and running the example with the nightly snapshot.
You can do this by cloning the MXNet Repo and changing directory to
Next, edit the
project.clj to look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
If you are running on linux, you should change the
Next, go ahead and do
lein test to make sure that everything builds ok. If you run into any trouble please refer to README for any missing dependencies.
After that do a
lein install to install the
clojure-mxnet jar to your local maven. Now you are ready to
cd examples/infer/object-detection to try it out. Refer to the README for more details.
If you run into any problems getting started, feel free to reach out in the Clojurian #mxnet slack room or open an issue at the MXNet project. We are a friendly group and happy to help out.
Thanks again to the community for the contributions to make this possible. It’s great seeing new things coming to life.
Happy Object Detecting!