The manufacturing of products often requires careful quality control, especially when health and safety depend on the quality of the product. For example, in the case of medical pills, small physical defects may not only impact the appearance of the product, but can result in incorrect dosages.
To help automate the detection of such defects, we set out to build an image recognition model in PerceptiLabs that could identify defective pills by analyzing images. A model like this could potentially help pharmaceutical companies, pharmacists, or medical practitioners identify physically defective pill products.
To train our model, we used the images from the Pill defect dataset. The dataset comprises 512x512 grayscale .png images divided into two classes representing normal versus defective pills. Figure 1 shows a couple of example images from this dataset:
To map the classifications to the images, we created a .csv file that associates each image file with the appropriate classification label (normal or defect) for use in loading the data using PerceptiLabs' Data Wizard. Below is a partial example of how the .csv file looks:
Example of the .csv file to load data into PerceptiLabs that maps the image files to their associated labels.
We've made the image data and this CSV file available for experimentation on GitHub.
Our model was built with just three Components:
Figure 2 shows the model's topology in PerceptiLabs:
Training and Results
We trained the model with 20 epochs in batches of 32, using the ADAM optimizer, a learning rate of 0.001, and a Cross Entropy loss function. With a training time of just under two minutes and 5 seconds, we were able to achieve a training and validation accuracy of 100%. Figure 3 shows PerceptiLabs' Statistics view during training.
Here we can see that the validation and training losses quickly dropped over the first and second epochs respectively, before stabilizing at 0. Similarly, accuracy ramped up quickly for validation and training over the first and second epoch before stabilizing at 1. The gradient graph shows that after about 30 iterations, the model doesn't improve any further.
Figure 4 shows the Confusion Matrix and Label Metrics Table test results for the model:
The Confusion Matrix shows that the model tests all samples correctly (i.e., there are no false positives or negatives). The Labels Metrics Table further corroborates this by showing normalized values of 1.0 (i.e., 100%) for the following: Categorical accuracy (accuracy for each category averaged over all of them), Top K Categorical Accuracy (frequency of the correct category among the top K predicted categories), Precision (accuracy of positive predictions), and Recall (percentage of positives found (i.e., not misclassified as negatives instead of positives).
A model like this could be used during the manufacturing, distribution, or dispensing of pills to help ensure quality and safety. For example, the model could be used to analyze photos or video frames acquired through cameras analyzing pills on an assembly line. The model itself could also be used as the basis for transfer learning to create additional models for detecting defects in other types of pills, materials, or products.
This use case is an example of how image recognition can be used to help ensure health and safety. If you want to build a deep learning model similar to this, run PerceptiLabs and grab a copy of our pre-processed dataset from GitHub.