Blogs

All About Python Libraries

Before moving towards the deeper context, let us have a look at what is Python and what do we mean by a library.

What is Python?

Python is a high-level programming language that can be easily interpreted by human beings. It is the most famous programming language in this modern period. Python is useful because of its smart libraries and algorithms.

What is a Library?

The library is a chunk of code that perform a specific functionality when we reuse it in our program. We can perform a functionality owned by the library by just importing it in our code. It prevents us to type thousands of lines of code and saves our time. Here is the list of standard Python libraries:

  • Matplotlib
  • Pandas
  • Requests
  • Numpy
  • SQLAlchemy
  • BeautifulSoup
  • Piglyt
  • Scrapy
  • Scipy
  • Python Twisted
  • PyGame
  • Pillow
  • pywin32
  • wxPython
  • iPython
  • Nose
  • Flask
  • SymPy
  • Fabric
  • PyGTK

Introduction to Few Deep Learning Libraries

There are some other Python libraries that are used specifically for Deep Learning. Deep Learning is a part of Machine Learning and Artificial Intelligence. As the name suggests, Deep Learning means you move deeply into networks and extract the information through layers. According to AI and ML experts, Python is the best choice for developing Deep Learning models because of its libraries that can be used to develop whatever you want.

PyTorch

PyTorch is one of the best open-source Machine Learning library also used for Deep Learning. It is based on the Torch library that was implemented in the C language almost 15 years ago. Whereas, PyTorch was developed back in 2016 using C++, Python, and CUDA which supports parallel computations. Moreover, this library is used for computer vision and Natural Language Processing applications.

TensorFlow

TensorFlow is another open-source library that was introduced in 2015. It was developed by the Google Brain team using Python, C++, and CUDA. This library is used for data flow and training Machine Learning models very easily. It is also used for high mathematical computations and applications such as Neural networks and Deep Neural Networks. It also comes up with TensorFlow Lite to trail Machine Learning models on your smartphone.

Keras

Keras is also one of the most useful and open source library for Neural Networks. Keras is written in Python and used for fast experimentation with Deep Neural Networks. It was mainly developed by Google for Open-Ended Neuro Electronic Intelligent Robot Operating System abbreviated as ONEIROS. Moreover, it is now available on the top of TensorFlow, R, and Microsoft Cognitive Toolkit.

Theano

Theano is one of the Deep Learning library, developed by the Montreal Institute of Learning Algorithms, University of Montreal. It is used to train Deep Neural Network algorithms. Using Theano you can optimize mathematical expressions and multi-dimensional arrays efficiently. It can be executed on CPU and GPU both.

Microsoft Cognitive Toolkit (CNTK)

Microsoft Cognitive Toolkit, previously known as CNTK is an open-source Deep Learning library. This Deep Learning framework was written in C++ and developed by Microsoft Research Team. Microsoft Cognitive Toolkit is used to drive neural networks in the form of graph and it was used by Microsoft for its products such as Skype, Bing, Cortana and Xbox.

Caffe

Caffe is a Deep Learning framework that is mainly used for Convolutional Neural Network, but in the last 3 years, it was extended to Deep Learning architecture and Recurrent Neural Network. Most interesting part is that this library was developed by a Ph.D. student, Yangqing Jia, who was studying in University of California, Berkeley. Caffe is developed in C++ with a Python interface.

Open CV

Open CV is an open-source Machine Learning and computer vision library. Open CV is used for many purposes such as detecting a face, to recognize a face, detect objects, classify human beings in a random video. It can also track moving objects in a video. Moreover, it is also used in surveillance systems. It is developed in the programming languages C++ and C.

MXNet

Apache MXNet is an open-source Deep-Learning library that is used to train and deploy Convolutional Neural Networks and Deep Neural Networks. It was developed by the Apache Software foundation and it can be used with multiple programming languages such as C++, Python, R, Julia, Java, JavaScript, Go, Perl, and Scala. Apache MXNet can be executed on both CPU and GPU.

Infer.NET

