Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python tutorial @ BIG (EPFL)

Python tutorial @ BIG (EPFL)

A short introduction to Python for Image Analysis and Deep Learning
@ BIG, EPFL, July 2016

PDF: https://perso.crans.org/besson/publis/slides/2016_07__Python_demo_at_EPFL/slides.pdf

Lilian Besson

July 18, 2016
Tweet

More Decks by Lilian Besson

Other Decks in Programming

Transcript

  1. Python tutorial @ BIG (EPFL) A short introduction to Python

    for Image Analysis and Deep Learning By Lilian Besson b e s s o n @ c r a n s . o r g @ BIG, EPFL, July 2016. 1
  2. Introduction This short tutorial will get you started with Python

    3. We will try to discover together what Daniel asked me yesterday. 2
  3. 1. Install Python 3 Try to do this on your

    laptop , during the tutorial 1. Download Anaconda (Python 3.5) from continuum.io/downloads (~ 346 Mo) 2. Install it: double­click the downloaded . p k g file (on Mac) or . e x e file (on Windows),, and follow the instructions 3. Check that Python ( p y t h o n 3 ) has been installed: $ p y t h o n 3 [ i t s h o u l d w o r k ] 3
  4. 2. Basic introduction to Python Not covered today Start with

    introtopython.org More in­depth tutorial: scipy­lectures.org (very good quality) Example: Hello World! : > > > p r i n t ( " H e l l o P y t h o n w o r l d ! " ) H e l l o P y t h o n w o r l d ! 4
  5. 3. Using the Spyder IDE The Spyder IDE is shipped

    with Anaconda Gives a nice MATLAB­like interface: advanced editing, interactive testing, debugging and introspection features A numerical computing environment thanks to the support of: I P y t h o n (enhanced interactive Python interpreter) and core Python libraries: N u m P y (linear algebra), S c i P y (signal and image processing) or m a t p l o t l i b (interactive 2D/3D plotting) Easy to debug: add breakpoint, previous/next buttons etc → It's Demo time! Other good IDE : the Jupyter notebook (in your browser) 5
  6. 4. Importing the main libraries They are all shipped with

    Anaconda! NumPy: i m p o r t n u m p y a s n p Scipy: i m p o r t s c i p y MatPlotLib: i m p o r t m a t p l o t l i b . p y p l o t a s p l t 6
  7. 4.1. First example: t = n p . l i

    n s p a c e ( 0 , 2 * n p . p i , 4 0 0 ) x = n p . c o s ( 2 * t ) y = n p . c o s ( 3 * t ) # V e c t o r i z e d f u n c t i o n s ! p l t . f i g u r e ( ) p l t . p l o t ( x , y , ' r + ­ ' ) # S h o r t c u t à ­ l a M A T L A B p l t . s h o w ( ) 7
  8. 4.1. Second example: f r o m s c i

    p y . s p e c i a l i m p o r t g a m m a x = n p . l i n s p a c e ( 0 . 1 , 3 , 4 0 0 ) y = g a m m a ( x ) # V e c t o r i z e d f u n c t i o n ! p l t . f i g u r e ( ) # ( O p t i o n a l ) p l t . p l o t ( x , y ) p l t . t i t l e ( " T h e f u n c t i o n $ \ G a m m a ( x ) $ o n $ [ 0 . 1 , 3 ] $ " ) # A n d L a T e X i s s u p p o r t e d ! ↑ p l t . s h o w ( ) # ( O p t i o n a l ) 9
  9. 5. Reading data, images etc, with s c i p

    y or s c i k i t ­ i m a g e They are all shipped with Anaconda! s c i p y . n d i m a g e implements a lot of image processing functions, mostly for n­dimensional images. → Cf. www.scipy­lectures.org/advanced/image_processing And s c i k i t ­ i m a g e (scikit­image.org) adds functions specific to 2D/3D images, and more. → Cf. www.scipy­lectures.org/packages/scikit­image For 3D plotting, use Mayavi (more complex) 11
  10. 5.1. Example: reading an image f r o m s

    c i p y i m p o r t n d i m a g e # m o d u l e f o r n ­ d i m a g e s i m p o r t m a t p l o t l i b . p y p l o t a s p l t # m o d u l e f o r p l o t t i n g f r o m s c i p y i m p o r t m i s c # s o m e t o y d a t a a r e t h e r e f a c e = m i s c . f a c e ( g r a y = T r u e ) # O r . . . f a c e = p l t . i m r e a d ( ' f a c e . p n g ' ) # O r . . . f r o m s k i m a g e . i o i m p o r t i m r e a d # i m p o r t a f u n c t i o n f a c e = i m r e a d ( ' f a c e . j p g ' ) p r i n t ( f a c e [ 0 , 0 ] ) # E x a m p l e , f i r s t p i x e l : 1 1 4 # D i s p l a y t h e i m a g e p l t . i m s h o w ( f a c e , c m a p = ' g r a y ' ) p l t . s h o w ( ) 12
  11. 5.2. Example: more on images... l x , l y

    = f a c e . s h a p e # C r o p p i n g , b y s l i c i n g t h e n d a r r a y ( m a t r i x ) c r o p _ f a c e = f a c e [ l x / 4 : ­ l x / 4 , l y / 4 : ­ l y / 4 ] # U p < ­ > d o w n f l i p f l i p _ u d _ f a c e = n p . f l i p u d ( f a c e ) # R o t a t i o n r o t a t e _ f a c e = n d i m a g e . r o t a t e ( f a c e , 4 5 ) r o t a t e _ f a c e _ n o r e s h a p e = n d i m a g e . r o t a t e ( f a c e , 4 5 , r e s h a p e = F a l s e ) p l t . f i g u r e ( ) p l t . s u b p l o t ( 2 , 3 , 1 ) # S u b p l o t l i k e i n M A T L A B p l t . i m s h o w ( f a c e , c m a p = ' g r a y ' ) p l t . s u b p l o t ( 2 , 3 , 2 ) p l t . i m s h o w ( c r o p _ f a c e , c m a p = ' g r a y ' ) # e t c . . . 14
  12. 6. Machine Learning in Python with s c i k

    i t ­ l e a r n Shipped with Anaconda! Importing scikit­learn: i m p o r t s k l e a r n a s s k , or f r o m s k l e a r n i m p o r t X X X Documentation on scikit­learn.org Lots of "not­deep" machine learning algorithm, easy to use Lots of examples! 16
  13. 7. Deep Learning in Python, with c a f f

    e , l a s a g n e or t e n s o r f l o w ... I don't do deep learning myself! So I don't know which library is the best... Warning: NOT shipped with Anaconda ! Every framework require a specific installation, usually not easy... → Try to ask to someone who already installed it! 17
  14. 7. Deep Learning in Python, with c a f f

    e , l a s a g n e or t e n s o r f l o w ... c a f f e : Python interface to a C++ engine, by Berkeley's Vision lab, caffe.berkeleyvision.org, see this example l a s a g n e : C and Python, built on top of t h e a n o , by Yoshua Bengio's lab (Montreal), lasagne.readthedocs.org, see this example 18
  15. 7. Deep Learning in Python, with c a f f

    e , l a s a g n e or t e n s o r f l o w ... t e n s o r f l o w : Python interface to a C++ engine, by Google, tensorflow.org, see this example. See also: tflearn.org for a nicer interface? Also interesting: keras.io, using either Theano or TensorFlow, pure Python, lots of examples 19
  16. References for Python 3 and basic tools P y t

    h o n 3 documentation: docs.python.org/3 introtopython.org for a small introduction to Python syntax and concepts S p y d e r documentation: pythonhosted.org/spyder I P y t h o n tutorial: ipython.readthedocs.io 21
  17. References for libraries (1/3) N u m P y documentation:

    docs.scipy.org/doc/numpy/reference S c i P y documentation: docs.scipy.org/doc/scipy/reference S c i P y for image manipulation: www.scipy­ lectures.org/advanced/image_processing M a t P l o t L i b documentation: matplotlib.org/contents.html M a t P l o t L i b tutorial: www.labri.fr/perso/nrougier/teaching/matplotlib 22
  18. References for libraries (2/3) s c i k i t

    ­ l e a r n tutorial: scikit­ learn.org/stable/tutorial/index.html s c i k i t ­ i m a g e tutorial: scikit­ image.org/docs/stable/overview.html Also on scipy­lectures.org: www.scipy­ lectures.org/packages/scikit­image 23
  19. References for libraries (3/3) t h e a n o

    documentation: deeplearning.net/software/theano l a s a g n e documentation: lasagne.readthedocs.org t e n s o r f l o w documentation: www.tensorflow.org/versions/r0.9/get_started/index.html t f l e a r n tutorial: tflearn.org/#quick­overview k e r a s tutorial: keras.io/#getting­started­30­seconds­to­keras 24