Showing posts with label classification. Show all posts
Showing posts with label classification. Show all posts

YOLOv8

In this post, I would like to describe the architecture of YOLOv8 from first principles.

Overview

Object classification using bounding boxes

The YOLO algorithm does object classification using bounding boxes.

YOLO uses boxes instead of more natural polygons because they allow the network to represent object locations with just 4 numbers, making computation more efficient and consistent across all grid cells. Polygons would require a variable number of points per object, complicating tensor shapes and slowing training and inference. Boxes are also easier to label, faster to process, and accurate enough for most real-time detection tasks that aim to locate and classify, not to outline exact shapes.

Image preprocessing and input tensor formation

The YOLO algorithm begins with a standard RGB image, typically captured in an 8-bit-per-channel format.
Each pixel’s three color values are first normalized from 0–255 to 0–1, which keeps numerical values small enough for the optimizer to adjust weights effectively.
The image is then resized to a fixed square dimension, such as 640×640, preserving the aspect ratio by adding gray padding (letterboxing) when needed. This keeps every image in a batch at the same height and width, so they can be stacked into a single tensor and processed together by the network.
Finally, the normalized and resized image is reordered from the height-width-channel (HWC) layout to the channel-height-width (CHW) layout, forming a contiguous tensor of shape [3, H, W]. This tensor serves as the model’s input layer, with each of the three planes corresponding to the red, green, and blue channels, which the convolutional filters process in parallel to extract spatial and color features.

What is a tensor?

A tensor is a structured container for numbers with many dimensions. A single number is called a rank-0 scalar, a list is a rank-1 vector, a table is a rank-2 matrix, and an image with multiple color channels is a three-dimensional rank-3 tensor. Each dimension represents an axis, such as height, width, or color channels. Tensors enable the network to perform mathematical operations on entire blocks of data at once, which is why they are efficient for training and running deep learning models on GPUs.

Additional input dimensions

In some systems, more channels can be added to the RGB input to provide extra information. An alpha channel can represent transparency, creating an RGBA image where each pixel holds red, green, blue, and opacity values. A depth channel can store distance information, which is helpful in applications that combine vision with LiDAR or stereo sensors. These additional channels simply expand the tensor’s channel dimension, for example, from [3, H, W] to [4, H, W] or [5, H, W]. The network can then be trained or fine-tuned to interpret these extra inputs as part of its feature extraction process.

Feature extraction backbone

After the input tensor is formed, YOLOv8 passes it through the backbone, a sequence of convolutional layers (CNN) that gradually transform raw pixel values into abstract features.
Early layers detect simple patterns such as edges and colors, while deeper layers capture shapes and object parts.

In YOLOv8, the backbone is a sequence of convolutional layers organized into repeating modules rather than a single long stack. It contains roughly 50–70 layers, depending on the model size (n, s, m, l, or x). These layers are grouped into functional blocks, each performing a specific transformation.

Initial convolution

The first layer expands the 3-channel RGB input into a larger set of feature maps (e.g., 32 or 64 channels) using a 3×3 kernel and stride 2, effectively halving the spatial resolution. This captures low-level textures, such as edges and corners.

C2f modules

These are improved versions of the CSP (Cross Stage Partial) blocks used in earlier YOLO versions. Each C2f module consists of several smaller convolutional layers connected by skip paths. The idea is to allow part of the feature map to bypass heavy computation, which improves gradient flow and efficiency.

Downsampling layers

Between major C2f blocks, 3×3 convolutions with stride 2 reduce the width and height of the feature maps while increasing the number of channels. This concentrates information and allows the network to detect objects of different sizes.


Batch normalization and activation

After each convolution, YOLOv8 applies batch normalization to stabilize training and SiLU (Sigmoid Linear Unit) activation to introduce nonlinearity. SiLU helps the network learn smooth transitions and often outperforms ReLU in detection tasks.

SPPF (Spatial Pyramid Pooling Fast)

