Which is Better For Your Machine Learning Task, OpenCV or TensorFlow

tensorflow vs opencv
made by the author

Backstory

I surf the internet everyday looking for machine learning and computer vision content. I like to stay up-to-date with what’s happening in the field of ML because this is a field that can surprise you almost everyday!

One question that I came across a few times is :

Which is better OpenCV or Tensorflow?

To some, this is not a valid question.

To others, this is a question worth thinking about.

The simplest answer is that Tensorflow is better than OpenCV and OpenCV is better than Tensorflow!

I hope I didn’t confuse you! If I did, please read on!

The reality is, each of these frameworks is destined to be used in specific areas in machine learning and computer vision. So, directly saying that one is better than the other just doesn’t make sense.

But we can say that one is better than the other in certain areas and vice versa.

In fact, Tensorflow is a machine learning framework that helps you build machine learning models. It is more famous for building neural networks, which are one type of algorithms and approaches in machine learning, called deep learning.

Hence, Tensorflow has been released to serve a main purpose which is building machine learning models. Here, I say machine learning and not just deep learning because there have been some efforts to make Tensorflow capable of training non-deep machine learning models such as decision forests.

OpenCV on the other hand, is a computer vision framework that helps you do all sorts of processing on images and videos. Since its release, it’s been a widely used tool for image processing tasks. It enables you to manipulate pixels easily, so that you can build your own image and video processing algorithms if you wish to do so.

With the rise of deep learning, OpenCV started integrating more functionalities that support deep learning based tasks by introducing a new module called DNN (Deep Neural Networks).

This DNN module was designed to make it easy to integrate deep learning models that have already been trained. That’s why you’ll find examples in OpenCV on how to integrate deep learning models for : image classification, image object detection and image segmentation.

Now, as you can see, all of these examples are deep learning models focused on computer vision tasks.

To the best of my knowledge, there is no support for language models for example (such as BERT).

Now, these 2 frameworks can have some similarities in specific cases. For example, with Tensorflow, you can manipulate images and prepare them for training your machine learning model.

OpenCV can also be used for doing some machine learning tasks. For example, you can train an SVM model, Logistic Regression model or Bag Of Visual Words model in OpenCV.

When to use which

source : pexels.com

Now, on to the use cases of each of these 2 widely used frameworks.

If you are working on building a new deep learning model for some specific task and with custom dataset then Tensorflow should be your choice.

If you are finished with training a deep learning model for a computer vision task, especially : image classification ,object detection and image segmentation, and you’re looking to deploy your model then OpenCV MIGHT be a good choice.

Both Tensorflow and OpenCV have C++ and Python APIs.

In Tensorflow, machine learning engineers use the Python API for the training part and they use the C++ API for deploying their models in a C++ application/API/SDK.

But as I mentioned before, OpenCV might be a good or even better choice for deployment and here’s why.

Many companies have been using OpenCV for a long time now. These companies are building computer vision products. They are very used to the framework and they don’t want to add another third party library (another dependency) to their production code. In this case, they might opt for using OpenCV to deploy their computer vision deep learning models.

Another very important point where OpenCV can be a better choice for production deployments is performance. In fact, for some deep learning models, running them in OpenCV can be an order of magnitude faster then running them in Tensorflow (even when using Tensorflow’s C++ API).

For some industries, this could be a crucial point.

Also, one other thing about the C++ APIs of both frameworks that can be a deal breaker is documentation. OpenCV’s documentation is much better (even if we’re just talking about the DNN module here) than Tensorflow’s C++ API’s documentation.

A final point where you might go with OpenCV instead of Tensorflow is that, with OpenCV, you can train an SVM model in C++. This means that in the same production code, you can train a model and deploy it.

Conclusion

To summarize:

  1. Tensorflow is better than OpenCV for some use cases and OpenCV is better than Tensorflow in some other use cases.
  2. Tensorflow’s points of strength are in the training side. OpenCV’s points of strength are in the deployment side, if you’re deploying your models as part of a C++ application/API/SDK.
  3. The main points of overlap between the two frameworks happen in computer vision tasks.
image made by the author

I am a Machine Learning Engineer working on solving challenging computer vision problems. I want to help you learn Machine Learning applied to Computer Vision problems. Here’s how.

  1. By helping you stay up-to-date with what’s happening in the field. I do this by sharing bite-size ML posts on LinkedIn and Twitter almost dailySo follow me there!
  2. By giving you a weekly digest of those bite-size posts on my newsletter. So subscribe to it!
  3. By writing articles here on Medium about different topics in Machine Learning. So follow me here!
  4. By giving you a free machine learning job-ready checklist to help you check all points you need to learn if you’re planning a career in ML, specifically in Computer Vision. You can get the checklist here.

5. Last but not least, by sharing with you my FREE introductory Tensorflow course that has more than 4 hours of video content, and you can ask me any question you have there.

Also, feel free to contact me on LinkedIn or Twitter if you have any questions or you just want to chat about ML!

Enjoy the read? Reward the writer.Beta

Sign up for The Variable

By Towards Data Science

Every Thursday, the Variable delivers the very best of Towards Data Science: from hands-on tutorials and cutting-edge research to original features you don't want to miss. Take a look.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store

Comments

Popular posts from this blog

A Short Guide To Metric Nuts and Bolts

CAN Bus Wiring Diagram, a Basics Tutorial

Best Free CFD Tutorial: FreeCAD Openfoam CFD Workbench