Image for post
Image for post

Understanding and Implementing Viola-Jones (Part Two)

Anmol Parande
Jan 27, 2019 · 4 min read
Image for post
Image for post
Image for post
Image for post
Source: White House Archives

The “Attentional Cascade”

The attentional cascade uses a series of Viola-Jones classifiers, each progressively more complex, to classify an image. An image is only put through by the nth classifier if the n-1th classifier classifies it as a positive example. If at any point a classifier does not think the image is a positive example, the cascade stops.

Image for post
Image for post
A diagram of the attentional cascade found in the original paper

Building the Algorithm

In their paper, Viola and Jones introduce an algorithm which gives fine control over the exact false positive rate of the resulting cascade.

Image for post
Image for post
class CascadeClassifier():
def __init__(self, layers):
self.layers = layers
self.clfs = []
def train(self, training):
pos, neg = [], []
for ex in training:
if ex[1] == 1:
pos.append(ex)
else:
neg.append(ex)
for feature_num in self.layers:
if len(neg) == 0:
print("Stopping early. FPR = 0")
break
clf = ViolaJones(T=feature_num)
clf.train(pos+neg, len(pos), len(neg))
self.clfs.append(clf)
false_positives = []
for ex in neg:
if self.classify(ex[0]) == 1:
false_positives.append(ex)
neg = false_positives
def classify(self, image):
for clf in self.clfs:
if clf.classify(image) == 0:
return 0
return 1
import pickle
def save(self, filename):
with open(filename+".pkl", 'wb') as f:
pickle.dump(self, f)
@staticmethod
with open(filename+".pkl", 'rb') as f:
return pickle.load(f)

Conclusion

The attentional cascade is a simple idea that focuses heavily on reducing the false positive rate. It speeds up classification time but may increase training time depending on how many features each strong classifier takes into account. Regardless, it is a useful structure that is not specific to Viola-Jones.

Data Driven Investor

from confusion to clarity not insanity

Sign up for DDI Highlights

By Data Driven Investor

Our Editor's Selection  Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Anmol Parande

Written by

Student of Electrical Engineering and Computer Science at UC Berkeley

Data Driven Investor

from confusion to clarity not insanity

Anmol Parande

Written by

Student of Electrical Engineering and Computer Science at UC Berkeley

Data Driven Investor

from confusion to clarity not insanity

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store