#143Which filter fires? (template detection)Easy

Which filter fires? (template detection)

Background

A filter is a template detector: applied to a patch, it gives a large response when the patch matches its pattern and a small one otherwise. Given a bank of filters, the one with the largest response is the pattern most present in the patch — exactly how a CNN decides "which feature is here".

The response of filter FF on a patch PP is the convolution scalar ijPijFij\sum_{ij} P_{ij}F_{ij}.

Problem statement

Implement strongest_filter(patch, filters) returning the index of the filter with the largest response on the patch.

Input

  • patch — array-like of shape (k, k).
  • filters — array-like of shape (M, k, k): M filters.

Output

Returns an int: the index of the filter whose sum(patch * filter) is largest.

Examples

Example 1

Input:  patch = [[1, 0], [0, 1]],
        filters = [[[1, 0], [0, 1]], [[0, 1], [1, 0]]]
Output: 0

Explanation: filter 0 matches the diagonal (response 2); filter 1 responds 0.

Example 2 — the other diagonal

Input:  patch = [[0, 1], [1, 0]], filters = same as above
Output: 1

Constraints

  • Response of each filter is sum(patch * filter).
  • Return the argmax over filters as a Python int.

Notes

  • Run this at every position and you get, per filter, a feature map of where its pattern appears — the basis of CNN feature extraction.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Forward diagonal example
  • Backward diagonal reference
  • Sample of three filters