At the end of the backbone, a special pooling block combines features from multiple receptive fields using different kernel sizes. This helps the network recognize objects at various scales before passing data to the neck.

The backbone produces a set of multi-scale feature maps (often at three resolutions, such as 80×80, 40×40, and 20×20) that encode both fine detail and high-level semantic information for the next stage, the neck.

The backbone reduces spatial resolution while increasing channel depth, trading image detail for semantic richness.
YOLOv8 uses a variant of the CSP (Cross Stage Partial) architecture with C2f modules, which balance accuracy and efficiency by allowing part of the feature map to bypass heavy computation.
The backbone produces a set of feature maps at multiple scales, ready to be fused by the next stage, the neck.





As an Amazon Associate I earn from qualifying purchases.

Classification - Machine Learning with Python - IBM AI Engineering certificate program on Coursera

NOTE: This is a continuation of the: "IBM AI Engineering certificate program on Coursera - Machine Learning with Python"

https://uki.blogspot.com/2022/09/ibmaiengineering.html


Please note that the Mathematic formulas (LaTex script) DO NOT show on the MOBILE phone, to read this post please use the desktop Chrome browser.

All images are copyrighted by IBM Developer Skills Network.

Table of Contents



  • Classification is a supervised machine learning approach
  • Categorizing some unknown items into a discrete set of categories or "classes".
  • Relationship between "feature" variables and the target attribute which is a categorical variable.


What is multi-class classification?

Multi-class classification can predict the target attribute (a field) with multiple discrete values to choose from.

What are examples of Multi-class classification use cases?
  • category to which the person belongs
  • churn detection: person switching brand loyalty
  • detect if a person responds to a particular advertising campaign


What are the types of classification algorithms?

  • Decision Trees (ID3, C4.5, C5.0)
  • Naïve Bayes,
  • Linear Discriminant Analysis
  • k-Nearest Neighbor
  • Logistic Regression
  • Neural Networks (DNN)
  • Support Vector Machines (SVM)


k-Nearest Neighbors (kNN)



K-Nearest Neighbors is a supervised learning algorithm. Where the data is 'trained' with data points corresponding to their classification. To predict the class of a given data point, it takes into account the classes of the 'K' nearest data points and chooses the class to which the majority of the 'K' nearest data points belong as the predicted class.


  • The kNN algorithm is a classification algorithm
  • The kNN algorithm classifies classes on their similarity to other classes 
  • The kNN can work with Minkowski/Eucleadian distance, but it does not have to.
  • The large, or the small, size of k in kNN does guarantee a good model, rather the k value needs to be found empirically.
  • kNN can be used to compute a continuous value, for example, the price of a home
How to determine which class?

Find several nearest neighbors in the data (e.g. age, income, etc.) and determine (vote) to which class they should belong.




This can be done using Euclidean Distance [reference 1].







How does the k-Nearest Neighbor work?

  • pick a value for K
  • calculate the distance of the unknown case from all cases
  • select the k-observations in the training data that are "nearest" to the unknown data point
  • predict the response of the unknown data point the most popular response value from the K-nearest neighbors.
How to select the K-value?
  • too small of the k sample may capture the "noise" in the data (an outlier)
  • a low value of k makes for a very complex model which may result in overfitting of the model
  • too large of the k can make the model overgeneralized
  • reserve part of your data to test the distribution of accuracy of the model for various k= 1 to n

How to calculate the distance between k-neighbors?
  • We can use the Minkowski distance (aka Euclidean distance)
  • normalize the unites
$$
Minkowski \ distance (x_1, x_2) =
\sqrt{
  \sum_{i=0}^{n}
  \left(
    x_{1i} - x_{2i}
  \right)^2 
} 
$$

Example (not normalized):
  • person 1
    • age 34
    • income 190
    • education 3
  • person 2
    • age 30
    • income 200
    • education 8

$$
Minkowski \ distance (x_1, x_2) =
\sqrt{
  \sum_{i=0}^{n}
  \left(
    x_{1i} - x_{2i}
  \right)^2 
} 
$$

