ahmed.fawzy@ci.menofia.edu.eg
‫المنوفية‬ ‫جامعة‬
‫والمعلومات‬ ‫الحاسبات‬ ‫كلية‬
‫المعلومات‬ ‫تكنولوجيا‬
‫بالحاسب‬ ‫الرؤية‬‫المنوفية‬ ‫جامعة‬
September 2018
• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad2
• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad3
• 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
• Preprocessing Noise
Filter
9/21/2018Ahmed F. Gad5
• 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
• 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
• 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
• 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
• Preprocessing
• Feature Extraction
• Application (e.g. Classification)
9/21/2018Ahmed F. Gad10
• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad11
List
TupleData
Structures
Which one is suitable for
storing images?
Let`s See.
Dict
Set
9/21/2018Ahmed F. Gad12
• 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
• 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
9/21/2018Ahmed F. Gad15
Original Result
Core i7 Machine
9/21/2018Ahmed F. Gad16
• Why not applying arithmetic operations rather than looping?
img_list = img_list + 50
9/21/2018Ahmed F. Gad17
• 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
• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad19
• 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
• 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
• After installing NumPy, we can import it in our programs and scripts.
9/21/2018Ahmed F. Gad22
9/21/2018Ahmed F. Gad23
• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad24
9/21/2018
Ahmed F. Gad
25
• 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
• 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
9/21/2018Ahmed F. Gad28
• 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
• Arithemetic Operations
• Operations between arrays
9/21/2018Ahmed F. Gad30
• Array Creation
• arange
• linspace
arange vs. linspace 9/21/2018Ahmed F. Gad31
• 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
• 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
• 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
For
While
9/21/2018Ahmed F. Gad35
9/21/2018
Ahmed F. Gad36
• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad37
• 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
• 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
• 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
• 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
• Apply erosion morphology operation using skimage.morphology
sub-module.
Binary Result
9/21/2018Ahmed F. Gad
42
• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad43
• 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
9/21/2018Ahmed F. Gad45
• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad46
• 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
• Overview about Computer Vision
• Traditional Data Storage in Python
• NumPy Arrays
• Matplotlib
• SciPy
• scikit-learn
• Other Libraries
• Further Reading
9/21/2018Ahmed F. Gad48
• 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
• 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

Python for Computer Vision - Revision 2nd Edition

  • 1.
    [email protected] ‫المنوفية‬ ‫جامعة‬ ‫والمعلومات‬ ‫الحاسبات‬‫كلية‬ ‫المعلومات‬ ‫تكنولوجيا‬ ‫بالحاسب‬ ‫الرؤية‬‫المنوفية‬ ‫جامعة‬ September 2018
  • 2.
    • Overview aboutComputer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad2
  • 3.
    • Overview aboutComputer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad3
  • 4.
    • Computer visionaims 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.
  • 6.
    • Preprocessing • FeatureExtraction 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.
    • Preprocessing • FeatureExtraction 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.
    • Preprocessing • FeatureExtraction 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.
    • Preprocessing • FeatureExtraction 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.
    • Preprocessing • FeatureExtraction • Application (e.g. Classification) 9/21/2018Ahmed F. Gad10
  • 11.
    • Overview aboutComputer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad11
  • 12.
    List TupleData Structures Which one issuitable for storing images? Let`s See. Dict Set 9/21/2018Ahmed F. Gad12
  • 13.
    • Image isMD. Which one supports MD storage? • Which one supports updating its elements? Both List Tuples are immutable. 9/21/2018 Ahmed F. Gad13
  • 14.
    • So, letsstart 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.
  • 16.
  • 17.
    • Why notapplying arithmetic operations rather than looping? img_list = img_list + 50 9/21/2018Ahmed F. Gad17
  • 18.
    • List makesdoing 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.
    • Overview aboutComputer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad19
  • 20.
    • Lists arealready 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.
    • Based onyour 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.
    • After installingNumPy, we can import it in our programs and scripts. 9/21/2018Ahmed F. Gad22
  • 23.
  • 24.
    • Overview aboutComputer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad24
  • 25.
  • 26.
    • The problemis 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.
    • When creatingthe 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.
  • 29.
    • After arraybeing 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.
    • Arithemetic Operations •Operations between arrays 9/21/2018Ahmed F. Gad30
  • 31.
    • Array Creation •arange • linspace arange vs. linspace 9/21/2018Ahmed F. Gad31
  • 32.
    • Indexing canbe 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.
    • 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.
    • For MDarrays, 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.
  • 36.
  • 37.
    • Overview aboutComputer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad37
  • 38.
    • The mainuse 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.
    • SciPy hasa 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.
    • SciPy providesmodules 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.
    • In additionto Scipy, there are other Python libraries for working with images. • Examples: • Python Image Library (PIL) • OpenCV • scikit-image 9/21/2018Ahmed F. Gad41
  • 42.
    • Apply erosionmorphology operation using skimage.morphology sub-module. Binary Result 9/21/2018Ahmed F. Gad 42
  • 43.
    • Overview aboutComputer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad43
  • 44.
    • After extractingfeatures, 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.
  • 46.
    • Overview aboutComputer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad46
  • 47.
    • Sometimes theproblem 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.
    • Overview aboutComputer Vision • Traditional Data Storage in Python • NumPy Arrays • Matplotlib • SciPy • scikit-learn • Other Libraries • Further Reading 9/21/2018Ahmed F. Gad48
  • 49.
    • Books • AhmedF. 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.
    • 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