Hi,

PyImageSearch readers have been asking me to cover PyTorch for the past few years. At the time, I had too much on my plate and wasn't able to cover it in the depth that I knew I wanted to.

Today that changes.

I've put together not only an intro 5-part series on an "Introduction to PyTorch," but I've also created another two series on intermediate and advanced PyTorch concepts:

Introduction to PyTorch

  • What is PyTorch?
  • Training your first CNN using PyTorch
  • Image Classification with Pre-trained Networks in PyTorch
  • Object Detection with Pre-trained Networks in PyTorch

Intermediate PyTorch for Computer Vision techniques

  • Image Data Loaders in PyTorch
  • Caveats with Image Data Loaders
  • Transfer Learning with Image Classification in PyTorch
  • Introduction to Distributed Training in PyTorch

Advanced PyTorch techniques

  • Training a DCGAN in PyTorch
  • Training an Object Detector from scratch in PyTorch
  • Training Image Segmentation Models in PyTorch

Those tutorials can be found inside PyImageSearch University.

But for today ... let's start with part 1 in our series where we discuss:

  • What PyTorch is
  • How to install PyTorch on your machine
  • Important PyTorch features, including tensors and autograd
  • How PyTorch supports GPUs
  • Why PyTorch is so popular among researchers
  • Whether or not PyTorch is better than Keras/TensorFlow
  • Whether you should be using PyTorch or Keras/TensorFlow in your projects

Click here to learn more about PyTorch

The big picture: PyTorch is one of the most popular deep learning libraries, competing with Keras and TensorFlow as the "most used" deep learning package.

As a Google Trends graph shows, over the years PyTorch has gained significant traction:

Image

PyTorch tends to be especially popular among the research community due to its Pythonic nature and ease of extendability (i.e., implementing custom layer types, network architectures, etc.).

How it works: PyTorch is based on Torch, a scientific computing framework for Lua. Prior to both PyTorch and Keras/TensorFlow, deep learning packages such as Caffe and Torch tended to be the most popular.

However, as deep learning started to revolutionize nearly all areas of computer science, developers and researchers wanted an efficient, easy-to-use library to construct, train, and evaluate neural networks in the Python programming language.

Python, along with R, are the two most popular programming languages for data scientists and machine learning, so it's only natural that researchers wanted deep learning algorithms inside their Python ecosystems.

Keras was first to the party, then TensorFlow soon after (which eventually became the default backend for the Keras API).

PyTorch came a year later, addressing many of the problems that existed in TensorFlow 1.x.

My thoughts: Unlike Keras/TensorFlow, where training a neural network is as simple as calling "model.fit," PyTorch requires that you write your training loop by hand. This is both a good and a bad thing:

  1. On one hand, you have complete and total control over the training process, making it easier to implement custom training procedures, much in part due to PyTorch's fantastic "autograd" feature
  2. But on the other hand, PyTorch makes it fairly easy to shoot yourself in the foot — nearly every deep learning practitioner has forgotten to zero their accumulated gradients before applying backpropagation and updating model parameters, which tends to be hard problem to debug (that simply wouldn't happen in a "model.fit" call in Keras/TensorFlow as the entire process is built into the function call)

Yes, but: Don't get too wrapped up in one library being "better than the other."

The honest truth is that the feature sets between the PyTorch 1.x and Keras/TensorFlow 2.x have converged (pun intended) and are essentially identical. This statement may seem bold, but it's backed up by the authors of PyTorch:

"Interestingly, with the advent of TorchScript and eager mode, both PyTorch and TensorFlow have seen their feature sets start to converge with the other's, though the presentation of these features and the overall experience is still quite different between the two." —  Eli Stevens, Luca Antiga, and Thomas Viehmann

My point here is that if you're a budding deep learning practitioner learning your first deep learning library, don't spend more than 5 minutes deciding.

Do you think TensorFlow is a badass name? Cool, learn TensorFlow/Keras first.

You're gunning for a research position after you finish grad school? Great, learn PyTorch first.

The best thing you can do for yourself is to start studying now. Your investment of time and resources will pay off big time in the coming years.

Stay smart: Learning your first deep learning library is a lot like learning your first programming language.

You likely struggled immensely when learning your first programming language because you not only hard to learn the syntax of the language, but also the design/logic flow of a program, including control structures like "if," "for," "while," etc.

But then, when you learned your second programming language, it was so much easier! You already knew the fundamentals of logic flow. All you needed to do then was learn the idiosyncrasies of the syntax for the language.

The same is true for deep learning libraries. What you learn in Keras/TensorFlow will transfer over to PyTorch and vice versa.

Again, don't get too caught up in the PyTorch vs. Keras/TensorFlow debate. It's a bunch of hot air. Just pick one and learn it.

And since you're here today, why not follow along with me and learn the basics of PyTorch?


Adrian Rosebrock
Chief PyImageSearcher

P.S. You're ready to study deep learning and computer vision...but you don't know where to start.

What you need is a book that:
  1. Starts with the fundamentals and then builds up to advanced techniques
  2. Helps you learn how to successfully apply CV/DL to your own projects and datasets
  3. Provides an actual real person to support you and help you as you work through it and have questions
The good news is that book does exist — it's called Deep Learning for Computer Vision with Python.

Rishiraj Acharya, teacher at Stanford University, had this to say about the book:

Image
While Sean Mackenzie, long-time PyImageSearch reader and customer said the following after I helped him through a tricky project:

"Thank you for the incredible customer service. It's amazing to know you care so much about providing great service rather than just putting out content and getting people to buy packages. It's obvious you care about the success of each one of your customers. I appreciate it."

I'd love to be able to help you on your CV/DL journey, just like I've helped Rishiraj, Sean, and tens of thousands of other PyImageSearch readers.

If you'd like to follow in their footsteps, I'd be happy to send you a PDF containing the Table of Contents and a few sample chapters to my deep learning book:

Click here to grab the PDF of sample chapters and Table of Contents

After clicking the above link, you'll receive a separate email with the PDF in a few short moments.