Infer.NET is an open-source Machine Learning library also used as a deep learning framework. This framework is developed by Microsoft and written in C# language. Infer.NET is used in Bayesian statistics to depict graphical models and it gives the statisticians the opportunity to practice probability modeling. This library has analytical tools such as Clustering, Hidden Markov Chain, and Bayesian analysis.

Among these useful Deep Learning libraries, these 4 libraries are considered the most efficient frameworks because they can be used in almost any Deep Learning model.

  • Tensor Flow
  • Keras
  • PyTorch
  • Open CV

We will discuss each of them in detail.

Important Libraries Used in Deep Learning

1. TensorFlow

TensorFlow was developed a couple of years ago by Google, when they feel that the data is expanding very fast. As discussed earlier, TensorFlow is an open-source library used for data flow graphs to build Deep Learning models. It has evolved with the innovation in Artificial Intelligence and Deep Learning. It means that if someone wants to excel his career in AI, he/she should have great knowledge of TensorFlow. The interesting part is that TensorFlow was not developed for Deep Learning, but eventually it proved its importance for Deep Learning development.

TensorFlow

Working

There are 2 components required for the working of Tensor Flow.

  • Tensors
  • Graphs

TensorFlow accepts the data in the form of Tensors. Tensors are the multi-dimensional arrays of greater dimensions. These high dimensional arrays help in accommodating high level of data.

TensorFlow code is executed on computers using GPU because it has to detect and recognize objects which require more computational power. It works on the basis of data flow graphs and it is easier to execute graphs, thus the whole execution becomes easy. All the execution in the graph is done by connecting tensors (multi-dimensional arrays) together.

A tensor has a node and an edge. The function of node is to carry mathematical computations and produces an output, whereas, the edge is used to explain the input/output operations between the nodes.

You can add the TensorFlow library in your code by simply writing a line of code:

import tensorflow as tf

Advantages of TensorFlow

  • One of the best advantages of TensorFlow is its graphical visualizations. These graphical visualizations are inherent unlike other libraries such as Theano and PyTorch.
  • Tensor flow has the advantage of the wonderful performance, regular updates and frequent new functionality addition.
  • It helps us to execute the subpart of the graph, therefore we can introduce and retrieve the discrete data. It helps us to debug our code.
  • We can use the TensorFlow library on our smartphone as well as on highly functional computers. TensorFlow Lite is the app for smartphones.
  • Its performance and accuracy is high, that is why its results are very clear and accurate.
  • We can also check the training progress of the models and track several metrics.

Applications of TensorFlow

TensorFlow is a great library that has several uses in industry and multi-national organizations. It is used for detection, recognition, perception, and classification of objects, faces and things. Below are the few best applications developed using the TensorFlow library.

  • Deep Speech developed by Firefox.
  • RankBrain is developed by Google to rank the best pages among billions of pages on top of the search. It is also developed using Tensor Flow.
  • Inception Image Classifier, also developed by Google for computer vision models.
  • To catch the drug addict candidates, Stanford University has developed a system using Deep Neural Networks.
  • An application is build that detects the faces or objects according to the input given and makes the collage on the basis of that output. It also uses a TensorFlow library.
  • It is also used for speech recognition systems.
  • Home surveillance systems.
  • Self-driving cars.
  • Sentiment analysis on the basis of reactions.
  • Image and video recognition and much more.

2. Keras

Keras is an open-source framework developed by a Google’s Engineer named Francois Chollet. It is easy to use a library that runs on the top of Theano, Microsoft Cognitive Toolkit and TensorFlow. Keras is further divided into another library called “Backend” that handles low-level computations because Keras is designed to handle high-level computations only. It performs fast executions, thus it is user-friendly, modular, and easy to extend.

Keras

What is Keras’ “Backend”?

Backend in Keras is used to perform all low-level computations such as tensor products, convolutional and several other things. TensorFlow is the default backend of Keras but we can also change the configuration. This backend will perform all low-level computations of Keras.

Architecture of Keras

Keras provides the complete library to develop any neural network including Convolutional Neural Network and Recurrent Neural Network. Let us have a look at the API of Keras. It is divided into 3 main parts.

  • Model
  • Layer
  • Core Modules

Model

There are two types of Keras model:

1. Sequential Model

