<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gayathri Selvaganapathi on Medium]]></title>
        <description><![CDATA[Stories by Gayathri Selvaganapathi on Medium]]></description>
        <link>https://medium.com/@gayathri.s.de?source=rss-c85e34fadf4a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*x9tNKkE80QIqqim3Kd9Eow.png</url>
            <title>Stories by Gayathri Selvaganapathi on Medium</title>
            <link>https://medium.com/@gayathri.s.de?source=rss-c85e34fadf4a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 24 May 2026 02:26:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gayathri.s.de/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Low Light Image Enhancement using CNN]]></title>
            <link>https://medium.com/@gayathri.s.de/low-light-image-enhancement-using-cnn-82e9eefc05b3?source=rss-c85e34fadf4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/82e9eefc05b3</guid>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[convolution-neural-net]]></category>
            <category><![CDATA[ml-model]]></category>
            <category><![CDATA[keras]]></category>
            <category><![CDATA[opencv]]></category>
            <dc:creator><![CDATA[Gayathri Selvaganapathi]]></dc:creator>
            <pubDate>Thu, 19 Sep 2024 03:54:22 GMT</pubDate>
            <atom:updated>2024-09-19T03:54:22.002Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-OpI559mgcwub0iGDtFx3w.png" /></figure><p>This project demonstrates how to build a Convolutional Neural Network (CNN) model to enhance low-light images. Using a combination of CNN layers, we aim to transform dark and noisy images into more vibrant and clear ones. The project uses the LOL dataset, which consists of paired low-light and bright images, and includes techniques for adding noise to images for training a robust enhancement model.</p><h3>Table of Content</h3><ol><li><strong>Prerequisites</strong></li><li><strong>Installation Steps</strong></li><li><strong>Model Overview</strong></li><li><strong>Model Summary</strong></li><li><strong>Training</strong></li><li><strong>Inference</strong></li><li><strong>Example Usage</strong></li><li><strong>Results</strong></li><li><strong>References</strong></li></ol><h3>1. Prerequisites</h3><ul><li>Google Colab (Recommended for using the mounted Google Drive and GPU support)</li><li>Keras</li><li>OpenCV</li><li>NumPy</li><li>Matplotlib</li><li>TQDM (for progress tracking)</li></ul><h3>2. Installation Steps</h3><ol><li>Clone the repository or download the code files.</li><li>Upload the LOL dataset(<a href="https://drive.google.com/drive/folders/1UBsbY3CczeT03BOF3a7-FJoHHL4aCHWf?usp=sharing">https://drive.google.com/drive/folders/1UBsbY3CczeT03BOF3a7-FJoHHL4aCHWf?usp=sharing</a>) to your Google Drive.</li><li>Mount Google Drive in your Colab session:</li></ol><pre>from google.colab import drive<br>drive.mount(&#39;/content/drive&#39;)</pre><p>4. Install necessary packages:</p><pre>import numpy as np <br>import pandas as pd <br>import os<br>import cv2 as cv<br>import matplotlib.pyplot as plt<br>from keras import backend as K<br>from keras.layers import add, Conv2D,MaxPooling2D,UpSampling2D,Input,BatchNormalization, RepeatVector, Reshape<br>from keras.models import Model<br>np.random.seed(1)</pre><p>4. Dataset: The dataset used is the LOL dataset for low-light image enhancement. The dataset consists of paired low-light (low) and normal light (high) images. You can download the dataset from the following link:</p><p><a href="https://drive.google.com/drive/folders/1UBsbY3CczeT03BOF3a7-FJoHHL4aCHWf?usp=sharing">https://drive.google.com/drive/folders/1UBsbY3CczeT03BOF3a7-FJoHHL4aCHWf?usp=sharing</a></p><p>5. LOL Dataset: Place the dataset in your Google Drive and modify the InputPath to point to the correct dataset directory.</p><pre>InputPath = &quot;/content/drive/MyDrive/ml_projects/low_light_image_enhancer/LOLdataset/train/high&quot;</pre><h3>3. Model Overview</h3><p>This project uses a CNN model with several convolutional layers to process and enhance the input low-light images. Below are the main components of the model:</p><ol><li>Noise Addition: Salt-and-pepper noise is artificially added to the input images to simulate real-world noise scenarios.</li></ol><pre>def addNoise(image):<br>    # salt and pepper noise<br>    noiseAddedImage = np.copy(image)<br>    <br>    # Adding salt (white) noise<br>    num_salt = np.ceil(image.size * 0.01)  # Percentage of image to be &quot;salt&quot;<br>    coords = [np.random.randint(0, i, int(num_salt)) for i in image.shape[:2]]<br>    noiseAddedImage[coords[0], coords[1], :] = 1  # Apply to all channels<br>    <br>    # Adding pepper (black) noise<br>    num_pepper = np.ceil(image.size * 0.01)  # Percentage of image to be &quot;pepper&quot;<br>    coords = [np.random.randint(0, i, int(num_pepper)) for i in image.shape[:2]]<br>    noiseAddedImage[coords[0], coords[1], :] = 0  # Apply to all channels<br>    <br>    return noiseAddedImage</pre><p>2. Data Preprocessing: The images are resized to 500x500 pixels and converted from BGR to RGB format. The images are darkened by manipulating the HSV channels to simulate low-light conditions.</p><pre>def PreProcessData(ImagePath):<br>    X_ = []<br>    y_ = []<br>    count = 0<br>    # Iterate over all images in the provided directory<br>    for imageName in tqdm(os.listdir(HighPath)):<br>        count += 1<br>        imagePath = os.path.join(HighPath, imageName)<br>        <br>        # Load the image<br>        low_img = cv.imread(imagePath)<br>        if low_img is None:<br>            print(f&quot;Warning: Skipping {imageName}, could not load the image.&quot;)<br>            continue<br>        <br>        # Convert BGR to RGB<br>        low_img = cv.cvtColor(low_img, cv.COLOR_BGR2RGB)<br>        <br>        # Resize the image to 500x500<br>        low_img = cv.resize(low_img, (500, 500))<br>        <br>        # Convert to HSV and darken the image by reducing the value channel<br>        hsv = cv.cvtColor(low_img, cv.COLOR_RGB2HSV)<br>        hsv[..., 2] = hsv[..., 2] * 0.2<br>        img_1 = cv.cvtColor(hsv, cv.COLOR_HSV2RGB)<br>        <br>        # Apply noise to the darkened image<br>        Noisey_img = addNoise(img_1)<br>        <br>        # Append the processed noisy image and original low image to the lists<br>        X_.append(Noisey_img)<br>        y_.append(low_img)<br>    <br>    # Convert the lists to NumPy arrays<br>    X_ = np.array(X_)<br>    y_ = np.array(y_)<br>    <br>    return X_, y_<br></pre><p>3. CNN Architecture: A custom CNN architecture is designed, consisting of multiple layers of Conv2D with varying filter sizes, combined with add layers to combine different feature maps.</p><pre>def InstantiateModel(in_):<br>        model_1 = Conv2D(16,(3,3), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(in_)<br>        model_1 = Conv2D(32,(3,3), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(model_1)<br>        model_1 = Conv2D(64,(2,2), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(model_1)<br>        <br>        model_2 = Conv2D(32,(3,3), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(in_)<br>        model_2 = Conv2D(64,(2,2), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(model_2)<br>        <br>        model_2_0 = Conv2D(64,(2,2), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(model_2)<br>        <br>        model_add = add([model_1,model_2,model_2_0])<br>        <br>        model_3 = Conv2D(64,(3,3), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(model_add)<br>        model_3 = Conv2D(32,(3,3), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(model_3)<br>        model_3 = Conv2D(16,(2,2), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(model_3)<br>        <br>        model_3_1 = Conv2D(32,(3,3), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(model_add)<br>        model_3_1 = Conv2D(16,(2,2), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(model_3_1)<br>        <br>        model_3_2 = Conv2D(16,(2,2), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(model_add)<br>        <br>        model_add_2 = add([model_3_1,model_3_2,model_3])<br>        <br>        model_4 = Conv2D(16,(3,3), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(model_add_2)<br>        model_4_1 = Conv2D(16,(3,3), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(model_add)<br><br>        model_add_3 = add([model_4_1,model_add_2,model_4])<br>        <br>        model_5 = Conv2D(16,(3,3), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(model_add_3)<br>        model_5 = Conv2D(16,(2,2), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(model_add_3)<br>        <br>        model_5 = Conv2D(3,(3,3), activation=&#39;relu&#39;,padding=&#39;same&#39;,strides=1)(model_5)<br>        <br>        return model_5</pre><h3>4. Model Summary</h3><p>* Input: 500x500 RGB image.</p><p>* Output: Enhanced 500x500 RGB image.</p><p>* Optimizer: Adam</p><p>* Loss: Mean Squared Error (MSE)</p><h3>5. Training</h3><p>The model is trained using noisy, darkened images as input and the corresponding high-light images as ground truth. The model is compiled using the Adam optimizer and trained over multiple epochs.</p><pre>Model_Enhancer.fit(GenerateInputs(X_, y_), epochs=53, verbose=1, steps_per_epoch=8, shuffle=True)</pre><h3>6. Inference</h3><p>Once the model is trained, you can perform inference on new low-light images. The function ExtractTestInput is used to preprocess test images, and the trained model generates enhanced images.</p><pre>Prediction = Model_Enhancer.predict(image_for_test)</pre><h3>7. Example Usage</h3><p>1.Load a test image.</p><p>2.Apply noise and darkening to simulate a low-light condition.</p><p>3.Run the model to get the enhanced image.</p><p>4.Compare the low-light image, the original image, and the enhanced output.</p><pre>image_for_test = ExtractTestInput(&quot;/path/to/test/image.png&quot;)<br>Prediction = Model_Enhancer.predict(image_for_test)</pre><h3>8. Results</h3><p>Below are sample outputs from the model:</p><ol><li>Original Image: The ground truth image in normal lighting.</li><li>Low Light Image: The darkened and noisy input to the model.</li><li>Enhanced Image: The output of the model, which restores brightness and reduces noise.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HfdUKlpWMPcvPbPyB3Qatw.png" /></figure><h3>9. References</h3><ol><li><a href="https://daooshee.github.io/BMVC2018website/">LOL Dataset</a></li><li><a href="https://keras.io/">Keras Documentation</a></li><li><a href="https://docs.opencv.org/">OpenCV Documentation</a></li><li><a href="https://github.com/Gayathri-Selvaganapathi/low_light_image_enhancer">My Github repo</a></li><li><a href="https://colab.research.google.com/drive/1wvEc5dTDirz14_66EnJtrT0DbIpvWc9l?usp=sharing">Referred Model</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=82e9eefc05b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Real-time Face Mask Detection System Using Keras, TensorFlow, and OpenCV]]></title>
            <link>https://medium.com/@gayathri.s.de/real-time-face-mask-detection-system-using-keras-tensorflow-and-opencv-54547c2ee350?source=rss-c85e34fadf4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/54547c2ee350</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[face-mask-detection]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[keras]]></category>
            <category><![CDATA[haar-cascades]]></category>
            <dc:creator><![CDATA[Gayathri Selvaganapathi]]></dc:creator>
            <pubDate>Sun, 01 Sep 2024 05:09:35 GMT</pubDate>
            <atom:updated>2024-09-01T05:09:35.505Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Njx09mqSub_WwdfJ" /><figcaption>Photo by <a href="https://unsplash.com/@jeshoots?utm_source=medium&amp;utm_medium=referral">JESHOOTS.COM</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Table of Contents</h3><ol><li><a href="#introduction">Introduction</a></li><li><a href="#understanding-the-project-requirements">Understanding the Project Requirements</a></li><li><a href="#project-overview">Project Overview</a></li><li><a href="#dataset-preparation-and-analysis">Dataset Preparation and Analysis</a></li><li><a href="#data-preprocessing">Data Preprocessing</a></li></ol><ul><li><a href="#loading-and-resizing-images">Loading and Resizing Images</a></li><li><a href="#normalization-and-label-encoding">Normalization and Label Encoding</a></li></ul><p><a href="#designing-the-convolutional-neural-network">6. Designing the Convolutional Neural Network</a></p><ul><li><a href="#choosing-the-right-architecture">Choosing the Right Architecture</a></li><li><a href="#model-architecture-explained">Model Architecture Explained</a></li></ul><p><a href="#model-training-and-evaluation">7. Model Training and Evaluation</a></p><ul><li><a href="#training-the-model">Training the Model</a></li><li><a href="#evaluating-model-performance">Evaluating Model Performance</a></li></ul><p><a href="#implementing-real-time-mask-detection">8. Implementing Real-time Mask Detection</a></p><ul><li><a href="#setting-up-opencv-for-real-time-detection">Setting Up OpenCV for Real-time Detection</a></li><li><a href="#loading-and-testing-the-trained-model">Loading and Testing the Trained Model</a></li></ul><p><a href="#deploying-the-system-for-practical-use">9. Deploying the System for Practical Use</a></p><p><a href="#challenges-and-considerations">10. Challenges and Considerations</a></p><ul><li><a href="#handling-false-positives-and-negatives">Handling False Positives and Negatives</a></li><li><a href="#optimizing-for-performance">Optimizing for Performance</a></li></ul><p><a href="#conclusion">11. Conclusion</a></p><p><a href="#code-snippets-recap">12. </a>References</p><h3>1. Introduction</h3><p>With the global outbreak of COVID-19, face masks have become an essential tool in preventing the virus’s spread. To enforce mask-wearing in public spaces, many organizations have turned to technology to automate this task. This blog provides an in-depth, step-by-step guide on building a face mask detection system using Python, Keras, TensorFlow, and OpenCV.</p><p>This system can detect whether individuals are wearing masks in real-time by analyzing video feeds, making it applicable for various public safety applications, such as monitoring entrances to buildings or public transportation.</p><h3>2. Understanding the Project Requirements</h3><p>Before diving into the technical details, it’s essential to understand the core requirements of this project:</p><ul><li><strong>Real-time detection</strong>: The system must process video frames quickly and accurately to determine mask usage.</li><li><strong>Accuracy</strong>: The model should effectively distinguish between masked and unmasked faces, minimizing false positives and negatives.</li><li><strong>Scalability</strong>: The solution should be adaptable for different environments, such as crowded places with varying lighting conditions.</li></ul><p>By keeping these requirements in mind, we ensure that the system we build is both practical and effective in real-world scenarios.</p><h3>3. Project Overview</h3><p>This project follows a structured approach, beginning with data collection and preprocessing, followed by model training and evaluation. Finally, we implement the trained model in a real-time detection system using OpenCV. The entire workflow is designed to be both modular and scalable, allowing for easy adaptations and improvements.</p><p>The project is divided into the following key stages:</p><ol><li><strong>Dataset Preparation</strong>: Collecting and organizing images of people with and without masks.</li><li><strong>Data Preprocessing</strong>: Resizing images, normalizing pixel values, and encoding labels for model input.</li><li><strong>Model Design and Training</strong>: Building and training a Convolutional Neural Network (CNN) to classify images.</li><li><strong>Real-time Detection Implementation</strong>: Using OpenCV to apply the model in real-time video feeds.</li><li><strong>Deployment and Optimization</strong>: Preparing the system for practical use, including handling challenges like varying lighting conditions and optimizing performance.</li></ol><h3>4. Dataset Preparation and Analysis</h3><h4>Dataset Source</h4><p>The dataset used in this project is sourced from Prajna Bhandary’s GitHub repository. It contains images of individuals with and without masks, categorized into respective folders. This dataset is ideal because it provides a balanced set of examples, which is crucial for training an accurate model.</p><h4>Analyzing the Dataset</h4><p>Understanding the dataset’s structure and content is vital before preprocessing. The images vary in resolution, lighting, and the number of faces per image, which introduces variability into the model’s training process. These factors need to be considered during preprocessing and model training to ensure robustness.</p><h3>5. Data Preprocessing</h3><p>Data preprocessing is a critical step in machine learning projects, especially when dealing with images. Proper preprocessing ensures that the data fed into the model is standardized, reducing the chances of overfitting and improving generalization.</p><h4>Loading and Resizing Images</h4><p>The first step in preprocessing is loading the images from the dataset. The images are then resized to a uniform shape (128x128 pixels in this case) to ensure consistency when feeding them into the CNN.</p><pre>import cv2<br>import os<br>import numpy as np<br><br># Directories for the dataset<br>masked_dir = &quot;dataset/with_mask/&quot;<br>unmasked_dir = &quot;dataset/without_mask/&quot;<br><br># Initialize lists for data and labels<br>data = []<br>labels = []<br><br># Function to load and resize images<br>def load_images_from_folder(folder, label):<br>    for filename in os.listdir(folder):<br>        img = cv2.imread(os.path.join(folder, filename))<br>        if img is not None:<br>            img = cv2.resize(img, (128, 128))<br>            data.append(img)<br>            labels.append(label)<br><br>load_images_from_folder(masked_dir, 1)<br>load_images_from_folder(unmasked_dir, 0)<br><br>data = np.array(data)<br>labels = np.array(labels)</pre><h4>Normalization and Label Encoding</h4><p>Normalization is applied to scale the pixel values to the range [0, 1], which speeds up the training process and helps the model converge faster. Labels are also one-hot encoded to prepare them for classification.</p><pre># Normalize the data<br>data = data.astype(&quot;float&quot;) / 255.0<br># One-hot encode the labels<br>from keras.utils import to_categorical<br>labels = to_categorical(labels, num_classes=2)</pre><p>The preprocessed data is then saved for use during the training phase. This ensures that the same data is consistently used throughout the project, eliminating any variability that might arise from different preprocessing runs.</p><h3>6. Designing the Convolutional Neural Network</h3><h4><strong>Choosing the Right Architecture</strong></h4><p>Selecting the appropriate CNN architecture is crucial for balancing accuracy and computational efficiency. Given the relatively simple task of binary classification (mask vs. no mask), we opt for a straightforward CNN architecture that includes a few convolutional layers followed by fully connected layers. This design is sufficient to capture the essential features in the images while maintaining a manageable computational load.</p><h4>Model Architecture Explained</h4><p>The CNN model consists of the following layers:</p><ol><li><strong>Convolutional Layers</strong>: Extract features from the input images using filters.</li><li><strong>Max-Pooling Layers</strong>: Reduce the spatial dimensions, helping to control overfitting.</li><li><strong>Fully Connected Layers</strong>: Perform the classification based on the features extracted by the convolutional layers.</li><li><strong>Dropout Layers</strong>: Regularize the model to prevent overfitting.</li></ol><pre>from keras.models import Sequential<br>from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout<br>model = Sequential([<br>    Conv2D(32, (3, 3), activation=&#39;relu&#39;, input_shape=(128, 128, 3)),<br>    MaxPooling2D(pool_size=(2, 2)),<br>    <br>    Conv2D(64, (3, 3), activation=&#39;relu&#39;),<br>    MaxPooling2D(pool_size=(2, 2)),<br>    <br>    Conv2D(128, (3, 3), activation=&#39;relu&#39;),<br>    MaxPooling2D(pool_size=(2, 2)),<br>    <br>    Flatten(),<br>    Dense(128, activation=&#39;relu&#39;),<br>    Dropout(0.5),<br>    Dense(2, activation=&#39;softmax&#39;)<br>])<br>model.compile(optimizer=&#39;adam&#39;, loss=&#39;categorical_crossentropy&#39;, metrics=[&#39;accuracy&#39;])<br>model.summary()</pre><p>This architecture strikes a balance between complexity and performance, making it suitable for real-time applications without requiring extensive computational resources.</p><h3>7. Model Training and Evaluation</h3><h4>Training the Model</h4><p>Training is conducted on the preprocessed dataset, with the data split into training and validation sets. We use the Adam optimizer, which is well-suited for training deep neural networks, along with categorical cross-entropy as the loss function.</p><pre>from sklearn.model_selection import train_test_split<br># Split the data into training and validation sets<br>trainX, testX, trainY, testY = train_test_split(data, labels, test_size=0.2, random_state=42)<br># Train the model<br>history = model.fit(trainX, trainY, validation_data=(testX, testY), epochs=10, batch_size=32)</pre><h4>Evaluating Model Performance</h4><p>After training, it’s essential to evaluate the model’s performance on the validation set. Metrics such as accuracy, precision, recall, and F1-score are considered to ensure the model performs well across different metrics, not just accuracy.</p><pre># Evaluate the model<br>loss, accuracy = model.evaluate(testX, testY, verbose=0)<br>print(f&quot;Validation Accuracy: {accuracy * 100:.2f}%&quot;)</pre><p>By evaluating these metrics, we gain insights into how well the model generalizes to unseen data, which is crucial for deploying it in real-world scenarios.</p><h3>8. Implementing Real-time Mask Detection</h3><h4>Setting Up OpenCV for Real-time Detection</h4><p>To implement real-time detection, we use OpenCV to capture video input from a webcam or any other video source. The face is detected in each frame using a pre-trained Haar cascade classifier, and then the model predicts whether the person is wearing a mask.</p><pre>import cv2<br># Load the pre-trained face detector<br>face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + &#39;haarcascade_frontalface_default.xml&#39;)<br># Start video capture<br>cap = cv2.VideoCapture(0)</pre><h4>Loading and Testing the Trained Model</h4><p>The trained CNN model is loaded, and each detected face in the video feed is passed through the model to determine whether it is masked or unmasked.</p><pre># Load the model<br>from keras.models import load_model<br>model = load_model(&quot;mask_detector.model&quot;)<br><br>while True:<br>    ret, frame = cap.read()<br>    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)<br>    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))<br>    <br>    for (x, y, w, h) in faces:<br>        face = frame[y:y+h, x:x+w]<br>        face = cv2.resize(face, (128, 128))<br>        face = face.astype(&quot;float&quot;) / 255.0<br>        face = np.expand_dims(face, axis=0)<br>        <br>        (mask, withoutMask) = model.predict(face)[0]<br>        <br>        label = &quot;Mask&quot; if mask &gt; withoutMask else &quot;No Mask&quot;<br>        color = (0, 255, 0) if label == &quot;Mask&quot; else (0, 0, 255)<br>        <br>        cv2.putText(frame, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)<br>        cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)<br>    <br>    cv2.imshow(&quot;Face Mask Detector&quot;, frame)<br>    <br>    if cv2.waitKey(1) &amp; 0xFF == ord(&#39;q&#39;):<br>        break<br><br>cap.release()<br>cv2.destroyAllWindows()</pre><p>This implementation allows for the system to be used in real-time applications, where it can be deployed in environments requiring continuous monitoring.</p><h3>9. Deploying the System for Practical Use</h3><h4>Deployment Scenarios</h4><p>The face mask detection system can be deployed in various settings, such as:</p><ul><li><strong>Public Transportation Hubs</strong>: Monitoring compliance with mask mandates.</li><li><strong>Corporate Offices</strong>: Ensuring employees adhere to safety protocols.</li><li><strong>Retail Environments</strong>: Automated monitoring at store entrances.</li></ul><h4>Deployment Considerations</h4><p>When deploying the system, it’s important to consider factors such as processing power, scalability, and ease of integration with existing security systems. The system should be capable of handling high throughput while maintaining accuracy.</p><h3>10. Challenges and Considerations</h3><h4>Handling False Positives and Negatives</h4><p>No model is perfect, and false positives (incorrectly identifying someone as wearing a mask) and false negatives (failing to detect a mask) are inevitable. Fine-tuning the model, experimenting with different thresholds, and adding more data for training can help mitigate these issues.</p><h4>Optimizing for Performance</h4><p>For real-time applications, performance is critical. Optimizations such as reducing the model’s size, using GPU acceleration, or employing quantization techniques can significantly improve detection speed without sacrificing accuracy.</p><h3>11.Conclusion</h3><p>This blog has provided a comprehensive guide to building a real-time face mask detection system using Keras, TensorFlow, and OpenCV. By following the steps outlined, you can develop and deploy a system capable of enhancing public safety in a variety of environments.</p><p>The project demonstrates the power of deep learning for real-time applications, showcasing how modern AI techniques can be applied to solve pressing challenges in today’s world.</p><h3>12. References</h3><ol><li><a href="https://github.com/Gayathri-Selvaganapathi/face_mask_detection">My GitHub page</a></li><li><a href="https://github.com/aieml/face-mask-detection-keras">Referred Model</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54547c2ee350" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Customer Segmentation Using Machine Learning]]></title>
            <link>https://medium.com/@gayathri.s.de/customer-segmentation-using-machine-learning-5acf775dcf50?source=rss-c85e34fadf4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/5acf775dcf50</guid>
            <category><![CDATA[pca-analysis]]></category>
            <category><![CDATA[customer-segmentation]]></category>
            <category><![CDATA[autoencoder]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[k-means-clustering]]></category>
            <dc:creator><![CDATA[Gayathri Selvaganapathi]]></dc:creator>
            <pubDate>Sat, 31 Aug 2024 13:19:30 GMT</pubDate>
            <atom:updated>2024-08-31T13:20:41.258Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mA6L_t_yWsuX4tWm" /><figcaption>Photo by <a href="https://unsplash.com/@storyfuel?utm_source=medium&amp;utm_medium=referral">Melanie Deziel</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Table of Contents</h3><ol><li>Introduction</li><li>Understanding the Dataset</li><li>Data Wrangling and Cleaning</li><li>Exploratory Data Analysis (EDA)</li><li>Unsupervised Learning Techniques</li></ol><ul><li>K-Means Clustering</li><li>Principal Component Analysis (PCA)</li><li>Autoencoders</li></ul><p>6. Visualizing Customer Segments</p><p>7. Conclusion and Insights</p><p>8. References</p><h3>1. Introduction</h3><p>Customer segmentation is a crucial technique in marketing analytics, where customers are divided into groups based on similar characteristics or behaviors. Understanding these segments allows companies to tailor their marketing strategies, optimize product offerings, and improve customer engagement.</p><p>In this blog, we’ll take you step by step through a customer segmentation process using a real-world credit card dataset, implementing advanced machine learning techniques. We’ll apply unsupervised learning algorithms such as <strong>K-Means clustering</strong>, <strong>Principal Component Analysis (PCA)</strong>, and <strong>Autoencoders</strong> to uncover hidden patterns in customer behavior.</p><p>By the end of this article, you’ll have a deeper understanding of customer segmentation and how machine learning can unlock valuable insights from your data.</p><h3>2. Understanding the Dataset</h3><p>The dataset used in this project consists of around 9,000 credit card customers, containing detailed information about their spending habits and payment behaviors. Key features include:</p><ul><li><strong>Balance</strong>: Total amount of credit card balance.</li><li><strong>Cash Advance</strong>: Total amount of cash advances taken on the card.</li><li><strong>Purchase Frequency</strong>: Frequency of purchases made by the customer.</li><li><strong>Payment Behavior</strong>: Whether the customer pays off their balance in full or just the minimum payment.</li><li><strong>Credit Limit</strong>: Maximum amount of credit extended to the customer.</li></ul><p>Each row in the dataset corresponds to one customer, and the various features describe their interaction with their credit card. This rich set of features makes the dataset ideal for machine learning-driven segmentation.</p><pre>import pandas as pd<br><br># Load the dataset<br>data = pd.read_csv(&#39;Marketing_data.csv&#39;)<br><br># Display the first few rows of the dataset<br>data.head()</pre><p>The dataset includes various numerical and categorical variables, which need to be handled properly for our machine learning models to perform well.</p><h3>3. Data Wrangling and Cleaning</h3><p>Raw data often requires pre-processing before it’s suitable for analysis. In this step, we handle missing values, remove irrelevant columns, and standardize our features. Data wrangling ensures that our dataset is ready for accurate machine learning modeling.</p><ol><li><strong>Handling Missing Values</strong>: Any missing data can bias our model’s performance. In this case, we fill missing values using the mean of the corresponding column.</li><li><strong>Feature Selection</strong>: We drop irrelevant columns such as customer IDs, which do not contribute to customer behavior.</li><li><strong>Standardization</strong>: We standardize numerical features to have a mean of 0 and a standard deviation of 1 to ensure that the clustering algorithm treats all features equally.</li></ol><p><strong>Code Snippet: Data Cleaning</strong></p><pre># Check for missing values<br>data.isnull().sum()<br><br># Fill missing values with the mean of the column<br>data.fillna(data.mean(), inplace=True)<br><br># Drop irrelevant features like Customer ID<br>data.drop([&#39;CUST_ID&#39;], axis=1, inplace=True)<br><br># Standardize numerical features for K-Means<br>from sklearn.preprocessing import StandardScaler<br>scaler = StandardScaler()<br>data_scaled = scaler.fit_transform(data)</pre><p>At the end of this step, we have a clean and standardized dataset, ready for the machine learning process.</p><h3>4. Exploratory Data Analysis (EDA)</h3><p>Exploratory Data Analysis (EDA) allows us to explore the data visually and identify patterns and relationships between different variables. This is a crucial step before applying any machine learning algorithms, as it helps us understand the data’s structure and identify potential challenges such as outliers or multicollinearity.</p><h4>Visualizing Feature Distributions</h4><p>We start by visualizing key features such as balance, purchase frequency, and credit limit to understand their distribution across the customer base.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WMt9YEmQBZ0HLgGOYVDSkA.png" /></figure><pre>import matplotlib.pyplot as plt<br>import seaborn as sns<br><br># Visualize the distribution of balance across customers<br>plt.figure(figsize=(10, 6))<br>sns.histplot(data[&#39;Balance&#39;], kde=True)<br>plt.title(&#39;Distribution of Customer Balances&#39;)<br>plt.show()<br><br># Visualize the distribution of purchase frequency<br>plt.figure(figsize=(10, 6))<br>sns.histplot(data[&#39;Purchase_Frequency&#39;], kde=True)<br>plt.title(&#39;Distribution of Purchase Frequency&#39;)<br>plt.show()</pre><h4>Correlation Matrix</h4><p>Next, we generate a correlation matrix to examine the relationships between different features. Features that are highly correlated can indicate redundant information, which may need to be addressed through dimensionality reduction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8E3Le94uHfaJfU74CYzHqw.png" /></figure><pre># Plotting the correlation matrix<br>plt.figure(figsize=(12, 8))<br>sns.heatmap(data.corr(), annot=True, cmap=&#39;coolwarm&#39;)<br>plt.title(&#39;Correlation Matrix&#39;)<br>plt.show()</pre><p>EDA provides us with a good sense of the data’s structure and its relationships, setting the stage for more advanced machine learning techniques.</p><h3>5. Unsupervised Learning Techniques</h3><p>Unsupervised learning is a type of machine learning that deals with data that doesn’t have predefined labels. In customer segmentation, unsupervised learning is ideal because we don’t know beforehand how many segments exist or what defines each segment.</p><p>We’ll explore three unsupervised learning methods:</p><ol><li><strong>K-Means Clustering</strong></li><li><strong>Principal Component Analysis (PCA)</strong></li><li><strong>Autoencoders</strong></li></ol><h3>K-Means Clustering</h3><p>K-Means is a popular algorithm that partitions data into distinct clusters based on similarity. In this case, we want to group customers who have similar credit card usage patterns.</p><h4>Determining the Optimal Number of Clusters</h4><p>We use the <strong>Elbow Method</strong> to determine the optimal number of clusters. The idea is to plot the Sum of Squared Errors (SSE) for different numbers of clusters and identify the “elbow point” where the SSE starts to decrease at a slower rate.</p><pre>from sklearn.cluster import KMeans<br>import numpy as np<br><br># Finding the optimal number of clusters using the Elbow method<br>sse = []<br>for k in range(1, 11):<br>    kmeans = KMeans(n_clusters=k, random_state=42)<br>    kmeans.fit(data_scaled)<br>    sse.append(kmeans.inertia_)<br><br># Plotting the Elbow curve<br>plt.figure(figsize=(10, 6))<br>plt.plot(range(1, 11), sse, marker=&#39;o&#39;)<br>plt.title(&#39;Elbow Method for Optimal Clusters&#39;)<br>plt.xlabel(&#39;Number of Clusters&#39;)<br>plt.ylabel(&#39;SSE&#39;)<br>plt.show()</pre><h4>Applying K-Means</h4><p>After determining the optimal number of clusters (let’s assume 5 clusters in this case), we apply K-Means to group the customers.</p><pre># Applying K-Means with 5 clusters<br>kmeans = KMeans(n_clusters=5, random_state=42)<br>data[&#39;Cluster&#39;] = kmeans.fit_predict(data_scaled)<br><br># Visualize the number of customers in each cluster<br>data[&#39;Cluster&#39;].value_counts()</pre><h3>Principal Component Analysis (PCA)</h3><p><strong>PCA</strong> is a powerful technique for dimensionality reduction. It transforms a large number of correlated features into a smaller set of uncorrelated components, making it easier to visualize and analyze the data.</p><p><strong>Applying PCA for Dimensionality Reduction</strong></p><p>We apply PCA to reduce the dimensionality of the dataset while retaining as much variance as possible. This also makes it easier to visualize the clusters in a 2D or 3D space.</p><pre>from sklearn.decomposition import PCA<br><br># Applying PCA to reduce dimensions to 2 components<br>pca = PCA(n_components=2)<br>data_pca = pca.fit_transform(data_scaled)<br><br># Visualizing the PCA result<br>plt.figure(figsize=(10, 6))<br>plt.scatter(data_pca[:, 0], data_pca[:, 1], c=data[&#39;Cluster&#39;], cmap=&#39;viridis&#39;)<br>plt.title(&#39;PCA: Visualizing Customer Segments&#39;)<br>plt.xlabel(&#39;Principal Component 1&#39;)<br>plt.ylabel(&#39;Principal Component 2&#39;)<br>plt.show()</pre><h3>Autoencoders</h3><p>Autoencoders are a type of neural network designed for unsupervised learning tasks, particularly for dimensionality reduction. Unlike PCA, which is a linear transformation, autoencoders can model complex, non-linear relationships in the data.</p><h4>Building and Training the Autoencoder</h4><p>We build a simple autoencoder to reduce the dataset’s dimensions and visualize the customer segments in a 2D space.</p><pre>from keras.models import Model<br>from keras.layers import Input, Dense<br><br># Define the Autoencoder model<br>input_dim = data_scaled.shape[1]<br>encoding_dim = 2<br><br>input_layer = Input(shape=(input_dim,))<br>encoded = Dense(encoding_dim, activation=&#39;relu&#39;)(input_layer)<br>decoded = Dense(input_dim, activation=&#39;sigmoid&#39;)(encoded)<br><br>autoencoder = Model(input_layer, decoded)<br>autoencoder.compile(optimizer=&#39;adam&#39;, loss=&#39;mse&#39;)<br><br># Train the autoencoder<br>autoencoder.fit(data_scaled, data_scaled, epochs=50, batch_size=256, shuffle=True</pre><h4>Extracting and Visualizing Encoded Features</h4><p>After training the autoencoder, we can extract the encoded features representing each customer in a reduced 2D space. This allows us to visualize the clusters created by the autoencoder.</p><pre># Extracting the encoder part of the autoencoder<br>encoder = Model(inputs=input_layer, outputs=encoded)<br># Encoding the data to the reduced dimensions<br>data_encoded = encoder.predict(data_scaled)<br># Visualizing the encoded results<br>plt.figure(figsize=(10, 6))<br>plt.scatter(data_encoded[:, 0], data_encoded[:, 1], c=data[&#39;Cluster&#39;], cmap=&#39;viridis&#39;)<br>plt.title(&#39;Autoencoder: 2D Projection of Customer Segments&#39;)<br>plt.xlabel(&#39;Encoded Dimension 1&#39;)<br>plt.ylabel(&#39;Encoded Dimension 2&#39;)<br>plt.show()</pre><p>This visualization allows us to see how the autoencoder has grouped similar customers together, potentially uncovering different patterns than the ones observed with PCA.</p><h3>6. Visualizing Customer Segments</h3><p>Visualization plays a critical role in interpreting the results of our clustering efforts. By visualizing customer segments in a reduced dimension space (such as 2D), we can better understand the characteristics of each cluster and how they differ from one another.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*7HzIh4cwSQQ-FqQPPi-OLg.png" /></figure><h3>Comparing PCA and Autoencoder Clusters</h3><p>Both PCA and Autoencoders reduce the dimensionality of our data, but they approach the problem differently. Comparing the clusters produced by these two techniques can provide insights into the underlying structure of the data.</p><pre># Visualizing the clusters using PCA<br>plt.figure(figsize=(10, 6))<br>plt.scatter(data_pca[:, 0], data_pca[:, 1], c=data[&#39;Cluster&#39;], cmap=&#39;viridis&#39;)<br>plt.title(&#39;PCA: Visualizing Customer Clusters&#39;)<br>plt.xlabel(&#39;Principal Component 1&#39;)<br>plt.ylabel(&#39;Principal Component 2&#39;)<br>plt.show()<br><br># Visualizing the clusters using Autoencoders<br>plt.figure(figsize=(10, 6))<br>plt.scatter(data_encoded[:, 0], data_encoded[:, 1], c=data[&#39;Cluster&#39;], cmap=&#39;viridis&#39;)<br>plt.title(&#39;Autoencoder: Visualizing Customer Clusters&#39;)<br>plt.xlabel(&#39;Encoded Dimension 1&#39;)<br>plt.ylabel(&#39;Encoded Dimension 2&#39;)<br>plt.show()</pre><h4>Insights from Visualizations</h4><p>By looking at these visualizations, we can derive the following insights:</p><ul><li><strong>Cluster Separation</strong>: How well-separated are the clusters? Better separation generally means more distinct customer groups.</li><li><strong>Cluster Size</strong>: Are some clusters significantly larger than others? This could indicate a broad customer segment or an over-representation of certain behaviors.</li><li><strong>Cluster Overlap</strong>: Overlapping clusters might suggest customers with mixed or transitional behaviors, where segmentation is less clear.</li></ul><p>These visualizations can guide the interpretation and application of customer segmentation strategies.</p><h3>7. Conclusion and Insights</h3><p>The journey through customer segmentation using machine learning has revealed the power of unsupervised learning techniques in extracting meaningful patterns from complex datasets. Here’s a summary of what we’ve achieved:</p><h4>Key Techniques Applied:</h4><ul><li><strong>K-Means Clustering</strong>: Grouped customers into distinct segments based on similar behaviors.</li><li><strong>Principal Component Analysis (PCA)</strong>: Reduced the dataset’s dimensionality, facilitating visualization and analysis.</li><li><strong>Autoencoders</strong>: Leveraged deep learning to uncover non-linear relationships in the data, providing a different perspective on customer segmentation.</li></ul><h4>Key Insights:</h4><ol><li><strong>Revolvers</strong>: Customers with high balances and frequent cash advances, representing a lucrative segment for credit card companies.</li><li><strong>Credit Purchasers</strong>: Customers who frequently purchase on credit and use installment payment facilities, possibly preferring deferred payments.</li><li><strong>VIP/Prime</strong>: High credit limit customers who pay their balance in full, an attractive segment for upselling premium services.</li><li><strong>Low Tenure Users</strong>: New customers with lower balances, which may represent an opportunity for targeted marketing to increase engagement.</li><li><strong>Low Activity Users</strong>: Customers with minimal card usage, who may require incentivization to increase their activity.</li></ol><h4>Application in Business:</h4><p>The insights derived from this analysis can be directly applied to create targeted marketing strategies. For instance:</p><ul><li><strong>Marketing Campaigns</strong>: Tailor campaigns to the specific needs of each segment, such as offering balance transfer promotions to Revolvers or loyalty programs to VIP customers.</li><li><strong>Product Customization</strong>: Develop new financial products or services that cater specifically to the unique behaviors of each customer segment.</li><li><strong>Customer Retention</strong>: Identify at-risk customers, such as Low Activity Users, and create personalized offers to increase their engagement.</li></ul><p>8.References</p><ol><li><a href="https://github.com/Gayathri-Selvaganapathi/customer_segmentation">My Github repo</a></li><li><a href="https://github.com/LaurentVeyssier/Customer-Marketing-segmentation-with-Machine-Learning">Referred Model</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5acf775dcf50" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Object Detection and Tracking with YOLOv8 and DeepSORT]]></title>
            <link>https://medium.com/@gayathri.s.de/object-detection-and-tracking-with-yolov8-and-deepsort-5d5981752151?source=rss-c85e34fadf4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d5981752151</guid>
            <category><![CDATA[traffic-tracking]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[deep-sort]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Gayathri Selvaganapathi]]></dc:creator>
            <pubDate>Sat, 31 Aug 2024 08:18:13 GMT</pubDate>
            <atom:updated>2024-08-31T08:21:50.815Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*m47h113rQBNBcnLZ" /><figcaption>Photo by <a href="https://unsplash.com/@hdbernd?utm_source=medium&amp;utm_medium=referral">Bernd 📷 Dittrich</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Table of Contents</h3><ol><li><strong>Introduction</strong></li></ol><ul><li>Overview of Object Detection and Tracking</li><li>Introduction to YOLOv8 and DeepSORT</li></ul><p><strong>2. Project Setup</strong></p><ul><li>Cloning the Repository</li><li>Setting Up the Development Environment</li></ul><p><strong>3. Implementation Steps</strong></p><ul><li>Downloading and Organizing Required Files</li><li>Running Object Detection with YOLOv8</li></ul><p><strong>4. Understanding the Code</strong></p><ul><li>Overview of the Main Script</li><li>Detailed Explanation of Key Functions</li></ul><p><strong>5. Results and Output</strong></p><ul><li>Interpreting the Output Videos</li><li>Speed Estimation and Vehicle Counting</li></ul><p><strong>6. Extending the Project</strong></p><ul><li>Customizing the Detection Model</li><li>Adding New Features</li></ul><p><strong>7. Conclusion</strong></p><ul><li>Summary of the Project</li><li>Potential Applications</li></ul><h3>1. Introduction</h3><p>Object detection and tracking are crucial components in modern computer vision applications, used in everything from autonomous vehicles to surveillance systems. In this blog, we’ll delve into the implementation of object detection, tracking, and speed estimation using YOLOv8 (You Only Look Once version 8) and DeepSORT (Simple Online and Realtime Tracking with a Deep Association Metric).</p><p><strong>YOLOv8</strong> is one of the latest iterations of the YOLO family, known for its efficiency and accuracy in detecting objects in images and videos. <strong>DeepSORT</strong> is an advanced tracking algorithm that enhances SORT (Simple Online and Realtime Tracking) by adding a deep learning-based feature extractor to improve object tracking accuracy, especially in challenging scenarios.</p><h3>2. Project Setup</h3><p>Before we dive into the code, let’s set up the project environment.</p><h4>Cloning the Repository</h4><p>First, clone the GitHub repository that contains the necessary code:</p><pre>git clone https://github.com/Gayathri-Selvaganapathi/vehicle_tracking_counting.git<br>cd vehicle_tracking_counting</pre><p>This repository includes scripts for object detection, tracking, and speed estimation, along with pre-trained models and sample data.</p><h4>Setting Up the Development Environment</h4><p>It’s essential to create a clean Python environment to avoid dependency conflicts. You can do this using virtualenv or conda:</p><pre># Using conda<br>conda create -n env_tracking python=3.8</pre><p>Install the required dependencies by running:</p><pre>pip install -r requirements.txt</pre><h3>3. Implementation Steps</h3><p>Now that we have our environment set up, we can proceed with the implementation.</p><h4>Downloading and Organizing Required Files</h4><p>The project requires some additional files that aren’t included in the GitHub repository, such as the DeepSORT model files and a sample video for testing.</p><ol><li><strong>Download the DeepSORT files</strong> from the provided Google Drive link.</li><li><strong>Unzip the downloaded files</strong> and place them in the appropriate directories as outlined in the project README.</li></ol><p>For example, the DeepSORT files should be placed in the yolov8-deepsort/deep_sort directory, and the sample video should be in yolov8-deepsort/data.</p><h4>Running Object Detection with YOLOv8</h4><p>With everything set up, you can now run the object detection and tracking script. Here’s how you can do it:</p><pre>python detect.py --source data/sample_video.mp4 --yolo-model yolov8 --deep-sort deep_sort_pytorch --output runs/detect</pre><p>This command processes the sample_video.mp4 file, detects objects using the YOLOv8 model, tracks them with DeepSORT, and saves the output video in the runs/detect directory.</p><h3>4. Understanding the Code</h3><p>Let’s break down the main parts of the code to understand how it works.</p><h4>Overview of the Main Script</h4><p>The primary script, detect.py, orchestrates the entire detection and tracking process. Here&#39;s a high-level view of what the script does:</p><ol><li><strong>Load the YOLOv8 model</strong>: This model is used for detecting objects in each frame.</li><li><strong>Initialize the DeepSORT tracker</strong>: This tracker assigns unique IDs to objects and tracks them across frames.</li><li><strong>Process the video frame by frame</strong>: For each frame, the script detects objects, tracks them, and then draws bounding boxes and labels around them.</li><li><strong>Output the processed video</strong>: The final video is saved with all the detected and tracked objects, along with their speeds if applicable.</li></ol><h4>Detailed Explanation of Key Functions</h4><p>Here are some critical functions in the script:</p><ul><li><strong>Initialize Tracker</strong></li></ul><pre>from deep_sort.deep_sort import DeepSort  <br>def init_tracker():     <br>  return DeepSort(&quot;deep_sort/model.ckpt&quot;, use_cuda=True)</pre><ul><li>This function initializes the DeepSORT tracker, which will be used to track detected objects across frames.</li><li><strong>Object Detection with YOLOv8</strong></li></ul><pre>def detect_objects(frame, model):     <br>  results = model(frame)    <br>  return results.xyxy[0]  # Returns bounding boxes and class labels</pre><ul><li>This function runs YOLOv8 on each frame of the video to detect objects. The function returns bounding boxes along with the class labels.</li><li><strong>Drawing Bounding Boxes</strong></li></ul><pre>def draw_boxes(frame, bbox, identities, names):<br>    for i, box in enumerate(bbox):<br>        x1, y1, x2, y2 = [int(i) for i in box]<br>        id = int(identities[i]) if identities is not None else 0    <br>        label = f&#39;{names[i]} {id}&#39;<br>        color = compute_color_for_labels(id)<br>        cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)<br>        cv2.putText(frame, label, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.75, color, 2)<br>    return frame</pre><ul><li>This function takes the bounding boxes and identities of tracked objects, draws them on the frame, and annotates them with the object’s name and ID.</li><li><strong>Speed Estimation</strong></li></ul><pre>def estimate_speed(coord1, coord2, fps):<br>    d_pixels = np.linalg.norm(np.array(coord2) - np.array(coord1))<br>    d_meters = d_pixels / PIXELS_PER_METER<br>    speed = d_meters * fps * 3.6  # Convert m/s to km/h<br>    return speed</pre><ul><li>This function estimates the speed of the tracked objects by calculating the distance they traveled between frames and converting it into km/h.</li></ul><h3>5. Results and Output</h3><p>After running the script, you should see an output video where objects are detected, tracked, and labeled with their IDs. The video will also display the estimated speed of moving objects if enabled.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kobGD7n82NJexeZhs5vtXQ.png" /></figure><h4>Interpreting the Output Videos</h4><p>In the output video:</p><ul><li><strong>Bounding Boxes</strong>: Each detected object will have a bounding box drawn around it.</li><li><strong>Object ID and Label</strong>: The label on the bounding box will show the object’s class and a unique ID assigned by the tracker.</li><li><strong>Speed Estimation</strong>: If speed estimation is enabled, the speed of each moving object will be displayed.</li></ul><h4>Speed Estimation and Vehicle Counting</h4><p>The script also includes features for counting vehicles and estimating their speeds. When a vehicle crosses a predefined line, it increments the vehicle count and estimates the speed using the Euclidean distance formula.</p><pre># Counting vehicles crossing a line<br>if is_crossing_line(bbox, line_position):<br>    vehicle_count += 1<br><br># Estimating speed<br>speed = estimate_speed(previous_coord, current_coord, fps)<br></pre><h3>6. Extending the Project</h3><p>This project can serve as a foundation for more advanced applications. Here are a few ideas:</p><h4>Customizing the Detection Model</h4><p>You can fine-tune the YOLOv8 model to detect specific object classes relevant to your application. This might involve retraining the model on a custom dataset.</p><h4>Adding New Features</h4><p>Consider implementing real-time processing, multi-camera tracking, or even integrating with a web-based dashboard for live monitoring and control.</p><h3>7. Conclusion</h3><p>In this blog, we walked through the implementation of a sophisticated object detection and tracking system using YOLOv8 and DeepSORT. This system is capable of not only detecting and tracking multiple objects but also estimating their speeds and counting vehicles.</p><p><strong>Potential Applications</strong>:</p><ul><li><strong>Traffic Monitoring</strong>: Detect and track vehicles, estimate their speed, and count them for traffic flow analysis.</li><li><strong>Surveillance</strong>: Monitor people or objects in a secure environment, track their movements, and raise alerts for suspicious activity.</li><li><strong>Autonomous Vehicles</strong>: Use this system as part of a larger autonomous driving stack to understand the environment and make driving decisions.</li></ul><p>This tutorial demonstrates the power and flexibility of combining state-of-the-art deep learning models for real-world applications. Whether you’re working on a personal project or a professional system, these techniques can be adapted and expanded to meet your needs.</p><h3>8. Reference</h3><ol><li><a href="https://github.com/Gayathri-Selvaganapathi/vehicle_tracking_counting">My GitHub Repo</a></li><li><a href="https://www.youtube.com/watch?v=nkptX_vXJKo">Referred Tutorial</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d5981752151" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building an Emotion-Based Music Recommender: A Step-by-Step Guide]]></title>
            <link>https://medium.com/@gayathri.s.de/building-an-emotion-based-music-recommender-a-step-by-step-guide-5f637cb7352b?source=rss-c85e34fadf4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/5f637cb7352b</guid>
            <category><![CDATA[recommendation-system]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Gayathri Selvaganapathi]]></dc:creator>
            <pubDate>Sat, 31 Aug 2024 05:08:34 GMT</pubDate>
            <atom:updated>2024-08-31T05:12:37.461Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9db2h9qQ_KmPjZAo" /><figcaption>Photo by <a href="https://unsplash.com/@marcelalaskoski?utm_source=medium&amp;utm_medium=referral">Marcela Laskoski</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Table of Contents</h3><ol><li>Introduction</li><li>Project Overview</li><li>Tools and Libraries</li><li>Setting Up the Project</li></ol><ul><li>Cloning the Repository</li><li>Data Collection</li><li>Training the Model</li><li>Creating the Web App</li></ul><p>5. Coding the Emotion Detection Logic</p><ul><li>Emotion Processor Class</li><li>Loading the Model and Labels</li><li>Using Mediapipe for Landmark Detection</li></ul><p>6. Integrating the Music Recommender</p><ul><li>Creating the UI</li><li>Handling the YouTube Search Query</li></ul><p>7. Handling Session State</p><p>8. Final Touches and Testing</p><p>9. Conclusion</p><p>10. References</p><h3>1. Introduction</h3><p>Welcome to this detailed walkthrough of building an <strong>Emotion-Based Music Recommender</strong>. This project leverages facial emotion recognition to recommend music that aligns with the user’s current mood. The application is built using a combination of computer vision, deep learning, and web technologies, creating an engaging and personalized user experience.</p><p>In this blog, we will guide you through every step of building this project, from setting up your environment to deploying the final web app.</p><h3>2. Project Overview</h3><p>In this project, the model captures the user’s facial expressions using their webcam, predicts their current emotion, and recommends music that matches this emotion by searching on YouTube.</p><h3>Supported Emotions</h3><ul><li>Happy</li><li>Sad</li><li>Angry</li><li>Surprised</li><li>Neutral</li><li>Rock (a fun addition to the emotions)</li></ul><h3>3. Tools and Libraries</h3><p>To build this project, we use several powerful tools and libraries:</p><ul><li><strong>Streamlit</strong>: A fast and simple way to create web applications for machine learning projects.</li><li><strong>Streamlit WebRTC</strong>: Captures and processes video in real-time within a Streamlit app.</li><li><strong>Mediapipe</strong>: Google’s open-source framework for building multimodal machine learning pipelines, used here for detecting facial and hand landmarks.</li><li><strong>Keras</strong>: Used to load and run the pre-trained emotion detection model.</li><li><strong>OpenCV</strong>: For image processing tasks.</li><li><strong>Numpy</strong>: For handling arrays and numerical operations.</li></ul><h3>4. Setting Up the Project</h3><h3>Cloning the Repository</h3><p>Start by cloning the repository that contains the base code for this project. If you haven’t already, you can get the code from the following link:</p><pre>git clone https://github.com/Gayathri-Selvaganapathi/emotion_based_music_recommendation.git<br>cd emotion-based-music-recommendation</pre><p>This repository contains all the scripts needed for data collection, model training, and the web app.</p><h3>Data Collection</h3><p>The first step is to collect data for training the emotion detection model. I have used this repo for the data collectiona nd training <a href="https://github.com/Pawandeep-prog/liveEmoji">https://github.com/Pawandeep-prog/liveEmoji</a>.</p><p>We achieve this by capturing images of different emotions such as happy, sad, angry, and so on.</p><p>Run the data_collection.py script to start collecting data:</p><pre>python data_collection.py</pre><p>The script will prompt you to enter the name of the emotion for which you want to collect data (e.g., “happy”). It then starts capturing images from your webcam.</p><p>Here’s a snippet of what the data collection code looks like:</p><pre>import mediapipe as mp <br>import numpy as np <br>import cv2 <br> <br>cap = cv2.VideoCapture(0)<br><br>name = input(&quot;Enter the name of the data : &quot;)<br><br>holistic = mp.solutions.holistic<br>hands = mp.solutions.hands<br>holis = holistic.Holistic()<br>drawing = mp.solutions.drawing_utils<br><br>X = []<br>data_size = 0<br><br>while True:<br> lst = []<br><br> _, frm = cap.read()<br><br> frm = cv2.flip(frm, 1)<br><br> res = holis.process(cv2.cvtColor(frm, cv2.COLOR_BGR2RGB))<br><br><br> if res.face_landmarks:<br>  for i in res.face_landmarks.landmark:<br>   lst.append(i.x - res.face_landmarks.landmark[1].x)<br>   lst.append(i.y - res.face_landmarks.landmark[1].y)<br><br>  if res.left_hand_landmarks:<br>   for i in res.left_hand_landmarks.landmark:<br>    lst.append(i.x - res.left_hand_landmarks.landmark[8].x)<br>    lst.append(i.y - res.left_hand_landmarks.landmark[8].y)<br>  else:<br>   for i in range(42):<br>    lst.append(0.0)<br><br>  if res.right_hand_landmarks:<br>   for i in res.right_hand_landmarks.landmark:<br>    lst.append(i.x - res.right_hand_landmarks.landmark[8].x)<br>    lst.append(i.y - res.right_hand_landmarks.landmark[8].y)<br>  else:<br>   for i in range(42):<br>    lst.append(0.0)<br><br><br>  X.append(lst)<br>  data_size = data_size+1<br><br><br><br> drawing.draw_landmarks(frm, res.face_landmarks, holistic.FACEMESH_CONTOURS)<br> drawing.draw_landmarks(frm, res.left_hand_landmarks, hands.HAND_CONNECTIONS)<br> drawing.draw_landmarks(frm, res.right_hand_landmarks, hands.HAND_CONNECTIONS)<br><br> cv2.putText(frm, str(data_size), (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0),2)<br><br> cv2.imshow(&quot;window&quot;, frm)<br><br> if cv2.waitKey(1) == 27 or data_size&gt;99:<br>  cv2.destroyAllWindows()<br>  cap.release()<br>  break<br><br><br>np.save(f&quot;{name}.npy&quot;, np.array(X))<br>print(np.array(X).shape)</pre><h3>Training the Model</h3><p>Once you have collected enough data for each emotion, the next step is to train the model. This can be done by running the training.py script:</p><pre>python training.py</pre><p>The script processes the images, trains a neural network on them, and saves the trained model as model.h5.</p><p>Here’s how the training script is be structured:</p><pre>import os  <br>import numpy as np <br>import cv2 <br>from tensorflow.keras.utils import to_categorical<br><br>from keras.layers import Input, Dense <br>from keras.models import Model<br> <br>is_init = False<br>size = -1<br><br>label = []<br>dictionary = {}<br>c = 0<br><br>for i in os.listdir():<br> if i.split(&quot;.&quot;)[-1] == &quot;npy&quot; and not(i.split(&quot;.&quot;)[0] == &quot;labels&quot;):  <br>  if not(is_init):<br>   is_init = True <br>   X = np.load(i)<br>   size = X.shape[0]<br>   y = np.array([i.split(&#39;.&#39;)[0]]*size).reshape(-1,1)<br>  else:<br>   X = np.concatenate((X, np.load(i)))<br>   y = np.concatenate((y, np.array([i.split(&#39;.&#39;)[0]]*size).reshape(-1,1)))<br><br>  label.append(i.split(&#39;.&#39;)[0])<br>  dictionary[i.split(&#39;.&#39;)[0]] = c  <br>  c = c+1<br><br><br>for i in range(y.shape[0]):<br> y[i, 0] = dictionary[y[i, 0]]<br>y = np.array(y, dtype=&quot;int32&quot;)<br><br>###  hello = 0 nope = 1 ---&gt; [1,0] ... [0,1]<br><br>y = to_categorical(y)<br><br>X_new = X.copy()<br>y_new = y.copy()<br>counter = 0 <br><br>cnt = np.arange(X.shape[0])<br>np.random.shuffle(cnt)<br><br>for i in cnt: <br> X_new[counter] = X[i]<br> y_new[counter] = y[i]<br> counter = counter + 1<br><br><br>ip = Input(shape=(X.shape[1]))<br><br>m = Dense(512, activation=&quot;relu&quot;)(ip)<br>m = Dense(256, activation=&quot;relu&quot;)(m)<br><br>op = Dense(y.shape[1], activation=&quot;softmax&quot;)(m) <br><br>model = Model(inputs=ip, outputs=op)<br><br>model.compile(optimizer=&#39;rmsprop&#39;, loss=&quot;categorical_crossentropy&quot;, metrics=[&#39;acc&#39;])<br><br>model.fit(X, y, epochs=50)<br><br><br>model.save(&quot;model.h5&quot;)<br>np.save(&quot;labels.npy&quot;, np.array(label))</pre><h3>Creating the Web App</h3><p>With the model trained, it’s time to create the web app using Streamlit. This involves setting up the user interface (UI) and integrating the emotion detection model.</p><h3>5. Coding the Emotion Detection Logic</h3><h3>Emotion Processor Class</h3><p>The core functionality of this project lies in detecting emotions from the user’s face in real-time. This is achieved by creating an EmotionProcessor class that handles the video frames captured by the webcam.</p><p>Here’s the complete EmotionProcessor class:</p><pre>class EmotionProcessor:<br>    def recv(self, frame):<br>        frm = frame.to_ndarray(format=&quot;bgr24&quot;)<br><br>        ##############################<br>        frm = cv2.flip(frm, 1)<br><br>        res = holis.process(cv2.cvtColor(frm, cv2.COLOR_BGR2RGB))<br><br>        lst = []<br><br>        if res.face_landmarks:<br>            for i in res.face_landmarks.landmark:<br>                lst.append(i.x - res.face_landmarks.landmark[1].x)<br>                lst.append(i.y - res.face_landmarks.landmark[1].y)<br><br>            if res.left_hand_landmarks:<br>                for i in res.left_hand_landmarks.landmark:<br>                    lst.append(i.x - res.left_hand_landmarks.landmark[8].x)<br>                    lst.append(i.y - res.left_hand_landmarks.landmark[8].y)<br>            else:<br>                for i in range(42):<br>                    lst.append(0.0)<br><br>            if res.right_hand_landmarks:<br>                for i in res.right_hand_landmarks.landmark:<br>                    lst.append(i.x - res.right_hand_landmarks.landmark[8].x)<br>                    lst.append(i.y - res.right_hand_landmarks.landmark[8].y)<br>            else:<br>                for i in range(42):<br>                    lst.append(0.0)<br><br>            lst = np.array(lst).reshape(1,-1)<br><br>            pred = label[np.argmax(model.predict(lst))]<br><br>            print(pred)<br>            cv2.putText(frm, pred, (50,50),cv2.FONT_ITALIC, 1, (255,0,0),2)<br><br>            np.save(&quot;emotion.npy&quot;, np.array([pred]))<br><br>        drawing.draw_landmarks(frm, res.face_landmarks, holistic.FACEMESH_TESSELATION,<br>                                landmark_drawing_spec=drawing.DrawingSpec(color=(0,0,255), thickness=-1, circle_radius=1),<br>                                connection_drawing_spec=drawing.DrawingSpec(thickness=1))<br>        drawing.draw_landmarks(frm, res.left_hand_landmarks, hands.HAND_CONNECTIONS)<br>        drawing.draw_landmarks(frm, res.right_hand_landmarks, hands.HAND_CONNECTIONS)<br><br>        ##############################<br><br>        return av.VideoFrame.from_ndarray(frm, format=&quot;bgr24&quot;)</pre><h3>Loading the Model and Labels</h3><p>Before using the model in our EmotionProcessor class, we need to load the model and labels:</p><pre>model = load_model(&quot;model.h5&quot;)<br>label = np.load(&quot;labels.npy&quot;)</pre><p>These lines ensure that the model is ready to predict emotions and that we can map the predictions to human-readable labels.</p><h3>Using Mediapipe for Landmark Detection</h3><p>Mediapipe is an essential part of this project, providing the tools needed to detect facial and hand landmarks. Here’s how Mediapipe is integrated</p><pre>holistic = mp.solutions.holistic<br>hands = mp.solutions.hands<br>holis = holistic.Holistic()<br>drawing = mp.solutions.drawing_utils</pre><p>These lines initialize Mediapipe’s holistic and hand models, which are used to extract landmarks from the user’s face and hands.</p><h3>6.Integrating the Music Recommender</h3><h3>Creating the UI</h3><p>The UI for this project is built using Streamlit, which allows us to quickly create interactive web applications. Here’s how the UI is set up:</p><pre>import streamlit as st<br>st.header(&quot;Emotion Based Music Recommender&quot;)<br># Button to trigger the recommendation<br>btn = st.button(&quot;Recommend me songs&quot;)</pre><h3>Handling the YouTube Search Query</h3><p>Once the emotion is detected, the app uses the emotion to create a YouTube search query. Here’s the code that handles this:</p><pre>if btn:<br>    if not emotion:<br>        st.warning(&quot;Please let me capture your emotion first&quot;)<br>        st.session_state[&quot;run&quot;] = &quot;true&quot;<br>    else:<br>        webbrowser.open(f&quot;https://www.youtube.com/results?search_query={emotion}+song&quot;)<br>        np.save(&quot;emotion.npy&quot;, np.array([&quot;&quot;]))<br>        st.session_state[&quot;run&quot;] = &quot;false&quot;</pre><p>This code ensures that the app only proceeds to recommend songs once an emotion has been detected. It then opens a new browser tab with a YouTube search query based on the detected emotion.</p><h3>7.Handling Session State</h3><p>Session state is crucial in this project, as it controls whether the webcam should continue capturing frames. Streamlit’s session state functionality allows us to manage this efficiently:</p><pre>if &quot;run&quot; not in st.session_state:<br>    st.session_state[&quot;run&quot;] = &quot;true&quot;</pre><p>By setting st.session_state[&quot;run&quot;] to false after a recommendation is made, we ensure that the webcam stops capturing frames, preventing unnecessary processing.</p><p>Here’s how we handle session state when the “Recommend me songs” button is pressed:</p><pre>if btn:<br>    if not emotion:<br>        st.warning(&quot;Please let me capture your emotion first&quot;)<br>        st.session_state[&quot;run&quot;] = &quot;true&quot;<br>    else:<br>        webbrowser.open(f&quot;https://www.youtube.com/results?search_query={emotion}+song&quot;)<br>        np.save(&quot;emotion.npy&quot;, np.array([&quot;&quot;]))<br>        st.session_state[&quot;run&quot;] = &quot;false&quot;</pre><h3>8.Final Touches and Testing</h3><p>With all the components integrated, it’s time to test the application. Run the app using the following command:</p><pre>streamlit run app.py</pre><p>Here’s what to do during testing:</p><ol><li><strong>Allow the webcam to capture your emotion</strong>: The app will detect your emotion in real-time.</li><li><strong>Click the “Recommend me songs” button</strong>: The app will open a YouTube search query in a new tab based on your detected emotion.</li></ol><h3>Testing Scenarios</h3><ul><li><strong>Scenario 1</strong>: Test with a happy expression and see if the app recommends happy songs.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PGbPpFwKc5VR9XEQVbTFwQ.png" /></figure><ul><li><strong>Scenario 2</strong>: Test with a sad expression and ensure the app recommends sad songs.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3O2bBbhEiHUDQ9IjvtiVOA.png" /></figure><ul><li><strong>Scenario 3</strong>: Test with a rock hand gesture and ensure the app recommends rock songs.</li></ul><h3>9.Conclusion</h3><p>In this blog, we’ve walked through the process of building an <strong>Emotion-Based Music Recommender</strong> using Streamlit, Mediapipe, Keras, and other powerful tools. This project showcases how AI can be used to create personalized user experiences by combining real-time emotion detection with music recommendations.</p><p>This project not only enhances your understanding of computer vision and deep learning but also demonstrates how these technologies can be integrated into interactive web applications.</p><p>Feel free to customize and expand this project. You might consider adding more emotions, integrating different music platforms, or even creating a mobile version of the app. The possibilities are endless!</p><h3>10.References</h3><ol><li><a href="https://github.com/Gayathri-Selvaganapathi/emotion_based_music_recommendation">My github</a></li><li><a href="https://github.com/Pawandeep-prog/liveEmoji/blob/main/data_collection.py">Referred emotion capture repo</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5f637cb7352b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Predicting Customer Churn Using XGBoost: A Comprehensive Guide]]></title>
            <link>https://medium.com/@gayathri.s.de/predicting-customer-churn-using-xgboost-a-comprehensive-guide-d2d45d412f3b?source=rss-c85e34fadf4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2d45d412f3b</guid>
            <category><![CDATA[churn-prediction]]></category>
            <category><![CDATA[customer-engagement]]></category>
            <category><![CDATA[xgboost]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Gayathri Selvaganapathi]]></dc:creator>
            <pubDate>Fri, 30 Aug 2024 08:39:38 GMT</pubDate>
            <atom:updated>2024-08-30T08:39:38.049Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Yk4fX0YiQrte3BiF" /><figcaption>Photo by <a href="https://unsplash.com/@blakewisz?utm_source=medium&amp;utm_medium=referral">Blake Wisz</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>Table of Contents</h4><ol><li>Introduction</li><li>Understanding the Dataset</li><li>Setting Up the Environment</li></ol><ul><li>Clone the GitHub Repository</li><li>Install Dependencies</li><li>Load the Dataset</li><li>Run the Jupyter Notebook</li></ul><p>4. Data Preprocessing</p><ul><li>Handling Missing Data and Categorical Variables</li><li>Correcting Numerical Data Formats</li><li>Feature Scaling</li></ul><p>5. Model Building</p><ul><li>Splitting the Data</li><li>Training the XGBoost Classifier</li><li>Evaluating the Model</li></ul><p>6. Hyperparameter Tuning</p><ul><li>Setting Up GridSearchCV</li><li>Evaluating the Tuned Model</li></ul><p>7. Conclusion</p><p>8. Next Steps</p><ul><li>Experiment with Additional Features</li><li>Try Different Algorithms</li><li>Deploy the Model</li></ul><p>9. References</p><h3>1. Introduction</h3><p>In today’s highly competitive market, customer retention is as crucial as acquiring new customers. For subscription-based businesses, understanding and predicting customer churn — when a customer stops using a service — can significantly impact revenue. By leveraging machine learning techniques, companies can predict which customers are likely to churn and take proactive measures to retain them.</p><p>In this blog post, we’ll walk through a detailed process of building a machine learning model to predict customer churn using the XGBoost algorithm, known for its efficiency and performance in classification tasks. We will cover everything from data preprocessing, model building, and evaluation to hyperparameter tuning. The dataset used in this project is sourced from Kaggle, and by the end of this post, you’ll have a clear understanding of how to implement a churn prediction model for your own datasets.</p><h3>2. Understanding the Dataset</h3><p>The dataset for this project provides a rich set of features related to customer behavior, including:</p><ul><li><strong>Average Order Value</strong>: The average value of orders placed by the customer.</li><li><strong>Discount Rates</strong>: The average discount the customer receives.</li><li><strong>Product Views</strong>: The number of product pages viewed by the customer.</li><li><strong>Session Details</strong>: Information about the customer’s interactions during their sessions.</li></ul><p>The target variable in this dataset is <strong>Churn</strong>, a binary indicator (0 or 1) representing whether a customer has churned.</p><p><strong>Dataset Overview</strong>:</p><ul><li><strong>File Name</strong>: data.csv</li><li><strong>Number of Columns</strong>: 20</li><li><strong>Key Features</strong>: average_order_value, discount_rate_per_visited_product, product_detail_view, location_code, etc.</li><li><strong>Target Variable</strong>: Churn</li></ul><h3>3. Setting Up the Environment</h3><p>Before we dive into the model-building process, you need to set up your Python environment. This involves installing the necessary libraries and tools required to execute the code.</p><h4>3.1 Clone the GitHub Repository</h4><p>The first step is to clone the repository containing all the code and data for this project.</p><pre>git clone https://github.com/Gayathri-Selvaganapathi/customer_churn_prediction.git<br>cd customer-churn-prediction</pre><h4>3.2 Install Dependencies</h4><p>Install the required Python packages using the requirements.txt file.</p><pre>pip install -r requirements.txt</pre><h4>3.3 Load the Dataset</h4><p>Download the dataset from <a href="https://www.kaggle.com/datasets/ermismbatuhan/digital-marketing-ecommerce-customer-behavior">Kaggle</a> and place the data.csv file in the root directory of the project.</p><h4>3.4 Run the Jupyter Notebook</h4><p>Open the Jupyter Notebook or JupyterLab and navigate to Customer_Churn_Prediction.ipynb. This notebook contains all the steps for data preprocessing, model building, and evaluation.</p><h3>4. Data Preprocessing</h3><p>Data preprocessing is a crucial step that prepares the dataset for model training. Proper preprocessing can greatly enhance model performance and ensure that the features fed into the model are relevant and correctly formatted.</p><h4>4.1 Handling Missing Data and Categorical Variables</h4><p>The dataset includes a variety of features, some of which are categorical and need to be converted into a format that the machine learning model can process. For example:</p><ul><li><strong>Location Code</strong>: Initially stored as an integer, this column represents categorical data (like postal codes). We convert it into a string and then into categorical data.</li><li><strong>Yes/No Columns</strong>: Columns such as credit_card_info_save and push_status are binary categorical variables. These are converted to integers (0 and 1) to facilitate the model&#39;s learning process.</li></ul><pre>df[&#39;location_code&#39;] = df[&#39;location_code&#39;].astype(str)<br>df[&#39;credit_card_info_save&#39;] = df[&#39;credit_card_info_save&#39;].replace({&#39;Yes&#39;: 1, &#39;No&#39;: 0})<br>df[&#39;push_status&#39;] = df[&#39;push_status&#39;].replace({&#39;Yes&#39;: 1, &#39;No&#39;: 0})</pre><h4>4.2 Correcting Numerical Data Formats</h4><p>Some numerical columns contain commas as thousand separators, which need to be replaced with dots to convert the data into float format. This step ensures that these values can be correctly used in mathematical operations during model training.</p><pre>df[&#39;average_order_value&#39;] = df[&#39;average_order_value&#39;].str.replace(&#39;,&#39;, &#39;.&#39;).astype(float)<br>df[&#39;discount_rate_per_visited_product&#39;] = df[&#39;discount_rate_per_visited_product&#39;].str.replace(&#39;,&#39;, &#39;.&#39;).astype(float)</pre><h4>4.3 Feature Scaling</h4><p>Feature scaling is essential in ensuring that all numerical values are within the same range. This step prevents features with larger scales from disproportionately influencing the model. We use Normalizer to scale the numerical features.</p><pre>from sklearn.preprocessing import Normalizer<br>scaler = Normalizer()<br>scaled_features = scaler.fit_transform(df[[&#39;average_order_value&#39;, &#39;discount_rate_per_visited_product&#39;]])<br>df_scaled = pd.DataFrame(scaled_features, columns=[&#39;average_order_value&#39;, &#39;discount_rate_per_visited_product&#39;])</pre><h3>5. Model Building</h3><p>With our data preprocessed and ready, we can now focus on building the model. The XGBoost classifier is a powerful tool that uses gradient boosting techniques to achieve high accuracy, especially for structured data.</p><h4>5.1 Splitting the Data</h4><p>Before training the model, we need to split the dataset into training and testing sets. The training set is used to train the model, while the testing set is used to evaluate its performance.</p><pre>from sklearn.model_selection import train_test_split<br>X = df.drop(&#39;Churn&#39;, axis=1)<br>y = df[&#39;Churn&#39;]<br>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)</pre><h4>5.2 Training the XGBoost Classifier</h4><p>We initialize the XGBoost classifier and train it on the training data. After training, we evaluate the model on the test set.</p><pre>import xgboost as xgb<br><br>xgb_clf = xgb.XGBClassifier()<br>xgb_clf.fit(X_train, y_train)<br>y_pred = xgb_clf.predict(X_test)</pre><h4>5.3 Evaluating the Model</h4><p>The model’s performance is evaluated using the accuracy score, which measures the proportion of correct predictions. Initially, the model achieves an accuracy of <strong>91.54%</strong>.</p><pre>from sklearn.metrics import accuracy_score<br>accuracy = accuracy_score(y_test, y_pred)<br>print(f&quot;Initial Model Accuracy: {accuracy * 100:.2f}%&quot;)</pre><h3>6. Hyperparameter Tuning</h3><p>Hyperparameter tuning involves adjusting the model’s parameters to optimize performance. XGBoost offers several hyperparameters that can be fine-tuned to improve the model’s accuracy.</p><h4>6.1 Setting Up GridSearchCV</h4><p>We use GridSearchCV to systematically test different combinations of hyperparameters. The parameters tuned include max_depth, learning_rate, gamma, and subsample.</p><pre>from sklearn.model_selection import GridSearchCV<br><br>param_grid = {<br>    &#39;max_depth&#39;: [3, 5, 7],<br>    &#39;learning_rate&#39;: [0.01, 0.05, 0.1],<br>    &#39;gamma&#39;: [0, 1, 5],<br>    &#39;subsample&#39;: [0.8, 1.0]<br>}<br>grid_search = GridSearchCV(estimator=xgb_clf, param_grid=param_grid, scoring=&#39;accuracy&#39;, cv=3)<br>grid_search.fit(X_train, y_train)</pre><h4>6.2 Evaluating the Tuned Model</h4><p>After hyperparameter tuning, the final model’s accuracy improves to <strong>92.72%</strong>, demonstrating the effectiveness of fine-tuning in enhancing model performance.</p><pre>final_accuracy = grid_search.best_score_<br>print(f&quot;Final Model Accuracy after Tuning: {final_accuracy * 100:.2f}%&quot;)</pre><h3>7. Conclusion</h3><p>Predicting customer churn is a vital aspect of maintaining a strong customer base in subscription-based businesses. By building a machine learning model using XGBoost, we were able to predict customer churn with an accuracy of over 92%. This project highlights the importance of data preprocessing, feature scaling, and hyperparameter tuning in developing robust machine learning models.</p><p>The techniques and methods demonstrated in this project can be applied to various business cases, making XGBoost a versatile tool</p><h3>8. Next Steps</h3><p>If you’re interested in exploring this project further, consider the following:</p><ol><li><strong>Experiment with Additional Features</strong>: Incorporate more features from the dataset or external sources to improve model performance.</li><li><strong>Try Different Algorithms</strong>: Compare XGBoost’s performance with other classification algorithms like Random Forest, SVM, or Neural Networks.</li><li><strong>Deploy the Model</strong>: Once satisfied with the model’s performance, deploy it into a production environment using tools like Flask, Django, or FastAPI.</li></ol><h3><strong>9. References</strong></h3><ol><li><a href="https://www.kaggle.com/datasets/ermismbatuhan/digital-marketing-ecommerce-customer-behavior">Kaggle Dataset</a></li></ol><p>2. <a href="https://github.com/Gayathri-Selvaganapathi/customer_churn_prediction">My GitHub Repo</a></p><p>3. <a href="https://www.youtube.com/@datascienceunlocked">Referred model</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2d45d412f3b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Anomaly Detection in Building Data]]></title>
            <link>https://medium.com/@gayathri.s.de/anomaly-detection-in-building-data-e6fe841cf285?source=rss-c85e34fadf4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/e6fe841cf285</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[unsupervised-learning]]></category>
            <category><![CDATA[anomaly-detection]]></category>
            <category><![CDATA[predictive-analytics]]></category>
            <dc:creator><![CDATA[Gayathri Selvaganapathi]]></dc:creator>
            <pubDate>Fri, 30 Aug 2024 04:43:22 GMT</pubDate>
            <atom:updated>2024-08-30T04:55:51.481Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*exFDkq6SUSjECDrMBUdh_w.png" /></figure><h3>Table of Contents</h3><ol><li>Introduction</li><li>Key Areas of Anomaly Detection in Building Management</li></ol><ul><li>2.1 Proactive Maintenance</li><li>2.2 Energy Efficiency</li><li>2.3 Occupant Comfort</li></ul><p>3. Understanding the Algorithms</p><ul><li>3.1 Angle-Based Outlier Detector (ABOD)</li><li>3.2 Gaussian Mixture Model (GMM)</li><li>3.3 Isolation Forest</li><li>3.4 Cluster-Based Local Outlier Factor (CBLOF)</li><li>3.5 Histogram-Based Outlier Detection (HBOS)</li><li>3.6 K-Nearest Neighbors (KNN)</li><li>3.7 Principal Component Analysis (PCA)</li><li>3.8 Support Vector Machine (SVM)</li></ul><p>4. Applying the Algorithms to Building Data</p><ul><li>4.1 Loading and Preprocessing the Data</li><li>4.2 Unsupervised Anomaly Detection</li><li>4.3 Supervised Anomaly Detection</li></ul><p>5. Resulting Plots</p><p>6. Conclusion and Future Work</p><p>7. References</p><h3>1. Introduction</h3><p>Buildings today are more than just physical structures; they are complex ecosystems embedded with sensors that continuously monitor various parameters such as temperature, humidity, energy consumption, and occupancy levels. This wealth of data presents an opportunity to not only manage building operations more efficiently but also to preemptively address potential issues through anomaly detection.</p><p>Anomalies in building data could indicate equipment failures, energy inefficiencies, or deviations in occupancy patterns that might signal security concerns. By employing machine learning techniques to detect these anomalies, building managers can transform their facilities into smart environments capable of proactive maintenance, optimized energy consumption, and enhanced occupant comfort.</p><p>In this comprehensive guide, we will explore a variety of machine learning algorithms for anomaly detection, applying both unsupervised and supervised methods to real-world building data. We will delve into the strengths and weaknesses of each approach, supported by detailed code snippets and visualizations.</p><h3>2. Key Areas of Anomaly Detection in Building Management</h3><p>Anomaly detection in building data is crucial for several reasons:</p><h3>2.1 Proactive Maintenance</h3><ul><li>Identifying anomalies in equipment performance allows for early intervention, reducing the likelihood of major failures. This not only minimizes downtime but also extends the life of critical systems, ultimately saving costs on repairs and replacements.</li></ul><h3>2.2 Energy Efficiency</h3><ul><li>Buildings consume a significant amount of energy, and even minor inefficiencies can lead to substantial waste. Anomaly detection can pinpoint irregularities in energy usage, enabling targeted interventions to reduce consumption, lower operational costs, and support sustainability initiatives.</li></ul><h3>2.3 Occupant Comfort</h3><ul><li>Comfort is paramount in residential and commercial spaces. Anomalies in environmental data (e.g., temperature, air quality) can lead to uncomfortable conditions for occupants. By detecting these issues early, building managers can take corrective action to maintain a pleasant and safe environment.</li></ul><h3>3. Understanding the Algorithms</h3><p>Various machine learning algorithms are employed for anomaly detection, each with its unique strengths and application scenarios. Here’s a deeper dive into some of the most effective methods:</p><h3>3.1 Angle-Based Outlier Detector (ABOD)</h3><ul><li><strong>Concept</strong>: ABOD evaluates the angle between pairs of data points with respect to the origin. Points with smaller angles compared to the majority are considered outliers.</li><li><strong>Application</strong>: Useful in high-dimensional data where traditional distance-based methods may struggle.</li></ul><p><strong>Code</strong>:</p><pre>from pyod.models.abod import ABOD<br>abod = ABOD()<br>abod.fit(preprocessed_data[[&#39;CO2&#39;, &#39;electricity&#39;]])<br>preprocessed_data[&#39;abod_anomaly&#39;] = abod.labels_<br># Plotting<br>plt.figure(figsize=(10, 6))<br>plt.scatter(preprocessed_data[&#39;CO2&#39;], preprocessed_data[&#39;electricity&#39;], c=preprocessed_data[&#39;abod_anomaly&#39;], cmap=&#39;coolwarm&#39;)<br>plt.title(&#39;ABOD Anomaly Detection&#39;)<br>plt.xlabel(&#39;CO2 Levels&#39;)<br>plt.ylabel(&#39;Electricity Consumption&#39;)<br>plt.show()</pre><h3>3.2 Gaussian Mixture Model (GMM)</h3><ul><li><strong>Concept</strong>: GMM assumes that the data is a mixture of several Gaussian distributions. It estimates the parameters (mean and covariance) of these distributions and assigns a likelihood to each data point. Points with low likelihood are flagged as anomalies.</li><li><strong>Application</strong>: Effective when the data naturally clusters around multiple centers (e.g., different operating states of a building system).</li></ul><p><strong>Code</strong>:</p><pre>from sklearn.mixture import GaussianMixture<br>gmm = GaussianMixture(n_components=2)<br>gmm.fit(preprocessed_data[[&#39;CO2&#39;, &#39;electricity&#39;]])<br>scores = gmm.score_samples(preprocessed_data[[&#39;CO2&#39;, &#39;electricity&#39;]])<br>preprocessed_data[&#39;gmm_anomaly&#39;] = (scores &lt; threshold).astype(int)<br># Plotting<br>plt.figure(figsize=(10, 6))<br>plt.scatter(preprocessed_data[&#39;CO2&#39;], preprocessed_data[&#39;electricity&#39;], c=preprocessed_data[&#39;gmm_anomaly&#39;], cmap=&#39;coolwarm&#39;)<br>plt.title(&#39;GMM Anomaly Detection&#39;)<br>plt.xlabel(&#39;CO2 Levels&#39;)<br>plt.ylabel(&#39;Electricity Consumption&#39;)<br>plt.show()</pre><h3>3.3 Isolation Forest</h3><ul><li><strong>Concept</strong>: Isolation Forest isolates observations by randomly selecting a feature and then randomly selecting a split value between the maximum and minimum values of the selected feature. The algorithm is based on the premise that anomalies are few and different, making them easier to isolate.</li><li><strong>Application</strong>: Widely used for its efficiency and scalability, especially in large datasets.</li></ul><p><strong>Example Code</strong>:</p><pre>from sklearn.ensemble import IsolationForest<br>isolation_forest = IsolationForest(contamination=0.01)<br>preprocessed_data[&#39;iforest_anomaly&#39;] = isolation_forest.fit_predict(preprocessed_data[[&#39;CO2&#39;, &#39;electricity&#39;]])<br># Identifying anomalies<br>anomalies = preprocessed_data[preprocessed_data[&#39;iforest_anomaly&#39;] == -1]<br># Plotting<br>plt.figure(figsize=(10, 6))<br>plt.plot(preprocessed_data[&#39;timestamp&#39;], preprocessed_data[&#39;CO2&#39;], label=&#39;CO2 Levels&#39;)<br>plt.scatter(anomalies[&#39;timestamp&#39;], anomalies[&#39;CO2&#39;], color=&#39;red&#39;, label=&#39;Anomaly&#39;, marker=&#39;x&#39;)<br>plt.title(&#39;CO2 Levels with Anomalies Detected by Isolation Forest&#39;)<br>plt.xlabel(&#39;Timestamp&#39;)<br>plt.ylabel(&#39;CO2 Levels&#39;)<br>plt.legend()<br>plt.show()</pre><h3>3.4 Cluster-Based Local Outlier Factor (CBLOF)</h3><ul><li><strong>Concept</strong>: CBLOF calculates the local outlier factor for each data point by comparing its distance to its closest cluster centroid. Points that deviate significantly from their cluster are considered outliers.</li><li><strong>Application</strong>: Suitable for data where natural clusters exist, such as different operational modes of HVAC systems.</li></ul><p><strong>Code</strong>:</p><pre>from pyod.models.cblof import CBLOF<br>cblof = CBLOF()<br>cblof.fit(preprocessed_data[[&#39;CO2&#39;, &#39;electricity&#39;]])<br>preprocessed_data[&#39;cblof_anomaly&#39;] = cblof.labels_<br># Plotting<br>plt.figure(figsize=(10, 6))<br>plt.scatter(preprocessed_data[&#39;CO2&#39;], preprocessed_data[&#39;electricity&#39;], c=preprocessed_data[&#39;cblof_anomaly&#39;], cmap=&#39;coolwarm&#39;)<br>plt.title(&#39;CBLOF Anomaly Detection&#39;)<br>plt.xlabel(&#39;CO2 Levels&#39;)<br>plt.ylabel(&#39;Electricity Consumption&#39;)<br>plt.show()</pre><h3>3.5 Histogram-Based Outlier Detection (HBOS)</h3><ul><li><strong>Concept</strong>: HBOS is a fast, unsupervised method that segments the data into bins (histograms) and assigns an anomaly score based on the density of each bin. Outliers are detected in regions with low density.</li><li><strong>Application</strong>: Effective in large datasets where speed is critical, such as real-time monitoring systems.</li></ul><p><strong>Code</strong>:</p><pre>from pyod.models.hbos import HBOS<br>hbos = HBOS()<br>hbos.fit(preprocessed_data[[&#39;CO2&#39;, &#39;electricity&#39;]])<br>preprocessed_data[&#39;hbos_anomaly&#39;] = hbos.labels_<br># Plotting<br>plt.figure(figsize=(10, 6))<br>plt.scatter(preprocessed_data[&#39;CO2&#39;], preprocessed_data[&#39;electricity&#39;], c=preprocessed_data[&#39;hbos_anomaly&#39;], cmap=&#39;coolwarm&#39;)<br>plt.title(&#39;HBOS Anomaly Detection&#39;)<br>plt.xlabel(&#39;CO2 Levels&#39;)<br>plt.ylabel(&#39;Electricity Consumption&#39;)<br>plt.show()</pre><h3>3.6 K-Nearest Neighbors (KNN)</h3><ul><li><strong>Concept</strong>: KNN detects anomalies by comparing each data point to its nearest neighbors. If a point is significantly different from its neighbors, it is considered an anomaly.</li><li><strong>Application</strong>: KNN is simple yet powerful, making it a popular choice for various anomaly detection tasks.</li></ul><p><strong>Code</strong>:</p><pre>from pyod.models.knn import KNN<br>knn = KNN()<br>knn.fit(preprocessed_data[[&#39;CO2&#39;, &#39;electricity&#39;]])<br>preprocessed_data[&#39;knn_anomaly&#39;] = knn.labels_<br># Plotting<br>plt.figure(figsize=(10, 6))<br>plt.scatter(preprocessed_data[&#39;CO2&#39;], preprocessed_data[&#39;electricity&#39;], c=preprocessed_data[&#39;knn_anomaly&#39;], cmap=&#39;coolwarm&#39;)<br>plt.title(&#39;KNN Anomaly Detection&#39;)<br>plt.xlabel(&#39;CO2 Levels&#39;)<br>plt.ylabel(&#39;Electricity Consumption&#39;)<br>plt.show()</pre><h3>3.7 Principal Component Analysis (PCA)</h3><ul><li><strong>Concept</strong>: PCA reduces the dimensionality of the data by transforming it into a set of orthogonal components. Anomalies are detected as points that do not align with the main axes of the data.</li><li><strong>Application</strong>: Ideal for high-dimensional data where traditional methods may be less effective.</li></ul><p><strong>Code</strong>:</p><pre>from sklearn.decomposition import PCA<br>pca = PCA(n_components=2)<br>transformed_data = pca.fit_transform(preprocessed_data[[&#39;CO2&#39;, &#39;electricity&#39;]])<br>preprocessed_data[&#39;pca_anomaly&#39;] = (np.abs(transformed_data) &gt; threshold).any(axis=1).astype(int)<br># Plotting<br>plt.figure(figsize=(10, 6))<br>plt.scatter(transformed_data[:, 0], transformed_data[:, 1], c=preprocessed_data[&#39;pca_anomaly&#39;], cmap=&#39;coolwarm&#39;)<br>plt.title(&#39;PCA Anomaly Detection&#39;)<br>plt.xlabel(&#39;PCA Component 1&#39;)<br>plt.ylabel(&#39;PCA Component 2&#39;)<br>plt.show()</pre><h3>3.8 Support Vector Machine (SVM)</h3><ul><li><strong>Concept</strong>: SVM constructs a hyperplane in a high-dimensional space that separates normal data points from anomalies. Data points that lie on the wrong side of the hyperplane are considered outliers.</li><li><strong>Application</strong>: SVM is a versatile and effective method, particularly in datasets with complex, non-linear boundaries.</li></ul><p><strong>Example Code</strong>:</p><pre>from sklearn.svm import OneClassSVM<br>svm = OneClassSVM(gamma=&#39;auto&#39;)<br>svm.fit(preprocessed_data[[&#39;CO2&#39;, &#39;electricity&#39;]])<br>preprocessed_data[&#39;svm_anomaly&#39;] = svm.predict(preprocessed_data[[&#39;CO2&#39;, &#39;electricity&#39;]])<br># Plotting<br>plt.figure(figsize=(10, 6))<br>plt.scatter(preprocessed_data[&#39;CO2&#39;], preprocessed_data[&#39;electricity&#39;], c=preprocessed_data[&#39;svm_anomaly&#39;], cmap=&#39;coolwarm&#39;)<br>plt.title(&#39;SVM Anomaly Detection&#39;)<br>plt.xlabel(&#39;CO2 Levels&#39;)<br>plt.ylabel(&#39;Electricity Consumption&#39;)<br>plt.show()</pre><h3>4. Applying the Algorithms to Building Data</h3><p>We applied these algorithms to a real-world dataset from the Lawrence Berkeley National Laboratory, focusing on indoor CO2 levels and miscellaneous electrical consumption. Here’s a detailed analysis of how each algorithm performed:</p><h3>4.1 Loading and Preprocessing the Data</h3><p>Before diving into anomaly detection, it’s crucial to preprocess the data. This step includes cleaning the data, handling missing values, and scaling features to ensure that the algorithms perform optimally.</p><pre>import pandas as pd<br>import numpy as np<br>from utils import preprocess_data<br># Load the dataset<br>data = pd.read_csv(&#39;building_data.csv&#39;)<br># Preprocess the data<br>preprocessed_data = preprocess_data(data)<br># Scaling the data<br>from sklearn.preprocessing import StandardScaler<br>scaler = StandardScaler()<br>preprocessed_data[[&#39;CO2&#39;, &#39;electricity&#39;]] = scaler.fit_transform(preprocessed_data[[&#39;CO2&#39;, &#39;electricity&#39;]])</pre><h3>4.2 Unsupervised Anomaly Detection</h3><p>We began with unsupervised methods, which do not require labeled data. These methods are particularly useful when we do not have a pre-defined notion of what constitutes an anomaly.</p><h4>Example: Isolation Forest</h4><p>Isolation Forest is a robust and scalable method for detecting anomalies. It works by isolating observations by randomly selecting a feature and then randomly selecting a split value between the maximum and minimum values of the selected feature.</p><pre>from sklearn.ensemble import IsolationForest<br># Initialize and fit the model<br>isolation_forest = IsolationForest(contamination=0.01)<br>preprocessed_data[&#39;iforest_anomaly&#39;] = isolation_forest.fit_predict(preprocessed_data[[&#39;CO2&#39;, &#39;electricity&#39;]])<br># Identifying anomalies<br>anomalies = preprocessed_data[preprocessed_data[&#39;iforest_anomaly&#39;] == -1]<br># Plotting<br>plt.figure(figsize=(10, 6))<br>plt.plot(preprocessed_data[&#39;timestamp&#39;], preprocessed_data[&#39;CO2&#39;], label=&#39;CO2 Levels&#39;)<br>plt.scatter(anomalies[&#39;timestamp&#39;], anomalies[&#39;CO2&#39;], color=&#39;red&#39;, label=&#39;Anomaly&#39;, marker=&#39;x&#39;)<br>plt.title(&#39;CO2 Levels with Anomalies Detected by Isolation Forest&#39;)<br>plt.xlabel(&#39;Timestamp&#39;)<br>plt.ylabel(&#39;CO2 Levels&#39;)<br>plt.legend()<br>plt.show()</pre><p>In this example, Isolation Forest effectively identified peaks in CO2 levels that deviate from the norm, flagging them as anomalies. This is crucial for early detection of potential ventilation issues in buildings.</p><h3>4.3 Supervised Anomaly Detection</h3><p>Next, we employed supervised methods, which require labeled data. These methods are powerful when historical data with known anomalies is available, allowing the model to learn the patterns associated with normal and anomalous behavior.</p><h4>Example: Long Short-Term Memory (LSTM)</h4><p>LSTM networks are a type of recurrent neural network (RNN) well-suited for time series forecasting and anomaly detection. They are capable of learning long-term dependencies in sequential data, making them ideal for detecting anomalies in time series data like CO2 levels or energy consumption.</p><pre>from tensorflow.keras.models import Sequential<br>from tensorflow.keras.layers import LSTM, Dense<br><br># Prepare the data for LSTM<br>X_train, y_train = prepare_lstm_data(preprocessed_data[&#39;CO2&#39;])<br># Build the LSTM model<br>model = Sequential()<br>model.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], 1)))<br>model.add(LSTM(units=50))<br>model.add(Dense(1))<br># Compile and fit the model<br>model.compile(optimizer=&#39;adam&#39;, loss=&#39;mean_squared_error&#39;)<br>model.fit(X_train, y_train, epochs=20, batch_size=32)<br># Predict anomalies<br>predictions = model.predict(X_train)<br>anomalies = np.where(np.abs(predictions - y_train) &gt; threshold)[0]<br># Plotting<br>plt.figure(figsize=(10, 6))<br>plt.plot(y_train, label=&#39;True CO2 Levels&#39;)<br>plt.plot(predictions, label=&#39;Predicted CO2 Levels&#39;)<br>plt.scatter(anomalies, y_train[anomalies], color=&#39;red&#39;, label=&#39;Anomaly&#39;, marker=&#39;x&#39;)<br>plt.title(&#39;CO2 Levels with Anomalies Detected by LSTM&#39;)<br>plt.xlabel(&#39;Time Step&#39;)<br>plt.ylabel(&#39;CO2 Levels&#39;)<br>plt.legend()<br>plt.show()</pre><p>In this example, the LSTM model predicted CO2 levels based on past data. Significant deviations between predicted and actual values were flagged as anomalies, highlighting periods where the building’s ventilation system might have underperformed.</p><h3>5. Resulting Plots</h3><p><strong>5.1. LSTM and Gradient Boosted Trees (GBT) Anomaly Detection</strong>:</p><ul><li>The first plot showcases anomalies detected in a time series by an LSTM (Long Short-Term Memory) model. The LSTM highlights numerous anomalies (marked in red), especially during peaks and fluctuations in the time series, indicating its sensitivity to abrupt changes.</li><li>The second plot uses Gradient Boosted Trees (GBT) for anomaly detection on the same dataset. GBT is more conservative, detecting fewer anomalies compared to LSTM, primarily flagging significant peaks. This comparison highlights the differences in sensitivity between the two models.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/989/1*N2Hsd521DJhNU4YXTQEcnA.png" /></figure><p><strong>5.2 Z-Score Anomaly Detection</strong>:</p><ul><li>The plot shows anomaly detection using the Z-Score method. This method highlights outliers where the data significantly deviates from the mean, as seen in the few points (marked in red) during extreme peaks in the time series. Z-Score is effective at identifying anomalies based on statistical thresholds.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/923/1*5HPtLzg2D_a4YjIeXOAapQ.png" /></figure><p><strong>5.3. Isolation Forest Anomaly Detection</strong>:</p><ul><li>The plot presents anomalies detected by the Isolation Forest algorithm. This method detects anomalies throughout the time series, including both peaks and troughs, marked in red. Isolation Forest is known for its ability to identify outliers by isolating points that differ significantly from the majority.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/923/1*jaihSYrK-zb5lafjNYjQKA.png" /></figure><p><strong>5.4. Local Outlier Factor (LOF) Anomaly Detection</strong>:</p><ul><li>The fifth plot illustrates anomalies detected by the Local Outlier Factor (LOF). LOF identifies anomalies based on the density of points, detecting areas where points are significantly less dense compared to their neighbors. The red marks indicate anomalies in both high and low regions of the time series.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/923/1*ik-zUKm3eLqFRgmsjVNtug.png" /></figure><p>These plots demonstrate how different anomaly detection algorithms highlight outliers in various sections of the time series data, each with its unique sensitivity and approach. The choice of algorithm impacts the type and number of anomalies detected, providing insights into the dataset’s behavior under different analysis techniques.</p><h3>6. Conclusion and Future Work</h3><p>This analysis illustrates the importance of selecting the right algorithm for anomaly detection in building data. Each method has its strengths and weaknesses, and the choice of algorithm should be informed by the specific characteristics of the data and the operational goals.</p><p><strong>Key Takeaways</strong>:</p><ol><li><strong>Isolation Forest</strong>: Effective and scalable, making it suitable for large datasets with complex patterns.</li><li><strong>LSTM</strong>: Powerful for time series data, especially when long-term dependencies are present.</li><li><strong>ABOD, GMM, CBLOF, HBOS, KNN, PCA, SVM</strong>: Each of these methods offers unique advantages depending on the data’s nature and the specific anomalies of interest.</li></ol><p><strong>Future Work</strong>:</p><ul><li><strong>Threshold Tuning</strong>: Adjusting the threshold values for algorithms like LSTM and Isolation Forest could improve the accuracy of anomaly detection.</li><li><strong>Model Ensemble</strong>: Combining multiple models might yield more robust results by leveraging the strengths of different approaches.</li><li><strong>Real-Time Monitoring</strong>: Implementing these models in a real-time monitoring system could provide continuous insights into building performance, enabling immediate action when anomalies are detected.</li></ul><p>By continuously refining these models and algorithms, we can move closer to creating truly intelligent buildings that anticipate and respond to changes, ensuring comfort, safety, and cost-effectiveness.</p><h3>7. References</h3><ol><li><a href="https://github.com/Gayathri-Selvaganapathi/anomaly_detection_building_data">My Github repo</a></li><li><a href="https://gitlab.com/wower/anomaly-detect-buildings">Referred models</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e6fe841cf285" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Predicting Energy Consumption Using Time Series Forecasting]]></title>
            <link>https://medium.com/@gayathri.s.de/predicting-energy-consumption-using-time-series-forecasting-da965751ab95?source=rss-c85e34fadf4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/da965751ab95</guid>
            <category><![CDATA[energy-consumption]]></category>
            <category><![CDATA[xgboost]]></category>
            <category><![CDATA[time-series-forecasting]]></category>
            <category><![CDATA[predictive-analytics]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Gayathri Selvaganapathi]]></dc:creator>
            <pubDate>Thu, 29 Aug 2024 09:47:30 GMT</pubDate>
            <atom:updated>2024-08-29T09:47:30.782Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9LQwilVT4vpxFQqH" /><figcaption>Photo by <a href="https://unsplash.com/@agebarros?utm_source=medium&amp;utm_medium=referral">Agê Barros</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Table of Contents</h3><ol><li>Introduction</li><li>Understanding Time Series Data</li><li>Project Overview</li><li>Data Preparation</li></ol><ul><li>Loading and Inspecting the Data</li><li>Visualizing the Data</li></ul><p>5. Feature Engineering</p><ul><li>Extracting Time-Based Features</li><li>Implementing Feature Engineering</li></ul><p>6. Model Training with XGBoost</p><ul><li>Splitting the Data</li><li>Training the XGBoost Model</li></ul><p>7. Model Evaluation and Forecasting</p><ul><li>Evaluating the Model</li><li>Visualizing Predictions</li></ul><p>8. Deploying with Streamlit</p><ul><li>Creating the Streamlit App</li><li>Running the Application</li></ul><p>9. Conclusion and Next Steps</p><h3>1. Introduction</h3><p>In the world of data science, time series forecasting is a crucial technique used to predict future values based on historical data. It is widely applied in various domains such as finance, weather prediction, and energy consumption. In this blog, we’ll explore how to predict energy consumption using time series forecasting with the XGBoost machine learning model. We’ll go through the steps of preparing the data, engineering features, training the model, and finally deploying the model using a Streamlit interface.</p><h3>2. Understanding Time Series Data</h3><p>Time series data consists of sequential observations recorded over time. Unlike regular datasets, time series data carries a temporal ordering which is crucial for analysis. The data often displays trends (long-term increase or decrease), seasonality (repeating patterns), and cycles (fluctuations at irregular intervals). Understanding these patterns is essential for accurate forecasting.</p><h3>3. Project Overview</h3><p>In this project, we aim to predict hourly energy consumption for a specific region using a historical dataset that spans over a decade. The project involves the following steps:</p><ul><li><strong>Data Preparation:</strong> Load and clean the data, ensuring it’s in a suitable format for analysis.</li><li><strong>Feature Engineering:</strong> Extract meaningful features from the data to help the model learn better.</li><li><strong>Model Training:</strong> Train an XGBoost model on the data to forecast future energy consumption.</li><li><strong>Model Evaluation:</strong> Assess the model’s performance using appropriate metrics and visualizations.</li><li><strong>Deployment:</strong> Use Streamlit to create a user-friendly web application for making predictions.</li></ul><h3>4. Data Preparation</h3><h4>Loading and Inspecting the Data</h4><p>We start by loading the dataset using Pandas. The dataset contains hourly energy consumption records with a Datetime column representing the timestamp.</p><pre>import pandas as pd<br># Load the dataset<br>df = pd.read_csv(&#39;energy_dataset/PJME_hourly.csv&#39;)<br>df.head()</pre><p>The first few rows of the dataset give us a glimpse into its structure:</p><pre>Datetime    PJME_MW<br>0   2002-01-01 01:00:00  5087.546<br>1   2002-01-01 02:00:00  5050.118<br>2   2002-01-01 03:00:00  4993.485<br>3   2002-01-01 04:00:00  4919.263<br>4   2002-01-01 05:00:00  4865.211</pre><p>Next, we set the Datetime column as the index and convert it to a datetime type for easier manipulation.</p><pre># Convert Datetime to datetime type and set as index<br>df[&#39;Datetime&#39;] = pd.to_datetime(df[&#39;Datetime&#39;])<br>df = df.set_index(&#39;Datetime&#39;)</pre><h4>Visualizing the Data</h4><p>Visualizing the data helps us understand its trends and seasonality. We can plot the entire time series to observe the patterns over time.</p><pre>import matplotlib.pyplot as plt<br># Plot the time series data<br>plt.figure(figsize=(10, 6))<br>df[&#39;PJME_MW&#39;].plot(title=&#39;Energy Consumption Over Time&#39;)<br>plt.show()</pre><p>The plot shows how energy consumption fluctuates over time, with noticeable seasonal patterns corresponding to different times of the year.</p><h3>5. Feature Engineering</h3><h4>Extracting Time-Based Features</h4><p>To improve our model’s performance, we create new features that capture the temporal aspects of the data. For example, the hour of the day, day of the week, and month can all provide valuable information.</p><pre>def create_features(df):<br>    &quot;&quot;&quot;<br>    Create time series features based on time series index.<br>    &quot;&quot;&quot;<br>    df = df.copy()<br>    df[&#39;hour&#39;] = df.index.hour<br>    df[&#39;dayofweek&#39;] = df.index.dayofweek<br>    df[&#39;quarter&#39;] = df.index.quarter<br>    df[&#39;month&#39;] = df.index.month<br>    df[&#39;year&#39;] = df.index.year<br>    df[&#39;dayofyear&#39;] = df.index.dayofyear<br>    df[&#39;dayofmonth&#39;] = df.index.day<br>    df[&#39;weekofyear&#39;] = df.index.isocalendar().week<br>    return df</pre><p>These features help the model recognize patterns, such as higher energy consumption during certain hours or seasons.</p><h4>Implementing Feature Engineering</h4><p>To streamline the feature engineering process, we encapsulate it in a function. This function adds all the relevant features to the DataFrame.</p><pre># Apply the feature creation function<br>df = create_features(df)</pre><p>Now our dataset includes the engineered features, which will be used in the model training process.</p><h3>6. Model Training with XGBoost</h3><h4>Splitting the Data</h4><p>Before training the model, we split the dataset into training and test sets. This allows us to train the model on historical data and evaluate its performance on unseen data.</p><pre># Split the data into training and test sets<br>train = df.loc[df.index &lt; &#39;2015-01-01&#39;]<br>test = df.loc[df.index &gt;= &#39;2015-01-01&#39;]</pre><h4>Training the XGBoost Model</h4><p>XGBoost is a powerful algorithm known for its speed and performance. We use it to train a regression model to predict energy consumption.</p><pre>import xgboost as xgb<br>from sklearn.metrics import mean_squared_error</pre><pre># Define features and target<br>features = [&#39;hour&#39;, &#39;day_of_week&#39;, &#39;month&#39;, &#39;year&#39;]<br>X_train = train[features]<br>y_train = train[&#39;PJME_MW&#39;]<br>X_test = test[features]<br>y_test = test[&#39;PJME_MW&#39;]<br># Initialize and train the model<br>model = xgb.XGBRegressor(n_estimators=1000, early_stopping_rounds=50, learning_rate=0.01)<br>model.fit(X_train, y_train, eval_set=[(X_test, y_test)], verbose=100)</pre><p>The model is trained using the features we created, with the number of estimators and learning rate adjusted to optimize performance.</p><h3>7. Model Evaluation and Forecasting</h3><h4>Evaluating the Model</h4><p>Once the model is trained, we evaluate its performance using the Root Mean Squared Error (RMSE), which penalizes large errors.</p><pre># Predict on the test set<br>y_pred = model.predict(X_test)<br># Calculate RMSE<br>rmse = mean_squared_error(y_test, y_pred, squared=False)<br>print(f&#39;RMSE: {rmse}&#39;)</pre><p>The RMSE provides a quantitative measure of how well the model is performing on the test set.</p><h4>Visualizing Predictions</h4><p>Visualizing the predictions alongside the actual data helps us see how closely the model’s predictions align with reality.</p><pre># Plot predictions vs actual<br>plt.figure(figsize=(10, 6))<br>plt.plot(test.index, y_test, label=&#39;Actual&#39;)<br>plt.plot(test.index, y_pred, label=&#39;Predicted&#39;, color=&#39;red&#39;)<br>plt.legend()<br>plt.title(&#39;Energy Consumption: Actual vs Predicted&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*36oAoOsskdjSPHcemb2UqA.png" /></figure><p>This plot allows us to visually inspect the model’s performance, highlighting areas where the predictions are accurate and where improvements might be needed.</p><h3>8. Deploying with Streamlit</h3><h4>Creating the Streamlit App</h4><p>To make our model accessible to users, we deploy it using Streamlit, which provides an easy-to-use interface for building web applications.</p><pre>import streamlit as st<br>import pandas as pd<br>import numpy as np<br>from datetime import datetime<br>import pickle<br>from functions import predict_energy</pre><pre># Load the pre-trained model<br>loaded_model = pickle.load(open(&#39;time_series_model.sav&#39;, &#39;rb&#39;))</pre><pre># Streamlit UI<br>st.title(&#39;Energy Consumption Prediction&#39;)</pre><pre># Input: Date and Time using Streamlit&#39;s date_input and time_input<br>selected_date = st.date_input(&#39;Select Date&#39;)<br>selected_time = st.time_input(&#39;Select Time&#39;)</pre><pre># Combine selected date and time into a datetime object<br>if selected_date and selected_time:<br>    date_time_obj = datetime.combine(selected_date, selected_time)</pre><pre>if st.button(&#39;Predict&#39;):<br>    try:<br>        prediction = predict_energy(date_time_obj,loaded_model)<br>        st.success(f&#39;Predicted Energy Consumption: {prediction:.2f} kWh&#39;)<br>    except Exception as e:<br>        st.error(f&quot;Error: {e}&quot;)</pre><p>This Streamlit app allows users to input a date and time, and get the predicted energy consumption for that specific moment.</p><h4>Running the Application</h4><p>To run the Streamlit app, simply execute the following command:</p><pre>streamlit run app.py</pre><p>This will start a local web server and open the app in your browser, providing a simple yet powerful interface for interacting with the model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UwyF_reNCFgad2xfh8GA1A.png" /></figure><h3>9. Conclusion and Next Steps</h3><p>In this project, we successfully predicted energy consumption using time series forecasting and XGBoost. We went through the entire process of data preparation, feature engineering, model training, and deployment. While the model performs well, there are always opportunities for improvement, such as adding more features or fine-tuning the model’s hyperparameters.</p><p>As a next step, consider exploring additional data sources like weather information or special events, which could further enhance the model’s accuracy.</p><h3>10.References</h3><ol><li><a href="https://github.com/Gayathri-Selvaganapathi/energy_consumption_prediction">My Git Repo</a></li><li><a href="https://www.kaggle.com/code/robikscube/time-series-forecasting-with-machine-learning-yt">The Kaggle Notebook</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da965751ab95" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Comprehensive Guide to Detecting and Identifying Defects in PCBs Using YOLOv5]]></title>
            <link>https://medium.com/@gayathri.s.de/comprehensive-guide-to-detecting-and-identifying-defects-in-pcbs-using-yolov5-5d3f44e06909?source=rss-c85e34fadf4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d3f44e06909</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[yolov5]]></category>
            <category><![CDATA[semiconductors]]></category>
            <category><![CDATA[computer-vision]]></category>
            <dc:creator><![CDATA[Gayathri Selvaganapathi]]></dc:creator>
            <pubDate>Thu, 29 Aug 2024 04:43:43 GMT</pubDate>
            <atom:updated>2024-08-29T04:47:09.987Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ANCrk8HyMNmsYcbVgaT97g.jpeg" /></figure><p>Printed Circuit Boards (PCBs) are essential components in modern electronics, providing the foundational structure for electronic circuits. With the increasing complexity of electronic devices, ensuring the quality and reliability of PCBs has become more critical than ever. Defects in PCBs can lead to failures in devices, resulting in costly repairs, product recalls, and damage to brand reputation. To mitigate these risks, automating the detection and identification of PCB defects using advanced machine learning techniques, such as YOLOv5, can be a game-changer.</p><p>In this comprehensive guide, we will walk through the entire process of detecting and identifying defects in PCBs using the YOLOv5 model. This process includes data collection, annotation, model training, and evaluation, all executed within a Python and Jupyter Notebook environment.</p><h4>Step 1: Data Collection and Preparation</h4><p>The foundation of any machine learning project is data. In our case, we need a dataset that includes images of PCBs with various defects. For this project, we sourced our data from <a href="https://www.kaggle.com/datasets/akhatova/pcb-defects">Kaggle</a>, a popular platform for datasets related to machine learning and data science.</p><p>The dataset we used includes images with six types of PCB defects: Missing Hole, Mouse Bite, Open Circuit, Short Circuit, Spur, and Spurious Copper. Once the dataset is downloaded, the first step is to extract it and organize the files for further processing.</p><pre>import zipfile<br>import os<br># Extract the dataset from the zip file<br>with zipfile.ZipFile(&#39;pcb_defects.zip&#39;, &#39;r&#39;) as zip_ref:<br>    zip_ref.extractall(&#39;pcb_defects&#39;)<br># Remove unnecessary files that are not needed for the training process<br>os.remove(&#39;pcb_defects/rotation&#39;)<br>os.remove(&#39;pcb_defects/python_file.py&#39;)</pre><p>After extraction, we inspect the contents of the dataset to ensure it contains the necessary images and annotations. The dataset is typically structured with images in one folder and corresponding annotation files in another. For YOLOv5, these annotations must be in a specific format, which leads us to our next step.</p><h4>Step 2: Data Annotation and Preprocessing</h4><p>YOLOv5, like other object detection models, requires annotations in a particular format. Each image in the dataset must have an associated text file that contains the bounding box coordinates and class labels for each defect present in the image. Unfortunately, the annotations provided with our dataset were in XML format, which is not directly compatible with YOLOv5. Therefore, we need to convert these XML files to the required text format.</p><p>To accomplish this, we use a Python package that automates the conversion of XML annotations to YOLO-compatible text files. This package can be found on GitHub, and the process involves cloning the repository and running the conversion script.</p><pre># Clone the repository for converting XML annotations to text format<br>!git clone https://github.com/user/xml_to_txt.git</pre><p>Copy all the folders in the Annotation folder to the xml folder in XmlToTxt repo,then run the below comments.</p><pre># Install the necessary dependencies from the requirements file<br>!pip install -r xml_to_txt/requirements.txt</pre><pre># Import the conversion module and run the conversion process<br>import os</pre><pre>os.chdir(&quot;XmlToTxt&quot;)</pre><pre>!python xmltotxt.py -c classes.txt -xml xml -out out</pre><p>Once the conversion is complete, we verify that each image now has a corresponding text file with annotations in the correct format and are precent in then ‘out’ folder.</p><p>Copy all these txt files to the image folder which have the images for training.</p><h4>Step 3: Split the data for training and testing</h4><p>For the yolo training the dataset folder has to be in the below particular format.</p><pre>Dataset/<br>│<br>├── images/<br>│   ├── train/<br>│   └── val/<br>│<br>└── labels/<br>    ├── train/<br>    └── val/</pre><p>For this purpose we have run the below code, which moves the images and labels to this particular yolo directory structure</p><pre>import os<br>from random import choice<br>import shutil<br>def to_v5_directories(images_train_path,images_val_path,labels_train_path,labels_val_path, dataset_source):<br>    imgs =[]<br>    xmls =[]<br>    trainPath = images_train_path<br>    valPath =  images_val_path<br>    crsPath = dataset_source<br>    train_ratio = 0.8<br>    val_ratio = 0.2<br>    totalImgCount = len(os.listdir(crsPath))/2<br>    for (dirname, dirs, files) in os.walk(crsPath):<br>        for filename in files:<br>            if filename.endswith(&#39;.txt&#39;):<br>                xmls.append(filename)<br>            else:<br>                imgs.append(filename)<br>    countForTrain = int(len(imgs)*train_ratio)<br>    countForVal = int(len(imgs)*val_ratio)<br>    trainimagePath = images_train_path<br>    trainlabelPath = labels_train_path<br>    valimagePath = images_val_path<br>    vallabelPath = labels_val_path<br>    for x in range(countForTrain):<br>        fileJpg = choice(imgs)<br>        fileXml = fileJpg[:-4] +&#39;.txt&#39;<br>        shutil.copy(os.path.join(crsPath, fileJpg), os.path.join(trainimagePath, fileJpg))<br>        shutil.copy(os.path.join(crsPath, fileXml), os.path.join(trainlabelPath, fileXml))<br>        imgs.remove(fileJpg)<br>        xmls.remove(fileXml)<br>    for x in range(countForVal):<br>        fileJpg = choice(imgs) <br>        fileXml = fileJpg[:-4] +&#39;.txt&#39; <br>        shutil.copy(os.path.join(crsPath, fileJpg), os.path.join(valimagePath, fileJpg))<br>        shutil.copy(os.path.join(crsPath, fileXml), os.path.join(vallabelPath, fileXml))<br>        imgs.remove(fileJpg)<br>        xmls.remove(fileXml)<br>    print(&quot;Training images are : &quot;,countForTrain)<br>    print(&quot;Validation images are : &quot;,countForVal)<br>#     shutil.move(crsPath, valPath)</pre><p>Then run this to split the images and labels for training and validation.</p><pre>to_v5_directories(&quot;PCB_DATASET/dataset/images/train&quot;,&quot;PCB_DATASET/dataset/images/val&quot;,&quot;PCB_DATASET/dataset/labels/train&quot;,&quot;PCB_DATASET/dataset/labels/val&quot;, &quot;PCB_DATASET/Annotations/{each_image_gropu}&quot;)</pre><h4>Step 4: Setting Up and Training the YOLOv5 Model</h4><p>With our dataset prepared and annotations in place, we move on to the training phase. YOLOv5 is a state-of-the-art object detection model known for its speed and accuracy. To train the model, we need to set up the environment, load the dataset, and configure the training parameters.</p><p>We opted to use Google Colab for training, leveraging its GPU support to accelerate the process. The first step is to upload the dataset to Google Drive and mount the drive in the Colab environment.</p><p>Then zip the dataset folder and upload to your gogle drive.p</p><pre>from google.colab import drive<br>drive.mount(&#39;/content/drive&#39;)</pre><pre># Unzip and prepare the dataset within the Google Colab environment<br>!unzip -q &quot;/content/drive/My Drive/PCB_DATASET.zip&quot; -d /content/</pre><p>Next, we clone the YOLOv5 repository from GitHub and install the necessary dependencies. This repository includes the pre-trained weights, configuration files, and training scripts needed to train the model on our PCB dataset.</p><pre>!git clone https://github.com/ultralytics/yolov5.git</pre><pre># Change the directory to the cloned YOLOv5 repository<br>%cd yolov5</pre><pre># Install the required dependencies for YOLOv5<br>!pip install -r requirements.txt</pre><h4>Configuring the Dataset</h4><p>Before training, we need to configure the dataset by creating a dataset.yaml file. This file defines the paths to the training and validation datasets, the number of classes, and their names. This configuration ensures that YOLOv5 understands the structure of our data.</p><pre># Content of dataset.yaml<br>train: /content/PCB_DATASET/dataset/images/train<br>val: /content/PCB_DATASET/dataset/images/val<br># Number of classes in the dataset<br>nc: 6<br># Class names<br>names: [&#39;Missing_Hole&#39;, &#39;Mouse_Bite&#39;, &#39;Open_Circuit&#39;, &#39;Short_Circuit&#39;, &#39;Spur&#39;, &#39;Spurious_Copper&#39;]</pre><p>This YAML file is then uploaded to the YOLOv5 directory in Colab, and we are ready to start training the model.</p><h4>Training the YOLOv5 Model</h4><p>Training the YOLOv5 model involves specifying several parameters, such as the image size, batch size, number of epochs, and the type of YOLOv5 model to use. YOLOv5 offers several model sizes, ranging from the small and fast YOLOv5n to the larger and more accurate YOLOv5x.</p><pre># Training the YOLOv5 model<br>!python train.py --img 640 --batch 16 --epochs 300 --data dataset.yaml --weights yolov5s.pt --project pcb_defects_run1</pre><p>In this command:</p><ul><li>--img 640 specifies the input image size.</li><li>--batch 16 sets the batch size for training.</li><li>--epochs 300 sets the number of training iterations. More epochs can lead to better accuracy but also require more time.</li><li>--data dataset.yaml points to our dataset configuration file.</li><li>--weights yolov5s.pt specifies the pre-trained YOLOv5 model weights to be used.</li><li>--pcb_defect_run1 names the output directory where the training results will be stored.</li></ul><p>Training begins, and the model iteratively improves as it learns to detect and classify PCB defects.</p><h4>Step 5: Evaluating the Model</h4><p>Once training is complete, evaluating the model’s performance is crucial. YOLOv5 provides several tools to assess the model, including precision-recall curves, confusion matrices, and other metrics. These evaluations help us understand how well the model is detecting and classifying defects.</p><p>With the number of epochs as 300, the model’s accuracy is shown as 93%.</p><pre>import matplotlib.pyplot as plt<br>from IPython.display import Image</pre><pre># Display the confusion matrix for the trained model<br>Image(&#39;runs/train/pcb_defects/confusion_matrix.png&#39;)</pre><pre># Display the precision-recall curve<br>Image(&#39;runs/train/pcb_defects/PR_curve.png&#39;)</pre><p>The precision-recall curve and confusion matrix are particularly useful for understanding how well the model differentiates between the various defect types. These tools allowed us to fine-tune the model for better performance.</p><h4>Step 6: Validating and Predicting</h4><p>With a well-trained model, the final step is to validate it using a separate validation dataset and make predictions on new images. This step ensures that the model generalizes well to unseen data and can accurately detect and classify PCB defects in real-world scenarios.</p><pre># Run the model on validation images and display the results<br>!python val.py --weights runs/train/pcb_defects/weights/best.pt --data dataset.yaml</pre><pre># Visualize the predicted results on a sample image<br>Image(&#39;runs/val/pcb_defects/predictions.jpg&#39;)</pre><p>The model’s predictions were significantly improved after increasing the number of training epochs. The model is now capable of accurately detecting defects in PCBs, making it a valuable tool for automating quality control in electronics manufacturing.</p><h4>Step 7: Analysing the Confusion Matrix and Precision-Recall Curve</h4><p>The confusion matrix provides a detailed breakdown of the model’s performance across different defect categories. Each row represents the predicted class, while each column represents the actual class. Here’s a breakdown of what the confusion matrix tells us about the model’s performance:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2uUJt5LlZZfzEzaDSRic6A.png" /></figure><ol><li><strong>Missing Hole</strong>: The model has a perfect prediction accuracy for the ‘Missing Hole’ class, as indicated by a value of 1.00 in the corresponding cell. This means every ‘Missing Hole’ defect was correctly identified.</li><li><strong>Mouse Bite</strong>: The model achieved an accuracy of 0.80 for the ‘Mouse Bite’ class, with a slight misclassification of 0.03 as ‘Spurious Copper’. This indicates that the model generally performs well on this class but has room for improvement in distinguishing it from similar defects.</li><li><strong>Open Circuit</strong>: The model correctly identified ‘Open Circuit’ defects with an accuracy of 0.83. However, 12% of these defects were misclassified as ‘Spurious Copper’, which suggests that these two classes might have overlapping features that confuse the model.</li><li><strong>Short Circuit</strong>: The model showed high accuracy (0.95) in detecting ‘Short Circuit’ defects, with minimal misclassification, indicating that this class is well-represented in the training data or that the features are distinct.</li><li><strong>Spur</strong>: The model struggled with the ‘Spur’ class, showing a lower accuracy of 0.81 and a significant confusion with ‘Spurious Copper’ (0.24). This suggests that the features of ‘Spur’ defects are often mistaken for those of ‘Spurious Copper’.</li><li><strong>Spurious Copper</strong>: The accuracy for ‘Spurious Copper’ is 0.87, but there is considerable misclassification with the background (0.36), indicating that the model sometimes confuses this defect with non-defect areas.</li></ol><h4>Precision-Recall Curve Analysis</h4><p>The Precision-Recall (PR) curve further provides insights into the model’s ability to handle the imbalance between the positive class (defects) and the negative class (background or no defect).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mGprhqvzb6E41XVdsngPvQ.png" /></figure><ul><li><strong>Overall Performance</strong>: The mean Average Precision (mAP@0.5) for all classes is 0.896, which is a strong indicator of the model’s overall performance.</li><li><strong>Class-Specific Performance</strong>:</li><li><strong>Missing Hole</strong>: Exhibits near-perfect precision and recall (0.995), affirming its ease of detection by the model.</li><li><strong>Mouse Bite</strong>: Has a lower precision (0.836), which suggests that while the model is generally accurate, there are a few instances where the model incorrectly predicts this class.</li><li><strong>Open Circuit</strong>: The precision is 0.886, showing that the model is fairly good at detecting this class but still misclassifies some defects.</li><li><strong>Short Circuit</strong>: With a precision of 0.934, this class is well-detected, aligning with the confusion matrix results.</li><li><strong>Spur</strong>: This class has the lowest precision at 0.819, reflecting the confusion noted in the confusion matrix. This might require further model refinement or more data.</li><li><strong>Spurious Copper</strong>: With a precision of 0.909, the model performs well on this class, but there is still some misclassification that lowers the score.</li></ul><h4>8. Conclusion</h4><p>The confusion matrix and precision-recall curve together paint a detailed picture of the model’s strengths and weaknesses in detecting PCB defects. The model excels in detecting ‘Missing Hole’ and ‘Short Circuit’ defects but shows some confusion between similar defect types like ‘Spur’ and ‘Spurious Copper’.</p><p>Improving the model might involve increasing the number of epochs, augmenting the dataset, or fine-tuning the model parameters to better distinguish between the more similar defect types. Despite some areas for improvement, the overall performance of the model is strong, making it a valuable tool for automated PCB defect detection.</p><h4>9. Reference</h4><ol><li><a href="https://github.com/Gayathri-Selvaganapathi/PCB_defect_detection_yolo">My GitHub page</a></li><li><a href="https://github.com/MBDNotes/YOLOv5_PCB_Defects_Detection">The model referred</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d3f44e06909" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Predicting Food Delivery Time with Machine Learning: A Technical Overview]]></title>
            <link>https://medium.com/@gayathri.s.de/predicting-food-delivery-times-with-machine-learning-a-technical-overview-a119c3f05049?source=rss-c85e34fadf4a------2</link>
            <guid isPermaLink="false">https://medium.com/p/a119c3f05049</guid>
            <category><![CDATA[predictive-analytics]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-analysis]]></category>
            <dc:creator><![CDATA[Gayathri Selvaganapathi]]></dc:creator>
            <pubDate>Tue, 27 Aug 2024 05:12:20 GMT</pubDate>
            <atom:updated>2024-08-27T08:22:52.538Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r8mEKNF0R24wAEICn4yqMQ.png" /></figure><p><strong>Table of Contents</strong></p><ol><li>Introduction</li><li>Project Overview</li><li>Project Structure</li><li>Step-by-Step Implementation</li></ol><ul><li>1. Data Exploration and Preprocessing</li><li>2. Feature Engineering</li><li>3. Model Training and Evaluation</li><li>4. Building the Streamlit Web Application</li><li>5. Running the Application</li><li>6. Deployment Considerations</li></ul><p>5. Conclusion</p><p>6. Future Enhancement</p><p>7. References</p><h3>1. Introduction</h3><p>The rise of online food delivery platforms has revolutionized the way we enjoy our meals, bringing convenience and a wide variety of choices to our fingertips. However, one challenge that persists is the accuracy of delivery time predictions. Accurate predictions are crucial for both customer satisfaction and operational efficiency. This blog post delves into a machine learning project designed to predict food delivery times, providing a detailed overview of the project’s structure, methodology, and implementation.</p><h3>2. Project Overview</h3><p>The core objective of this project is to develop a machine learning model that predicts the delivery time of food orders based on various features. These features might include the restaurant’s location, delivery distance, weather conditions, traffic data, and more. The model is trained on historical data and is deployed as a web application, where users can input relevant details and receive an estimated delivery time.</p><h3>3. Project Structure</h3><p>The project is organized into several key files, each serving a distinct purpose:</p><ul><li><strong>app.py</strong>: This is the main entry point of the project, hosting the Streamlit web application. The Streamlit app allows users to input delivery details and receive predictions. It handles HTTP requests and responses, integrating the trained machine learning model to provide real-time predictions.</li><li><strong>functions.py</strong>: This file contains a collection of utility functions used throughout the project. These functions are responsible for data preprocessing, feature engineering, and the prediction process. The modular approach in this file ensures that the code is reusable and maintainable.</li><li><strong>Food-Delivery-Predicting.ipynb</strong>: This Jupyter notebook is the heart of the data science process in the project. It contains the entire workflow of the project, from data exploration and cleaning to model training and evaluation. The notebook format allows for an interactive approach to model development, making it easier to visualize data and understand the model&#39;s performance.</li><li><strong>Dataset</strong> : The dataset used for training the machine learning model. It includes various features that potentially influence delivery time, such as the distance between the restaurant and the delivery address, weather conditions, order time, and more.</li></ul><h3>4. Step-by-Step Implementation</h3><p>Let’s walk through the key steps involved in developing the food delivery time prediction model.</p><h4>1. Data Exploration and Preprocessing</h4><p>Data exploration is the first step in any machine learning project. The dataset (train.csv) is loaded into a Pandas DataFrame, and the initial analysis is conducted to understand the data&#39;s structure. This step includes checking for missing values, identifying outliers, and understanding the distribution of various features.</p><p>Next, data preprocessing is performed. This involves cleaning the data, handling missing values, and transforming categorical variables into numerical ones through techniques like one-hot encoding. Feature scaling is also applied to ensure that all features contribute equally to the model’s predictions.</p><h4>2. Feature Engineering</h4><p>Feature engineering is the process of creating new features from existing ones to improve the model’s performance. In this project, several new features were engineered, such as:</p><ul><li><strong>Distance Categories</strong>: Categorizing delivery distances into bins to help the model better understand short vs. long deliveries.</li><li><strong>Time of Day</strong>: Creating features that capture the time of day, such as morning, afternoon, evening, and night, to account for variations in traffic and restaurant operation speeds.</li><li><strong>Weather Conditions</strong>: Incorporating weather data, which can significantly impact delivery times due to factors like rain or extreme temperatures.</li></ul><p>These features were carefully selected and transformed based on domain knowledge and exploratory data analysis (EDA) results.</p><h4>3. Model Training and Evaluation</h4><p>With the data prepared, the next step is model training. Several machine learning models were considered, including linear regression, decision trees, and gradient boosting algorithms. After comparing their performance using cross-validation and metrics like Mean Absolute Error (MAE) and Root Mean Squared Error (RMSE), the best-performing model was selected.</p><p>Hyperparameter tuning was conducted to optimize the model’s performance further. This process involves adjusting the model’s parameters to find the best combination that minimizes the error on unseen data.</p><h4>4. Building the Streamlit Web Application</h4><p>Once the model was trained and evaluated, it was integrated into a streamlit web application (app.py). Streamlit is a lightweight web framework in Python that allows for rapid development of web applications. In this project, Streamlit was used to create an interface where users can input delivery details and receive a predicted delivery time.</p><p>The application flow in app.py is straightforward:</p><ol><li><strong>User Input</strong>: The user provides input through a web form, including details like restaurant location, delivery distance, and weather conditions.</li><li><strong>Prediction</strong>: The input data is passed to the prediction function, which preprocesses the input and feeds it into the trained model.</li><li><strong>Output</strong>: The predicted delivery time is displayed to the user on the web page.</li></ol><p>The Streamlit application is designed to be user-friendly and responsive, providing real-time predictions to enhance the user experience.</p><h4>5. Running the Application</h4><p>To run the application locally, users need to set up their environment by installing the required dependencies. This can be done using pip and the requirements.txt file, which lists all necessary Python packages.</p><p>Once the environment is set up, the application can be started by running app.py. The Streamlit server will start, and users can access the application via their web browser at <a href="http://127.0.0.1:5000/.">http://127.0.0.1:5000/.</a></p><h4>6. Deployment Considerations</h4><p>While this project currently runs locally, the next logical step would be to deploy it to a cloud platform like AWS, Heroku, or Google Cloud. Deployment would make the application accessible to a broader audience, enabling real-time predictions for actual delivery operations.</p><h3>5. Conclusion</h3><p>Predicting food delivery times with machine learning is a practical application of data science that can have a significant impact on the food delivery industry. By accurately predicting delivery times, businesses can improve customer satisfaction, optimize delivery logistics, and reduce operational costs.</p><p>This project showcases the end-to-end process of developing a machine learning model, from data exploration and feature engineering to model training and deployment. By following the outlined steps, you can create a robust predictive model and integrate it into a web application, providing valuable insights and enhancing the user experience.</p><h3>6. Future Enhancements</h3><p>There are several areas where this project can be expanded or improved:</p><ul><li><strong>Incorporating Real-Time Data</strong>: Integrating real-time traffic and weather data can improve the accuracy of predictions.</li><li><strong>Model Improvement</strong>: Exploring more advanced models, such as deep learning, or using ensemble methods could enhance performance.</li><li><strong>Scalability</strong>: Deploying the model on a scalable cloud platform would allow for handling a large volume of requests, making the solution viable for commercial use.</li></ul><p>By continuing to iterate on this project, you can build a powerful tool that not only predicts delivery times but also drives business growth and customer satisfaction.</p><p>This technical blog provides an in-depth look at the process and considerations involved in predicting food delivery times using machine learning. It aims to guide developers, data scientists, and enthusiasts through the journey of creating a similar project, from data exploration to deployment.</p><h3>7. References</h3><ol><li><a href="https://github.com/Gayathri-Selvaganapathi/pred_food_delivery_time">My github repo</a></li><li><a href="https://www.youtube.com/watch?v=WWOs_k_-uLI">Model referred</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a119c3f05049" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>