Install OpenCV3 into virtualenv on Mac

This post introduces how to install OpenCV3 into a virtualenv on Mac.

If you have not setup virtualenv on your mac, check my post here to do that before you proceed the tutorial in this post.

Let us get started.

Step 1: Activate your virtualenv in your terminal

for example:

$ source ~/ipy-jupyter-venv3/bin/activate  

(ipy-jupyter-venv3)$  # Your prompt should change

Step 2: Install OpenCV (modules) according to your needs

  1. If you need only main modules, in your activated virtualenv in your terminal, run the following
(ipy-jupyter-venv3)$ pip3 install opencv-python 

2. If you need both main and contrib modules (check extra modules listing from OpenCV documentation),  in your activated virtualenv in your terminal, run the following

(ipy-jupyter-venv3)$ pip3 install opencv-contrib-python 

Step 3: Test whether openCV is installed correctly

To test whether OpenCV installed correctly into your virtualenv, in your terminal type in those command below in bold.

(ipy-jupyter-venv3) liping$ python3.6

Python 3.6.5 (default, Mar 30 2018, 06:41:53)

[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin

Type “help”, “copyright”, “credits” or “license” for more information.

>>> import cv2

>>> cv2.__version__

‘3.4.3’

>>

Step 4 (optional): To uninstall opencv inside virtualenv, following the command below according to how you installed it.

(ipy-jupyter-venv3) Liping:~$ pip3 uninstall opencv-python 

or

(ipy-jupyter-venv3) Liping:~$ pip3 uninstall opencv-contrib-python

 

Summary:

In this post, you learned how to install and uninstall OpenCV into your virtualenv.

Setting up Python and Jupyter Notebook on Mac

This page provides the instructions for how to install and run IPython and  Jupyter Notebook in a virtualenv  on Mac.

Most probably your Mac has already come with Python installed (see step 1 and step 2 below to check whether Python and Python 3 is installed on your mac, because my Mac book air has both Python and Python3.6 installed, I will go ahead to step 3 to install virtualenv).  

Open a terminal on your mac and type in Python and Python3 to see whether Python and Python3 installed on your mac. See below for example.

Follow the steps below for detailed instructions.

Step 1: Check whether Python is installed on your Mac

To test whether your Mac has Python 2.x or 3.x, look at the instructions below.

Open a terminal  (in your launchpad, type in terminal, and then click and open it.)

Type in python, if you see similar thing as the pic shown below, it means python 2.7 was installed on your Mac

type exit() to exit from the Python 2.7 environment

Type python3 in your terminal, if you see similar warning as the pic shown below, it means you will need to install Python 3 (see the steps below for installation). If you see older version (< 3.5) of python 3 installed on your mac, follow step 2 to install Python 3.5

Step 2: Install Python 3.5

I recommend you download Python 3.5.4 from here, if you do not have Python 3.x pre-installed on your mac.

https://www.python.org/downloads/mac-osx/ 

Click the installer and install it following the wizard.

When you finish the installation, type python3 in your terminal, if you see similar stuff in the pic below, Python 3.5 is installed successfully and you are ready to proceed to step 3.

Step 3: Install virtualenv 

Note: the command text in blue is the exact command you should type into your terminal, the same through this whole instruction.

Lipings-MacBook-Pro:~ Liping$ pip3 install virtualenv

Step 4: Setup virtualenv environment

Issue the command below to set up a virtualenv environment that we will use later, using the vitualenvwe installed in step 3 above.

$ virtualenv --system-site-packages -p python3 ~/ipy-jupyter-venv3

Step 5: Install IPython

Before installing IPython and Jupyter Notebook, be sure to activate your python virtual environment first.

$ source ~/ipy-jupyter-venv3/bin/activate 
(ipy-jupyter-venv3)$  # Your prompt should change

Type the following command to install IPython

(ipy-jupyter-venv3) liping:~pip3 install ipython

Step 6: Install Jupyter Notebook

Use the following command to install Jupyter Notebook

(ipy-jupyter-venv3) liping:~$ pip3 install jupyter

Step 7: Test jupypter notebook installation

(ipy-jupyter-venv3) liping:~$ which python3

/Users/Liping/ipy-jupyter-venv3/bin/python3
#for python 3.x
(ipy-jupyter-venv3) liping:~$ which ipython3

/Users/Liping/ipy-jupyter-venv3/bin/ipython3
#for python 3.x
(ipy-jupyter-venv3) liping:~$ which jupyter-notebook

/Users/Liping/ipy-jupyter-venv3/bin/jupyter-notebook

Step 8: Add Kernel

The Jupyter Notebook and other frontends automatically ensure that the IPython kernel is available. However, if you want to use a kernel with a different version of Python, or in a virtualenv environment, you’ll need to install that manually. 

We are using virutalenv, so we need to install IPython kernel in the virtualenv we created in Step 4 above.

(ipy-jupyter-venv3) liping:~$  python3 -m ipykernel install --user --name myipy_jupter_env3 --display-name "ipy-jupyter-venv3"

Installed kernelspec myipy_jupter_env3 in /Users/Liping/Library/Jupyter/kernels/myipy_jupter_env3

 

Step 9: Before we are running our jupyter Notebook, let us create a folder from which we will start our jupyter notebook.

The following command will change our directory to Desktop (cd refers to change directory)

(ipy-jupyter-venv3) Lipings-MacBook-Pro:~ Liping$ cd Desktop 

the following command will help us create a folder named Geog597_ML_session under our Desktop folder

(ipy-jupyter-venv3) Lipings-MacBook-Pro:Desktop Liping$ mkdir test_jupyter

the following command will change our directory into the folder we just created.

(ipy-jupyter-venv3) Lipings-MacBook-Pro:Desktop Liping$ cd test_jupyter/

(ipy-jupyter-venv3) Lipings-MacBook-Pro:test_jupyter Liping$ 

Step 10: Run Jupyter Notebook

(ipy-jupyter-venv3) Lipings-MacBook-Pro:test_jupyter Liping$ jupyter-notebook

Then your default browser should automatically open a web page similar to the one shown below.

If the web page does not show up automatically, just type localhost:8888 into your browser, you should see the page shown above.

 

Step 11: Testing and Using Jupyter Notebook

By this point you should have Jupyter Notebook running, and you should be connected to it using a web browser. Jupyter Notebook is very powerful and has many features. Below I will outline a few of the basic features to get you started using the notebook. Automatically, Jupyter Notebook will show all of the files and folders in the directory it is run from (for our case, it is empty now, because we have not put anything in that folder Desktop/test_jupyter we just created in step 9 ).

To create a new notebook file, select New > ipy-jupyter-venv3 from the top right pull-down menu (Note: this is the so called kernel we installed in Step 8 above):

This will open a notebook. We can now run Python code in the cell or change the cell to markdown (markdown is for note, not for code). For example, change the first cell to accept Markdown by clicking Cell > Cell Type > Markdown from the top navigation bar, or by click the Markdown shown in the pic below. We can now write notes using Markdown,  for example, type the following into the cell after changing it to markdown:

# Testing

Hit Ctrl + Enter, you will see it the text changed into heading style.

          Then insert one cell by the menu shown below (insert Cell Below).

type in the following

a = 5
b = 10
print (a+b)

and then hit Ctrl + Enter.  You should see the following.

Step 12: Stop jupyter Notebook.

To stop the Jupyter Notebook process, press CTRL+C,

type Y, and hit ENTER to confirm. The following will be displayed:

Step 13: Exit virtualenv environment

Once you are done, remember to exit your virtualenv using the following command:

(ipy-jupyter-venv3) Lipings-MacBook-Pro:test_jupyter Liping$

deactivate

Note that your prompt changes back (see the pic below)

Step 14: Instructions for using Jupyter Notebook next time

Next time when you need to use you jupyther notebook, following the steps summarized below.

Lipings-MacBook-Pro:~ Liping$ source ~/ipy-jupyter-venv3/bin/activate  

(ipy-jupyter-venv3)$  # Your prompt should change

Change directory to where you would like to start your jupyter-notebook

(ipy-jupyter-venv3) Lipings-MacBook-Pro:~ Liping$ cd Desktop/test_jupyter

(ipy-jupyter-venv3) Lipings-MacBook-Pro:test_jupyter Liping$ 

Run jupyter notebook:

(ipy-jupyter-venv3) Lipings-MacBook-Pro:test_jupyter Liping$ jupyter-notebook

 

Congratulations, you have now set up ipython and jupyter notebook on your mac!

Timing how long a python script runs

This post introduces several ways to find out how long a python script takes to complete its execution.

  • If you are using Linux or Mac OS, in your terminal
$ time ./your_script.py
  • Several ways to do the task by adding a few lines of code in your py script.
import time
startTime = time.time()

your_func() 

#python3: 
print ("It took", time.time() - startTime, "seconds.")

See the following for an example in python 3. 

import time
import functools

startTime = time.time()

print(functools.reduce(lambda x,y: x+y, [47,11,42,13]))

#python3:
print ("It took", time.time() - startTime, "seconds.")

Another way to do the same thing:

from datetime import datetime
startTime = datetime.now()

#do something

#Python 2: 
print datetime.now() - startTime 

#Python 3: 
print(datetime.now() - startTime)

One more way to do the same thing with a nicely formatted output.

import sys
import timeit

startTime = timeit.default_timer()

#do some nice things...

stopTime = timeit.default_timer()
totalRunningTime = stopTime - startTime

# output running time in a nice format.
mins, secs = divmod(totalRunningTime, 60)
hours, mins = divmod(mins, 60)

sys.stdout.write("Total running time: %d:%d:%d.\n" % (hours, mins, secs))

If you want to compare two blocks of code / functions quickly you can do the following:

import timeit

startTime = timeit.default_timer()
your_func1()
#python3
print(timeit.default_timer() - startTime)

startTime2 = timeit.default_timer()
your_func2()
#python3
print(timeit.default_timer() - starTime2)

[django] Sending matplotlib generated figure(s) to django web app

This post introduces how to serve the figures generated by matplotlib to django web app without saving on the server.

In your django views py file, import the following libraries

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

from io import BytesIO
import base64

Note: the two lines of code above in blue need to be placed at the very beginning of the py script; otherwise, you would meet the following error: _tkinter.TclError: no display name and no $DISPLAY environment variable.

In the views py file, in the function that you defined to pass the image data to the front end template file, add the following code:

buf = BytesIO()
plt.savefig(buf, format='png', dpi=300)
image_base64 = base64.b64encode(buf.getvalue()).decode('utf-8').replace('\n', '')
buf.close()

Note: in the function that you defined to pass the image data to the front end template file in your views py file, remember to send the value of the variable image_base64 via, for example, json.

Now, in your front end template file, you can add the following image tag.

<img src="data:image/png;base64,{{image_base64}}" alt="some text to display to your users when the image does not show correctly" width=500 height=auto />

You now should be able to see the figure displayed on your web app page:)