The sequential model of Keras is the linear composition of layers. This sequential model represents all the neural networks that are available. It is easy to understand and have very comprehensive functionality.

2. Functional API

This functional API is used to create complex models that cannot be created by TensorFlow and Theano.

Layer

Keras provides built-in layers that are used to create complicated Deep Neural Networks and other Neural Networks. Below are the 4 layers of Keras.

  • Core Layers
  • Convolutional Layers
  • Pooling Layers
  • Recurrent Layers

Core Modules

There are some pre-defined functions or core modules related to neural networks in Keras framework. These core modules are as follows:

  • Activation Module
  • Loss Module
  • Optimizer Module
  • Regularizers

Features of Keras

  • Keras is easy to use and easy to extend.
  • Keras is highly modular and expressive.
  • It has a high-level interface that makes it easy to understand for its users.
  • It uses Tensor Flow or Theano as its backend that performs low-level computations.
  • It easily runs on CPU and GPU.
  • Keras supports fully connected, convolutional, recurrent, pooling, embedding and other models of neural networks, and these models are used to build more complex models.
  • Debugging is very easy in Keras because it is a Python-based library.

Advantages of Keras

  • Keras is very useful to implement Deep Learning models and Natural Language Processing units.
  • It is a user-friendly API and helps a lot in creating the Neural Networks model.
  • There is no need to write hundreds of lines of code to create a neural network. It is possible with a few lines of code.
  • We can choose any API from Theano, CNTK or TensorFlow to perform the backend functionality in Keras.
  • It provides pre-trained weights. We do not need to train models for prediction or feature extraction.
  • Through Keras, we can train our models on a single GPU or multiple GPUs. We can process a huge amount of data through Keras.

3. PyTorch

Python was released back in 2016, developed by Facebook’s AI Research Lab (FAIR). It is written in Python, C++, and CUDA language. Since the release, several researchers are adopting PyTorch because of its high computational power. It can build highly complex neural networks. It is developed to run high numerical computations which becomes easier as it is developed in Python programming language.

PyTorch

You can also use other Python libraries such as NumPy, SciPy, and Cython with PyTorch to extend its functionality. Unlike TensorFlow which is a static library, PyTorch is a dynamic framework which provides more flexibility to build complex architecture. It is used by AI developers, researchers, and students to build innovative systems.

Tensors in PyTorch

PyTorch tensors are multi-dimensional arrays like NumPy arrays but they can also be computed on GPU. On the other hand, NumPy arrays do not support GPU. You just need to cast a PyTorch tensor to a CUDA datatype so that it can run on a GPU. This is one of the biggest advantage of using PyTorch. These tensors are as follows:

  • Float tensor (32-bit float)
  • Double Tensor (64-bit float)
  • Half Tensor (16-bit int)
  • Long Tensor (64-bit int)

Features of PyTorch

  • It is very easy to use and operate.
  • As it is developed in Python, it uses core Python concepts such as classes, conditional loops, structures and dictionaries that make it easy to understand as we are familiar with this language.
  • It provides a platform that offers dynamic computational graphs.
  • You can do amendments in graph on runtime.
  • According to experts, you can use this library when you do not have an idea that how much time the neural network will take.

Advantages of PyTorch

  • One of the most important benefit of PyTorch is that it is developed in Python, which is the most growing programming language in modern era.
  • It is very easy for the developers to code with PyTorch as they have been familiar with Python.
  • PyTorch is easy to learn because of its easy syntax as compared to other Deep Learning libraries.
  • There have been vast research and development in this technology so it is very organized and there is a solution available to every problem.
  • You can easily debug PyTorch code using different Python debugging tools such as pdb and ipdb.

4. OpenCV

OpenCV is a wonderful open-source library that is used for Machine Learning, Computer Vision, and Image Processing. It performs real-time detection and recognition of objects and human beings. Moreover, it is used to detect the handwriting of humans.

OpenCV

It was developed in Python, C++, Java, and C. It is compatible for Windows, Linux, macOS, and Android. It is well known for real-time recognition and detection. Other than computer vision, OpenCV is also used for Deep Learning.

Deep Learning – OpenCV