$$ = \sqrt{  \left( 34 - 30 \right)^2 + \left( 190 - 200 \right)^2 + \left( 3 - 8 \right)^2 }  \\ = 11.87 $$



Evaluation Metrics in Classification



Harmonic Mean [see references 2, 3 ]
$$ harmonic \ mean  = \frac{2*A*B}{A+B} $$


Classification Accuracy
compare the historical actual data $ y $ vs predicted values $ \hat{y} $
  • Jaccard index (similarity coefficient, intersection-over-union)


  • F1 score (Confusion matrix)
    • TP = True Positives
    • FN = False Negatives
    • FP = False Positives
    • TN = True Negatives
    • precision = TP / (TP + FP)
      • use when identifying positives is a priority
      • example: classify butterfly species
    • recall  = TP / (TP + FN) 
      • use when false negatives are a problem
      • example: identify non-existing obstacles at highway speed
    • F1 score is a harmonic mean of the precision and recall scores
      • F1 score = $ \frac{2 * precision * recall}{precision + recall} $ 
    • Finally, we can average multiple F1 score results




Logarithmic (Log) Loss

  • Sometimes the output is the probability of the class label
    • continuous values between 0 to 1 (or 0 to 100%, or NO to YES)
  • Log Loss measures the performance of the classifier where the output is a probability

$$ error = y \cdot log(\hat{y}) + ( 1 -y ) \cdot \log(1 - \hat{y}) $$
Where:
  • $ \hat{y} $ is a predicted result
  • y is the actual observed result
Then, we can calculate the Log Loss as an average across all observations (rows n):

$$ LogLoss = - \frac{1}{n} \sum_{i=1}^{n} ( error_i ) $$

The classifier with the lower Log Loss has better performance.





Lab: kNN










References














As an Amazon Associate I earn from qualifying purchases.

Regression - Machine Learning with Python - IBM AI Engineering certificate program on Coursera

Machine Learning with Python



Please note that the Mathematic formulas (LaTex script) DO NOT show on the MOBILE phone, to read this post please use the desktop Chrome browser.

All images are copyrighted by IBM.




Definitions:

Machine learning is a subfield of computer science that gives "computers the ability to learn without being explicitly programmed."

Machine Learning tries to train on a large quantity of data and derive solutions to cases not encountered during the training.

AI, a subset of machine Learning, tries to make computers intelligent with vision, language, creativity, etc.

Deep Learning is a subset of AI, where computers learn and make decisions on their own.




The course covers:
  • Regression / Estimation
    • predicting continuous values
  • Classification
    • predicting the item class or category
  • Clustering
    • finding the structure of the data, summarization
  • Association
    • finding co-occurring items or events
  • Anomaly detection
    • discovering abnormal or unusual cases
  • Sequence mining
    • predicting next events; e.g. click stream (Markov Model, HMM)
  • Dimension Reduction
    • reducing the size of the data (PCA)
  • Recommendation Systems
    • discovering preferences
Software tools:
  • Scikit Learn
    • algorithms for machine learning
  • SciPy
    • signal processing, optimization, statistics, etc.
  • NumPy
    • arrays, dictionaries, data structures, etc.
  • MatPlotLib
    • 2D and 3D plotting
  • Pandas
    • high-performance data structures, data importing, manipulation, and analysis, numerical tables and time-series
Projects:
  • Cancer detection
  • Economic trends
  • Customer churn
  • Recommendation engines
  • more

Example:

Benign or malignant?


Pipeline:

  1. Data Preprocessing
  2. Train vs Test data split
  3. Algorithm Setup
  4. Model Fitting
  5. Prediction
  6. Evaluation
  7. Model Export
scikit-learn functions


Supervised vs Unsupervised Algorithms

Supervised learning is using a "labeled" data set.
data column = feature
data row = observation

There are 2 types of "supervised" learning:
  • Classification
  • Regression






The unsupervised model draws conclusions on unlabeled data.


