Training Facial Recognition Algorithms with Alibaba’s Mars

Singular Value Decomposition (SVD)

In [1]: import numpy as npIn [2]: a = np.random.rand(20000, 100)In [3]: %time U, s, V = np.linalg.svd(a)
CPU times: user 4min 3s, sys: 10.2 s, total: 4min 13s
Wall time: 1min 18s
In [1]: import mars.tensor as mtIn [2]: a = mt.random.rand(20000, 100, chunk_size=100)In [3]: %time U, s, V = mt.linalg.svd(a).execute()
CPU times: user 5.42 s, sys: 1.49 s, total: 6.91 s
Wall time: 1.87 s

Principal Component Analysis (PCA)

import numpy as npa = np.random.randint(0, 256, size=(10000, 100))
a_mean = a.mean(axis=1, keepdims=True)
a_new = a - a_mean
cov_a = (a_new.dot(a_new.T)) / (a.shape[1] - 1)
# Use SVD to find the first 20 feature values in the covariance matrix
U, s, V = np.linalg.svd(cov_a)
V = V.T
vecs = V[:, :20]
# Use low-dimensional feature vectors to represent the original data
a_transformed = a.dot(vecs)
import mars.tensor as mta = mt.random.randint(0, 256, size=(10000, 100))
a_mean = a.mean(axis=1, keepdims=True)
a_new = a - a_mean
cov_a = (a_new.dot(a_new.T)) / (a.shape[1] - 1)
# Use SVD to find the first 20 feature values in the covariance matrix
U, s, V = mt.linalg.svd(cov_a)
V = V.T
vecs = V[:, :20]
# Use low-dimensional feature vectors to represent the original data
a_transformed = a.dot(vecs).execute()

Facial Recognition

import mars.tensor as mt
from mars.session import new_session
session = new_session()def cov(x):
x_new = x - x.mean(axis=1, keepdims=True)
return x_new.dot(x_new.T) / (x_new.shape[1] - 1)
def pca_compress(data_mat, k):
data_mean = mt.mean(data_mat, axis=0, keepdims=True)
data_new = data_mat - data_mean

cov_data = cov(data_new)
U, s, V = mt.linalg.svd(cov_data)
V = V.T
vecs = V[:, :k]
data_transformed = vecs.T.dot(data_new)
return session.run(data_transformed, data_mean, vecs)
# Find the Euclidean distance
def compare(vec1, vec2):
distance = mt.dot(vec1, vec2) / (mt.linalg.norm(vec1) * mt.linalg.norm(vec2))
return distance.execute()

Future Work

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store