OpenCV comes up with its highly improvised Deep Learning module called a Deep Neural Network. This neural network is used for pre-trained models and it supports Python and C++ both.

Working

  • At first, it loads a model from the computer.
  • It pre-processes an image using any algorithm.
  • Results in the output classification.

Experts say that we should not train our Deep Learning models directly using OpenCV, rather we should train them using some other framework or library and then use them in OpenCV for further detection and classification.

Applications of OpenCV

There is a vast list of applications that are build using the OpenCV library. Some of them are listed below:

  • Object detection and recognition
  • Face detection and recognition
  • Count the number of people (using face detection algorithm)
  • Count the number of vehicles on highways
  • Driver-less car system
  • Medical image detection

Advantages of OpenCV

  • There are various algorithms based on OpenCV that provide us many features such as detecting eyes, converting an image to greyscale, applying bounding box around eyes or face.
  • All the computer vision tasks are performed by using this library. It is used by many multinational companies and government agencies.
  • It gives us real-time detection and recognition, thus helps Police and other agencies to catch any robbery.

Now, have a look at how to set up or install different Python IDEs.

Setting Up Anaconda

We can install Anaconda on Windows and Mac both. Let us discuss both one by one.

Step 1: Install the Anaconda Installer from the official website.

Step 2: Click the installer to launch.

Step 3: Click Next.

Step 4: See at the bottom of the page you will see the “I agree” button, click it.

Step 5: After checking the checkbox “Just me”, click on the “Next” button

Step 6: Select the folder in which you want to store to install Anaconda and click “Next”

Anaconda

Step 7: Click on the 2nd Checkbox of default Python 3.8, then click on “Next”

Anaconda

Do not select “Add Anaconda 3 to my PATH environment variable” unless you want to run multiple versions of Python and Anaconda to your system.

Step 8: Click the “Next” button

Step 9: Click on the “Finish” button after ensuring that the two checkboxes are checked.

Your Anaconda for Windows is installed and you can go to your specified folder for further working.

Installing Anaconda for Mac

Step 1: Download the graphical macOS installer for python.

Step 2: Double click the downloaded file and click continue to start the installation.

Step 3: Answer the prompts on the Introduction, Read me and License.

Step 4: Click the “Install” button. If you want to change the directory to keep files, you can select on “Change Install Location”.

Anaconda

Step 5: Choose “Install for me only” and then click on the “Continue button”

Anaconda

Step 6: Then you will be directed to this screen. If you want to install PyCharm, click on the link, otherwise click “Continue”.

Anaconda

Step 7: Anaconda for Mac is successfully installed. Click on the “Close” button.

Anaconda

Setting up R

Installing R for Windows

Follow these steps to install R on Windows:

  • Search www.r-ptoject.org on Google or any search engine.
  • Click on “Download R”.
  • Select the CRAN location and click the link.
  • Click on the “Download R for Windows” link on top of the page.
  • Click on “Install R for the first time” link as you do not have installed R before
  • Click on “Download R for Windows”
  • Save the file on any folder and run the .exe file
  • R is downloaded successfully

Installing R for Mac

  • Open the Google search engine and type www.r-project.org
  • Click the "download R" link
  • Select a CRAN location and click the link.
  • Click on the "Download R for macOS" link at the top of the page.
  • Click on the latest version of Files
  • A file will be downloaded. You have to install the file and follow further instructions.
  • R for Mac is successfully installed

Setting up Spyder

Installing Spyder on Windows

Step 1: Go to the official website of Spyder and move to the installer

Step 2: Select the download option from the main menu

Step 3: You can download the Spyder IDE directly from the Anaconda platform

Installing Spyder on Windows

Note: This will take you to download the Anaconda. If you have already downloaded the Anaconda, then directly open the Anaconda. Otherwise, follow the steps above to install Anaconda.

Step 4: Go to the option of “All programs” and then select “Anaconda Navigator” from that.

Installing Spyder on Windows

Step 5: Click on the “Launch” button and after some time your Spyder IDE will open. Now you are good to proceed and code.

Setting Up Jupyter

Installing Jupyter Notebook on Windows

Step 1: Launch the Anaconda navigator and click on “Install” button on Jupyter Notebook.

