0% found this document useful (0 votes)
2 views7 pages

Computer Vision Lab Manual 1

The document discusses the advantages of using NumPy for mathematical calculations over Pandas, highlighting memory optimization and faster computation for homogeneous data types. It also explains the differences between containers and virtual machines, emphasizing the efficiency of containers in application deployment. Additionally, the document covers the benefits of GPUs in computer vision and compares the performance of arrays and lists in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Computer Vision Lab Manual 1

The document discusses the advantages of using NumPy for mathematical calculations over Pandas, highlighting memory optimization and faster computation for homogeneous data types. It also explains the differences between containers and virtual machines, emphasizing the efficiency of containers in application deployment. Additionally, the document covers the benefits of GPUs in computer vision and compares the performance of arrays and lists in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Computer Vision Lab Manual 1

Introduction to Computer Vision & OpenCV

Why Use NumPy for Mathematical


Calculations Instead of Pandas?
Data Homogeneity vs. Heterogeneity
 NumPy Arrays (Homogeneous Data):
o All elements in a NumPy array are of the same data type.
o This uniformity allows for optimized memory storage and faster
vectorized operations.
 Pandas DataFrames (Heterogeneous Data):
o Can contain multiple data types (e.g., integers, floats, strings) in
different columns.
o This flexibility is ideal for structured data analysis but introduces
overhead that makes numerical operations slower compared to NumPy.

Memory Optimization
 Python’s Default Integer:
o Python’s built-in int type is an object, which typically takes about 28
bytes per integer.
o This extra overhead makes it inefficient when processing images,
which contain thousands or millions of pixels.
 NumPy’s Data Types:
o With NumPy, you can explicitly set the data type using the dtype
parameter.
o For example, using np.uint8 (an 8-bit unsigned integer) ensures each
number takes 1 byte.
o You can also use other types like np.int32 (4 bytes per number) by
employing the astype() function for type conversion.

Example Calculation

Consider an image of size 640×640 pixels.

1. Number of Pixels:

640×640=409,600 pixels640 \times 640 = 409,600 \


text{ pixels}640×640=409,600 pixels

2. Memory Usage Using Python’s Default Integers:


o Each integer takes ~28 bytes.

409,600 pixels×28 bytes≈11,468,800 bytes(≈11.47 MB)409,600 \


text{ pixels} \times 28 \text{ bytes} \approx 11,468,800 \text{ bytes} \quad
(\approx 11.47 \
text{ MB})409,600 pixels×28 bytes≈11,468,800 bytes(≈11.47 MB)

3. Memory Usage Using NumPy with uint8:


o Each pixel takes 1 byte.

409,600 pixels×1 byte=409,600 bytes(≈0.41 MB)409,600 \text{ pixels} \


times 1 \text{ byte} = 409,600 \text{ bytes} \quad (\approx 0.41 \
text{ MB})409,600 pixels×1 byte=409,600 bytes(≈0.41 MB)

4. Memory Usage Using NumPy with int32:


o Each pixel takes 4 bytes.

409,600 pixels×4 bytes=1,638,400 bytes(≈1.64 MB)409,600 \text{ pixels} \


times 4 \text{ bytes} = 1,638,400 \text{ bytes} \quad (\approx 1.64 \
text{ MB})409,600 pixels×4 bytes=1,638,400 bytes(≈1.64 MB)

Key Takeaway: NumPy’s efficient data storage dramatically reduces memory


usage compared to using Python’s default integer type—making it ideal for image
processing tasks where memory and speed are crucial.

Faster Computation
 Vectorized Operations:
NumPy arrays support vectorized operations that apply functions element-
wise without explicit Python loops. This is significantly faster than iterating
over Python lists or objects.
 Optimized Low-Level Code:
NumPy’s underlying implementation in C allows it to perform mathematical
calculations at a much lower level, reducing the execution time.

Better Control Over Data Types


 With NumPy, you have explicit control over the precision and size of the