For more details about using data url to pass image data to front end html file, check here (pdf).

 

 

Tips about Jupyter Notebook

This post provides some tips about Jupyter Notebook.

  • When you open a pdf file in a browser via your running Jupyter Notebook.

You may encounter some error similar to this “Error! (file) is not UTF-8 encoded”,

the solution:

check the address in your browser, for example, for the following case,

http://localhost:8000/edit/confusionmatrix.pdf

if I change the edit in the url address given above to  view

http://localhost:8000/view/confusionmatrix.pdf

You will see your beautiful fig or document in pdf format.

  • Another similar situation:

When you open a html file in your browser via your running Jupyter Notebook, and you would like to edit the html file, instead of seeing its display,

the solution:

change view to edit in the url address of the file, see the following for an example, then you should be able to edit your html file.

http://localhost:8000/view/index.html 

to

http://localhost:8000/edit/index.html

Happy Jupyter Notebooking!

Dynamic GPU usage monitoring (CUDA)

To dynamically  monitor NVIDIA GPU usage, here I introduce two methods:

method 1: use nvidia-smi

in your terminal, issue the following command:

$ watch -n 1 nvidia-smi

It will continually update the gpu usage info (every second, you can change the 1 to 2 or the time interval you want the usage info to be updated).

method 2: use the open source monitoring program glances with its GPU monitoring plugin