Unsupervised techniques:
  • Dimension reduction
  • Density estimation
  • Market basket analysis
  • Clustering
    • Discovering structure
    • Summarization
    • Anomaly detection


 

Linear Regression

Introduction to Regression




Simple Linear Regression



Data:
  • X: independent variable
    • explanatory variables
    • can be measured on a categorical or continuous scale
  • Y: dependent variable 
    • which we try to predict
    • needs to be continuous and cannot be a discrete value
Types of regression models:
  • Simple regression  (1 feature vs the dependent variable)
    • simple linear regression
    • simple non-linear regression
  • Multiple regression   (comparing 2+ features)
    • multiple linear regression
    • multiple non-linear regression 
Applications:
  • sales forecasting
  • satisfaction analysis
  • price estimation
  • employment income
Regression algorithms:
  • ordinal regression
  • poison regression
  • fast forest quantile regression
  • Linear, Polynomial, Lasso, Stepwise, Ridge regression
  • Bayesian linear regression
  • Neural network regression
  • Boosted decision tree regression
  • KNN (K-nearest neighbors)
Fit line:
  • it is a polynomial written as $ ŷ=\theta_0 + \theta_1 x_1  $ 
    • where 
      • y is a particular, observed, dependent variable (i.e. emissions )
      • $ \hat{y} $, or y "hat" is the response variable or predicted value (ideal value on fitted regression line)
      • $  \theta_0 $ y-intercept of the line
      • $ \theta_1 $ is the slope or gradient of the line
      • $  x_1  $ is the independent variable or a single predictor (i.e engine size in liters)
      • $ \theta_0  $ and $  \theta_1 $ are also called the coefficients of the equation

Error

The difference between the ŷ and y is the error.

The mean of the squared sum of errors (differences) formula:

$$ MSE = \frac{1}{n} \sum_{i=1} ^{n} \left( y_i - ŷ_i \right)^2 $$

Objective:
We have to find the best parameters $ \theta_0 $ and $ \theta_1 $ to minimize the MSE. 

Options to find $ \theta_0 $ and $ \theta_1 $:
  • mathematical approach
  • optimization approach


$$
\theta_1 =
\frac{
\sum_{i=1} ^{s}
\left( x_i - \bar{x} \right) 
\left( y_i - \bar{y} \right) 
}{
\sum_{i=1} ^{s}
\left( x_i - \bar{x} \right)^2 
}
$$

where:
  • s = n  , or number of observations (rows in the table)
  • $ \bar{x} = \frac{\sum_{i=1} ^{n} \left( x_i \right)  }{n}  $ or, x "bar" is mean of x
  • $ \bar{y} = \frac{\sum_{i=1} ^{n} \left( y_i \right)  }{n}  $ or,  y "bar" is mean of y


Conclusions for Linear Regression:
  • very fast
  • no parameter tuning
  • easy to interpret

Model Evaluation in Regression Models


Calculate the Error:

$$
\hat{y} =
\frac{1}{n} 
\sum_{j=1}^{n}   
|    y_j - \hat{y}_j     |
$$

Understanding the difference:
  • train and test on the same data
    • High training accuracy is not necessarily a good thing
    • Overfitting
      • memorized the input to output data and produced a non-generalized model
      • aka: rote learning
      • provides bad results for the input data that the model was not trained on
  • train and test on the split data



Wrong "Out of Sample Accuracy" is the percentage of correct predictions that the model makes on data that the model has NOT been trained on.

"Out of Sample Accuracy" is the accuracy of an overly trained model (which may capture noise and produced a non-generalized model)

Evaluation:
  • testing on the portion of the test data (not split, not randomized)
    • high "training accuracy"
    • low "out of sample" accuracy
  • testing on the split data (randomized)
    • more accurate evaluation for "out of sample" accuracy
    • highly dependent on which data is selected

K-fold cross-validation





Evaluation Metrics in Regression Models

https://www.coursera.org/learn/machine-learning-with-python/lecture/5SxtZ/evaluation-metrics-in-regression-models