numbers you work with. This means you can match your data type exactly to
the needs of your application (for example, using an 8-bit integer for pixel
values that range from 0 to 255).

Containers and Virtual Machines in Simple


Words
What Are Containers?
 Definition:
A container is a lightweight, isolated environment that packages a program
with all the necessary libraries and dependencies.
 Purpose:
It ensures that the program runs consistently across different systems,
eliminating “it works on my machine” issues.

Difference Between Containers and Virtual Machines


(VMs)
Virtual Machines
Feature Containers
(VMs)

Size Small (MBs) Large (GBs)

Faster (shares host


Speed Slower (runs full OS)
OS)

Boot Time Starts in seconds Takes minutes

Resource Requires more


More efficient
Use resources

Portability Highly portable Less portable


 Virtual Machines:
They include a full operating system, making them bulkier and slower to
start.
 Containers:
They share the host operating system kernel but isolate applications in
separate spaces.

How Google Colab Uses Containers


 Pre-installed Libraries:
Google Colab provides containers that come with pre-installed libraries like
TensorFlow, PyTorch, and OpenCV.
 Hardware Acceleration:
Containers can be configured to use GPUs, which greatly speed up
computations.
 Ephemeral Environment:
Once your session ends, the container is deleted along with any temporary
files or packages installed during the session.

Why is GPU Used in Computer Vision?


Advantages of GPUs
 Parallel Processing:
GPUs consist of thousands of smaller cores that can perform many
calculations in parallel.
 Matrix Operations:
Computer vision tasks involve a large number of matrix operations (e.g.,
convolution), which GPUs can execute very efficiently.
 Acceleration of Deep Learning:
Training and inference in neural networks benefit from the parallelism
provided by GPUs, reducing overall computation time.

CPU vs. GPU Comparison


Feature CPU GPU

Cores Few powerful cores Thousands of smaller cores

Processing Sequential Parallel

Performan Optimized for high-throughput


General-purpose tasks
ce tasks

Usage OS operations, basic Deep learning, image processing,


Feature CPU GPU

tasks gaming

Why Are Arrays Faster Than Lists in Python?


 Memory Efficiency:
Arrays store elements in contiguous blocks of memory, which allows for faster
access and manipulation.
 Fixed Data Type:
Python lists can hold elements of different types, but this flexibility introduces
overhead. Arrays enforce a single data type, reducing this overhead.
 Vectorized Operations:
NumPy arrays allow for element-wise operations across the entire array
without the need for explicit loops, resulting in significant performance gains.

Why Are DataFrames Used? (Heterogeneous


vs. Homogeneous Data)
 NumPy Arrays:
o Best for numerical operations where all data is of the same type.
o Faster and more memory-efficient for large-scale numerical
computations, such as image processing.
 Pandas DataFrames:
o Designed to handle structured data with mixed data types
(heterogeneous data).
o Provide rich functionality for data manipulation, cleaning, and analysis,
making them ideal for tabular data.

Feature NumPy Arrays Pandas DataFrames

Data
Homogeneous Heterogeneous
Type

Slightly slower (due to indexing


Speed Faster (for numerical tasks)
overhead)

Image processing, numerical Data analysis, handling structured


Usage
operations data
Conclusion
In this lab, we covered:

 Memory and Speed:


NumPy uses less memory and performs calculations faster than Python’s
default integer types and Pandas because it is optimized for homogeneous
data and supports vectorized operations.
 Containers vs. VMs:
Containers provide lightweight, isolated environments that are ideal for
deploying consistent applications across different systems, whereas VMs run
full operating systems and are more resource-intensive.
 GPU in Computer Vision:
GPUs excel in parallel processing and matrix computations, making them
essential for handling the intensive calculations required in computer vision.
 Arrays vs. Lists:
Arrays are stored in contiguous memory and enforce a single data type,
making them much faster than Python lists for numerical computations.
 Use Cases for DataFrames:
While NumPy arrays are best for numerical tasks, Pandas DataFrames are
powerful tools for data analysis when dealing with mixed data types.

You might also like