in your terminal, issue the following command to install glances with its GPU monitoring plugin

$ sudo pip install glances[gpu]

to launch it, in your terminal, issue the following command:

 $ sudo glances

Then you should see your GPU usage etc. It also monitors the CPU, disk IO, disk space, network, and a few other things

For more commonly used Linux commands, check my other posts at here  and here .

Resources about Azure Batch AI

This post provides some resources about Microsoft Auzre Batch AI.

Introduction:

Batch AI is a managed service that enables data scientists and AI researchers to train AI and other machine learning models on clusters of Azure virtual machines, including VMs with GPU support. You describe the requirements of your job, where to find the inputs and store the outputs, and Batch AI handles the rest.

Hands-on tutorials:

Videos:

 

Run Jupyter notebook from terminal with tmux

This post will walk you through how to run a Jupyter notebook script from terminal with tmux (check here for my post about tmux usage).

When you are running Jupyter on a remote server or on cluster/ cloud resources, there are situations where you would like the Jupyter on the remote server or cluster continue running without termination when you shut down your laptop or desktop that you used to access the remote server. tmux will help with this.

In this post, we cover how to let your jupyter notebook running on a remote server continue running without termination via tmux.

Step 1: connect to your remote server with port forwarding

check the Step 5-2 in my post here about setting up Jupyter notebook for how to access your remote server with port forwarding, if you are not familiar with it.

Step 2: install tmux 

 check here for my post about tmux installation and usage

Step 3: install runipy  python package

Check here for runipy installation and usage.

Step 4: in your terminal type the following command, then it will go into tmux window

