Skip to content
PetalEdgeDocs

End-to-end projects

Find defects with visual anomaly detection

Photograph good parts, train a Visual Anomaly Detection model, and watch it flag a defective part live with your webcam.

Try it in Studio
On this page

What you will build

A model that knows what a good part looks like and flags one that does not: a scratched lid, a missing screw, a stained label. You will use Visual Anomaly Detection with the GMM scoring function and Studio's default settings.

Time: about 1 hour, most of it for taking photos.

What you need

  • A PetalEdge account.
  • A phone or camera, and a computer with a webcam for the live test.
  • About 40 identical items in good condition, or one item photographed 40 times, for example a bottle cap, a coin or a printed card.
  • A few damaged items, or a way to make one look damaged (a pen mark, a sticker, a missing piece).
  • A fixed spot to photograph them: the same background, distance and lighting every time.

1. Create the project

  1. In Studio, open the menu under your profile picture and choose Create new project.
  2. Name it, for example Cap inspection.
  3. Choose Image and select Create Project.

More: Create a project.

2. Take and upload photos

  1. Take at least 40 photos of good items from the same position. Small changes are fine (turn the item, move it a little) but keep the camera, background and light the same.
  2. Take 5 to 10 photos of defective items in the same position.
  3. Put the good photos in a folder called good and the defect photos in a folder called defect. With Upload folder, the folder name becomes the label.
  4. Open Data acquisition. Upload the good folder with Upload category on Automatically split between training and testing.
  5. Upload the defect folder with Upload category set to Testing data.

More: Collecting image data.

3. Design the impulse

  1. Open Create impulse. Set Width and Height to 96 and keep Resize mode.
  2. Add a processing block: choose Image.
  3. Add a learning block: choose Visual Anomaly Detection.
  4. Select Save Impulse.
  5. Open Image, select Save parameters, then Generate Features.

4. Train

  1. Open Visual Anomaly Detection in the left menu. The first time, Studio asks you to choose a scoring function: keep GMM.
  2. Under Normal classes, make sure good is ticked and defect is not.
  3. Select Start Training. Training usually takes under a minute.

More: Training a visual anomaly model.

5. Read the results

  1. Look at AUROC in the Anomaly explorer. Above 0.9 means the model separates your good and defective items well.
  2. In the Score distribution, the blue good images should sit left of the dashed threshold line and the striped defect images right of it.
  3. Under Test images, select Wrong. Select a missed defect to see its heatmap: does the model look at the right spot?
  4. If a few good images are flagged, try Best F1, or drag the Threshold slider until Normal flagged and Anomalies caught look right for you.

6. Try it live

  1. Open Live classification and choose Webcam as the Image source.
  2. Show the camera a good item in the same spot you photographed: it should read Normal.
  3. Show it a defective item: it should read Anomaly, and the Overlay view lights up the defect.

More: Evaluating a visual anomaly model.

7. Deploy

Open Deployment, choose your device (for example Raspberry Pi or the generic TFLite package), keep INT8 if it is offered, and select Build Deployment Package.

More: Deploying a visual anomaly model.

What you learned

  • An anomaly model trains on good parts only; defect photos are just for testing.
  • AUROC tells you how well the model separates good from bad, before you pick a threshold.
  • The threshold trades false alarms against missed defects, and you can move it without retraining.