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

ProbStats and ML Assignments #33

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2,562 changes: 2,562 additions & 0 deletions Anupriya/Kaggle/kaggle_supervised.ipynb

Large diffs are not rendered by default.

697 changes: 697 additions & 0 deletions assMath/ass2B/2B_Anupriya.ipynb

Large diffs are not rendered by default.

572 changes: 572 additions & 0 deletions assMath/ass2a/2A_Anupriya.ipynb

Large diffs are not rendered by default.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
HostUrl=about:internet
13 changes: 13 additions & 0 deletions assMath/probStat/Anupriya/kalmanfilter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Assignment
Make changes in deep_sort/kalman_filter_assignment.py
Run test.py code to test your code
Don't try to search for orignal code, The assignment is to get you more familliar with using statistics in real life problem setting, and it's for your own benifit.

## Dependencies

The code is compatible with Python 2.7 and 3. The following dependencies are
needed to run the tracker:

* NumPy
* sklearn
* OpenCV
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "1.3.2"
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
# vim: expandtab:ts=4:sw=4
import numpy as np
import scipy.linalg


"""
Table for the 0.95 quantile of the chi-square distribution with N degrees of
freedom (contains values for N=1, ..., 9). Taken from MATLAB/Octave's chi2inv
function and used as Mahalanobis gating threshold.
"""
### Don't change
chi2inv95 = {
1: 3.8415,
2: 5.9915,
3: 7.8147,
4: 9.4877,
5: 11.070,
6: 12.592,
7: 14.067,
8: 15.507,
9: 16.919,
}


class KalmanFilter(object):
"""
A simple Kalman filter for tracking bounding boxes in image space.

The 8-dimensional state space

x, y, a, h, vx, vy, va, vh

contains the bounding box center position (x, y), aspect ratio a, height h,
and their respective velocities.

Object motion follows a constant velocity model. The bounding box location
(x, y, a, h) is taken as direct observation of the state space (linear
observation model).

"""

def __init__(self):
dt=1.0

# Create Kalman filter model matrices.
##ADD code
# Motion and observation uncertainty are chosen relative to the current
# state estimate. These weights control the amount of uncertainty in
# the model. This is a bit hacky.
##ADD code
# for state extrapolation equation
# state transition matrix
self.F = np.array([[1, 0, 0, 0, dt, 0, 0, 0],
[0, 1, 0, 0, 0, dt, 0, 0],
[0, 0, 1, 0, 0, 0, dt, 0],
[0, 0, 0, 1, 0, 0, 0, dt],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1]])
# no control matrix since this is a constant velocity model
# measurement matrix intitialised as 4 x 4 identity matrix for measuring x,y,a,h
self.H = np.array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])
# for covariance extrapolation equation
# Process noise covariance
self.Q = np.array([[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1]])
# Measurement noise covariance
self.R = np.array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]])

def initiate(self, measurement):
"""Create track from unassociated measurement.

Parameters
----------
measurement : ndarray
Bounding box coordinates (x, y, a, h) with center position (x, y),
aspect ratio a, and height h.

Returns
-------
(ndarray, ndarray)
Returns the mean vector (8 dimensional) and covariance matrix (8x8
dimensional) of the new track. Unobserved velocities are initialized
to 0 mean.

"""
##ADD code
# intially the x,y,a,h are observed and equal to measurement
# unobserved velocities have 0 mean (last 4 entries)
mean=np.array([0,0,0,0,0,0,0,0])
mean[:4]=measurement
covariance =np.array([[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1]])
return mean, covariance

def predict(self, mean, covariance):
"""Run Kalman filter prediction step.

Parameters
----------
mean : ndarray
The 8 dimensional mean vector of the object state at the previous
time step.
covariance : ndarray
The 8x8 dimensional covariance matrix of the object state at the
previous time step.

Returns
-------
(ndarray, ndarray)
Returns the mean vector and covariance matrix of the predicted
state. Unobserved velocities are initialized to 0 mean.

"""
##ADD code
# basically the prediction equation, here state extrapolation -> x(n+1)= F.x(n) and x(n) is the mean so
mean=self.F.dot(mean)
# and now covariance extrapolation -> P(n+1)= F.P(n).F(transpose) + Q
covariance = self.F.dot(covariance).dot(self.F.T)+ self.Q
return mean, covariance

def project(self, mean, covariance):
"""Project state distribution to measurement space.

Parameters
----------
mean : ndarray
The state's mean vector (8 dimensional array).
covariance : ndarray
The state's covariance matrix (8x8 dimensional).

Returns
-------
(ndarray, ndarray)
Returns the projected mean and covariance matrix of the given state
estimate.

"""
##ADD code
# measurement equations?
mean=self.H.dot(mean)
covariance=self.H.dot(covariance).dot(self.H.T)+self.R
return mean, covariance