$ tmux

Step 5: Start jupyter notebook within your tmux session with the following command

$ jupyter notebook --no-browser

The –no-browser option prevents Jupyter from automatically opening a browser window.

Let this terminal stay running.

Step 6: from your laptop, ssh to your remote server (does not need port forwarding this time)

Step 7: cd to where the jupyter notebook script located that you would like to run from terminal

If you do not know what does cd mean and do, check my post for a list of commonly used Linux commands.

Step 8:  use the following command to run your ipynb script (this will save the output of each cell back to the notebook file)

$ runipy -o MyNotebook.ipynb 

To save the notebook output as a new notebook, run:

$ runipy MyNotebook.ipynb OutputNotebook.ipynb

If your ipynb script without any error itself, it should be running on the server now.

Step 9:  Things to pay attention to:

Do not close the terminal where you run the ipynb script within tmux session on your computer that you used to connect to the remote server, that will cause the termination of running the ipynb. But you can make your laptop in sleep or even shut down the computer, the tmux session will keep the ipynb running on your remote server and save the output in the ipynb.

References:

https://www.datasciencebytes.com/bytes/2015/12/18/using-jupyter-notebooks-securely-on-remote-linux-machines/ 

http://forums.fast.ai/t/ipython-notebook-on-a-remote-server-with-tmux/10044/2

Checking from command line if Jupyter server is running and kill if needed

This post provides instructions on how to check whether a Jupyter server is running from command line and kill if needed.

Normally, you can kill a Jupyter server from the same terminal window where you launched your Jupyter notebook by hit CTRL + C, then type yes, to shut down the kernels of Your jupyter notebook.

But, there are situations where you want to know whether a Jupyter-notebook running on your remote server, but the Jupyter notebook was started on another desktop (e.g., your office desktop), (and now you are working at home from your laptop, and want to check whether the notebook is still running).

After you login to your Server where you Jupyter notebook was installed and running, you can use the following command to list runing notebooks.

$ jupyter notebook list

You will see a list of running notebooks in the terminal, if you have several running ones.

You can use the following command to kill specific notebook (identified by the port it runs the jupyter) that you would like to stop.

$ jupyter notebook stop 8888

P.S.:

Each server should start on a new port. jupyter notebook list is reading a set of data files – each notebook server you run writes a file when it starts up, and attempts to remove it when it shuts down. If you see different listed servers on the same port, that means some of them exited without successful removal of the file when it created (for example, unexpected shut down of the notebook would cause this happens).

 

References:

https://github.com/jupyter/notebook/issues/1950

https://github.com/jupyter/notebook/issues/2844

 

 

Run Jupyter Notebook script from terminal

Normally people run jupyter notebook via browser, but in some situation, we will need to run it from terminal, for example, when running the script takes long time.

This post introduces how to run a jupyter notebook script from terminal.

Solution  I:

runipy can do this. runipy will run all cells in a notebook. If an error occurs, the process will stop.

  • Install  runipy package
$ pip3 install runipy # for python 3.x 
$ pip install runipy  # for python 2.x
  • runipy command-line usages

 

  • To run a .ipynb file as a script, run:
$ runipy MyNotebook.ipynb
  • To save the output of each cell back to the notebook file, run:
$ runipy -o MyNotebook.ipynb
  • To save the notebook output as a new notebook, run:
$ runipy MyNotebook.ipynb OutputNotebook.ipynb
  • To run a .ipynb file and generate an HTML report, run:
$ runipy MyNotebook.ipynb --html report.html

 

Solution  II:

The latest versions of jupyter comes with  the nbconvert command tool for notebook conversion allows us  to do this without any extra packages.

Just go to your terminal and type:

$ jupyter nbconvert --to notebook --execute mynotebook.ipynb --output mynotebook.ipynb

This will open the notebook, execute it, capture new output, and save the result in mynotebook.nbconvert.ipynb. By default, nbconvert will abort conversion if any exceptions occur during execution of a cell. If you specify --allow-errors (in addition to the --execute flag) then conversion will continue and the output from any exception will be included in the cell output.

if you meet this error,

raise exception(“Cell execution timed out”)

$ jupyter nbconvert --to notebook --execute --allow-errors --ExecutePreprocessor.timeout=180 mynotebook.ipynb 

You can use the –inplace flag as well:

$ jupyter nbconvert --to notebook --execute --inplace mynotebook.ipynb

 

check here for more (updated) usages about nbconvert jupyter command tool.

 

References:

https://pypi.python.org/pypi/runipy

http://nbconvert.readthedocs.io/en/latest/usage.html#convert-notebook 

Can I run Jupyter notebook cells in commandline?