Week 12 · Capstone · Build a visual inspection system
Compare a rule and a learned detector under lighting, view, and camera changes.
Compare a rule and a learned detector under lighting, view, and camera changes.
Work through the problem
Classify small panels as sound or defective. A defect interrupts a horizontal bright band. A simple rule uses the dimmest column inside the central band. Fit a separate model to small images, then evaluate both on untouched synthetic batches representing familiar, dim, shifted, and noisy-camera conditions.
Report defect recall and false-positive rate. A missed defect and a rejected good unit have different costs. The lab uses five units for a miss and one for a false alert: cost per panel = (5 FN+FP)/N. Use a separate calibration set before changing thresholds to meet a real requirement.
Run the Python lab
import numpy as np
from sklearn.linear_model import LogisticRegression
rng = np.random.default_rng(44)
def batch(n,lighting=1,shift=0,noise=.07):
x = rng.normal(.12,noise,(n,12,12))
y = rng.integers(0,2,n)
x[:,5+shift:7+shift,1:11]+=.64*lighting
for i in np.flatnonzero(y):
j = rng.integers(3,8)
x[i,5+shift:7+shift,j:j+2]-=.61*lighting
return x,y
def flat(x):
return x.reshape(len(x),-1)
x,y = batch(400)
fit = LogisticRegression(solver="liblinear").fit(flat(x),y)
for name, options in [("Familiar",{}),("Dim",{"lighting":.48}),("Shifted view",{"shift":2}),("Noisy camera",{"noise":.21})]:
x,y = batch(200,**options)
rule = x[:,5:7,2:10].mean(1).min(1)<.43
learned = fit.predict(flat(x)).astype(bool)
for key, pred in [("Rule",rule),("Learned",learned)]:
fn,fp = ((y==1)&~pred).sum(),((y==0)&pred).sum()
print(name,key,f"recall {1-fn/y.sum():.2f}, FPR {fp/(len(y)-y.sum()):.2f}, cost {(5*fn+fp)/len(y):.2f}")
Output from the lab
Familiar Rule recall 1.00, FPR 0.00, cost 0.00
Familiar Learned recall 1.00, FPR 0.00, cost 0.00
Dim Rule recall 1.00, FPR 0.99, cost 0.48
Dim Learned recall 1.00, FPR 0.91, cost 0.45
Shifted view Rule recall 1.00, FPR 1.00, cost 0.58
Shifted view Learned recall 1.00, FPR 1.00, cost 0.58
Noisy camera Rule recall 0.99, FPR 0.07, cost 0.06
Noisy camera Learned recall 0.83, FPR 0.09, cost 0.47
Practice
- What should block deployment on a new camera?
Check your reasoning
- Set a minimum recall and maximum false-positive rate before measuring them on held-out examples from that camera. If it fails, route those images to manual inspection.
Further reading · Szeliski · Computer Vision: Algorithms and Applications