“How can I draw a more detailed outline around an object?”
This is probably one of the most frequently asked questions I get after someone reads my previous article on how to do object detection using TensorFlow. There is good news, I finally have the answer.
Facebook AI Research (FAIR) just open sourced their Detectron platform. This means that the software that FAIR uses for object detection research is now available to all of us developers. One of the many things that this new platform can do is object masking. Object masking takes objected detection a step further and instead of just drawing a bounding box around the image, it can actually draw a complex polygon.
Detectron makes it incredibly simple to get object masking running out of the box. Here’s how:
The first step is to install Caffe2:
git clone --recursive https://github.com/caffe2/caffe2.git && cd caffe2make && cd build && sudo make installpython -c 'from caffe2.python import core' 2>/dev/null && echo "Success" || echo "Failure"
Note: Currently, you MUST have an NVIDIA GPU with CUDA in order to use Detectron.
git clone https://github.com/cocodataset/cocoapi.gitcd cocoapi/PythonAPImake install
After we successfully install Caffe2 all we need to do is clone the Detectron repo:
git clone https://github.com/facebookresearch/Detectron.git && cd Detectron
Install the requirements:
pip install \numpy \pyyaml \matplotlib \opencv-python>=3.0 \setuptools \Cython \mock \scipy
And build the python modules:
cd lib && make && cd ..
Now all we have to do is run the following command to run object masking on the demo images:
python tools/infer_simple.py \--cfg configs/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml \--output-dir demo/output \--image-ext jpg \--wts \https://s3-us-west-2.amazonaws.com/detectron/35861858/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml.02_32_51.SgT4y1cO/output/train/coco_2014_train:coco_2014_valminusminival/generalized_rcnn/model_final.pkl \demo
You’ll be able to find the output as PDFs in the Detectron/demo/output
directory.
Here are some of my own test examples, most of them worked pretty well.
But as always, the Kardashians continue to stump AI, because are they real?
Note: If you want to test your own images just add JPG files to the
Detectron/demo
directory. For best results keep the images around 600–800px.
This model does a great job at masking very general objects, however a lot of us need something more specific to our use case. For that, keep a look out for my next tutorial on how to build your own custom object masking model!
Thanks for reading! If you have any questions, feel free to reach out at [email protected], connect with me on LinkedIn, or follow me on Medium and Twitter.
If you found this article helpful, it would mean a lot if you gave it some applause👏 and shared to help others find it! And feel free to leave a comment below.