# Understanding and Implementing Viola-Jones (Part Two)

Jan 27, 2019 · 4 min read

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.

# 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.

`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 pickledef save(self, filename):    with open(filename+".pkl", 'wb') as f:        pickle.dump(self, f)@staticmethodwith 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

### By Data Driven Investor

Our Editor's Selection  Take a look

Written by

Written by

## Self-Driving Car: Finding Lane Lines

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