Created
April 12, 2016 21:45
-
-
Save danoneata/e2738d0571f40f97ea5125192800cb18 to your computer and use it in GitHub Desktop.
Computing sufficient statistics for Fisher vectors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def descriptors_to_sufficient_statistics(xx, gmm, **kwargs): | |
# yael assumes that the data is in C-contiguous format. | |
xx = np.ascontiguousarray(np.atleast_2d(xx)) | |
N = xx.shape[0] | |
K = gmm.k | |
D = gmm.d | |
# Compute posterior probabilities using yael. | |
Q = gmm_predict_proba(xx, gmm) # NxK | |
# Get parameters and reshape them. | |
pi = yael.fvec_to_numpy(gmm.w, K) # 1xK | |
mu = yael.fvec_to_numpy(gmm.mu, K * D).reshape(K, D) # DxK | |
sigma = yael.fvec_to_numpy(gmm.sigma, K * D).reshape(K, D) # DxK | |
# Compute the sufficient statistics of descriptors. | |
Q_sum = np.sum(Q, 0)[:, np.newaxis] / N # Kx1 | |
Q_xx = np.dot(Q.T, xx) / N # KxD | |
Q_xx_2 = np.dot(Q.T, xx ** 2) / N # KxD | |
# Merge sufficient statistics into a vector. | |
return np.hstack((Q_sum, Q_xx.flatten(), Q_xx_2.flatten())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment