Having started my graduate studies in computer vision and machine learning before deep learning became mainstream, I belong to the generation of grad students who used hand-crafted features. Most of the research code was available in MATLAB or C++ . My favorite libraries were VLFeat in MATLAB and OpenCV in C++. With the popularity of deep learning in computer vision since the introduction of well known AlexNet, tools like torch, pytorch, tensorflow, keras and caffe are being used to solve many research problems. Python provides a friendly interface for using some of these tools. This motivated me to learn Python and once I started using it, I found libraries which make using Python easy even for non-deep learning projects. It was time to switch from MATLAB to Python! Stepping out of comfort zone was uneasy, but I learned new and useful skills. The more I used it, the more fun I had.

Why Python instead of MATLAB?

  • MATLAB is expensive and you need to buy licences for each toolbox (unless your university provides it). Python is free and open source.
  • MATLAB toolboxes and functions can have a black box structure whereas in Python you can access the ufnction sin libraries.
  • Deep learning research is very fast paced and it makes sense to use the open source models and codes being made available by the research community, which widely uses Python instead of Matlab now.
  • In MATLAB, functions cannot be defined in the same script, each function is a separate file. In Python, functions can be defined in the same file and it makes writing code and debugging easier.
  • Python has better string handling.
  • Python has a huge variety of IDEs available.

On this blog, I will share some code snippets that helped me in transitioning from MATLAB to Python. The blog posts assume familiarity with MATLAB or another programming language as well as concepts in computer vision and machine learning concepts. Let’s get started with setting up the development environment.

Installations:

To get started, download and install Anaconda. I use Python 3.x installation. Anaconda comes with useful Python packages: numpy, scipy, matplotlib, scikit-learn,, statsmodels and pandas. You can check the versions in terminal. It should look something similar to the example below.

foo@bar:~$ conda -V
conda 4.3.21
foo@bar:~$ python -V
Python 2.7.12 :: Anaconda custom (x86_64)

Install OpenCV:

foo@bar:~$ conda install -c conda-forge opencv

or

foo@bar:~$ conda install -c menpo opencv

Install required deep learning packages (check their corresponding websites for most recent installation commands):

foo@bar:~$ conda install pytorch torchvision -c pytorch
foo@bar:~$ conda install -c conda-forge tensorflow
foo@bar:~$ pip install keras
foo@bar:~$ conda install theano

To check version of a package:

foo@bar:~$ python
>>> import numpy
>>> print(numpy.__version__)
1.14.2
>>> exit()

To update all packages:

foo@bar:~$ conda update --all

To update a single package:

foo@bar:~$ conda update numpy

In case of installation errors, googling the error always helped me - mostly directing me to stack overflow.

Development environment:

On Ubuntu, I use:

  • vim to edit my python files
foo@bar:~$ vim filename.py
  • terminal to run python files
foo@bar:~$ python filename.py
  • ipdb as debugger

  • Jupyter notebooks for interactive interface: helpful for visualization or debugging with images

foo@bar:~$ jupyter notebook

You can also try basic features of Jupyter notebook with Python without installing it here.

If you are looking for a more interactive IDE, check this link.

Looking forward to sharing the tips and tricks I am learning as code snippets!


Parneet Kaur

Written by

Postdoc Scientist at Johnson & Johnson *Opinions are my own*

    Updated