Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Python for Computer Vision - Revision 2nd Edition

Python is a powerful tool for computer vision applications. This presentation reviews the essential libraries required for image analysis using Python. These libraries include NumPy, SciPy, Matplotlib, Python Image Library (PIL), scikit-image, and scikit-learn.

  • Be the first to comment

Python for Computer Vision - Revision 2nd Edition

  1. 1. ahmed.fawzy@ci.menofia.edu.eg ‫المنوفية‬ ‫جامعة‬ ‫والمعلومات‬ ‫الحاسبات‬ ‫كلية‬ ‫المعلومات‬ ‫تكنولوجيا‬ ‫بالحاسب‬ ‫الرؤية‬‫المنوفية‬ ‫جامعة‬ September 2018
  2. 2. • Overview about Computer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad2
  3. 3. • Overview about Computer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad3
  4. 4. • Computer vision aims to enable the computer to see the world same as or better than humans. Computer should analyze the images to identify objects and recognize them to understand the image. • To do this, computer needs to store and process images. CAT 9/21/2018Ahmed F. Gad4
  5. 5. • Preprocessing Noise Filter 9/21/2018Ahmed F. Gad5
  6. 6. • Preprocessing • Feature Extraction 819,840 Values 640x427x3 Size in Bytes 819,840 8 bits Size in KBytes 819,840/1,024= 800.625 KByte KByte Size in MBytes 800.625/1,024= 0.782 MByte MByte 9/21/2018Ahmed F. Gad6
  7. 7. • Preprocessing • Feature Extraction Dataset: 2,000 Images Dataset Size 0.782x2,000 = 1,564 MByte 1,564/1,024= 1.527 GByte GByte 9/21/2018Ahmed F. Gad7
  8. 8. • Preprocessing • Feature Extraction Dataset: 50,000 38.18 GByteSize Intensive in its Memory and Processing Requirements Reduce Amounts of Data How to Represent Image using Less Amount of Data? Feature Extraction 9/21/2018Ahmed F. Gad8
  9. 9. • Preprocessing • Feature Extraction Summarizes Images For example, use 2,000 values rather than 819,840 Element 0 Element 0 … Element 1,999 Histogram of Oriented Gradients (HOG) Scale-Invariant Feature Transform (SIFT) Examples Advantage of using Features rather Image Pixels Values is that Features are Robust to Variations such as illumination, scale, and rotation. 9/21/2018Ahmed F. Gad9
  10. 10. • Preprocessing • Feature Extraction • Application (e.g. Classification) 9/21/2018Ahmed F. Gad10
  11. 11. • Overview about Computer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad11
  12. 12. List TupleData Structures Which one is suitable for storing images? Let`s See. Dict Set 9/21/2018Ahmed F. Gad12
  13. 13. • Image is MD. Which one supports MD storage? • Which one supports updating its elements? Both List Tuples are immutable. 9/21/2018 Ahmed F. Gad13
  14. 14. • So, lets start storing an image into a Python list. The following code reads an image in the img_list variable. • Let`s use the PIL (Python Image Library) for reading an image in a list. 9/21/2018Ahmed F. Gad14
  15. 15. 9/21/2018Ahmed F. Gad15 Original Result
  16. 16. Core i7 Machine 9/21/2018Ahmed F. Gad16
  17. 17. • Why not applying arithmetic operations rather than looping? img_list = img_list + 50 9/21/2018Ahmed F. Gad17
  18. 18. • List makes doing operations over the images more complex and also time consuming because they require pixel by pixel processing. • The best way for storing images is using arrays. • Rather than being time efficient in processing images, arrays has many other advantages. Can you imagine what? 9/21/2018Ahmed F. Gad18
  19. 19. • Overview about Computer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad19
  20. 20. • Lists are already available in Python. To use Python arrays, additional libraries must be installed. • The library supporting arrays in Python is called Numerical Python (NumPy). • NumPy can be installed using Python command-line. • It is also available in all-in-one packages such as Anaconda. 9/21/2018Ahmed F. Gad20
  21. 21. • Based on your environment, you can install new modules. • For traditional Python distributions, use the PIP installer. pip install numpy • For Anaconda, use the conda installed conda install numpy • It is by default available in Anaconda. 9/21/2018Ahmed F. Gad21
  22. 22. • After installing NumPy, we can import it in our programs and scripts. 9/21/2018Ahmed F. Gad22
  23. 23. 9/21/2018Ahmed F. Gad23
  24. 24. • Overview about Computer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad24
  25. 25. 9/21/2018 Ahmed F. Gad 25
  26. 26. • The problem is expecting uint8 data type but another data type was used. • To know what is the array data type, use the dtype array property. How to make the conversion to uint8? 9/21/2018Ahmed F. Gad26
  27. 27. • When creating the array, set the dtype argument of numpy.array to the desired data type. • dtype argument can be set to multiple types. 9/21/2018Ahmed F. Gad27
  28. 28. 9/21/2018Ahmed F. Gad28
  29. 29. • After array being created, use the astype method of numpy.ndarray. • It make a new copy of the array after being casted to the specified type in the dtype argument. 9/21/2018Ahmed F. Gad29
  30. 30. • Arithemetic Operations • Operations between arrays 9/21/2018Ahmed F. Gad30
  31. 31. • Array Creation • arange • linspace arange vs. linspace 9/21/2018Ahmed F. Gad31
  32. 32. • Indexing can be forward or backward. • Forward indexing: from start to end. • Backward indexing: from end to start. • General form of indexing: my_array[start:stop:step] • In backward indexing, the index of the last element is -1. Start End 0 2 End Start -3 -1 Forward Backward9/21/2018Ahmed F. Gad32
  33. 33. • Forward: my_array[start=0:stop=6:step=2] • Backward: my_array[start=-1:stop=-6:step=-2] • Get all elements starting from index 3 9/21/2018Ahmed F. Gad33
  34. 34. • For MD arrays, indexing can be applied for each individual dimension. Intersection between the different dimensions will be returned. • Forward: my_array[start=0:stop=3:step=2, start=1:stop=4:step=1] • Forward: my_array[start=-1:stop=-3:step=-1, start=0:stop=3:step=1] 9/21/2018Ahmed F. Gad34
  35. 35. For While 9/21/2018Ahmed F. Gad35
  36. 36. 9/21/2018 Ahmed F. Gad36
  37. 37. • Overview about Computer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad37
  38. 38. • The main use of NumPy is to support numerical arrays in Python. According to the official documentation, NumPy supports nothing but the array data type and most basic operations: indexing, sorting, reshaping, basic element-wise functions, etc. • SciPy supports everything in NumPy and also adds new features not existing in NumPy. We can imagine that NumPy is a subset of SciPy. • Let`s explore what is in SciPy. SciPy NumPy 9/21/2018Ahmed F. Gad38
  39. 39. • SciPy has a collection of algorithms and functions based on NumPy. User can use high-level commands to perform complex operations. • SciPy is organized into a number of sub-packages. 9/21/2018Ahmed F. Gad39
  40. 40. • SciPy provides modules for working with images from reading, processing, and saving an image. • This example applies Sobel edge detector to an image using SciPy. 9/21/2018Ahmed F. Gad 40
  41. 41. • In addition to Scipy, there are other Python libraries for working with images. • Examples: • Python Image Library (PIL) • OpenCV • scikit-image 9/21/2018Ahmed F. Gad41
  42. 42. • Apply erosion morphology operation using skimage.morphology sub-module. Binary Result 9/21/2018Ahmed F. Gad 42
  43. 43. • Overview about Computer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad43
  44. 44. • After extracting features, next is to train a machine learning (ML) model for building applications such as classification. • One of the most popular Python library for building and training ML algorithms is scikit-learn. • We will discuss an example in which the random forest ensemble technique is trained based on the Sonar Dataset for classifying an object as either a mine or a rock. It is available at this page (https://archive.ics.uci.edu/ml/machine-learning- databases/undocumented/connectionist-bench/sonar/sonar.all- data). • The dataset has 208 samples and each sample has 60 numerical inputs and a single output. The target is M when the object is mine and R for rocks. • In our example, half of the samples within each class are used for training and the other half for testing. In other words, 104 samples for training and another 104 samples for testing. 9/21/2018Ahmed F. Gad44
  45. 45. 9/21/2018Ahmed F. Gad45
  46. 46. • Overview about Computer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad46
  47. 47. • Sometimes the problem is complex and needs deep learning. Some of the libraries used for building deep learning models include: • TensorFlow • Keras • Theano • PyTorch 9/21/2018Ahmed F. Gad47
  48. 48. • Overview about Computer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad48
  49. 49. • Books • Ahmed F. Gad 'Practical Computer Vision Applications Using Deep Learning with CNNs'. Apress, 2019, 978-1-4842-4167-7. https://amazon.com/Practical-Computer-Vision- Applications-Learning/dp/1484241665 • Ahmed F. Gad "TensorFlow: A Guide To Build Artificial Neural Networks Using Python". LAP LAMBERT Academic Publishing, 2017, 978-620-2- 07312-7. https://www.amazon.com/TensorFlow- Artificial-Networks-artificial- explanation/dp/6202073128 • Tutorials • https://www.kdnuggets.com/author/ahmed-gad • http://youtube.com/AhmedGadFCIT • http://slideshare.com/AhmedGadFCIT • https://linkedin.com/in/AhmedFGad 9/21/2018 Ahmed F. Gad 49
  50. 50. • SciPy • https://docs.scipy.org/doc/scipy/reference • NumPy • https://docs.scipy.org/doc/numpy/reference • Matplotlib • https://matplotlib.org/contents.html • Anaconda • https://www.anaconda.com • scikit-image • http://scikit-image.org • scikit-learn • http://scikit-learn.org • TensorFlow • https://tensorflow.org • Keras • https://keras.io • Theano • http://deeplearning.net/software/theano • PyTorch • https://pytorch.org 9/21/2018Ahmed F. Gad50

×