Visual Anomaly Detection in Fabric using FOMO-AD - Raspberry Pi 5
Use a Raspberry Pi 5 to spot anomolies in fabric or other textiles with Edge Impulse FOMO-AD.
Last updated
Use a Raspberry Pi 5 to spot anomolies in fabric or other textiles with Edge Impulse FOMO-AD.
Last updated
Created By: Naveen Kumar
Public Project Link: https://studio.edgeimpulse.com/studio/384963
The current practice of anomaly detection in the textile industry predominantly relies on visual inspection by skilled workers, which, while effective, is subject to human error due to vision fatigue and inattention. The industry has been exploring alternative methods such as spectrum-based, statistics-based, and combined approaches to enhance efficiency and accuracy. However, these methods often come with stringent sample requirements and may not be suitable for all types of textiles. Despite the progress, the industry continues to face challenges in generalizing these systems across the vast range of fabric types and colors, and in integrating them seamlessly into the existing production lines without disrupting the workflow.
In the pursuit of excellence in textile manufacturing, the detection of visual anomalies is crucial. This project is dedicated to developing a machine learning-based visual anomaly detection system to identify defects in fabrics, which can range from subtle pattern inconsistencies to noticeable flaws. The system is trained to learn from good samples to detect anomalies, offering a promising solution to the challenges of manual inspection and the limitations of other automated methods.
For this project, we will use the latest Raspberry Pi 5 and the Raspberry Pi High-Quality Camera with a 6mm 3MP Wide Angle Lens. Fabrics can have a wide range of anomalies, from tiny pinholes to subtle variations in texture or color. High-resolution cameras provide detailed images that can improve the accuracy of defect detection algorithms, reducing false positives and negatives.
Although this system will be a proof of concept, we will use an M5Stack 6060-PUSH Linear Motion Control to keep it close to an industrial setup. Linear motion platforms provide precise control over the movement of the inspection system, allowing for accurate positioning. The stable movement of a linear motion platform is ideal for use with high-resolution cameras, ensuring that the images captured are clear and free of motion blur, which is crucial for detecting anomalies.
The 6060-PUSH Linear Motion Control is based on a stepper motion controlled over the RS485 communication protocol. We will use an M5Stack Atom Lite (ESP32) with an ATOMIC RS485 Base to control its movement. Also, we will use the M5Stack Flashlight Unit as an overhead lighting system to ensure the fabric is evenly lit, providing consistent lighting conditions. It prevents shadows or highlights that could be mistaken for defects. The flashlight is connected to the Atom Lite using a Grove connector.
We have designed and 3D-printed a base plate with a Lego connector for the Flashlight Unit to mount on the top of the Raspberry PI High-Quality Camera. The overhead lighting positioned correctly can reduce glare, which might otherwise interfere with the camera's ability to detect anomalies.
The flashlight unit with the camera looks as is shown below.
We have used a metal prototyping plate as a platform for the fabrics.
We have used a desk camera stand to mount the overhead camera and lighting assembly. The M5 Atom Lite is connected to the Raspberry Pi 5 over the USB connection. The final hardware setup looks as follows.
We are using a set of dust cloths as the fabric.
We will be using a Python script for the data collection phase. The code below is used for streaming the images over the network and can be displayed on a web page. It will be imported by the data collection and inferencing scripts.
File: stream.py
The script below is used to capture the image from the camera and save it to the Raspberry Pi 5 storage.
File: capture_image.py
The Arduino sketch below should be uploaded to the M5 Atom Lite (ESP32) to control the flashlight and send a message to the Raspberry Pi 5 to trigger the camera.
File: data_collection.ino
Now execute the command below to start the data collection process.
We have collected a total of 93 images of the fabric mostly with the label No Anomaly. The images without any anomalies are required by the FOMO-AD learning block that we will be using for the training. A few images with fabricated anomalies are taken for testing the model, later. A few example images are shown below.
We need to create a new project to upload data to Edge Impulse Studio.
The data is uploaded using the Edge Impulse CLI. You can install the CLI by following the instuctions here: https://docs.edgeimpulse.com/docs/cli-installation. Please remember to execute the following command to upload the No Anomaly images only for the Training.
We can add a few Anomaly/No Anomaly images to the Testing dataset using the following commands.
We can see the uploaded datasets in the Edge Impulse Studio Data Acquisition page.
Go to the Impulse Design > Create Impulse page, click Add a processing block, and then choose Image, which preprocesses and normalizes image data, and optionally reduces the color depth. Also, on the same page, click Add a learning block, and choose FOMO-AD (Images), which finds outliers in new data, extracts visual features using a pre-trained model on the data, and a Gaussian mixture model (GMM).
A Gaussian Mixture Model represents a probability distribution as a mixture of multiple Gaussian (normal) distributions. Each Gaussian component in the mixture represents a cluster of data points with similar characteristics. Thus, GMMs work using the assumption that the samples within a dataset can be modeled using different Gaussian distributions. Anomaly detection using GMM involves identifying data points with low probabilities. If a data point has a significantly lower probability of being generated by the mixture model compared to most other data points, it is considered an anomaly; this will output a high anomaly score.
We are using an image size of 640x640, which is required for better model accuracy. Now click on the Save Impulse button.
Next, go to the Impulse Design > Image page set the Color depth parameter as RGB, and click the Save parameters button which redirects to another page where we should click on the Generate Feature button. It usually takes a couple of minutes to complete feature generation.
We can see the 2D visualization of the generated features in the Feature Explorer.
Now go to the Impulse Design > FOMO-AD page and choose the Neural Network architecture. We are using the MobileNetV2 0.35 transfer learning model with the pre-trained weight provided by the Edge Impulse Studio. Also, we have chosen the Capacity parameter as medium. The higher the capacity, the higher the number of (Gaussian) components, and the more adapted the model becomes to the original distribution.
Now click the Start Training button and wait until the training is completed. By definition, there should be as few as possible anomalies in the training dataset, and thus accuracy is not calculated during training. Later we will run the Model testing to learn more about the model performance. On completion, it displays the estimated On-device performance of the Raspberry Pi for the EON Compiler engine.
Next, navigate to the Deployment page, select Linux (AARCH64) as the deployment target, and click on Build at the bottom of the page. An eim model (an Edge Impulse packaged model) will be downloaded to the computer.
We should copy the model file to the Raspberry Pi 5 and make it an executable.
The Python script (below) is responsible for loading the model, capturing the image, performing inferencing, and displaying the results on the web page.
File: classify-camera.py
Execute the script as follows:
You should see the following on the console:
Also, we need to upload the following Arduino Sketch to the M5 Atom, which controls the Linear Motion Control and the flashlight automatically on button press.
The following video demonstrates the operation of the systems. Any anomalies found are displayed using square bounding boxes. The inference bounding boxes are shown only when the Linear Motion Control stops moving, to ensure that the entire fabric is within the camera's region of interest (ROI).
This project aims to revolutionize the field of textile quality control through the application of machine learning for visual anomaly detection. By utilizing cutting-edge technology, such as Edge Impulse FOMO-AD, we can significantly elevate the standards of fabric inspection and quality control in the textile industry.