def update(self, mean, covariance, measurement):
"""Run Kalman filter correction step.

Parameters
----------
mean : ndarray
The predicted state's mean vector (8 dimensional).
covariance : ndarray
The state's covariance matrix (8x8 dimensional).
measurement : ndarray
The 4 dimensional measurement vector (x, y, a, h), where (x, y)
is the center position, a the aspect ratio, and h the height of the
bounding box.

Returns
-------
(ndarray, ndarray)
Returns the measurement-corrected state distribution.

"""
##ADD code
# update equation -> x(n,n)=x(n,n-1) + K(n)(z(n)-H.x(n,n-1))
# but we need K(n), A 4x4 matrix
# Kalman gain equation -> K(n) = P(n,n-1).H(transpose).(H.P(n,n-1).H(transpose)+R)(inverse)

Kalman_gain= covariance.dot(self.H.T).dot(np.linalg,inv(self.H.dot(covariance).dot(self.H.T)+self.R))

innovation=measurement - np.dot(self.H,mean) # as it is conventionally called

new_mean= mean+ np.dot(Kalman_gain,innovation)

# covariance update equation (longer form, cause shorter form is "numerically unstable" according to text) ->
# P(n,n)= (I-H.K(n)).P(n,n-1).(I-H.K(n))(transpose) + K(n).R.K(n)(transpose)

newterm= np.eye(4)-np.dot(self.H,Kalman_gain)

new_covariance=newterm.dot(covariance).dot(newterm.T) + Kalman_gain.dot(self.R).dot(Kalman_gain.T)

return new_mean, new_covariance

def gating_distance(self, mean, covariance, measurements, only_position=False):
"""Compute gating distance between state distribution and measurements.

A suitable distance threshold can be obtained from `chi2inv95`. If
`only_position` is False, the chi-square distribution has 4 degrees of
freedom, otherwise 2.

Parameters
----------
mean : ndarray
Mean vector over the state distribution (8 dimensional).
covariance : ndarray
Covariance of the state distribution (8x8 dimensional).
measurements : ndarray
An Nx4 dimensional matrix of N measurements, each in
format (x, y, a, h) where (x, y) is the bounding box center
position, a the aspect ratio, and h the height.
only_position : Optional[bool]
If True, distance computation is done with respect to the bounding
box center position only.

Returns
-------
ndarray
Returns an array of length N, where the i-th element contains the
squared Mahalanobis distance between (mean, covariance) and
`measurements[i]`.

"""
### Don't change anything
mean, covariance = self.project(mean, covariance)
if only_position:
mean, covariance = mean[:2], covariance[:2, :2]
measurements = measurements[:, :2]

cholesky_factor = np.linalg.cholesky(covariance)
d = measurements - mean
z = scipy.linalg.solve_triangular(
cholesky_factor, d.T, lower=True, check_finite=False, overwrite_b=True
)
squared_maha = np.sum(z * z, axis=0)
return squared_maha
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# vim: expandtab:ts=4:sw=4
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# vim: expandtab:ts=4:sw=4
import numpy as np


class Detection(object):
"""
This class represents a bounding box detection in a single image.

Parameters
----------
ltwh : array_like
Bounding box in format `(x, y, w, h)`.
confidence : float
Detector confidence score.
feature : array_like
A feature vector that describes the object contained in this image.
class_name : Optional str
Detector predicted class name.
instance_mask : Optional
Instance mask corresponding to bounding box
others : Optional any
Other supplementary fields associated with detection that wants to be stored as a "memory" to be retrieve through the track downstream.

Attributes
----------
ltwh : ndarray
Bounding box in format `(top left x, top left y, width, height)`.
confidence : ndarray
Detector confidence score.
feature : ndarray | NoneType
A feature vector that describes the object contained in this image.

"""

def __init__(self, ltwh, confidence, feature, class_name=None, instance_mask=None, others=None):
# def __init__(self, ltwh, feature):
self.ltwh = np.asarray(ltwh, dtype=np.float32)
self.confidence = float(confidence)
self.feature = np.asarray(feature, dtype=np.float32)
self.class_name = class_name
self.instance_mask = instance_mask
self.others = others

def get_ltwh(self):
return self.ltwh.copy()

def to_tlbr(self):
"""Convert bounding box to format `(min x, min y, max x, max y)`, i.e.,
`(top left, bottom right)`.
"""
ret = self.ltwh.copy()
ret[2:] += ret[:2]
return ret

def to_xyah(self):
"""Convert bounding box to format `(center x, center y, aspect ratio,
height)`, where the aspect ratio is `width / height`.
"""
ret = self.ltwh.copy()
ret[:2] += ret[2:] / 2
ret[2] /= ret[3]
return ret
Loading