Installing Jupyter Notebook on Windows

Step 2: When the installation is complete, you will see the “Launch” button instead of “Install”.

Installing Jupyter Notebook on Windows

Step 3: Click on the “Launch” button.

Step 4: When the launching is finished, you are directed towards the IDE and your Installing process is completed.

Installing Jupyter Notebook on Mac

Step 1: Install the Anaconda Mac OS version from the Anaconda website.

Step 2: Run the installer package of Anaconda.

Step 3: Now, open your terminal and write this command: vi .bash_profile.

Step 4: It will show this output: export PATH =”anaconda3/bin:$PATH”.

Step 5: Change it to: export PATH = “$PATH:/anaconda3/bin”.

Step 6: Write jupyter notebook on the terminal and run. Your installation will be started as soon as you click.

Setting up Google Colab

Step 1: Go to your Google Drive, click on “New”, dropdown to “More” and then click on “Colaboratory”.

Setting up Google Colab

Step 2: Open your Jupyter Notebook and click on “NEW PYTHON 3 NOTEBOOK”

Setting up Google Colab

Step 3: Select the free GPU service. For what Google Colab is meant for.

Setting up Google Colab

Step 4: Now, you can install different Python libraries like NumPy, SciPy, PyCharm, etc. and code.

Installation of Multiple Deep Learning Libraries

In this part, we will learn to install some useful Deep Learning libraries.

How to install TensorFlow

You can install TensorFlow using pip and Anaconda. We will discuss both one by one.

Installation with pip

Step 1: At first, check the version of Python that is running on your computer by typing $ pip3 –version. It will give you the latest version installed.

Step 2: Go to cmd and type $ pip install –upgrade TensorFlow. This command will take some time to install all the packages of TensorFlow.

Step 3: Now, you can install the GPU for tensor flow by typing $ pip install TensorFlow-gpu.

Step 4: Now, check whether your TensorFlow library is installed or not. Type $ Python to move to Python terminal.

Step 5: Type “import TensorFlow as tf”, if all goes well, it means that the TensorFlow library is successfully installed.

Installation with Anaconda prompt

Step 1: Open the Anaconda Prompt while typing in search.

Step 2: You have to move in the virtual environment to install TensorFlow in Anaconda, by typing “$ conda create –n tensorver”. Tensorver is the name of a virtual environment.

Step 3: Now, activate the environment by typing “$ activate tensorver”

Step 4: Now type “$ conda install TensorFlow”

This process will take some time as it has to install several packages along with it. To see the progress bar type “y” and it will show you in command prompt.

How to Install Keras

Step 1: At first, create a virtual environment.

For Linux/Mac OS

Type “python3 –m venv kerasenv” and activate the virtual environment by typing “$ cd kerasvenv $ source bin/activate”.

For Windows

Type “py -m venv Keras” and move to folder and type “.\env\scripts\activate.

Step 3: Install the following Python libraries to install the Keras as Keras depends upon these libraries. You can install these libraries by using pip install [name of library] and press enter.

  • NumPy
  • SciPy
  • Pandas
  • Scikit-Learn
  • Matplotlib
  • Seaborn
  • After installing all these libraries install the Keras.

Step 4: Type “pip install Keras”. Your installation will be started as soon as you type this command.

How to install OpenCV

You can download OpenCV by pip command using Command Line Prompt.

Before moving to step 1, ensure that your latest version of Python is installed on your computer.

Step 1: Type “pip install OpenCV-python”.

It will take some time as it had to install several packages and components. You can see the progress of installation as well.

Step 2: Verify that whether OpenCV is successfully installed or not.

Conclusion

Now you have understood the basic concepts of multiple Deep Learning libraries, their working advantages, and how to install. Now if someone asks you the question that which Deep Learning library is most beneficial? What you will answer? According to me, TensorFlow holds the charge for being on top of all Deep Learning libraries. There are several Deep Learning algorithms that are used to create magnificent products such as:

  • K-nearest Neighbors
  • Hierarchal clustering
  • K-means clustering
  • Recurrent Neural Networks
  • Artificial Neural Network
  • Convolutional Neural Network
  • Linear regression
  • Logical regression