Caffe2 - Installation



Presently, that you have got enough insights on the capabilities of Caffe2, it is time to experiment Caffe2 on your own. To utilize the pre-trained models or to build up your models in your own Python code, you should first install Caffe2 on your machine.

On the installation page of Caffe2 site which is accessible at the link https://caffe2.ai/docs/getting-started.html you would see the following to select your platform and install type.

installation_page

As you can see in the above screenshot, Caffe2 supports a few famous popular platforms including the mobile ones.

Presently, we shall understand the steps for MacOS installation on which all the projects in this study notes are tested.

MacOS Installation

The installation can be of four types as given below −

  • Pre-Built Binaries
  • Build From Source
  • Docker Images
  • Cloud

Depending upon your preference, select any of the above as your installation type. The guidelines given here are as per the Caffe2 installation site for pre-built binaries. It utilizes Anaconda for Jupyter environment. Execute the following command on your console prompt

pip install torch_nightly -f 
https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html

In addition to the above, you will require a few third-party libraries, which are installed utilizing the following commands −

conda install -c anaconda setuptools
conda install -c conda-forge graphviz  
conda install -c conda-forge hypothesis
conda install -c conda-forge ipython
conda install -c conda-forge jupyter
conda install -c conda-forge matplotlib
conda install -c anaconda notebook
conda install -c anaconda pydot
conda install -c conda-forge python-nvd3
conda install -c anaconda pyyaml
conda install -c anaconda requests
conda install -c anaconda scikit-image
conda install -c anaconda scipy

Some of the study notes in the Caffe2 website also require the installation of zeromq, which is installed utilizing the following command −

conda install -c anaconda zeromq

Windows/Linux Installation

Execute the following command on your console prompt −

conda install -c pytorch pytorch-nightly-cpu

As you must have noticed, you would require Anaconda to utilize the above installation. You will need to install the additional packages as determined in the MacOS installation.

Testing Installation

To test your installation, a small Python script is given below, which you can cut and paste in your Juypter project and execute.

from caffe2.python import workspace
import numpy as np
print ("Creating random data")
data = np.random.rand(3, 2)
print(data)
print ("Adding data to workspace ...")
workspace.FeedBlob("mydata", data)
print ("Retrieving data from workspace")
mydata = workspace.FetchBlob("mydata")
print(mydata)

When you execute the above code, you should see the following output −

Creating random data
[[0.06152718 0.86448082]
[0.36409966 0.52786113]
[0.65780886 0.67101053]]
Adding data to workspace ...
Retrieving data from workspace
[[0.06152718 0.86448082]
[0.36409966 0.52786113]
[0.65780886 0.67101053]]

The screenshot of the installation test page is appeared here for your quick reference −

testing_installation

Presently, that you have installed Caffe2 on your machine, continue to install the study notes applications.

Study Notes Installation

Download the study notes source using the following command on your console −

git clone --recursive https://github.com/caffe2/tutorials caffe2_tutorials

After the download is finished, you will discover several Python projects in the caffe2_tutorials folder in your installation directory. The screenshot of this folder is given for your fast perusal.

/Users/yourusername/caffe2_tutorials

tutorial_installation

You can open some of these study notes to see what the Caffe2 code looks like. The next two projects described in this study notes are largely based on the samples appeared above.

It is now time to do some Python coding of our own. Let us understand, how to utilize a pre-trained model from Caffe2. Later, you will learn to create your own trivial neural network for training on your own dataset.





Input your Topic Name and press Enter.