Use Case: Detecting Defective Pills

To help automate the detection of defects in medical pills we set out to build an image recognition model in PerceptiLabs that could identify defective pills by analyzing images.

Use Case: Detecting Defective Pills

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.

Dataset

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:

Figure 1: Examples of images from the dataset showing a normal pill (left) and a defective pill (right).
Figure 1: Examples of images from the dataset showing a normal pill (left) and a defective pill (right).

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:

images

labels

defect/pic.6.1561.0.png

defect

defect/pic.6.1562.0.png

defect

normal/pic.6.443.0.png

normal

normal/pic.6.444.0.png

normal

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.

Model Summary

Our model was built with just three Components:

Component 1: Convolutional Neural Network (CNN)

Patch size = 3, Stride = 2, Feature Maps = 3

Component 2: Dense 

Activation=ReLU, Neurons=128

Component 3: Dense 

Activation=Softmax, Neurons=2


Figure 2 shows the model's topology in PerceptiLabs:

Figure 2: Topology of the model in PerceptiLabs.
Figure 2: Topology of the model 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.

Figure 3: PerceptiLabs' Statistics View during training. The training loss and accuracy are shown in the upper left and right respectively, and the gradients can be seen in the lower-right corner.
Figure 3: PerceptiLabs' Statistics View during training. The training loss and accuracy are shown in the upper left and right respectively, and the gradients can be seen in the lower-right corner.‌‌

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:

Figure 4: Confusion Matrix and Label Metrics for the model.
Figure 4: Confusion Matrix and Label Metrics 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).

Vertical Applications

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.

Summary

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.