This project participates in the category 1 — Adapt & Detect: Robust Anomaly Detection in Real-World Applications, and my idea is to apply the Patchcore model to detect damaged bottles with good precision.
IntroductionI want to be original and creative, so I will use the tools of the Kaggle platform, which is a data science competition platform and online community of data scientists and machine learning professionals from Google LLC.
First open an account on the following website: https://www.kaggle.com/
Below I describe the notebook I made in Kaggle and you can consult in this Kaggle´s notebook: https://www.kaggle.com/code/guillermoperezg/mvtec-ad-patchcore-train-and-infer#Inferencing-blurred-image
This notebook contains the training, validation and inference of my model.
MethodologyLibraries
Anomalib is a deep learning library that aims to collect state-of-the-art anomaly detection algorithms for benchmarking on both public and private datasets. Below I show you the code cell where I installed the anomalib library and dependencies.
The libraries used are shown below:
Dataset MVTec-AD
MVTec AD is a dataset for benchmarking anomaly detection methods with a focus on industrial inspection. It contains over 5000 high-resolution images divided into 15 different object and texture categories. Each category comprises a set of defect-free training images and a test set of images with various kinds of defects as well as images without defects. We could now import the MVtec AD dataset using its specific datamodule implemented in anomalib. Here I configured image size in 256x256.
Model - Patchcore
PatchCore offers competitive inference times while achieving state-of-the-art performance for both detection and localization. To train the model, below I show you the configuration, where I selected the bottle category, image size of 256 x 256 pixels, training batch size of 32, and validation 16.
Validation
After training the Patchcore model, I obtained the following validation scores.
OpenVINO Inference
Before we can use OpenVINO inference, let's export the trained model to OpenVINO format first.
Load a test image
Let's read an image from the test set and perform inference using OpenVINO inferencer. But here we can also import any test image from our catalog.
Predictions
After loading the OpenVINO model we perform the inference. Predicting an image using OpenVINO inference is as calling predict method, where predictions contain any relevant information regarding the task type. Below I show you the prediction score of 0.8309104190970094
Visualizing inference results
Inferencing Blurred Image
The final step is to use OpenVINO inference with the same bottle image but applying blurred techniques with OpenCV such in the following notebook: Opencv - Bottle Image Processing
In the image below I show you the image after applying average blur with kernel size of (15, 15).
After applying OpenVINO inference, the predictions were 0.8181925430122461
The image display was as follows so that we can compare them with those prior to this experiment:
- From the results obtained, I can confirm that the Patchcore pre-training model is good, since I obtained the following metric results after fine tuning: 'pixel_AUROC': 0.9813711643218994, 'pixel_F1Score': 0.7295383810997009, 'image_AUROC': 1.0, and 'image_F1Score': 0.9919999837875366
- Then, I got a prediction score of 0.8309104190970094 from the image /MVTec/bottle/test/broken_large/001.png
- Finally I applied image blurred techniques with OpenCV to the same image, and obtained a prediction score of 0.8181925430122461.
- In this process, I faced the problem that the evaluation.py file did not work on the Kaggle platform, so in order not to stop, I decided to do the training and inference in the same notebook, and using the notebook "getting started " from the anomalib repository for reference.
- Another minor challenge was applying blurring with OpenCV to a test image, so I tried three techniques and chose one: average blur, gaussian blur and median blur.
- I think this model can be improved, so in the future I would like to apply data augmentation with Albumentations. I would also like to try training with NNFC compression, but currently it has only been tested with the Padim and STFPM models.
- This is the first time that I have contact with Anomalib and MVTec AD, but after this experience I believe that it has many powerful applications in critical problems of industrial automation or object recognition and failures that other models fail or that cannot be implemented on common boards. like the Raspberry Pi or Jetson Nano or Xilinx.
Comments