Error definition:
The difference between observed data points ($ y_i $) and the fitted trend (regression) line values ($ \hat{y} $).

Mean Absolute Error (MAE):    
  • easy to understand

$$
MAE =
\frac{1}{n}
\sum_{j=1}^{n}
    | \hspace{0.5em}
      y_j - \hat{y}_j
    \hspace{0.5em} |
$$

Mean Squared Error (MSE):
  • more commonly used
  • stresses the large errors, exponentially increasing them (hence $ error^2 $)

$$
MSE =
\frac{1}{n}
\sum_{j=1}^{n}
    \left(
      y_j - \hat{y}_j
    \right)^2
$$

Root Mean Squared Error (RMSE):
  • MOST commonly used
  • interpretable in the same units as the response vector, or y-units, easy to relate the information

$$
RMSE =
\sqrt{
  \frac{1}{n}
  \sum_{j=1}^{n}
      \left(
        y_j - \hat{y}_j
      \right)^2
}
$$


Relative Absolute Error (RAE):
  • aka: residual sum of squares
  • normalizes the value by dividing the derived error by the mean error
$$
RAE =
\frac
{
  \sum_{j=1}^{n}
      |
        y_j - \hat{y}_j
      |
}{
  \sum_{j=1}^{n}
      |
        y_j - \bar{y}
      |
}
$$


Relative Absolute Error (RSE):
  • widely used by the data community to calculate $ R^2 $
    • $ R^2 = 1- RSE $
    • it is a popular metric of your model: shows how close the data values are to the fitted regression line
    • the higher the $ R^2 $ the better the model fits your data
$$
RAE =
\frac
{
  \sum_{j=1}^{n}
      \left(
        y_j - \hat{y}_j
      \right)^2
}{
  \sum_{j=1}^{n}
      \left(
        y_j - \bar{y}
      \right)^2
}
$$

You should do your own investigation of when to use each error estimation method.


Lab: Simple Linear Regression (1hr)




At this point, I decided that adding about 1 hour of setup work will be beneficial in the long run:
_REPOS/UoL_CS/IBM_AI_Eng/ML_Python/ML0101EN-Reg-Simple-Linear-Regression-Co2.ipynb


Multiple Linear Regression


When should I use Multiple Linear Regression?

- When there are multiple dependent variables and EACH independent variable has a linear correlation with the dependent variable.


  • Most of the applications of Linear Regression use multiple variables.
$$  \hat{y} = \theta_0 + \theta_1 x_1 + \theta_2 x_2 + ... + \theta_n x_n $$
$$  \hat{y} = \theta^T  X $$
$$  \theta^T = \left[ \theta_0, \theta_1, \theta_2, ... \right] $$
 
$$
X = \begin{bmatrix}
1 \\
x_1 \\
x_2 \\
x_3 \\
\vdots
\end{bmatrix}
$$

Where:
  • $  \hat{y} $ is a "dot" product of two vectors $ \theta^T  X $, 
    • in one-dimensional space, it is an equation of a line
    • in two-dimensional space, it is a plane
    • in multi-dimensional space, it is a hyper-plane
  • $ \theta^T  $ is the n-by-1 vector of unknown parameters in the multi-dimensional space, 
    • traditionally it is shown as transpose $ \theta $, 
    • it is also called: 
      • the vector of parameters, 
      • vector of coefficients, 
      • or the weight vector of the regression equation
  • T indicates "transpose" (see reference 4)
  • X is the feature set vector
  • the first element of X is 1, an intercept, or bias parameter

We have to optimize the parameters $ \theta $ in $  \hat{y} = \theta^T  X $ to result in the fewest errors.

  • For example, let's assume:
    • for a given set of parameters we get the result for row 1:
      • $  \hat{y}_1 $ = 140
    • from the observation dataset, we see
      • $  y_1 $ = 196
    • hence:
      • $  y_1 - \hat{y} $ = 196 -140 = 56 
        • which is called residual error for a single observation
        • or distance from the regression line


