mirror of
https://github.com/ami-sc/fire-detection.git
synced 2024-07-07 08:27:50 +02:00
Update notebooks directory
This commit is contained in:
parent
a539d7abb8
commit
83dbf73171
338
notebooks/DiscreteWaveletTransform.ipynb
Normal file
338
notebooks/DiscreteWaveletTransform.ipynb
Normal file
File diff suppressed because one or more lines are too long
|
@ -1,306 +0,0 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"***\n",
|
||||
"\n",
|
||||
"**PCA - Test Notebook**\n",
|
||||
"\n",
|
||||
"The following notebook serves as a template and testing code for the implementation of the Principal Component Analysis (PCA).\n",
|
||||
"\n",
|
||||
"***"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"- **Required Libraries:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import cv2\n",
|
||||
"import numpy as np"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(460, 860, 3)"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Load an image for testing purposes.\n",
|
||||
"test_img = cv2.imread(\"../data/fire_dataset/fire_images/fire.1.png\")\n",
|
||||
"test_img.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Visualize the image.\n",
|
||||
"cv2.imshow(\"Test Image\", test_img)\n",
|
||||
"cv2.waitKey(0)\n",
|
||||
"cv2.destroyAllWindows()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"- Red Channel Dimensions: (460, 860)\n",
|
||||
"- Green Channel Dimensions: (460, 860)\n",
|
||||
"- Blue Channel Dimensions: (460, 860)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Get the channels of the image.\n",
|
||||
"b, g, r = cv2.split(test_img)\n",
|
||||
"\n",
|
||||
"# Check the dimensions of the channels.\n",
|
||||
"print(\"- Red Channel Dimensions:\", r.shape)\n",
|
||||
"print(\"- Green Channel Dimensions:\", g.shape)\n",
|
||||
"print(\"- Blue Channel Dimensions:\", b.shape)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Normalize each channel so that each value is in the scale of [0, 1].\n",
|
||||
"# Each value (pixel) can have a maximum value of 255.\n",
|
||||
"r_norm = r / 255\n",
|
||||
"g_norm = g / 255\n",
|
||||
"b_norm = b / 255"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(3, 395600)"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# \"Flatten\" the channels.\n",
|
||||
"r_norm = r_norm.reshape([-1])\n",
|
||||
"g_norm = g_norm.reshape([-1])\n",
|
||||
"b_norm = b_norm.reshape([-1])\n",
|
||||
"\n",
|
||||
"# Stack the individual arrays to a single array.\n",
|
||||
"flat_rgb = np.vstack([r_norm, g_norm, b_norm])\n",
|
||||
"flat_rgb.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"- To perform a Principal Component Analysis (PCA), we must first center the data.\n",
|
||||
"- This is done by subtracting the mean $\\mu$.\n",
|
||||
"\n",
|
||||
"$$x' = x - \\mu$$\n",
|
||||
"\n",
|
||||
"- We consider each pixel as a sample, living in a 3-channel (3D) space.\n",
|
||||
"- This means we have a $3 \\times n$ array:\n",
|
||||
" - The rows are the dimensions.\n",
|
||||
" - The columns are the pixels.\n",
|
||||
"- The mean is calculated *across columns*.\n",
|
||||
"- We do not need to standardize because data is already in the same scale."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(3, 395600)"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Center the data.\n",
|
||||
"x_center = flat_rgb - np.mean(flat_rgb, axis = 1, keepdims = True)\n",
|
||||
"x_center.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"- Compute the covariance matrix of the centered data as follows:\n",
|
||||
"\n",
|
||||
"$$C = \\dfrac{1}{N} X \\: X^T$$\n",
|
||||
"\n",
|
||||
"- We get a $D \\times D$ matrix (in this case, $3 \\times 3$)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(3, 3)"
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"cov = np.cov(x_center)\n",
|
||||
"cov.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"- Now, we must perform the eigendecomposition of the covariance matrix.\n",
|
||||
"- We get $3$ eigenvalues associated with the $3$ corresponding eigenvectors."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"eigenvalues, eigenvectors = np.linalg.eig(cov)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"- NumPy does not return the eigenvalues and eigenvectors in sorted order.\n",
|
||||
"- We must sort them manually."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Sort the eigenvalues and eigenvectors in order of increasing variance kept.\n",
|
||||
"# The \"-\" is needed for ascending order.\n",
|
||||
"idx = np.argsort(-eigenvalues)\n",
|
||||
"eigenvalues = eigenvalues[idx]\n",
|
||||
"eigenvectors = eigenvectors[:,idx]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(9,)"
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# \"Flatten\" the eigenvectors for the model.\n",
|
||||
"eigenvectors = eigenvectors.reshape([-1])\n",
|
||||
"eigenvectors.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"***\n",
|
||||
"\n",
|
||||
"- The theory is that each eigenvector is a *summary* of each channel (one eigenvector per channel).\n",
|
||||
"- Each eigenvector captures the variation in each channel.\n",
|
||||
"- It should be an accurate representation of the relevant parts of the channel.\n",
|
||||
"- The model should be able to differentiate between a *fire* and a *non-fire* image using the eigenvectors.\n",
|
||||
"- This also means we do not need the pixels anymore. We can give the eigenvectors directly to the model."
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Fire_Detection",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.3"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
12
notebooks/README.md
Normal file
12
notebooks/README.md
Normal file
|
@ -0,0 +1,12 @@
|
|||
# `/notebooks`
|
||||
|
||||
***
|
||||
|
||||
## Directory Contents
|
||||
|
||||
- `./README.md`:
|
||||
- This file.
|
||||
- `./XGBoost_Model.ipynb`:
|
||||
- Full training and hyperparameter tuning process for the XGBoost model used in the motion detection code.
|
||||
- `./DiscreteWaveletTransform.ipynb`:
|
||||
- Example analysis for the histogram generation and wavelet decomposition.
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user