Introduction to convolutions using Python

| | | | | | | | | | | | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

Learning Features
Capability Development or Feature Extraction — it is the process of extracting useful patterns from the input data that will help the forecasting model better understand the real nature of the problem. A good learning function will expose patterns in a way that dramatically improves the accuracy and performance of the applied machine learning algorithms, which would be impossible or too expensive for machine learning itself. The function learning algorithms find common patterns that are important for distinguishing between the required classes and automatically extract them. After this process, they are ready for use in classification or regression problems.
Let’s take a look at the popular image classification problem, classification of face and faceless face images. In the early days of computer vision, scientists tried to solve this problem by hand coding algorithms to detect possible features of a human face, such as shape, eyes, nose, lips, etc. This approach usually gave poor results because a face can look like so many varieties. that it was impossible to explain even a significant portion of the signs. A simple change in lighting or orientation can change the image in such a way that algorithms can no longer recognize faces.
In 1998, Yang Lecun introduced the concept of convolutional neural networks, which is capable of classifying images of handwritten characters with an accuracy of about 99 %. The big advantage of convolutional neural networks is that they are extraordinarily good at detecting features in images that grow after each level, resulting in high-level features. The final layers (can be one or more) use all of these generated features for classification or regression.
convolution
convolution — it is an operation that is performed on an image to extract elements from it, applying a smaller tensor, called a kernel, like a sliding window over the image. Depending on the values ‚Äã‚Äãin the convolutional kernel, we can pick up certain patterns from the image. In the following example, we will demonstrate how to detect horizontal and vertical edges in an image using the appropriate kernels.

python3

import numpy as np

import matplotlib.pyplot as plt


# let img1 be unremarkable image

img1 = np. array ([np.array ([ 200 , 200 ]), np.array ([ 200 , 200 ])])

img2 = np.array ([np.array ( [ 200 , 200 ]), np.array ([ 0 , 0 ])])

img3 = np.array ([np.array ([ 200 , 0 ]), np.array ([ 200 , 0 ])])

kernel_horizontal = np .array ([np.array ([ 2 , 2 ]), np.array ([ - 2 , - 2 ])])

print (kernel_horizontal, ’is a kernel for detecting horizontal edges ’ )

kernel_vertical = np.array ([np.array ([ 2 , - 2 ]), np.array ([ 2 , - 2 ])])

print (kernel_vertical, ’is a kernel for detecting vertical edges’ )


# We’ll apply kernels to images
# element-wise multiplication followed by addition

def apply_kernel (img, kernel):

return np. sum (np.multiply (img, kernel ))


# Img1 rendering
plt.imshow (img1)

plt.axis ( ’off ’ )

plt.title ( ’ img1’ )

plt.show ()


# Checking horizontal and vertical objects in image1

print ( ’Horizontal edge confidence score:’ , apply_kernel (img1,

kernel_horizontal))

print ( ’Vertical edge confidence score:’ , apply_kernel (img1 ,

kernel_vertical))


# Img2 rendering
plt.imshow (img2)

plt.axis ( ’off’ )

plt.title ( ’img2’ )

plt.show ()


# Checking horizontal and vertical objects in image2

print ( ’Horizontal edge confidence score:’ , apply_kernel (img2,

kernel_horizontal))

print ( ’Vertical edge confidence score:’ , apply_kernel (img2,

kernel_vertical))


# Img3 rendering
plt.imshow (img3)

plt.axis ( ’off’ )

plt.title ( ’ img3’ )

plt.show ()


# Checking horizontal and vertical objects in image3

print ( ’Horizontal edge confidence score:’ , apply_kernel (img3,

kernel_horizontal))

print ( ’Vertical edge confidence score:’ , apply_kernel (img3,

kernel_vertical))


Exit:

[[ 2 2]
[-2 -2]] is a kernel for detecting horizontal edges
[[2 -2]
[2 -2]] is a kernel for detecting vertical edges

Horizontal edge confidence score: 0
Vertical edge confidence score: 0

Horizontal edge confidence score: 800
Vertical edge confidence score: 0

Horizontal edge confidence score: 0
Vertical edge confidence score: 800

👻 Read also: what is the best laptop for engineering students?

Introduction to convolutions using Python __del__: Questions

How can I make a time delay in Python?

5 answers

I would like to know how to put a time delay in a Python script.

2973

Answer #1

import time
time.sleep(5)   # Delays for 5 seconds. You can also use a float value.

Here is another example where something is run approximately once a minute:

import time
while True:
    print("This prints once a minute.")
    time.sleep(60) # Delay for 1 minute (60 seconds).

2973

Answer #2

You can use the sleep() function in the time module. It can take a float argument for sub-second resolution.

from time import sleep
sleep(0.1) # Time in seconds

Introduction to convolutions using Python __del__: Questions

How to delete a file or folder in Python?

5 answers

How do I delete a file or folder in Python?

2639

Answer #1


Path objects from the Python 3.4+ pathlib module also expose these instance methods:

We hope this article has helped you to resolve the problem. Apart from Introduction to convolutions using Python, check other __del__-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Xu Wu

Singapore | 2023-03-24

dis Python module is always a bit confusing 😭 Introduction to convolutions using Python is not the only problem I encountered. I just hope that will not emerge anymore

Boris Porretti

Massachussetts | 2023-03-24

I was preparing for my coding interview, thanks for clarifying this - Introduction to convolutions using Python in Python is not the simplest one. Checked yesterday, it works!

Schneider Williams

Milan | 2023-03-24

Python functions is always a bit confusing 😭 Introduction to convolutions using Python is not the only problem I encountered. Will get back tomorrow with feedback

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically