Tutorial Slides PDF
Tutorial Slides PDF
June 2012
Matt Leotta
Amitha Perera
Patrick Reynolds
Eran Swears
Yong Zhao
Varun Ganapathi
(Kitware / Google)
June 2012
1 / 201
This presentation is copyrighted by Kitware, Inc. distributed under the Creative Commons by Attribution License 3.0 http://creativecommons.org/licenses/by/3.0 Source code used in this presentation and in accompanying examples and exercises is distributed under the Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 Approved for Public Release, Distribution Unlimited
Introduce Python for scientic computing. Explain how to access functionality in Python from third party vision libraries (OpenCV, ITK). Cover some advanced computing topics using Python:
parallel processing with threads GPU processing with PyCUDA and PyOpenCL calling C/C++ code from Python calling legacy MATLAB code from Python
Provided examples and hands-on exercises for all of the above. Rae of an ASUS Transformer Pad and more.
(Kitware / Google) Python for MATLAB Users June 2012 3 / 201
Schedule (Approximate)
8:30 9:00 10:0010:30 10:30 11:15 12:301:30 1:30 2:15 3:003:30 3:30 4:15 4:45 5:00 Overview, Virtual Machine Setup Intro to Python Coee Break Python Scripting Scientic Python Numpy, Matplotlib, SciPy Lunch Using OpenCV Using Simple ITK Coee Break Parallel Processing / GPU Processing Interfacing Python with C/C++ Interfacing Python with MATLAB Wrap-up, Rae
(Kitware / Google)
June 2012
4 / 201
Get a USB Memory Stick Install VirtualBox from it Import the virtual machine le Boot the virtual machine Get familiar with the environment and directories
(Kitware / Google)
June 2012
6 / 201
Media Content
Directories and Files
VirtualBoxInstallers
VirtualBox-4.1.16-78094-Linux amd64.run (64-bit Linux) VirtualBox-4.1.16-78094-Linux x86.run (32-bit Linux) VirtualBox-4.1.16-78094-OSX.dmg (Mac) VirtualBox-4.1.16-78094-Win.exe (Windows) LinuxPackages
RPM (Fedora 1417, Mandriva 20102011, RHEL/CentOS 46, SUSE 1011, OpenSUSE 11.311.4) DEB (Debian 57, Ubuntu 8.04, 10.0412.04)
VirtualMachine
matlab-to-python.ova
https://www.virtualbox.org/wiki/Downloads (Kitware / Google) Python for MATLAB Users June 2012 7 / 201
Install VirtualBox
(Kitware / Google)
June 2012
8 / 201
(Kitware / Google)
June 2012
9 / 201
To Be Continued...
Sharing code and data is often overlooked but is as important as sharing algorithms and results in published scientic research. Shared code promotes reproducible research and scientic validation. Computer vision research code is commonly written in MATLAB. Sharing research code written in MATLAB is a good rst step, but . . . MATLAB code can only be executed by a privileged few who can aord MATLAB license fees.
(Kitware / Google)
June 2012
12 / 201
(Kitware / Google)
June 2012
13 / 201
MATLAB Pricing
MATLAB Component Inidvidual Matlab License MATLAB Compiler Toolboxes Computer Vision System Image Processing Optimization Parallel Computing Statistics Neural Network Signal Processing Symbolic Math Total Pricing as of April 12th, 2012.
More Info: http://www.mathworks.com/store/productIndexLink.do (Kitware / Google) Python for MATLAB Users June 2012 14 / 201
Price $2,150 $5,000 $1,350 $1,000 $1,000 $1,000 $1,000 $1,000 $1,000 $1,000 $15,500 per user
These options solve the cost and license restrictions, but have the same technical limitations MATLAB. None of these options are perfectly compatible with MATLAB code. Each has a relatively small user community.
(Kitware / Google) Python for MATLAB Users June 2012 15 / 201
Why Python?
Cost / License
Python and related packages are free (have no licensing fee) Python and related packages use permissive BSD-like licenses.
Language design
Python has well-designed language features supporting small and large code bases Python code is highly readable
IDE Anjuta Eric Python IDE Geany IDLE Komodo IDE MonoDevelop NetBeans (7.0 and up will no longer support Python) Ninja_IDE PIDA PyCharm PyDev PyScripter Python Tools for Visual Studio Spyder Stani's Python Editor Wing wxGlade
Developer Naba Kumar Detlev Offenbach Team Guido van Rossum et al. ActiveState Novell and the Mono community Oracle Team Team JetBrains Aptana Microsoft Pierre Raybaut et al. Stani Wingware Alberto Griggio
Latest stable release version 3.0.3.0 4.4.18 and 5.1.6 0.20 3.2 6.0.0 2.4.2 6.9 2.0-Beta3 0.6.2 2.0.2 2.4.0 2.4.3 1.1 2.1.9 0.8.4h 4.1.3-1 0.6.3
Latest stable release date 2011-06-10 2011-10-02 2011-01-07 2011-05-13 2010-10-07 2011-01-19 2010-06-15 2010-08-04 2012-02-07 2012-02-01 2011-09-20 2012-02-10 2012-03-31 2008-02-14 2012-01-11 2008-02-02
Platform Unix-like Independent Independent Independent Cross-platform Cross-platform Cross-platform Independent Cross-platform Cross-platform Eclipse (Cross-platform) Windows Windows Independent Independent Linux/Windows/OS X Independent
Toolkit GTK+ Qt GTK2 Tkinter Mozilla platform Gtk# Swing Qt PyGTK Swing SWT
License GPL GPL GPL Open source Proprietary LGPL GPL GPL GPL Proprietary EPL MIT Licence Apache License 2.0
There are several choices for Python development environments. The above list is from Wikipedia (taken April 2012). Spyder is most like the MATLAB IDE.
http://en.wikipedia.org/wiki/List_of_integrated_devel...for_Python (Kitware / Google) Python for MATLAB Users June 2012 17 / 201
The original IDE, bundled with most Python installations. IDLE is simple and not very intuitive to use.
More info: http://docs.python.org/library/idle.html (Kitware / Google) Python for MATLAB Users June 2012 18 / 201
IPython Qt Console allows for inline plotting. We will use IPython Qt Console to learn the basics of Python.
More info: http://ipython.org/ (Kitware / Google) Python for MATLAB Users June 2012 19 / 201
IPython Notebook lets you keep a math notebook in a web browser. SimPy adds symbolic math for a Maple or Mathematica alternative. Run ipython notebook --profile=sympy
More info: http://ipython.org/ and http://sympy.org/ (Kitware / Google) Python for MATLAB Users June 2012 20 / 201
Spyder provides an IDE similar to MATLAB. We will use Spyder later in this tutorial.
More info: http://code.google.com/p/spyderlib/ (Kitware / Google) Python for MATLAB Users June 2012 21 / 201
Select the matlab-to-python VM. Press the Start button. Wait for the OS to boot. You will be automatically logged in as username tutorial password tutorial
(Kitware / Google)
June 2012
23 / 201
The bar on the left is the App launcher. Some Apps have additional options when you right click. Drop down menus are on the top of the screen (as in MacOS X).
(Kitware / Google)
June 2012
24 / 201
Key Applications
Key Applications
Firefox Spyder IPython Terminal A web browser. A MATLAB-like Python IDE. An enhanced interactive Python shell. The Gnome terminal emulator.
(Kitware / Google)
June 2012
25 / 201
Introduction to Python
Python Interpreter
(Kitware / Google) Python for MATLAB Users
IPython Interpreter
June 2012 27 / 201
(Kitware / Google)
June 2012
28 / 201
Basic Math
Try typing commands directly into the IPython interpreter.
In [1]: 2 + 2 Out[1]: 4 In [2]: 2.0 + 2.0 Out[2]: 4.0 In [3]: 5.0 / 2 Out[3]: 2.5 In [4]: 5 / 2 Out[4]: 2 In [5]: 5.0 // 2.0 Out[5]: 2.0 In [6]: 5 % 2 Out[6]: 1 In [7]: 3 ** 2 Out[7]: 9 In [8]: _ + 2 Out[8]: 11 # This is a comment
More Examples: http://docs.python.org/tutorial/introduction.html (Kitware / Google) Python for MATLAB Users June 2012 29 / 201
Special Types
bool - Boolean with values False or True. None - A null object, more about this later.
Container Types
str - A string of characters. tuple - A xed sequence of objects. list - A mutable sequence of objects. dict - A dictionary mapping one set of objects to another. Plus various lesser used containers.
Type Conversions
Types are easily converted using the syntax type(object) Type conversion even works to and from strings Some type conversions (e.g. complex to oat) are not well dened and throw an exception.
In [9]: float (7) Out[9]: 7.0 In [10]: int (3.14159) Out[10]: 3 In [11]: str (28) Out[11]: 28 In [12]: float ( 5.5 ) Out[12]: 5.5 In [13]: complex (4.5) Out[13]: (4.5+0 j ) In [14]: bool (0) Out[14]: False In [15]: float ( True ) Out[15]: 1.0 More info: http://docs.python.org/library/stdtypes.html (Kitware / Google) Python for MATLAB Users June 2012 31 / 201
Assignment
Variables are created during assignment
In [16]: my_var -----------------------------------------------------------Traceback (most recent call last): File "<ipython console>", line 1, in <module> NameError: name my var is not defined In [17]: my_var = 10 In [18]: my_var Out[18]: 10
Complex Numbers
Python natively supports complex numbers.
Imaginary numbers are written with the j or J sux.
In [24]: 1 j * 1 J Out[24]: ( -1+0 j ) In [25]: complex (3 , 2) Out[25]: (3+2 j ) In [26]: a = ( -1 + 3 j ) * 2 j In [27]: a Out[27]: ( -6 -2 j ) In [28]: a . real Out[28]: -6.0 In [29]: a . imag Out[29]: -2.0 In [30]: abs ( a ) Out[30]: 6 .3 2455 532 033 675 9 In [31]: a . conjugate () Out[31]: ( -6+2 j ) # access the real part # can also create with the complex function
More Examples: http://docs.python.org/tutorial/introduction.html (Kitware / Google) Python for MATLAB Users June 2012 33 / 201
Boolean Operators
Python x or y x and y not x MATLAB x || y x && y ~x Result if x is false, then y, else x (short-circuit) if x is false, then x, else y (short-circuit) if x is false, then True, else False Result if logical(x), then 1, else logical(y) (short-circuit) if logical(x), then logical(y), else 0 (short-circuit) if logical(x), then 0, else 1
Examples
MATLAB > 0.0 || hello 1 > 10 && (5 || 6) 1 > ~ 5 0 More info: http://docs.python.org/library/stdtypes.html (Kitware / Google) Python for MATLAB Users June 2012 34 / 201 Python > 0.0 or hello hello > 10 and (5 or 6) 5 > not 5 False
Comparisons
MATLAB < <= > >= == ~= N/A N/A Python < <= > >= == != is is not Meaning strictly less than less than or equal strictly greater than greater than or equal equal not equal object identity negated object identity
These comparisons always return a Boolean value. Comparison between dierent numeric types casts from int to float to complex as needed. For example 3.0 == 3 is True. Only not equal operators dier in syntax. The concept of object identity is not found in MATLAB.
More info: http://docs.python.org/library/stdtypes.html (Kitware / Google) Python for MATLAB Users June 2012 35 / 201
Object Identity
Every Python object has an identity. The function id(object) returns a unique identifying integer. The operation x is y is equivalent to id(x) == id(y). Assignment allows multiple variables to refer to the same object. For immutable types (e.g. numeric types, strings, tuples) creation of new objects may or may not refer to same-valued exiting object. For mutable type (e.g. list, dict) creation always creates a new object.
In [32]: x , y = 3 , 4.4 In [33]: x == y , x is y , id ( x ) , id ( y ) Out[33]: ( False , False , 30294792 , 32375168) In [34]: x = 2 + 2.4 In [35]: x == y , x is y , id ( x ) , id ( y ) # here x is y could be True or False Out[35]: ( True , False , 37311776 , 32375168) In [36]: x = y In [37]: x == y , x is y , id ( x ) , id ( y ) Out[37]: ( True , True , 32375168 , 32375168) # here x is y must be True
More info: http://docs.python.org/reference/datamodel.html (Kitware / Google) Python for MATLAB Users June 2012 36 / 201
Tuples
Tuples are immutable sequences of objects of any type. Tuple items are comma separated and often enclosed in parentheses. A trailing comma is required to make a tuple of length one.
In [38]: t = 1 , hello , (3.14 , None ) , In [39]: t [0] Out[39]: 1 2.5
In [40]: t [0] = 2 # item assignment not allowed ( immutable ) TypeError: tuple object does not support item assignment In [41]: t [1:3] # access a slice of the objects ( start : end ) Out[41]: ( hello , (3.14 , None )) In [42]: len ( t ) Out[42]: 4 In [43]: 2.5 in t Out[43]: True # the length of the tuple
In [44]: t + (5 ,) # concatenate tuples Out[44]: (1 , hello , (3.14 , None ) , 2.5 , 5) In [45]: (1 , 2) * 3 # concatenate n shallow copies Out[45]: (1 , 2 , 1 , 2 , 1 , 2) More info: http://docs.python.org/library/stdtypes.html (Kitware / Google) Python for MATLAB Users June 2012 37 / 201
Strings
Strings are immutable sequences of characters. Strings are enclosed in single or double quotes. The concatenation and indexing syntax is the same as Tuple. Strings have many built-in functions.
In [46]: s = " Hello World " In [47]: s . upper () Out[47]: HELLO WORLD In [48]: s [3:5] + - * 3 + s [:: -1] Out[48]: lo - - - dlroW olleH In [49]: s . split () Out[49]: [ Hello , World ] In [50]: - . join (( a , b , c )) Out[50]: a - b - c In [51]: s . replace ( " Hello " , " Goodbye " ) Out[51]: Goodbye World # to upper case
# string splitting
# substring replacement
In [52]: " item %05 d has value % g " % (23 , 3.14**2) Out[52]: item 00023 has value 9.8596 More info: http://docs.python.org/library/string.html (Kitware / Google)
June 2012
38 / 201
Lists
Lists are mutable sequences of objects of any type. List items are comma separated and enclosed in square brackets. The concatenation and indexing syntax is the same as Tuple.
In [53]: v = [1 , hello , (3.14 , None ) , In [54]: v [0] = 2 In [55]: v . append (0.5) In [56]: del v [2] 2.5]
In [57]: v Out[57]: [2 , hello , 2.5 , 0.5] In [58]: v . sort () # sort items in place
In [59]: v Out[59]: [0.5 , 2 , 2.5 , hello ] In [60]: v = ([] ,)*3 In [61]: v [0]. append (1) In [62]: v Out[62]: ([1] , [1] , [1]) # a tuple with 3 shallow copies of the same empty list # append to the first list in the tuple # all lists update because they are shallow copies
More info: http://docs.python.org/library/stdtypes.html (Kitware / Google) Python for MATLAB Users June 2012 39 / 201
Dictionaries
Dictionaries are mutable mapping of one object to another. Key:value pairs are comma separated and enclosed in curly braces.
In [63]: d = { a : 1 , b : 2 , c : 3} In [64]: d [ a ] Out[64]: 1 In [65]: d [ d ] = 4 # access a value by key
In [66]: d . keys () # a list of dictionary keys Out[66]: [ a , c , b , d ] In [67]: d . values () # a list of dictionary values Out[67]: [1 , 3 , 2 , 4] In [68]: d . items () # a list of ( key , value ) tuples Out[68]: [( a , 1) , ( c , 3) , ( b , 2) , ( d , 4)]
More info: http://docs.python.org/library/stdtypes.html (Kitware / Google) Python for MATLAB Users June 2012 40 / 201
IPython Commands
IPython adds several interactive capabilities to standard Python.
Tab Completion: start typing and hit the Tab key to get a list of possible completions. Works for variables, functions, paths, etc. History: access previous commands and their results
adds __ and ___ for second and third to last results. use the command history to list the history of commands. use In[n] to access the n-th command string. use Out[n] to access the n-th result.
Documentation: type objectName? to get info about objectName. Note, the standard Python help(objectName) also provides some documentation. Auto Call: lets you write functions as statements. For example, abs x calls abs(x). MATLAB also allows this. IPython provides many more features not covered here.
http://ipython.org/ipython-doc/rel-0.11/interactive/tutorial.html (Kitware / Google) Python for MATLAB Users June 2012 41 / 201
IPython Commands
Examples of using IPython history
In [1]: a = 1 In [2]: a += 1 In 1: 2: 3: [3]: history a = 1 a += 1 _ip . magic ( " history " )
In [4]: a Out[4]: 2 In [5]: In [2] Out[5]: u a += 1\ n In [6]: exec In [2] In [7]: a Out[7]: 3 In [8]: Out [4] + Out [7] Out[8]: 5 # like eval in MATLAB , Python also has eval for expressions
http://ipython.org/ipython-doc/rel-0.11/interactive/tutorial.html (Kitware / Google) Python for MATLAB Users June 2012 42 / 201
Python Scripting
(Kitware / Google)
June 2012
44 / 201
(Kitware / Google)
June 2012
45 / 201
Hello World
The simple way
print " Hello World "
""" Prints the string " Hello World " and exits . """ my_text = " Hello World " def main (): """ The main function . """ print my_text
(Kitware / Google)
June 2012
46 / 201
Call the main function only if this le is run as a script (not imported).
14 15 if __name__ == " __main__ " : main ()
(Kitware / Google)
June 2012
47 / 201
Running a Script
In Spyder run the active script with Menu RunRun, or press F5 Adjust the conguration settings and press the Run button Future runs use the same conguration Change the run conguration with Menu RunCongure, or press F6 Alternatively, in a terminal window (from the home directory) run python Tutorial/Examples/HelloWorld.py
(Kitware / Google)
June 2012
48 / 201
Conditionals
MATLAB if a > 10 a = 10; end Python if a > 10: a = 10
Syntax of basic conditionals is very similar. MATLAB uses keyword end to designate the end of a block. Python uses : to start a block and indentation to designate duration.
These rules apply generally to loops, functions, classes, etc. Indentation is important in Python! Choose any indentation, but consistent indentation denes a block.
MATLAB if a > 10 disp ( a greater than 10 ) elseif a < 5 disp ( a less than 5 ) else disp ( a is , a ) end Python if a > 10: print a greater than 10 elif a < 5: print a less than 5 else : print a is , a
Loops
MATLAB x = 1; while x < 10 x = x * 2; end Python x = 1 while x < 10: x = x * 2
MATLAB and Python while loops are essentially the same. break and continue statements are the same in both languages.
MATLAB X = [5 4 3 2 1]; for v = X disp ( v ) end Python X = [5 , 4 , 3 , 2 , 1] for v in X : print v
MATLAB and Python for loops each iterate over a container. MATLAB uses symbol = while Python uses keyword in.
(Kitware / Google)
June 2012
50 / 201
Generating Ranges
MATLAB has special syntax to generate a range of numbers
first:last first:step:last
3 5
Note that last is inclusive while end is not. In MATLAB first i last, but in Python first i <end. Pythons range function is limited to integer types. NumPy adds the arange function which generalizes to oats.
(Kitware / Google) Python for MATLAB Users June 2012 51 / 201
Dening Functions
MATLAB function [s , d ] = sum_diff (x , y ) % SUM_DIFF Sum and diff of two values . % x is the first value % y is the second value s = x + y; d = x - y; Python def sum_diff (x , y ): """ Sum and diff of two values . x is the first value y is the second value """ return x + y , x - y
Python always returns exactly one object with the return statement.
a standalone or missing return statement returns None. returning multiple comma-separated objects (as above) returns a tuple.
MATLAB > [ v1 , v2 ] = sum_diff (7 , 4) v1 = 11 v2 = 3 > v3 = sum_diff (7 , 4) v3 = 11 Python > v1 , v2 = sum_diff (7 , 4) > v3 = sum_diff (7 , 4) > print " v1 = " , v1 , " , v2 = " , v2 , \ " , v3 = " , v3 v1 = 11 , v2 = 3 , v3 = (11 , 3)
(Kitware / Google)
June 2012
52 / 201
Python optional arguments can be omitted and/or specied as key=value pairs in any order, e.g in the above example my_func(1, 3.14, hi) is the same as my_func(1, opt2=hi)
http://docs.python.org/tutorial/controlflow.html#keyword-arguments (Kitware / Google) Python for MATLAB Users June 2012 53 / 201
MATLAB and Python have dierent strategies for organizing and calling libraries of functions. In MATLAB, importing functions is implicit.
Each function must reside in a le of the same name. When a function is called, the MATLAB path is searched to nd the rst matching function. Dealing with functions in dierent packages with the same name is problematic.
(Kitware / Google)
June 2012
54 / 201
Generally init .py includes statements to import all the sub-packages or modules recursively. So the following usually works, but it imports the whole vision package.
import vision vision . features . harris . detect () vision . tracking . kalman . predict () # executes vision / __init__ . py # call a function in harris . py # call a function in kalman . py
http://docs.python.org/tutorial/modules.html (Kitware / Google) Python for MATLAB Users June 2012 57 / 201
http://docs.python.org/tutorial/modules.html (Kitware / Google) Python for MATLAB Users June 2012 58 / 201
The Python path is set with environment variable PYTHONPATH. Modify sys.path to make temporary changes during execution.
In [9]: import sys In [10]: sys . path . append ( " / home / tutorial " ) In [11]: sys . path Out[11]: [, / usr / bin , / usr / lib / python2 .7 , ... / usr / lib / python2 .7/ dist - packages / IPython / extensions , / home / tutorial ]
http://docs.python.org/tutorial/modules.html (Kitware / Google) Python for MATLAB Users June 2012 59 / 201
Exercise 1
Basic Python
Open the le Exercises/ex1.py Write the missing function sqrt_dict to compute a dictionary mapping the rst n integers to the square roots of their values. Hint: use the sqrt function from the math module. This example uses sys.argv to access a list of command line arguments. Exercises/ex1.py
19 20 21 22 23 24 25 26 27 28 # parse the command line argument # require exactly 1 argument ( argv [0] is the script name ) if len ( sys . argv ) != 2: print " usage : % s number " % sys . argv [0] return # convert the command line argument to an interger num = int ( sys . argv [1]) # call the function values = sqrt_dict ( num )
(Kitware / Google)
June 2012
60 / 201
Exercise 1: Answer
Basic Python
def sqrt_dict ( n ): """ Compute a dictionary mapping the first n integers to square roots . """ d = dict () for i in range ( n ): d [ i ] = math . sqrt ( i ) return d
(Kitware / Google)
June 2012
61 / 201
PyLab is a meta-package that includes NumPy, SciPy, and Matplotlib. The easiest (and most MATLAB-like) way to work with scientic Python is to import everything into the global name space.
from pylab import *
IPython can be started with the --pylab command line option. Spyder starts its standard Python interpreter with these scientic libraries imported, much like PyLab, but not PyLab per se.
More info: http://www.scipy.org/PyLab (Kitware / Google) Python for MATLAB Users June 2012 64 / 201
http://docs.scipy.org/doc/scipy/reference/tutorial/general.html (Kitware / Google) Python for MATLAB Users June 2012 65 / 201
NumPy
Numpy provides a good website for mapping MATLAB syntax into equivalent NumPy syntax: http://www.scipy.org/NumPy_for_Matlab_Users This tutorial will only cover the most common cases.
More info: http://www.scipy.org/NumPy_for_Matlab_Users (Kitware / Google) Python for MATLAB Users June 2012 66 / 201
NumPy Array
Use numpy.array to represent multidimensional arrays.
>>> A = array ([1 , 2 , 3]) >>> B = array ([[1 , 2 , 3] , [4 , 5 , 6]]) # create a one dimensional array from a list
>>> C = array ([ B , B , B , B ]) # create a 3 D array from a list of 2 D arrays >>> A . shape (3 ,) >>> B . shape (2 ,3) >>> C . shape (4 ,2 ,3) >>> A . ndim 1 >>> A . dtype dtype ( int64 ) # the number of dimensions # the size of each dimension
(Kitware / Google)
June 2012
67 / 201
NumPy Array
The function numpy.dot is required for matrix (or tensor) multiplication. This syntax may seem ugly, but makes operations more consistent over arrays with dimensions greater than 2.
(Kitware / Google)
June 2012
68 / 201
NumPy Matrix
Numpy provides a special numpy.matrix subclass of numpy.array. numpy.matrix acts more like a MATLAB matrix.
MATLAB > M = [1 2; 2 1]; > disp ( M * M ) 5 4 4 5 Python > M = matrix ( 1 2; 2 1 ) > print M * M [[5 4] [4 5]]
numpy.matrix can use a MATLAB-style string initializer. Use from numpy.matlib import * to override common array constructors (zeros(), eye(), etc.) with matrix versions. numpy.matrix only handles two dimensional arrays. numpy.array is more consistent and still preferred in most cases.
More info: http://www.scipy.org/NumPy_for_Matlab_Users (Kitware / Google) Python for MATLAB Users June 2012 69 / 201
2. ,
More info: http://docs.scipy.org/doc/numpy/user/basics.types.html (Kitware / Google) Python for MATLAB Users June 2012 70 / 201
More info: http://www.scipy.org/NumPy_for_Matlab_Users (Kitware / Google) Python for MATLAB Users June 2012 71 / 201
More info: http://www.scipy.org/NumPy_for_Matlab_Users (Kitware / Google) Python for MATLAB Users June 2012 72 / 201
More info: http://www.scipy.org/NumPy_for_Matlab_Users (Kitware / Google) Python for MATLAB Users June 2012 73 / 201
The r_ class also provides range generation. If an argument uses slice notation it is expanded to a range r_[first:end:step] is the same as arange(first,end,step)
>>> r_ [5:10:0.5] array ([ 5. , 5.5 , # generates a range from 5 to 10 with step 0.5 6. , 6.5 , 7. , 7.5 , 8. , 8.5 , 9. , 9.5])
When the step is replaced with an imaginary number the slice has a dierent interpretation, first:last:num
>>> r_ [5:10:5 j ] array ([ 5. , # generates 5 samples between 5 and 10 , inclusive 6.25 , 7.5 , 8.75 , 10. ])
More info: http://www.scipy.org/NumPy_for_Matlab_Users (Kitware / Google) Python for MATLAB Users June 2012 74 / 201
More info: http://www.scipy.org/NumPy_for_Matlab_Users (Kitware / Google) Python for MATLAB Users June 2012 75 / 201
Matplotlib
d + v v = p + 2 v + g dt x2 W311 2 = U311 + 81 2
dx =
matplotlib
2
m1 m2 FG = G r2
2
d2
Matplotlib provides plotting capabilities that are similar to MATLAB. Syntax is the same or similar to MATLAB in most cases. Matplotlib has some advantages over MATLAB plots
Handles TeX equation typesetting within plots. Easy export to PDF using vector graphics. Plots often look nicer than MATLAB plots.
More info: http://matplotlib.sourceforge.net/ (Kitware / Google) Python for MATLAB Users June 2012 76 / 201
0.5
1.5
2.0
0.000 40
60
80
140
160
1.01 0.79 0.56 0.34 0.11 -0.11 -0.34 -0.56 -0.79 -1.01 4
More info: http://matplotlib.sourceforge.net/users/screenshots.html (Kitware / Google) Python for MATLAB Users June 2012 77 / 201
Examples/simple plot.py
1 2 3 4 5 6 7 8 9 10 11 from pylab import * t = arange (0.0 , 2.0 , 0.01) s = sin (2 * pi * t ) plot (t , s , linewidth =1.0) xlabel ( time ( s ) ) ylabel ( voltage ( mV ) ) title ( About as simple as it gets , folks ) grid ( True ) show ()
When not using pylab, these functions are found in matplotlib.pyplot. The gure is not displayed until show() is called.
More info: http://matplotlib.sourceforge.net/users/screenshots.html (Kitware / Google) Python for MATLAB Users June 2012 78 / 201
Matplotlib has an interactive plotting mode that updates plots after each command, and show() is not needed. Interactive plotting is enabled with ion() and disabled with ioff(). Interactive mode is enabled automatically in Spyder and ipython --pylab
More info: http://matplotlib.sourceforge.net/users/shell.html (Kitware / Google) Python for MATLAB Users June 2012 79 / 201
Exercise 2
Plotting Lena with NumPy, SciPy, and Matplotlib
1 2 3
>>> from scipy . misc import lena >>> imshow ( lena ()) >>> gray ()
Plot the transpose of Lena using lena().T. Plot 100 random points on top of Lena with x and y between 0 and 512 using rand and scatter. Plot a histogram of Lena with 100 bins using hist. Hint: Flatten the image to 1-D using lena().flatten() Plot Lena with a sub-matrix from (200,200) to (400,400) ipped horizontally. Hint: Create a modied image array using slice notation.
Part 1
(Kitware / Google)
Part 2
Part 3
Python for MATLAB Users
Part 4
Part 5
June 2012 80 / 201
Exercise 2: Answers
Plotting Lena with NumPy, SciPy, and Matplotlib
Exercises/Answers/ex2-answer.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 from scipy . misc import lena figure ( " Part 1 " ) imshow ( lena ()) gray () figure ( " Part 2 " ) imshow ( lena (). T ) gray () figure ( " Part 3 " ) imshow ( lena ()) points = rand (2 , 100) * 512 scatter ( points [0] , points [1]) figure ( " Part 4 " ) hist ( lena (). flatten () , 100) figure ( " Part 5 " ) image = lena () image [200:400 , 200:400] = image [200:400 , 400:200: -1] imshow ( image )
(Kitware / Google)
June 2012
81 / 201
SciPy
Contains many scientic Python modules built on top of NumPy. Most interesting for computer vision work are ndimage Multi-dimensional image processing linalg Linear Algebra (extends numpy.linalg) tpack Fourier Transforms optimize Optimization io File IO (read/write MATLAB les) In this tutorial, we are only going to scratch the surface of what is contained in these libraries with a few examples.
More info: http://www.scipy.org/ (Kitware / Google) Python for MATLAB Users June 2012 82 / 201
SciKits
There are also many add-on toolkits for SciPy, collectively called SciKits Most interesting for computer vision work are scikits-image Image Processing scikits-learn Machine Learning and Data Mining ann Approximate Nearest Neighbor library wrapper mlabwrap Call MATLAB from Python
More info: http://scikits.appspot.com/ (Kitware / Google) Python for MATLAB Users June 2012 83 / 201
SciPy Central
Looking for an equivalent to MATLAB Central for sharing snippets of scientic Python code? Try SciPy Central (scipy-central.org) Its not as extensive as MATLAB Central, but its fairly new.
More info: http://scipy-central.org/ (Kitware / Google) Python for MATLAB Users June 2012 84 / 201
SciPy By Example
Lets work through an example of writting a Canny edge detector. This is not a very ecient implementation of Canny edge detection. The goal is to illustrates some SciPy and Numpy functionality. Open the le Examples/scipy canny.py
(Kitware / Google)
June 2012
85 / 201
First import what we need: NumPy and and NDImage from SciPy. These modules are imported to abreviated names np and ndi. Note that the constant pi (i.e. ) is imported into the global namespace to make math expressions easier to follow. Examples/scipy canny.py
4 5 6 import numpy as np from numpy import pi import scipy . ndimage as ndi
(Kitware / Google)
June 2012
86 / 201
Usually all imports happen at the top of a le, but this is not required. In this example some more things are imported only when this le is run as a script. When run as a script, this code calls canny_edges on the Lena image and displays the result in a matplotlib GUI. When imported into another module, there is no dependency on matplotlib or Lena.
(Kitware / Google) Python for MATLAB Users June 2012 87 / 201
Input Image
Canny Edges
(Kitware / Google)
June 2012
88 / 201
29
Input Image
(Kitware / Google)
30 31
dx Sobel horizontal
(Kitware / Google) Python for MATLAB Users
dy Sobel vertical
June 2012 91 / 201
33 34
Gradient Magnitude
(Kitware / Google) Python for MATLAB Users
Gradient Orientation
June 2012 92 / 201
36
37
Label connected components using 8-neighborhood (3x3 mask of ones). Examples/scipy canny.py
labels , num_labels = ndi . measurements . label ( edge_map , np . ones ((3 , 3)))
39
Remove connected edges not containing a value above the high threshold. Examples/scipy canny.py
40 41 42
for i in range ( num_labels ): if max ( mag [ labels == i ]) < high_thresh : edge_map [ labels == i ] = False
Hysteresis
Python for MATLAB Users
(Kitware / Google)
June 2012
97 / 201
This tutorial will focus on directly using Python interfaces to large open source C++ projects:
OpenCV ITK
Several packages for computer vision are built on top of some combination of NumPy, SciPy, OpenCV, and various other modules SimpleCV http://simplecv.org/ PyVision http://sourceforge.net/projects/pyvision/ PyCVF http://pycvf.sourceforge.net/ PyCam http://code.google.com/p/pycam/ NDVision https://launchpad.net/ndvision
(Kitware / Google)
June 2012
99 / 201
(Kitware / Google)
June 2012
100 / 201
What is OpenCV?
C/C++ library with Python wrappers Open source Very active community Implementations of many computer vision algorithms
(Kitware / Google)
June 2012
101 / 201
Graphical User Interface(GUI) - very easy procedural interface Input/Output - load/write images and movies in a variety of formats Image Processing - ltering,edges,histograms, etc. Machine Learning - SVMs, Decision Trees, Features Object Detection and MORE! (http://docs.opencv.org/)
(Kitware / Google)
June 2012
102 / 201
Two versions of wrappers: cv and cv2 cv2 contains cv cv2 is preferred because it natively uses numpy Examples/opencv rand.py
3 4 import cv2 import numpy as np
(Kitware / Google)
June 2012
103 / 201
Examples/opencv rand.py
1 2 3 4 5 6 7 8 9 10 # !/ usr / bin / python import cv2 import numpy as np
a = np . random . rand (256 , 256) cv2 . imshow ( Random Image , a ) cv2 . waitKey () cv2 . destroyWindow ( Random Image )
Run Examples/opencv_rand.py
(Kitware / Google)
June 2012
104 / 201
Show the array as an image in an OpenCV GUI window. The string is both the window identier and the window title.
8 cv2 . imshow ( Random Image , a )
Wait for the user to press a key. Optionally this function can take a timeout in milliseconds.
9 cv2 . waitKey ()
Close the window and free memory. This is not strictly required in this case because it happens automatically when the script terminates.
10 cv2 . destroyWindow ( Random Image )
(Kitware / Google)
June 2012
105 / 201
(Kitware / Google)
June 2012
106 / 201
(Kitware / Google)
June 2012
107 / 201
(Kitware / Google)
June 2012
108 / 201
(Kitware / Google)
June 2012
110 / 201
(Kitware / Google)
June 2012
111 / 201
(Kitware / Google)
June 2012
112 / 201
Run the Good Features to Track algorithm to nd 50 points. The last two arguments are a threshold and minimum distance between points.
17 features = cv2 . g o o d F e a tu r e s T o T r a ck ( image_gray , 50 , .01 , 50)
The features are a 3D array of shape (50, 1, 2). Reshape it to (50, 2). The -1 means infer from data length.
18 features = features . reshape (( -1 , 2))
(Kitware / Google)
June 2012
113 / 201
Exercise 3
OpenCV
Open the le Exercises/ex3.py. Modify the code to run goodFeaturesToTrack on the movie. Then modify it to run Canny edge detection instead. Exercises/ex3.py
16 17 18 19 20 21 22 23 24 while True : valid_frame , image = movie . read () if not valid_frame : break # Insert code here to process the image before displaying it cv2 . imshow ( Exercise 3: OpenCV , image ) cv2 . waitKey (5)
(Kitware / Google)
June 2012
114 / 201
Exercise 3: Answer
OpenCV
Exercises/Answers/ex3-answer1.py
19 20 21 22 23 24 image_gray = cv2 . cvtColor ( image , cv2 . cv . CV_RGB2GRAY ) features = cv2 . g o o d F e a t ur e s T o T r a c k ( image_gray , 50 , .01 , 50) if features is not None : for x , y in features . reshape (( -1 , 2)): cv2 . circle ( image , (x , y ) , 10 , (0 , 0 , 255)) cv2 . imshow ( Exercise 3: OpenCV , image )
Exercises/Answers/ex3-answer2.py
19 20 21 image_gray = cv2 . cvtColor ( image , cv2 . cv . CV_RGB2GRAY ) edges = cv2 . Canny ( image_gray , 100 , 120) cv2 . imshow ( Exercise 3: OpenCV , edges )
(Kitware / Google)
June 2012
115 / 201
Simple ITK
Introduction to ITK Introduction to SimpleITK Getting SimpleITK Basic Operations Advanced Operations Building Bridges with Other Libraries
(Kitware / Google)
June 2012
116 / 201
What is ITK ?
C++ Library Open Source (Apache 2.0 License) Generic Programming / C++ Templates Image Processing Image Segmentation Image Registration ITKs main foci are segmentation and registration, especially in the context of 3D images.
(Kitware / Google)
June 2012
117 / 201
Funding
(Kitware / Google)
June 2012
118 / 201
History
(Kitware / Google)
June 2012
119 / 201
History
(Kitware / Google)
June 2012
120 / 201
Project Prole
Total lines of code: 1,886,674 Active developers: 56 (past 12 months) Total developers: 146 (full history of the project) Top 2% largest open source teams in the world Estimated cost of development: $34.5 M1
Ohloh: http://www.ohloh.net/p/itk
(Kitware / Google) Python for MATLAB Users June 2012 121 / 201
Funding
First 10 years of development = $15M Refactoring of ITKv4 in 2011 = $5M Yearly maintenance supported by NLM
(Kitware / Google)
June 2012
122 / 201
Simple ITK
SimpleITK provides a template-free layer on top of ITK that is automatically wrapped for Python.
More info: http://simpleitk.org (Kitware / Google) Python for MATLAB Users June 2012 123 / 201
Two main options are building from source with Python wrappings Or easy install (easy install is much easier).
bash ~/ easy_install SimpleITK # It s that easy ( you may have to be root ).
Lets walk through an example of basic Simple ITK usage from Examples/sitk intro.py.
(Kitware / Google)
June 2012
124 / 201
Its very easy to create images using SimpleITK. The images will be two-dimensional or three-dimensional based on the constructor Examples/sitk intro.py
4 5 6 7 8 import SimpleITK as sitk # Create Images image = sitk . Image (256 , 256 , 256 , sitk . sitkInt16 ) twoD = sitk . Image (64 , 64 , sitk . sitkFloat32 )
(Kitware / Google)
June 2012
125 / 201
sitk is the module Image is the constructor for the Image class Height, width, depth (omit depth for 2D images) Datatype (more on this later)
(Kitware / Google)
June 2012
126 / 201
Examples/sitk intro.py
11 12 13 print image . GetPixel (0 , 0 , 0) image . SetPixel (0 , 0 , 0 , 1) print image . GetPixel (0 , 0 , 0)
Get the voxel value at [0,0,0]? Hmm, I dont like it, so set to 1 What is the value at [0,0,0] now?
(Kitware / Google)
June 2012
127 / 201
Examples/sitk intro.py
16 17 18 print image [0 , 0 , 0] image [0 , 0 , 0] = 10 print image [0 , 0 , 0]
Without warning, we sprinkled syntactic sugar on you! image[0,0,0] is shorthand for image.GetPixel(0,0,0) image[0,0,0] = 10 is shorthand for image.SetPixel(0,0,0,10)
(Kitware / Google)
June 2012
128 / 201
Simple ITK supports a wide array of pixel types To get a human readable assessment of the pixel type: Examples/sitk intro.py
21 print image . G e t P i x e l I D T y p e A s S t r i n g ()
(Kitware / Google)
June 2012
129 / 201
Images are created using SimpleITK.Image ( w, h, d, Type ) Images can be 2- or 3-dimensional Images can describe themselves Images have simple pixel accessors
(Kitware / Google)
June 2012
130 / 201
The output of SmoothingRecursiveGaussian is of type oat The input image is signed short Most SimpleITK lters with 2 inputs require the same type We need to cast in order to dierence the input image with the smoothed image
(Kitware / Google)
June 2012
131 / 201
Introducing Cast
(Kitware / Google)
June 2012
132 / 201
Extracting a region of interest from a SimpleITK image is easy with sitk.Extract. Examples/sitk intro.py
31 32 33 size = [64 , 64 , 0] start = [64 , 0 , 0] sitk . Show ( sitk . Extract ( image , size , start ) , " Extracted " )
(Kitware / Google)
June 2012
133 / 201
Now that we can do basic ltering, lets look at more advanced lters How about Otsu Thresholding? Examples/sitk example.py
11 12 13 14 15 16 17 import SimpleITK as sitk image = sitk . ReadImage ( filename ) sitk . Show ( image , Slice ) image = sitk . Cast ( image , sitk . sitkFloat32 ) thresh = sitk . OtsuThreshold ( image , 0 , 230 , 15) sitk . Show ( thresh , Thresholded )
(Kitware / Google)
June 2012
134 / 201
(Kitware / Google)
June 2012
135 / 201
(Kitware / Google)
June 2012
136 / 201
Exercise 4
SimpleITK
Open the le Exercises/ex4.py. Lets take the previous examples and build on them.
1
Use sitk.RecursiveGaussian to smooth the image, then plot the histogram with Matplotlib. Use sitk.Threshold to segment bone from the input image.
(Kitware / Google)
June 2012
137 / 201
Exercise 4: Answer
SimpleITK
Great Job! your code should look something like this Exercises/Answers/ex4-answer.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import SimpleITK as sitk import pylab image = sitk . ReadImage ( filename ) image_array = sitk . GetA rra yFr omI mage ( image ) sitk . Show ( image ) blurred = sitk . R ecur siv eGa uss ian ( image , 4) blurred_array = sitk . Get Arr ayF rom Imag e ( blurred ) # Generate a normal and blurred histogram pylab . figure () pylab . hist ( image_array . flatten ()) pylab . figure () pylab . hist ( blurred_array . flatten ()) pylab . show () # Threshold the image properly thresh = sitk . Threshold ( image , 210 , 255) sitk . Show ( thresh )
(Kitware / Google)
June 2012
138 / 201
(Kitware / Google)
June 2012
139 / 201
Credits
Material taken from the following tutorials: The SimpleITK tutorial from MICCAI 2011 (https: //github.com/SimpleITK/SimpleITK-MICCAI-2011-Tutorial). The ITKv4 next generation tutorial (https://github.com/InsightSoftwareConsortium/ ITKv4-TheNextGeneration-Tutorial).
(Kitware / Google)
June 2012
140 / 201
Multi-Threading in Python
Python supports multi-threading of code but has an important limitation. Only one thread can execute Python code at a time. Multi-thread can still be useful in certain cases such as
running multiple I/O-bound tasks simultaneously code that uses a lot of C-based modules (e.g. NumPy and SciPy)
http://docs.python.org/library/threading.html (Kitware / Google) Python for MATLAB Users June 2012 142 / 201
Multi-Processing in Python
The multiprocessing module provides convenient ways to divide Python execution over multiple processes. Create a pool of worker processes and divide up the work. Examples/multiproc.py
1 2 3 4 5 6 7 8 9 10 from m ul tiprocessing import Pool def f ( x ): return x * x if __name__ == __main__ : pool = Pool ( processes =4) result = pool . apply_async (f , [10]) print result . get ( timeout =1) print pool . map (f , range (10))
# # # #
start 4 worker processes evaluate " f (10)" asynchronously prints "100" unless your computer is slow prints "[0 , 1 , 4 ,... , 81]"
http://docs.python.org/library/multiprocessing.html (Kitware / Google) Python for MATLAB Users June 2012 143 / 201
Modern GPGPU
GPUs have been evolving from dedicating graphics rendering hardware, to powerful general computing devices:
Modern GPGPU
Massive amount of processing cores (3072 CUDA cores in Nvidia GeForce GTX 690 single card) Each core has its own dedicated register le Each cluster of cores share L1 cache Clusters of cores share L2 cache Huge memory throughput (512bit, 384GB/sec for Nvidia GeForce 690) Atomic operations 64-bit precision Error-correcting Code (ECC) memory to meet HPC requirement GPU Vs CPU on HPC performance
Most CV developers: 10 to 500x speed-up depending on the application and implementation Intel: GPUs are ONLY up to 14 times faster than CPU.
http://blogs.nvidia.com/tag/kepler/index.html (Kitware / Google) Python for MATLAB Users June 2012 145 / 201
Cons:
single precision no ow control and branching very limited kernel size lack of cross-kernel synchronization
(Kitware / Google)
June 2012
146 / 201
Enable C/C++ like direct parallel thread programming Enable cross-thread synchronization Enable direct memory access Enable atomic operations Eectively remove the kernel size limit Enable full support for ow control and branching GPUs become truly general purpose
CUDA Programming Model (OpenCL has very similar model)
http://ixbtlabs.com/articles3/video/cuda-1-p5.html (Kitware / Google) Python for MATLAB Users June 2012 147 / 201
Many hardwired graphics rendering resources in GPU, including texture engine, 3D transformation, make GPU a natural t for computer vision and computational photography processing. OpenVIDIA: Parallel GPU Computing Vision Library developed by Nvidia OpenCV: more and more GPU Modules Nvidia CUDA Zone: An Image and Computer Vision GPU library, including segmentation, feature processing, stereo imaging, machine learning and data processing
(Kitware / Google)
June 2012
148 / 201
Play to the strengths of each programming environment Python + CUDA: PyCUDA Python + OpenCL: PyOpenCL
http://mathema.tician.de/software/pycuda (Kitware / Google) Python for MATLAB Users June 2012 149 / 201
(Kitware / Google)
June 2012
150 / 201
PyCUDA work ow
http://mathema.tician.de/software/pycuda (Kitware / Google) Python for MATLAB Users June 2012 151 / 201
PyCUDA Example
import pycuda . driver as cuda import pycuda . autoinit , pycuda . compiler import numpy
a = numpy . random . randn (4 ,4). astype ( numpy . float32 ) # create a 4 x4 array random numbers a_gpu = cuda . mem_alloc ( a . nbytes ) # alloc the same sized array in GPU cuda . memcpy_htod ( a_gpu , a ) # copy the data from host to device mod = pycuda . compiler . SourceModule ( """ # specify a CUDA kernel function __global__ void twice ( float * a ) { int idx = threadIdx . x + threadIdx . y *4; a [ idx ] *= 2; } """ ) func = mod . get_function ( " twice " ) # get the handle of CUDA kernel func ( a_gpu , block =(4 ,4 ,1)) # run the CUDA function with a configuration a_doubled = numpy . empty_like ( a ) cuda . memcpy_dtoh ( a_doubled , a_gpu ) print a_double print a # create an empty buffer on host # copy result from device to host
http://mathema.tician.de/software/pycuda (Kitware / Google) Python for MATLAB Users June 2012 152 / 201
Meant to look and feel just like numpy +, -, *, /, fill, sin, exp, rand, basic indexing, norm, inner product, ... Mixed types (int32 + float32 = float64) print gpuarray for debugging Allows access to raw bits
http://mathema.tician.de/software/pycuda (Kitware / Google) Python for MATLAB Users June 2012 153 / 201
# specify the detail of element - wise operation lin_comb = Ele men twi seK ern el ( " float a , float *x , float b , float *y , float * z " , "z[i] = a*x[i] + b*y[i]") c_gpu = gpuarray . empty_like ( a_gpu ) # create a GPU array of same size lin_comb (5 , a_gpu , 6 , b_gpu , c_gpu ) # run the E lem e ntw ise Ker nel function assert la . norm (( c_gpu - (5* a_gpu +6* b_gpu )). get ()) < 1e -5 print a_gpu print b_gpu print c_gpu
http://mathema.tician.de/software/pycuda (Kitware / Google) Python for MATLAB Users June 2012 154 / 201
# specify the detail of the reduction operation dot = Re ductionKernel ( dtype_out = numpy . float32 , neutral = " 0 " , reduce_expr = " a + b " , map_expr = " x [ i ]* y [ i ] " , arguments = " const float *x , const float * y " ) from pycuda . curandom import rand as curand x = curand ((1000*1000) , dtype = numpy . float32 ) y = curand ((1000*1000) , dtype = numpy . float32 ) x_dot_y = dot (x , y ). get () x_dot_y_cpu = numpy . dot ( x . get () , y . get ()) print print print print x y x_dot_y x_dot_y_cpu
http://mathema.tician.de/software/pycuda (Kitware / Google) Python for MATLAB Users June 2012 155 / 201
http://mathema.tician.de/software/pycuda Complete documentation MIT License (no warranty, free for all use) Requires: numpy, Python 2.4+ (Win/OS X/Linux) Support via mailing list
http://mathema.tician.de/software/pycuda (Kitware / Google) Python for MATLAB Users June 2012 156 / 201
http://mathema.tician.de/software/pyopencl (Kitware / Google) Python for MATLAB Users June 2012 157 / 201
a = numpy . random . rand (50000). astype ( numpy . float32 ) ctx = cl . c r ea t e _ s o m e _ co n t e x t () queue = cl . CommandQueue ( ctx )
# create a buffer on device and copy the data in host buffer to device buffer a_buf = cl . Buffer ( ctx , cl . mem_flags . READ_WRITE , size = a . nbytes ) cl . e n q u e u e _ w r i t e _ b u f f e r ( queue , a_buf , a ) # specify an OpenCL kernel function prg = cl . Program ( ctx , """ __kernel void twice ( __global float * a ) { int gid = get_global_id (0); a [ gid ] *= 2; } """ ). build () # run an OpenCL kernel function prg . twice ( queue , a . shape , None , a_buf ). wait ()
http://mathema.tician.de/software/pyopencl (Kitware / Google) Python for MATLAB Users June 2012 158 / 201
Metaprogramming
GPU code does not need to be a compile-time constant Code is data - it wants to be reasoned about at run time Automated tuning Data types Specialized code for given problem Constants faster than variables Loop unrolling
http://mathema.tician.de/software/pycuda (Kitware / Google) Python for MATLAB Users June 2012 159 / 201
Showcase...
(Kitware / Google)
June 2012
160 / 201
There are several options for wrapping C/C++ code in Python Calling the Python C API Manually (not for the faint of heart) Interfacing with shared libraries using ctypes Using Cython to generate C API calls (good for optimizing critical pieces of code) Using SWIG to generate C API calls (good for wrapping larger libraries) Using Boost.Python to bridge between Python and C++
(Kitware / Google)
June 2012
162 / 201
Calling the C API directly allows you to get the most performance possible and have the greatest control of execution. This is also one of the least easily maintainable methods. For more information, see http://docs.python.org/extending/extending.html. There is a simple example at Examples/c-api-demonstration/cdemo.c
(Kitware / Google)
June 2012
163 / 201
This can be very useful, but doesnt feel as pythonic as some of the other approaches.
(Kitware / Google) Python for MATLAB Users June 2012 164 / 201
Cython
Cython is a tool for compiling Python-like code into C to be compiled into python extension modules. There are sets of annotations that can further optimize code through static typing, removal of bounds checking, etc. An example of a pyx le and generated C le can be found in Examples/cython Lets go through a simple example.
More Info: http://www.cython.org/ (Kitware / Google) Python for MATLAB Users June 2012 165 / 201
Cython
By adding type declarations we can go a great deal faster, even with this simple example.
(Kitware / Google)
June 2012
166 / 201
Cython
This code compiled with Cython is 400% faster, but we lose compatibility with Python through the calls to cdef.
0 1 2 3 4 5 6 7 8 9 10 def f ( double x ): return x **2 - x def integrate_f ( double a , double b , int N ): cdef int i cdef double s , dx s = 0 dx = (b - a )/ N for i in range ( N ): s += f ( a + i * dx ) return s * dx
(Kitware / Google)
June 2012
167 / 201
Cython
This code compiled with Cython is 15,000% faster, but the calls are no able to raise Python errors.
0 1 2 3 4 5 6 7 8 9 10 cdef double f ( double x ) except ? -2: return x **2 - x def integrate_f ( double a , double b , int N ): cdef int i cdef double s , dx s = 0 dx = (b - a )/ N for i in range ( N ): s += f ( a + i * dx ) return s * dx
(Kitware / Google)
June 2012
168 / 201
Cython
(Kitware / Google)
June 2012
169 / 201
Cython
Here is an example of how to compile your cython into a loadable module (taken from http://docs.cython.org/src/reference/compilation.html).
0 1 2 $ cython yourmod . pyx $ gcc - shared - pthread - fPIC - fwrapv - O2 - Wall -fno - strict - aliasing \ -I / usr / include / python2 .7 -o yourmod . so yourmod . c
This should give you an idea of the possibilities for rapid optimization with Cython. See http://cython.org/ for more information.
(Kitware / Google)
June 2012
170 / 201
SWIG
SWIG is a great option for wrapping existing C/C++ code for many dierent languages, including Python An example of swig usage can be found in Examples/swig SWIG is generally used for large projects that need semi-automated wrapping for multiple languages (SimpleITK uses this for its wrapping).
More Info: http://www.swig.org/ (Kitware / Google) Python for MATLAB Users June 2012 171 / 201
SWIG
Here is an example of a C le we want to automatically wrap with SWIG
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 # include < time .h > double My_variable = 3.0; int fact ( int n ) { if ( n <= 1) return 1; else return n * fact (n -1); } int my_mod ( int x , int y ) { return ( x % y ); } char * get_time () { time_t ltime ; time (& ltime ); return ctime (& ltime ); }
(Kitware / Google)
June 2012
172 / 201
SWIG
(Kitware / Google)
June 2012
173 / 201
SWIG
To generate our example wrapping and build example.c itself, we run the following commands.
3 4 5 swig - python example . i gcc -c example . c example_wrap . c -I / usr / include / python2 .7 ld - shared example . o example_wrap . o -o _example . so
Swig generates a shared library and python wrapper for importing it as shown below.
In [9]: import example In [10]: example . fact (10) Out[10]: 3628800
(Kitware / Google)
June 2012
174 / 201
SWIG
This pattern can be used to wrap existing C and C++ libraries in a maintainable way. There is also support for converting between STL types and Python types.
(Kitware / Google)
June 2012
175 / 201
Boost.Python
Boost.Python is an alternative to SWIG.
Only for making Python bindings (not multi-language) Designed to make wrapping of C++ easy. Handles C++ classes, virtual functions, inheritance, etc. Only involves writing C++ code (no interface les)
More Info: http://www.boost.org/doc/libs/1_49_0/libs/python/doc/ (Kitware / Google) Python for MATLAB Users June 2012 176 / 201
(Kitware / Google)
June 2012
178 / 201
OMPC Example
Convert a Simple Matlab function that plots random numbers to Python Examples/plot rand nums ex1.m Convert .m to .pym
In [11]: import ompc In [12]: import p lo t_ r an d _n um s _e x 1 In [13]: p l o t _r a nd _n u ms _ ex 1 (100) 1 2 3 4 5 6 function p l ot _ ra nd _ nu m s_ ex 1 ( maxX ) % Function to plot a vector of random numbers x = 1: maxX ; randx = rand (1 , maxX ); plot (x , randx )
Python > import scipy . io as sio > xx = sio . loadmat ( xx . mat ) > print xx { xx : array ([[ ([[1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11]] , [ u vars1 ] , [[ array ([[15 , 16]] , dtype = uint8 ) , array ([ u var1 ] , dtype = < U4 ) , array ([[17 , 18]] , dtype = uint8 ) , array ([ u var2 ] , dtype = < U4 )]])]] , dtype =[( range , | O4 ) , ( name , | O4 ) , ( cellvalues , | O4 )]) , __version__ : 1.0 , __header__ : MATLAB 5.0 MAT - file , Platform : PCWIN64 , Created on : Tue May 29 12:53:43 2012 , __globals__ : []}
(Kitware / Google)
June 2012
180 / 201
Mlabwrap Example
Call Matlabs Surf() function from Python
1 2 3 4 5 6 7 8 9 10 11 # !/ usr / bin / python # Copyright 2011 , Kitware , Inc . # http :// mlabwrap . sourceforge . net / Examples for calling matlab functions from Python from mlabwrap import mlab ; from numpy import * def main (): Calling Matlab s surf () command xx = arange ( -2* pi , 2* pi , 0.2) # Define range of inputs mlab . surf ( subtract . outer ( sin ( xx ) , cos ( xx ))) # call matlab s surf () function
More Info: http://mlabwrap.sourceforge.net/ (Kitware / Google) Python for MATLAB Users June 2012 181 / 201
Wrap-Up
Rae Prize 1
Rae Prize 2
(Kitware / Google)
June 2012
184 / 201
Send Us Feedback
Thank You!
matt.leotta
yongzhao
varun
@kitware.com
@google.com
(Kitware / Google)
June 2012
185 / 201
Appendix
The virtual machine provides a nice way to try out software without worrying about installation. Eventually youll want to install these toolkit in your native OS. The following slides provide basic installation instructions for
Windows Mac OS X Linux
(Kitware / Google)
June 2012
188 / 201
Installing on Windows
It is dicult to install individual toolkits on Windows. Packages installed independently often do not work together because
package versions may be incompatible with each other packages are compiled with dierent compilers.
Lucky there is a project called Python(x,y) which installs almost everything you need for scientic Python and congures it to work together.
(Kitware / Google)
June 2012
189 / 201
1 2
Download the package installer (currently Python(x,y)-2.7.2.2.exe) Run the installer executable and accept the license agreement
More info: http://code.google.com/p/pythonxy/ (Kitware / Google) Python for MATLAB Users June 2012 190 / 201
More info: http://code.google.com/p/pythonxy/ (Kitware / Google) Python for MATLAB Users June 2012 191 / 201
Select the installation location Select the Start Menu folder Press the Install button.
More info: http://code.google.com/p/pythonxy/ (Kitware / Google) Python for MATLAB Users June 2012 192 / 201
More info: http://code.google.com/p/pythonxy/ (Kitware / Google) Python for MATLAB Users June 2012 193 / 201
More info: http://code.google.com/p/pythonxy/ (Kitware / Google) Python for MATLAB Users June 2012 194 / 201
Installing on OS X
This should get you setup with all the tools shown today in as clean of a way as possible.
(Kitware / Google)
June 2012
195 / 201
Installing on OS X
(Kitware / Google)
June 2012
196 / 201
Installing on OS X
First, we assume you have XCode installed. Next, install homebrew using the instructions here: https://github.com/mxcl/homebrew/wiki/installation. Now that we have homebrew, we will install our binary dependencies
brew brew brew brew install install install install gfortran # for scipy pyqt pyside opencv
(Kitware / Google)
June 2012
197 / 201
Installing on OS X
What is a virtual environment? Python virtual environments let you keep all of your dependencies in one place. (through environment variable magic) To install virtualenv, use the instruction here: http://pypi.python.org/pypi/virtualenv Now create a virtual environment like so:
virtualenv down_with_matlab source down_with_matlab / bin / activate
(Kitware / Google)
June 2012
198 / 201
Installing on OS X
The order and distinction between pip and easy install is important.
easy_install readline pip install sphinx pip install spyder pip install ipython pip install tornado easy_install zmq pip install numpy pip install scipy pip install matplotlib pip install sympy easy_install SimpleITK
Now you can use spyder, ipython, and all of these libraries as long as the virtual environment is activated. Bonus: None of this is cluttering up your systems site-packages.
(Kitware / Google)
June 2012
199 / 201
Installing on Linux
The same virtualenv, pip, and easy install tools used for Mac OS X can be used for Linux, but there is an easier way. Most linux distributions have packages for scientic Python tools that make them easy to install.
Package are (usually) pre-built, so you dont have to wait for them to compile. Dependencies are automatically installed. If you install Spyder, you automatically get Python, NumPy, SciPy, etc.
A few toolkits may not be packaged and will still require pip or easy install.
(Kitware / Google)
June 2012
200 / 201
In Ubuntu, you can use the Ubuntu Software Center and install packages. Equivalently, from the command line you can run
sudo apt - get install spyder
to select
Installing Spyder provides most of what is needed. Other useful packages to install are
sudo apt - get install ipython - notebook ipython - qtconsole python - opencv imagej sudo easy_install SimpleItk
The apt-get commands will also work on other Debian-based OSes (like Debian and Mint).
(Kitware / Google)
June 2012
201 / 201