Rule-based vision or deep learning? Start with rules. Here is when to switch
Start with rules. Switch to deep learning when a rule cannot separate your classes even on the pictures you tuned it on, because it will only do worse on the line. On coffee roast, a colour rule got 91.4% of its own tuning pictures right, so we trained a network, and it named 24 of 24 new beans correctly. Whichever you choose, fix the lighting first: that network was trained with brightness varied by 15% and still got 4 of 24 wrong with the lamp 10% brighter.
The decision
- Can you write the pass criterion as a measurement with a tolerance? A length, a diameter, a count, a position, a colour that is clearly different. Use rules, and stop here.
- If not, try a rule anyway and score it on the pictures you tuned it with. If it cannot separate them there, no amount of tuning will fix it on the line. Train a network.
- Are the defects rare and different every time? Then there are too few of each to teach a network. Fit an anomaly model on good parts only.
- Did a network find something you need to size? Let it find, and let rules measure.
In every case: hold the lighting steady, and keep a plain rule that checks the part is there before the network's answer is believed.
Why rules come first
A rule gives a number you can hold against a drawing, and says why a part failed. It needs no training pictures, and when someone asks why a part was scrapped, you can answer. Our screw-kit example sizes eight M3 screws by length, each to within 0.29 mm, the ISO 4759-1 grade A tolerance for the shortest of them. It gave the right verdict on 12 of 12 rendered test pictures, at about 185 ms a frame. A network would only say which size each screw resembles, and nobody could check that against a drawing.
When to switch: the coffee roast test
A roaster wants to confirm that the batch coming out is the roast that was ordered. Roast has a colour, so we started with a colour rule: find the bean, take its average colour, and name the roast whose average is nearest. Each roast's average came from all 1,194 training pictures, the most help a colour rule can be given.
Then we applied step 2 and scored the rule on those same 1,194 pictures. It got 91.4% right. Green and light it almost never confused, but it called 52 of the 300 medium beans dark. Medium and dark differ in the sheen of the oils and the texture of the surface more than in average colour, and the average is all a colour rule sees. A rule that cannot sort the pictures it was built from will not sort new ones.
So we trained a small network (MobileNetV3-Small) on the same pictures in Portia. It took about ten minutes on a laptop's graphics card. On 24 beans neither had seen, six of each roast:
| Pictures | Colour rule | Network |
|---|---|---|
| Its own 1,194 tuning pictures | 91.4% right | (learned from them) |
| 24 new beans | 21 right | 24 right |
| Same 24, lamp 10% brighter | 21 right | 20 right |
| Empty plate | no answer | "green", score 0.76 |
For roast, the network is the right tool. Its weak spots are in the last two rows: a brighter lamp, and a plate with no bean on it.
Fix the lamp before you train
With the pictures made 10% brighter, the network named four beans wrongly and scored each wrong answer between 0.97 and 1.00, so a confidence threshold would not have caught them. It had been trained with brightness varied by 15% either way, so that a drifting lamp would not matter, and it mattered anyway. On copies made 10% dimmer, or with a warmer lamp, it got every bean right. There is no telling in advance which change a network will tolerate.
Do not count on training to cover a drifting lamp. Enclose the station, use a lamp built for machine vision, and before going live, run the inspection on pictures taken with the lamp at the ends of its range.
Keep a rule beside every network
Shown a bare plate, the network said "green" with a score of 0.76. A classifier always names one of the classes it knows. The bean finder, a plain threshold, is what noticed there was nothing to judge, so the inspection marks that frame as not believable.
When the defects are unknown
On a softgel filling line, leaks, dents, stains and bubbles are rare and each one is different, so nobody has a hundred of each to train on. Our capsule example fits a model on 482 pictures of good trays and fails any tray where something looks unlike them. On trays it had never seen, it caught two defect trays in three and rejected one good tray in 120, at about 1.9 s a tray. Expect to tune that balance on your own good parts.
When a network finds something you must measure
On real photographs of concrete walls, a crack is hard to separate from stains and texture with a threshold, so a network paints every crack pixel. Then ordinary blob tools trace a line along each crack and measure its length, because the line needs a length, and a network gives no dimension you can check. It was right on all 26 frames. The network took 0.63 s a patch and the measuring about 6 ms.
Questions
Does deep learning need a GPU on the line?
Not for a small classifier. The roast network ran in 115 ms a bean on a laptop's processor, against 81 ms for the colour rule. Training it used the laptop's graphics card.
How many pictures does it need?
The roast classifier was trained on 1,194 labelled pictures, about 300 of each roast. The capsule model was fitted on 482 good trays and no defects. Take them on your own line, under your own lamp.
Can one inspection use both?
In Portia, yes. A network is one node in the flowchart and the rules are others, so the crack inspection is a network followed by a blob tool, in one file. You train the network in Portia too.
What does it cost?
Portia's engineering environment is free, every tool and every seat, training included. You pay only when you deploy: $495, once per vision controller, with every tool, AI included.
What are the limits of the roast test?
In this dataset each roast is a different coffee, photographed in its own sessions, which makes roast easier to tell apart than it is on a real roaster. The lamp changes were made by scaling the pictures' brightness. On your line, train on your own beans, one coffee at several roasts.
Run the test on your own parts
The roast inspection ships with Portia as an example, with its pictures, its trained network and the right answers. Swap the network for a colour match, score it on your pictures, and you have step 2 of the decision.
Download the beta