Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robust matched filter #138

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/hyperspectral/hyperspectral/math/covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ def shrinkage_covariance(data, regularizer='ridge', approx='mean-mahalanobis', t
def LMM(α):
β = (1 - α) / (n - 1)
Gα = n * β * S + α * T
Ginv = np.linalg.inv(Gα)
detG = np.linalg.det(Gα)
r0 = np.trace(np.dot(Ginv, S))
return (p * math.log(2 * math.pi) + math.log(detG)
+ math.log(1 - β * r0) + r0 / (1 - β * r0)) / 2
try:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks useful.

Ginv = np.linalg.inv(Gα)
detG = np.linalg.det(Gα)
r0 = np.trace(np.matmul(Ginv, S))
return (p * math.log(2 * math.pi) + math.log(detG) +
math.log(1 - β * r0) + r0 / (1 - β * r0)) / 2
except np.linalg.LinAlgError:
return np.inf

def LHL(α):
# TODO: implement Hoffbeck/Landgrebe approximation
Expand Down
2 changes: 1 addition & 1 deletion src/hyperspectral/hyperspectral/target/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .matched_filter import normalized_matched_filter
from .matched_filter import normalized_matched_filter, robust_filter_vector, robust_matched_filter
79 changes: 78 additions & 1 deletion src/hyperspectral/hyperspectral/target/matched_filter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import math

import numpy as np
from scipy.optimize import root_scalar

from hyperspectral.math.rpca import whitening_matrix, whiten

Expand Down Expand Up @@ -30,9 +31,85 @@ def normalized_matched_filter(image, target, clutter_cov, center=False):

if center:
r,c,d = image.shape
image = image - np.median(image.reshape((r * c, d)), axis=0)
med = np.median(image.reshape((r * c, d)), axis=0)
image = image - med
target = target - med

X̃ = whiten(image, white_matrix=white)
s̃ = whiten(target, white_matrix=white)

return np.divide(np.einsum('i,rci->rc', s̃, X̃), np.sqrt(np.einsum('rci,rci->rc',X̃, X̃)))/math.sqrt(np.inner(s̃, s̃))


def quad_form(ζ, s̃, λ, ε):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some discussion/description in a comment would be welcome here. It is not clear which if any of the references this is associated with.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you are finding the Lagrange multiplier as in equation 13 of Manolakis, et al.?

val = np.sum(np.divide(np.square(s̃), np.square(1 + ζ * λ))) - ε
deriv = -2 * np.sum(
np.multiply(λ,
np.divide(
np.square(s̃),
(1 + ζ * λ) ** 3
)
)
)
return val, deriv


def robust_filter_vector(Σ, s0, ε, ζ0):
assert len(s0.shape)==1, "Expecting vector-valued s0"
k = len(s0)
assert Σ.shape == (k, k), "Expecting compatible, square Σ"

λ, Q = np.linalg.eigh(Σ)
s̃ = np.matmul(Q.T, s0)
result = root_scalar(quad_form, args=(s̃, λ, ε), method="newton", fprime=True, x0=ζ0)

if not result.converged:
raise ValueError("Iteration to find ζ failed to converge!")

ζ = result.root

Minv = np.linalg.inv(Σ - (1/ζ) * np.eye(k))

return np.matmul(Minv, s0) / np.inner(s0, np.matmul(np.matmul(np.matmul(Minv, Σ), Minv), s0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Equation 16 from Manolakis, et. al.



def robust_matched_filter(image, target, clutter_cov, ε=1e-6, ζ0= 1e-6, center=False):
"""
Apply a robust matched filter to perform a target detection on a set of spectra.

Arguments:
image (np.array): spectral data to perform the matched filter on; can be of
shape (n, p) or (r, c, p) for p-dimensional spectra
target (np.array): p-dimensional array giving the approximate target spectrum
clutter_cov (np.array): p×p matrix describing the background covariance; best
to derive this with a shrinkage covariance estimator such as
sklearn.covariance.LedoitWolf
ε (real): max distance from given target spectrum to actual target spectrum
ζ (real): initial guess for RMF regularization parameter
center (bool or np.array): Boolean flag to determine if data should be
median-centered before applying filter, or spectrum to center signals on
prior to filtering

Reference:
Manolakis, D., Lockwood, R., Cooley, T., & Jacobson, J. (2009, August).
Hyperspectral detection algorithms: Use covariances or subspaces?. In Imaging
Spectrometry XIV (Vol. 7457, p. 74570Q). International Society for Optics and
Photonics.
"""
if isinstance(center, bool) and center:
r,c,d = image.shape
med = np.median(image.reshape((r * c, d)), axis=0)
image = image - med
target = target - med
elif isinstance(center, np.ndarray):
image = image - center
target = target - center

h = robust_filter_vector(clutter_cov, target, ε, ζ0)

if len(image.shape) == 2:
return np.dot(image, h)
elif len(image.shape) == 3:
return np.einsum('rci,i->rc', image, h)
else:
raise ValueError("Input image must be n×p or r×c×p numpy array")
906,685 changes: 453,349 additions & 453,336 deletions src/hyperspectral/notebooks/MIF_torch_demo.ipynb

Large diffs are not rendered by default.

283 changes: 283 additions & 0 deletions src/hyperspectral/notebooks/RMF demo.ipynb

Large diffs are not rendered by default.

Binary file added src/hyperspectral/notebooks/data/tpa_extract.npz
Binary file not shown.
Binary file added src/hyperspectral/notebooks/data/tpa_spectrum.npz
Binary file not shown.
1 change: 1 addition & 0 deletions src/hyperspectral/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ jupyter==1.0.0
matplotlib==3.1.2
rasterio==1.1.7
Shapely==1.7.1
sklearn==0.23.2
tqdm==4.51.0