You are reading the article A Friendly Introduction To Real updated in November 2023 on the website Minhminhbmm.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested December 2023 A Friendly Introduction To Real
Overview
Real-time object detection is taking the computer vision industry by storm
Here’s a step-by-step introduction to SlimYOLOv3, the latest real-time object detection framework
We look at the various aspects of the SlimYOLOv3 architecture, including how it works underneath to detect objects
IntroductionHumans can pick out objects in our line of vision in a matter of milliseconds. In fact – just look around you right now. You’ll have taken in the surroundings, quickly detected the objects present, and are now looking at this article. How long did that take?
That is real-time object detection. How cool would be it if we could get machines to do that? Now we can! Thanks primarily to the recent surge of breakthroughs in deep learning and computer vision, we can lean on object detection algorithms to not only detect objects in an image – but to do that with the speed and accuracy of humans.
Do you want to learn real-time object detection but aren’t sure where to start? Do you want to build a computer vision model that detects objects like the above video? Then this article is for you!
Do you want to learn real-time object detection but aren’t sure where to start? Do you want to build a computer vision model that detects objects like the above video? Then this article is for you!
We will first look at the various nuances of object detection (including the potential challenges you might face). Then, I will introduce the SlimYOLOv3 framework and deep dive into how it works underneath to detect objects in real-time. Time to get excited!
If you’re new to the wonderful world of computer vision, we have designed the perfect course for you! Make sure you check it out here:
Table of Contents
What is Object Detection?
Applications of Object Detection
Why Real-Time Object Detection?
Challenges during Real-Time Object Detection
Introduction to SlimYOLOv3
Understanding the Architecture of SlimYOLOv3
What is Object Detection?Before we dive into how to detect objects in real-time, let’s cover our basics first. This is especially important if you’re relatively new to the world of computer vision.
Object detection is a technique we use to identify the location of objects in an image. If there is a single object in the image and we want to detect that object, it is known as image localization. What if there are multiple objects in an image? Well, that’s what object detection is!
Let me explain this using an example:
The image on the left has a single object (a dog) and hence detecting this object will be an image localization problem. The image on the right has two objects (a cat and a dog). Detecting both these objects would come under object detection.
If you wish to get an in-depth introduction to object detection, feel free to refer to my comprehensive guide:
Now, you might be wondering – why is object detection required? ANd more to the point, why do we need to perform real-time object detection? We’ll answer these questions in the next section.
Applications of Object DetectionObject Detection is being widely used in the industry right now. Anyone harboring ambitions of working in computer vision should know these applications by heart.
The use cases of object detection range from personal security to automated vehicle systems. Let’s discuss some of these current and ubiquitous applications.
Self-Driving CarsThis is one of the most interesting and recent applications of Object detection. Honestly, it’s one I am truly fascinated by.
Self-driving cars (also known as autonomous cars) are vehicles that are capable of moving by themselves with little or no human guidance. Now, in order for a car to decide its next step, i.e. either to move forward or to apply breaks, or to turn, it must know the location of all the objects around it. Using Object Detection techniques, the car can detect objects like other cars, pedestrians, traffic signals, etc.
Face Detection and Face RecognitionFace detection and recognition are perhaps the most widely used applications of computer vision. Every time you upload a picture on Facebook, Instagram or Google Photos, it automatically detects the people in the images. This is the power of computer vision at work.
Action Recognition Object CountingNow, here’s the thing – most of the applications require real-time analysis. The dynamic nature of our industry leans heavily towards instant results and that’s where real-time object detection comes into the picture.
Why Real-Time Object Detection?Let’s take the example of self-driving cars. Consider that we have trained an object detection model which takes a few seconds (say 2 seconds per image) to detect objects in an image and we finally deployed this model in a self-driving car.
Do you think this model will be good? Will the car be able to detect objects in front of it and take action accordingly?
Certainly not! The inference time here is too much. The car will take a lot of time to make decisions which might lead to serious situations like accidents as well. Hence, in such scenarios, we need a model that will give us real-time results. The model should be able to detect objects and make inferences within microseconds.
Some of the commonly used algorithms for object detection include RCNN, Fast RCNN, Faster RCNN, and YOLO.
The aim of this article is not to deep dive into these techniques but to understand the SlimYOLOv3 architecture for real-time object detection. If you wish to learn more about these techniques, check out the below tutorials:
These techniques work really well when we do not need real-time detection. Unfortunately, they tend to stumble and fall when faced with the prospect of real-time analysis. Let’s look at some of the challenges you might encounter when trying to build your own real-time object detection model.
Challenges of Performing Real-Time Object DetectionReal-time object detection models should be able to sense the environment, parse the scene and finally react accordingly. The model should be able to identify what all types of objects are present in the scene. Once the type of objects have been identified, the model should locate the position of these objects by defining a bounding box around each object.
So, there are two functions here. First, classifying the objects in the image (image classification), and then locating the objects with a bounding box (object detection).
We can potentially face multiple challenges when we are working on a real-time problem:
How do we deal with variations? The variations might be of difference in the shape of objects, brightness level, etc.
Deploying object detection models. This generally takes A LOT of memory and computation power, especially on machines we use on a daily basis
Finally, we must also keep a balance between detection performance and real-time requirements. Generally, if the real-time requirements are met, we see a drop in performance and vice versa. So, balancing both these aspects is also a challenge
So how can we overcome these challenges? Well – this is where the crux of the article begins- the SlimYOLOv3 framework! SlimYOLOv3 aims to deal with these limitations and perform real-time object detection with incredible precision.
Let’s first understand what SlimYOLOv3 is and then we will look at the architecture details to have a better understanding of the framework.
Introduction to SlimYOLOv3Can you guess how a deep learning pipeline works? Here’s a quick summary of a typical process:
First, we design the model structure
Fine-tune the hyperparameters of that model
Train the model and
Finally, evaluate it
There are multiple components or connections in the model. Some of these connections, after a few iterations, become redundant and hence we can remove these connections from the model. Removing these connections is referred to as pruning.
Pruning will not significantly impact the performance of the model and the computation power will reduce significantly. Hence, in SlimYOLOv3, pruning is performed on convolutional layers. We will learn more about how this pruning is done in the next section of this article.
After pruning, we fine-tune the model to compensate for the degradation in the model’s performance.
A pruned model results in fewer trainable parameters and lower computation requirements in comparison to the original YOLOv3 and hence it is more convenient for real-time object detection.
Let’s now discuss the architecture of SlimYOLOv3 to get a better and clearer understanding of how this framework works underneath.
Understanding the Architecture of SlimYOLOv3The below image illustrates how SlimYOLOv3 works:
SlimYOLOv3 is the modified version of YOLOv3. The convolutional layers of YOLOv3 are pruned to achieve a slim and faster version. But wait – why are we using YOLOv3 in the first place? Why not other object detection algorithms like RCNN, Faster RCNN?
Why YOLOv3?There are basically two types (or two categories) of deep object detection models:
Detectors belonging to the RCNN family fall under two-stage detectors. The process contains two stages. First, we extract region proposals and then classify each proposal and predict the bounding box. These detectors generally lead to good detection accuracy but the inference time of these detectors with region proposals requires huge computation and run-time memory
Detectors belonging to the YOLO series fall under single stage detectors. It is a single-stage process. These models utilize the predefined anchors that cover spatial position, scales, and aspect ratios across an image. Hence, we do not need an extra branch for extracting region proposals. Since all computations are in a single network, they are more likely to run faster than the two-stage detectors. YOLOv3 is also a single stage detector and currently the state-of-the-art for object detection
Sparsity trainingThe next step is the sparsity training of this YOLOv3 model:
Here, we prune the YOLOv3 model using the following steps:
First, we evaluate the importance of each component of the YOLOv3 model. I will discuss the details of how to decide the importance of these components shortly
Once the importance is evaluated, we remove the less important components
The removed components can either be an individual neural connection or the network structures. To define the importance of each component, we rank each neuron of the network based on their contribution. There are multiple ways to do it:
We can take the L1/L2 regularized means of neuron weights
The mean activation of each neuron
Number of times the output of a neuron wasn’t zero
In SlimYOLOv3, the importance is calculated based on the L1 regularized means of neuron weights which are considered as the scaling factor. The absolute value of these scaling factors is the importance of a channel. To accelerate the convergence and improve the generalization of the YOLOv3 model, the batch normalization layer is used after every convolutional layer.
SlimYOLOv3We then define a global threshold, let’s say ŷ, and discard any channel that has a scaling factor less than this threshold. In this way, we prune the YOLOv3 architecture and get the SlimYOLOv3 architecture:
While evaluating the scaling factor, the maxpool layers and the upsample layers of the YOLOv3 architecture have not been considered since they have nothing to do with the channel number of the layer number.
Fine-tuningWe now have the SlimYOLOv3 model, so what’s next?
We fine-tune it so as to compensate for the degradation in performance and finally evaluate the fine-tuned model to determine whether the pruned model is suitable for deployment.
In SlimYOLOv3, a penalty factor of α = 0.0001 is used to perform channel pruning.
End NotesWe’ve covered a lot of ground in this article. We saw the different object detection algorithms like RCNN, Fast RCNN, Faster RCNN, as well as the current state-of-the-art for object detection YOLO. Then, we looked at the SlimYOLOv3 architecture which is the pruned version of YOLO and can be used for real-time object detection.
I’m excited to get my hands on the code for SlimYOLOv3! I will try to implement SlimYOLOv3 and will share my learning with you guys.
Related
You're reading A Friendly Introduction To Real
Yourport: A Friendly And Feature
“YourPort” is a unique startpage that combines all these fortes into a singular package.
Introduction Usage and FeaturesWhen you first visit YourPort you will view search engine features at the top of the site’s page. To search for anything you can type in your query, select the sort of results you want – web, images, maps, or video – and then execute the search.
By default the search engine is Google; this can always be changed to one of the alternatives offered: Yahoo, AltaVista, Bing, AOL, or Ask.
In the top left of these shortcuts you will notice buttons that let you create custom tabs that include your own shortcuts. New tabs can be created and existing ones can be renamed or deleted.
For fresh tabs you will find empty shortcut slots as shown in the image below.
Alternatively you can select from a list of popular sites already available in YourPort’s options. To access these options place the mouse pointer in the top right of an empty slot. A ‘Popular Sites’ button will appear that will contain options to add the shortcuts for popular websites; these websites will be sorted in the menu categorically.
While adding site shortcuts you do not need to adhere to any positioning rules. You can start filling the shortcuts in whichever order you are comfortable with. You can fill all the empty shortcut slots or only some of them.
The “Share this tab” option lets you quickly share your customized tabs with your contacts. A direct URL can be obtained for the tab so you can land on your favorite ports tab rather than the default tab.
ConclusionWith its simple interface, user-friendliness, and richness of features YourPort serves as an excellent startpage. To quickly access your most frequented websites, all you have to do is start using YourPort. Once you begin using the site it will quite easily replace whichever startpage you are currently using.
Visit YourPort
Hammad
Hammad is a Business student and computer geek who cover latest technology news and reviews at AppsDaily. Apart from that, I like to review web services and softwares which can be helpful for the readers.
Subscribe to our newsletter!
Our latest tutorials delivered straight to your inbox
Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time.
A Gentle Introduction To Handling A Non
Introduction
They all fall under the concept of time series data! You cannot accurately predict any of these results without the ‘time’ component. And as more and more data is generated in the world around us, time series forecasting keeps becoming an ever more critical technique for a data scientist to master.
But time series is a complex topic with multiple facets at play simultaneously.
For starters, making the time series stationary is critical if we want the forecasting model to work well. Why? Because most of the data you collect will have non-stationary trends. And if the spikes are erratic how can you be sure the model will work properly?
The focus of this article is on the methods for checking stationarity in time series data. This article assumes that the reader is familiar with time series, ARIMA, and the concept of stationarity. Below are some references to brush up on the basics:
Table of contents
A Short Introduction to Stationarity
Loading the Data
Methods to Check Stationarity
ADF Test
KPSS Test
Types of Stationarity
Strict Stationary
Trend Stationary
Difference Stationary
Making a Time Series Stationary
Differencing
Seasonal Differencing
Log transform
1. Introduction to Stationarity‘Stationarity’ is one of the most important concepts you will come across when working with time series data. A stationary series is one in which the properties – mean, variance and covariance, do not vary with time.
Let us understand this using an intuitive example. Consider the three plots shown below:
In the first plot, we can clearly see that the mean varies (increases) with time which results in an upward trend. Thus, this is a non-stationary series. For a series to be classified as stationary, it should not exhibit a trend.
Moving on to the second plot, we certainly do not see a trend in the series, but the variance of the series is a function of time. As mentioned previously, a stationary series must have a constant variance.
If you look at the third plot, the spread becomes closer as the time increases, which implies that the covariance is a function of time.
The three examples shown above represent non-stationary time series. Now look at a fourth plot:
In this case, the mean, variance and covariance are constant with time. This is what a stationary time series looks like.
Think about this for a second – predicting future values using which of the above plots would be easier? The fourth plot, right? Most statistical models require the series to be stationary to make effective and precise predictions.
So to summarize, a stationary time series is the one for which the properties (namely mean, variance and covariance) do not depend on time. In the next section we will cover various methods to check if the given series is stationary or not.
2. Loading the DataIn this and the next few sections, I will be introducing methods to check the stationarity of time series data and the techniques required to deal with any non-stationary series. I have also provided the python code for applying each technique. You can download the dataset we’ll be using from this link: AirPassengers.
Before we go ahead and analyze our dataset, let’s load and preprocess the data first.
#loading important libraries import pandas as pd import matplotlib.pyplot as plt %matplotlib inline #reading the dataset train = pd.read_csv('AirPassengers.csv') #preprocessing train.timestamp = pd.to_datetime(train.Month , format = '%Y-%m') train.index = train.timestamp train.drop('Month',axis = 1, inplace = True) #looking at the first few rows #train.head()#Passengers
Month
1949-01-01 112
1949-02-01 118
1949-03-01 132
1949-04-01 129
1949-05-01 121
Looks like we are good to go!
3. Methods to Check StationarityThe next step is to determine whether a given series is stationary or not and deal with it accordingly. This section looks at some common methods which we can use to perform this check.
Visual testConsider the plots we used in the previous section. We were able to identify the series in which mean and variance were changing with time, simply by looking at each plot. Similarly, we can plot the data and determine if the properties of the series are changing with time or not.
train['#Passengers'].plot()Although its very clear that we have a trend (varying mean) in the above series, this visual approach might not always give accurate results. It is better to confirm the observations using some statistical tests.
Statistical testInstead of going for the visual test, we can use statistical tests like the unit root stationary tests. Unit root indicates that the statistical properties of a given series are not constant with time, which is the condition for stationary time series. Here is the mathematics explanation of the same :
Suppose we have a time series :
yt = a*yt-1 + ε t
where yt is the value at the time instant t and ε t is the error term. In order to calculate yt we need the value of yt-1, which is :
yt-1 = a*yt-2 + ε t-1
If we do that for all observations, the value of yt will come out to be:
yt = an*yt-n + Σεt-i*ai
If the value of a is 1 (unit) in the above equation, then the predictions will be equal to the yt-n and sum of all errors from t-n to t, which means that the variance will increase with time. This is knows as unit root in a time series. We know that for a stationary time series, the variance must not be a function of time. The unit root tests check the presence of unit root in the series by checking if value of a=1. Below are the two of the most commonly used unit root stationary tests:
ADF (Augmented Dickey Fuller) TestThe Dickey Fuller test is one of the most popular statistical tests. It can be used to determine the presence of unit root in the series, and hence help us understand if the series is stationary or not. The null and alternate hypothesis of this test are:
Null Hypothesis: The series has a unit root (value of a =1)
Alternate Hypothesis: The series has no unit root.
If we fail to reject the null hypothesis, we can say that the series is non-stationary. This means that the series can be linear or difference stationary (we will understand more about difference stationary in the next section).
Python code:
#define function for ADF test from statsmodels.tsa.stattools import adfuller def adf_test(timeseries): #Perform Dickey-Fuller test: print ('Results of Dickey-Fuller Test:') dftest = adfuller(timeseries, autolag='AIC') dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used']) for key,value in dftest[4].items(): dfoutput['Critical Value (%s)'%key] = value print (dfoutput) #apply adf test on the series adf_test(train['#Passengers']) Results of Dickey-Fuller Test: Test Statistic 0.815369 p-value 0.991880 #Lags Used 13.000000 Number of Observations Used 130.000000 Critical Value (1%) -3.481682 Critical Value (5%) -2.884042 Critical Value (10%) -2.578770 dtype: float64Test for stationarity: If the test statistic is less than the critical value, we can reject the null hypothesis (aka the series is stationary). When the test statistic is greater than the critical value, we fail to reject the null hypothesis (which means the series is not stationary).
2 . KPSS (Kwiatkowski-Phillips-Schmidt-Shin) TestKPSS is another test for checking the stationarity of a time series (slightly less popular than the Dickey Fuller test). The null and alternate hypothesis for the KPSS test are opposite that of the ADF test, which often creates confusion.
The authors of the KPSS test have defined the null hypothesis as the process is trend stationary, to an alternate hypothesis of a unit root series. We will understand the trend stationarity in detail in the next section. For now, let’s focus on the implementation and see the results of the KPSS test.
Null Hypothesis: The process is trend stationary.
Alternate Hypothesis: The series has a unit root (series is not stationary).
Python code:
#define function for kpss test from statsmodels.tsa.stattools import kpss #define KPSS def kpss_test(timeseries): print ('Results of KPSS Test:') kpsstest = kpss(timeseries, regression='c') kpss_output = pd.Series(kpsstest[0:3], index=['Test Statistic','p-value','Lags Used']) for key,value in kpsstest[3].items(): kpss_output['Critical Value (%s)'%key] = value print (kpss_output)Results of KPSS test: Following are the results of the KPSS test – Test statistic, p-value, and the critical value at 1%, 2.5%, 5%, and 10% confidence intervals. For the air passengers dataset, here are the results:
Results of KPSS Test: Test Statistic 1.052175 p-value 0.010000 Lags Used 14.000000 Critical Value (10%) 0.347000 Critical Value (5%) 0.463000 Critical Value (2.5%) 0.574000 Critical Value (1%) 0.739000 dtype: float64
Test for stationarity: If the test statistic is greater than the critical value, we reject the null hypothesis (series is not stationary). If the test statistic is less than the critical value, if fail to reject the null hypothesis (series is stationary). For the air passenger data, the value of the test statistic is greater than the critical value at all confidence intervals, and hence we can say that the series is not stationary.
I usually perform both the statistical tests before I prepare a model for my time series data. It once happened that both the tests showed contradictory results. One of the tests showed that the series is stationary while the other showed that the series is not! I got stuck at this part for hours, trying to figure out how is this possible. As it turns out, there are more than one type of stationarity.
So in summary, the ADF test has an alternate hypothesis of linear or difference stationary, while the KPSS test identifies trend-stationarity in a series.
3. Types of StationarityLet us understand the different types of stationarities and how to interpret the results of the above tests.
Strict Stationary: A strict stationary series satisfies the mathematical definition of a stationary process. For a strict stationary series, the mean, variance and covariance are not the function of time. The aim is to convert a non-stationary series into a strict stationary series for making predictions.
Trend Stationary: A series that has no unit root but exhibits a trend is referred to as a trend stationary series. Once the trend is removed, the resulting series will be strict stationary. The KPSS test classifies a series as stationary on the absence of unit root. This means that the series can be strict stationary or trend stationary.
Difference Stationary: A time series that can be made strict stationary by differencing falls under difference stationary. ADF test is also known as a difference stationarity test.
It’s always better to apply both the tests, so that we are sure that the series is truly stationary. Let us look at the possible outcomes of applying these stationary tests.
4. Making a Time Series Stationary
Now that we are familiar with the concept of stationarity and its different types, we can finally move on to actually making our series stationary. Always keep in mind that in order to use time series forecasting models, it is necessary to convert any non-stationary series to a stationary series first.
DifferencingIn this method, we compute the difference of consecutive terms in the series. Differencing is typically performed to get rid of the varying mean. Mathematically, differencing can be written as:
yt‘ = yt – y(t-1)
where yt is the value at a time t
Applying differencing on our series and plotting the results:
train['#Passengers_diff'] = train['#Passengers'] - train['#Passengers'].shift(1) train['#Passengers_diff'].dropna().plot() Seasonal DifferencingIn seasonal differencing, instead of calculating the difference between consecutive values, we calculate the difference between an observation and a previous observation from the same season. For example, an observation taken on a Monday will be subtracted from an observation taken on the previous Monday. Mathematically it can be written as:
yt‘ = yt – y(t-n)
n=7 train['#Passengers_diff'] = train['#Passengers'] - train['#Passengers'].shift(n) TransformationTransformations are used to stabilize the non-constant variance of a series. Common transformation methods include power transform, square root, and log transform. Let’s do a quick log transform and differencing on our air passenger dataset:
train['#Passengers_log'] = np.log(train['#Passengers']) train['#Passengers_log_diff'] = train['#Passengers_log'] - train['#Passengers_log'].shift(1) train['#Passengers_log_diff'].dropna().plot() End NotesIn this article we covered different methods that can be used to check the stationarity of a time series. But the buck doesn’t stop here. The next step is to apply a forecasting model on the series we obtained. You can refer to the following article to build such a model: Beginner’s Guide to Time Series Forecast.
Related
Phonerescue – A Friendly And Speedy Android Data Recovery Tool
This is a sponsored article and was made possible by iMobie. The actual contents and opinions are the sole views of the author who maintains editorial independence, even when a post is sponsored.
Data on your phone can be a fragile thing. One second you’re tapping through your family albums, the next second you’re uploading them to Facebook without realising, then POOF, before you know it, you’ve swiped them out of existence.
PhoneRescue claims to offer a solution to that, letting you recover not only photos, but also contacts, call logs, messages, videos, music and app documents. The mobile data recovery app has already earned itself a solid reputation for the iPhone, and now it’s arrived on Android. I got the chance to put it through its paces.
SetupThere are quite a few things you need to do before your phone is ready for the data recovery, but at least the app is honest about them by giving you a list of “Quick Tips” to consider before doing your data recovery. It doesn’t say any of them are mandatory, but seeing as it’s worded like a bunch of instructions rather than tips, it’s probably best you do what it says.
During setup the app tells you that you need to have a rooted device to access the “Deep Scan” functionality and even offers to do your rooting for you. While a nice idea in principle, this option sadly didn’t work for me. As there’s a separate Android app for each manufacturer (Samsung, HTC, Sony etc.), maybe it got confused because I have an unlocked HTC One M8 running LineageOS, rather than the default HTC UI.
My bootloader was already unlocked, and it was just a case of me flashing SuperSU to get root privileges, so getting root access wasn’t much of a problem for me. With that said, if you’re phone’s unrooted and you want a deep scan, be prepared to find your own rooting solutions if the built-in one here doesn’t work.
You’ll also need to enable USB debugging and grant root access to PhoneRescue, adding a couple more hoops to the process before you’re ready.
Using PhoneRescueOnce it’s made its findings, you can choose to recover files to your PC or directly to your phone, which is welcome. It does the job quickly, too, so you won’t be twiddling your thumbs for too long while waiting for it to complete its search.
The developers’ website has plenty of tips for using PhoneRescue, including guides on how to recover photos and how to recover messages, respectively.
Is PhoneRescue Reliable?It’s the most crucial question. After all that rooting and effort to get it working, does PhoneRescue do the job? The answer is yes, to an extent. I’ve factory-reset my phone a couple of times in the past, and PhoneRescue didn’t manage to find anything from before those resets. (There’s a good chance they’ve been completely overwritten by now, so it may well have been an impossible task.)
PhoneRescue did, however, do a good job of finding files I’d deleted from my device since the reset – photos, messages, and a video, to be exact. It’s also very quick and easy on the eyes, making it less intimidating than certain other recovery software.
ConclusionPhoneRescue (the HTC version, at least) is a very good tool but doesn’t yet match the deep-scanning feature set of the iPhone version which includes more tools and tweaks than this one. With time, however, it can catch up. Less experienced users or those just wanting a quick-and-easy tool for recovering their phone data will find everything they need here, and the fact that it does its job so quickly makes it a winner.
Download PhoneRescue (Android version)
Robert Zak
Content Manager at Make Tech Easier. Enjoys Android, Windows, and tinkering with retro console emulation to breaking point.
Subscribe to our newsletter!
Our latest tutorials delivered straight to your inbox
Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time.
A Simple Introduction To Web Scraping With Beautiful Soup
The purpose of this series is to learn to extract data from websites. Most of the data in websites are in HTML format, then the first tutorial explains the basics of this markup language. The second guide shows a way to scrape data easily using an intuitive web scraping tool, which doesn’t need any knowledge of HTML. Instead, the last tutorials are focused on gathering data with Python from the web. In this case, you need to grasp to interact directly with HTML pages and you need some previous knowledge of it.
The post is the fourth in a series of tutorials to build scrapers. Below, there is the full series:
As an example, I am going to parse a web page using two Python libraries, Requests and Beautiful Soup. The list of countries by greenhouse gas emissions will be extracted from Wikipedia as in the previous tutorials of the series.
You surely aren’t allowed to scrape data from all the websites. I recommend you first look at the chúng tôi file to avoid legal implications. You only have to add ‘/robots.txt’ at the end of the URL to check the sections of the website allowed/not allowed.
Web scraping is the process of collecting data from the web page and store it in a structured format, such as a CSV file. For example, if you want to predict the Amazon product review’s ratings, you could be interested in gathering information about that product on the official website.
1. Import librariesThe first step of the tutorial is to check if all the required libraries are installed:
!pip install beautifulsoup4 !pip install requestsOnce we terminated to look, we need to import the libraries:
Let’s import:
from bs4 import BeautifulSoup import requests import pandas as pdBeautiful Soup is a library useful to extract data from HTML and XML files. A sort of parse tree is built for the parsed page. Indeed, an HTML document is composed of a tree of tags. I will show an example of HTML code to make you grasp this concept.
Illustration by Author
Since the HTML has a tree structure, there are also ancestors, descendants, parents, children and siblings.
2. Create Response ObjectTo get the web page, the first step is to create a response object, passing the URL to the get method.
req = requests.get(url) print(req) Request-Response Protocol. Illustration by Author.
This operation can seem mysterious, but with a simple image, I show how it works. The client communicates with the server using a HyperText Transfer Protocol(HTTP). In this line of code, it’s like when we type the link on the address bar, the browser transmits the request to the server and then the server performs the requested action after it looked at the request.
3. Create a Beautiful Soup objectLet’s create the Beautiful Soup object, which parses the document using the HTML parser. In this way, we transform the HTML code into a tree of Python objects, as I showed before in the illustration.
soup = BeautifulSoup(req.text,"html.parser") print(soup)If you print the object, you’ll see all the HTML code of the web page.
4. Explore HTML treeAs you can observe, this tree contains many tags, which contain different types of information. We can get access directly to the tags, just writing:
soup.head soup.body soup.body.h1A more efficient way is to use the find and find_all methods, which filter the element(s in case of find_all method).
row1 = tab.find('tr') print(row1)Using the find method, we zoom a part of the document within the
tags, which are used to build each row of the table. In this case, we got only the first row because the function extracts only one element. Instead, if we want to gather all the rows of the table, we use the other method:
rows = tab.find_all('tr') print(len(rows)) print(rows[0])We obtained a list with 187 elements. If we show the first item, we’ll see the same output as before. find_all method is useful when we need to zoom in on more parts with the same tag within the document.
5. Extract elements of the table
To store all the elements, we create a dictionary, which will contain only the names of the columns as keys and empty lists as values.
rows = tab.find_all('tr') cols = [t.text.rstrip() for t in rows[0].find_all('th')] diz = {c:[] for c in cols} print(diz)So, we iterate over the rows of the table, excluding the first:
for r in rows[1:]: diz[cols[0]].append(r.find('th').text. replace('xa0', '').rstrip()) row_other = r.find_all('td') for idx,c in enumerate(row_other): cell_text = c.text.replace('xa0', '').rstrip() diz[cols[idx+1]].append(cell_text)The first column is always contained within the
tags, while the other columns are within the
tags. To avoid having “n” and “xa0”, we use respectively the rstrip and replace functions.
In this way, we extract all the data contained in the table and save it into a dictionary. Now, we can transform the dictionary into a pandas DataFrame and export it into a CSV file:
df = pd.DataFrame(diz) df.head() df.to_csv('tableghg.csv')Finally, we can have an overview of the table obtained. Isn’t it amazing? And I didn’t write many lines of code.
Final thoughtsI hope you found useful this tutorial. Beautiful Soup can be the right tool for you when the project is small. On the other hand, if you have to deal with more complex items in a web page, such as Javascript elements, you should opt for another scraper, Selenium. In the last case, it’s better to check the third tutorial of the series. Thanks for reading. Have a nice day!
A Basic Introduction To Activation Function In Deep Learning
This article was published as a part of the Data Science Blogathon.
Introduction
The activation function is defined as follows: The activation function calculates a weighted total and then adds bias to it to decide whether a neuron should be activated or not. The Activation Function’s goal is to introduce non-linearity into a neuron’s output.
A Neural Network without an activation function is basically a linear regression model in Deep Learning, since these functions perform non-linear computations on the input of a Neural Network, enabling it to learn and do more complex tasks. Therefore, studying the derivatives and application of activation functions, also as analysing the pros and drawbacks of each activation function, is essential for selecting the proper type of activation function that may give non-linearity and accuracy in a particular Neural Network model.
We know that neurons in a Neural Network work following their weight, bias, and activation function. We would change the weights and biases of the neurons in a Neural Network based on the output error. Back-propagation is the term for this process. Because the gradients are supplied simultaneously with the error to update the weights and biases, activation functions, therefore, enable back-propagation.
Table of Contents
Why Do We Need Activation Functions in CNN?
Variants Of Activation Function
Python Code Implementation
Conclusion
Why do we need it?Non-linear activation functions: Without an activation function, a Neural Network is just a linear regression model. The activation function transforms the input in a non-linear way, allowing it to learn and as well as accomplish more complex tasks.
Mathematical proof:-
The diagram’s elements include:- A hidden layer, i.e. layer 1:- A visible layer, i.e. layer 2:- A visible layer, i.e.
a(1) = z(1)
= W(1)X + b(1) (1)
Here,
Layer 1’s vectorized output is z(1).
W(1) denotes the vectorized weights (w1, w2, w3, and w4) applied to hidden layer neurons, X denotes the vectorized input features (i1 and i2), and b denotes the vectorized bias (b1 and b2).
Any linear function has a(1) being vectorized form.
(Note that the activation function is not taken into consideration here.)
The output layer, or layer 2, is as follows:
Layer 2 input is z(2) = W(2)a(1) + b(2) a(2) = z(2)a(1) + b(2) a(2) = z(2)a(1) + b(2) a(2) = z(2)a(1) + b(2) a(2) = z(2)a(1) + b(2) a(2) (2)
Output layer calculation:
lue of z(1).
(W(2) * [W(1)X + b(1)]) + b = (W(2) * [W(1)X + b(1)]) (2)
[W(2) * W(1)] = z(2) * [W(2)*b(1) + b(2)] + X
[W(2) * W(1)] = z(2) * [W(2)*b(1) + b(2)] + X
Let,
W = [W(2) * W(1)]
b = [W(2)*b(1) + b(2)]
z(2) = W*X + b is the final result.
Which is a linear function once again.
Even after applying a hidden layer, this observation yields a linear function, hence we can deduce that no matter how many hidden layers we add to a Neural Network, all layers will behave the same way because the combination of two linear functions yields a linear function.
1). Linear Function: –
• Equation: The equation for a linear function is y = ax, which is very much similar to the equation for a straight line.
• -inf to +inf range
• Applications: The linear activation function is only used once, in the output layer.
Source: V7labs
• Problems: If we differentiate a linear function to introduce non-linearity, the outcome will no longer be related to the input “x” and the function will become constant, hence our procedure will not show any behaviour.
For example, determining the price of a home is a regression problem. Because the price of an apartment might be a large or little number, we can employ linear activation at the output layer. Even in this case, any non-linear function at the hidden layers of the Neural Network is required.
2) The sigmoid function:
• It’s a function that is being plotted in the form of ‘S’ Shape.
• Formula: A = 1/(1 + e-x)
• Non-linear in nature. The values of X ranges from -2 to 2, but the Y values are highly steep. This indicates that slight changes in x will result in massive changes in Y’s value.
• 0 to 1 value of the range
3). Tanh Function: Tanh function, also identified as Tangent Hyperbolic function, is an activation that almost always works better than sigmoid function. It’s simply a sigmoid function that has been adjusted. Both are related and can be deduced from one another.
• Equation: f(x) = tanh(x) = 2/(1 + e-2x) – 1 OR tanh(x) = 2 * sigmoid(2x) – 1 OR tanh(x) = 2 * sigmoid(2x) – 1
• Range of values: -1 to +1
• Uses:- Usually employed in hidden layers of a Neural Network since its values change from -1 to 1, causing the hidden layer’s mean to be 0 or very close to it, which aids in data centring by bringing the mean close to 0. This makes learning the next layer much more straight.
4). RELU (Rectified linear unit) is the fourth letter in the alphabet. It’s the most used activation method. Hidden layers of neural networks are primarily used.
• Formula: A(x) = maximum (0,x). If x is positive, it returns x; else, it returns 0.
• Value Range: (inf, 0)
• Non-linear in nature, which means simply backpropagating errors and also having the ReLU function activating many layers of neurons.
• Applications: Because it includes fewer mathematical operations, ReLu is less computationally expensive than tanh and sigmoid. Only a few neurons are active at a time, making the network scarce and efficient for computation.
Simply put, the RELU function learns much faster than the sigmoid and Tanh functions.
5). Softmax Function: The softmax function is a type of sigmoid function that comes in handy when dealing with categorization issues.
• Non-linearity in nature
• Uses: Typically utilised when dealing with many classes. The softmax function would divide by the sum of the outputs and squeeze the outp
• Output: The softmax function is best used in the classifier’s output layer, where we’re trying to define the class of each input using probabilities.
Selecting The Right Activation Function
If one is unsure about the activation function to utilise, just select RELU, which is a broad activation function that is used in most circumstances these days. If our output layer is meant to be used for binary identification/detection, the sigmoid function is an obvious choice.
import numpy as np #designing the function for sigmoidfig, ax = plt.subplots(figsize=(9, 5))
#Axis spines are basically the lines that are confining the given plot area ax.spines['left'].set_position('center') ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') axxaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') # Create and show the sigmoid plotax.plot(x,sigmoid(x)[1], color=”#9621E2″, linewidth=3, label=”derivative”)
#placing the legend on the upper right corner of the axes ax.legend(loc="upper right", frameon=False) fig.show()Output, Source: Author
ConclusionRead more blogs on Analytics Vidhya.
My name is Pranshu Sharma and I am a Data Science Enthusiast
The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion.
Related
Update the detailed information about A Friendly Introduction To Real on the Minhminhbmm.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!