From Lab to Live: Implementing Open-Source AI Models for Real-Time Unsupervised Anomaly Detection in Images
Photo by Randy Fath on Unsplash
The journey of taking an open-source artificial intelligence (AI) model from a laboratory setting to real-world implementation can seem daunting. However, with the right understanding and approach, this transition becomes a manageable task.
This blog post aims to serve as a compass on this technical adventure. We'll demystify key concepts, and delve into practical steps for implementing anomaly detection models effectively in real-time scenarios.
Let's dive in and see how open-source models can be implemented in production, bridging the gap between research and practical applications.
Understanding Unsupervised Anomaly Detection
Unsupervised anomaly detection is a machine learning technique that uncovers unusual patterns or outliers in data, without any prior training on what these anomalies might look like. In the context of images, this means identifying areas within the image that deviate significantly from what's considered 'normal'.
Implementing that in real-time involves using deep-learning models which can rapidly process incoming visual data, detect irregularities in a matter of milliseconds, and respond accordingly. It's like having a vigilant digital watchdog capable of recognizing anything out-of-the-ordinary at high speed.
With open-source models, you have access to this technology as well as the collective wisdom of researchers and developers worldwide who continually refine these tools for better performance.
Role and Importance of Anomaly Detection in Images
Anomaly detection in images plays a crucial role in numerous fields, ranging from healthcare to security. In healthcare, it can aid in identifying abnormal structures or changes in medical imagery like X-Rays or MRI scans, potentially flagging early signs of diseases. In security applications such as surveillance systems, it can help detect unusual activities or objects within the monitored area.
The importance of anomaly detection also extends to quality control in manufacturing, where it can spot defects on assembly lines avoiding costly recalls and, hopefully, ensuring customer satisfaction.
Real-time implementation of open-source models for this purpose allows these sectors to react quickly to anomalies and make informed decisions instantly.
Anomalib: A Deep Learning Library for Anomaly Detection in Images
Anomalib is an open-source library for unsupervised anomaly detection in images. It offers a collection of state-of-the-art models that can be trained on your specific images.
Example of an anomaly detected with Anomalib
For achieving the best training results, it is advised to obtain a suitable quantity of images that are free from any abnormalities. It is preferable to have a few hundred images for this purpose. Furthermore, in order to perform testing and validation, it is recommended to acquire a few images that do include anomalies as well.
If your images have anomalies, you'll need to make a mask highlighting these areas. You don't need a sophisticated tool for that. For example with GIMP, it's as simple as:
- Drag and drop your original image (e.g., "001.png”) into GIMP.
- Go to the Layer menu and select New Layer.
- With the new layer selected, use the Pencil tool set to white to mark the anomalies.
- Use the Bucket Fill tool to color the unmarked areas in black.
- Save the mask using File -> Export As, naming it "001_mask.png".
Keep in mind that masks are for testing, not training. Thus, only a few anomaly images are required for this.
Afterward, you can train one of the Anomalib models and test its performance under simulated conditions. I would recommend EfficientAd or FastFlow for real-time applications, as they are significantly faster than other models as of October 2023. Additionally, you can explore Anomaly Detection on Paper With Code to find the ideal model for your specific use case.
The easiest way to get started with Anomalib, is to clone their repository from Github and use the train script as follows:
python tools/train.py --config <path/to/model/config.yaml> --model <model name>
Sample config files are available in the repo, and it lets you set the paths of the folders containing your pictures for training and testing. Then, once your model is trained and validated, you can use the inference script to test it under simulated conditions on a single image or a folder of images. For example, with PyTorch, you can run the inference script as follows:
python tools/inference/torch_inference.py \
--weights results/your/model.pt \
--input your/image.png \
--output results
This will generate a visualization that highlights the areas of the image that the model has identified as anomalies. You can set the flag --visualization_mode
between full
and simple
to change the visualization mode. Select full
to view the original image with its mask, heatmap, and segmentation.