We can use the Means Square Error formula to calculate the error for all the observations:

$$
MSE =
\frac{1}{n}
\sum_{j=1}^{n}
    \left(
      y_j - \hat{y}_j
    \right)^2
$$

Methods to find optimal coefficients:
  • Ordinary Least Squares
    • Linear algebra operations
    • it takes a long time for large datasets (10k+ rows)
    • Scikit-learn uses the plain Ordinary Least Squares method
  • Optimization Approach
    • Gradient Decent
    • a proper approach for the large data sets




Concerns:
  • multiple linear regression may give you a better predictive model
  • avoid overfitting
  • convert variables to continuous numbers
  • analyze the relationships between dependent and independent variables
    • use scatterplots to check for linearity, if there is no dependency then do not use it


Lab: Multiple Linear Regression















Non-Linear (Polynomial) Regression


Non-linear regression is a method to model the non-linear relationship between the independent variables 𝑥x and the dependent variable 𝑦y. Essentially any relationship that is not linear can be termed as non-linear and is usually represented by the polynomial of 𝑘k degrees (maximum power of 𝑥x). For example:

$$ 𝑦 = 𝑎𝑥^3 + 𝑏𝑥^2 + 𝑐𝑥 + 𝑑 $$



Non-linear functions can have elements like exponentials, logarithms, fractions, etc. For example:
$$ 𝑦 = log(𝑥) $$


$$  \hat{y} = \frac{ \theta_0 }{ 1 + \theta_1^ \left( x -  \theta_2 \right) } $$

We can have a function that's even more complicated such as :

$$ 𝑦 = log(𝑎𝑥^3 + 𝑏𝑥^2 + 𝑐𝑥 + 𝑑) $$


How do we know whether the problem is linear or non-linear?
  • inspect data visually
  • fit non-linear model
  • transform your data






Exponential (hockey stick) functions:


$$  \hat{y} =
\theta_0
+ \theta_1
\theta_2^x
$$









Logarithmic (inverse hockey stick) Regression


$$  \hat{y} =
\log{
  \left(
     \theta_0
  + \theta_1 x
  + \theta_2 x^2 
  + \theta_3 x^3 
 \right)
 }
$$





In the case below, 

$$  \hat{y} =  
\theta_0
+ \theta_1  
\log{x}
$$










Quadratic (parabolic) Regression

$$  \hat{y} =
\theta_0
+ \theta_1 x
+ \theta_2 x^2
$$

in the example below:

$$  \hat{y} =
\theta_0 * 0
+ \theta_1 x * 0
+ \theta_2 x^2
$$






Cubic (s-curve) Polynomial (3rd degree) Regression


$$  \hat{y} =
\theta_0
+ \theta_1 x
+ \theta_2 x^2
+ \theta_3 x^3
$$




Logistic Regression (sigmoid curve)



generic:
$$ Y = a + \frac{b}{1+ c^{(X-d)}} $$

Specific below:
$$ Y = \hat{y} = 0 + \frac{-3}{1 + 3^{(X-2)}} $$














Given the 3rd-degree polynomial equation:

$$
x_1 = x \\
x_2 = x^2 \\
x_3 = x^3
$$

The model is converted to a simple (special case of multiple) linear regression:


$$  \hat{y} =
\theta_0
+ \theta_1 x_1
+ \theta_2 x_2
+ \theta_3 x_3
$$

Least Squares is a method of estimating unknown parameters in a linear regression model by 
minimizing the sum of the squares of the differences between $ y $ and  $\hat{y} $.



References

  1. https://oeis.org/wiki/List_of_LaTeX_mathematical_symbols
  2. https://tex.stackexchange.com/questions/13865/how-to-use-latex-on-blogspot
  3. Special Characters (i.e. Greek) LaTex https://uki.blogspot.com/search/label/Jupyther%20Lab
  4. https://en.wikipedia.org/wiki/Transpose
  5. https://www.atqed.com/latex-column-vector





As an Amazon Associate I earn from qualifying purchases.

apt quotation..