Skip to content
Snippets Groups Projects
Commit acb7592b authored by Richard Torenvliet's avatar Richard Torenvliet
Browse files

Add reconstruction with the possibility of changing the highest eigenvalues

parent a54d0723
No related branches found
No related tags found
No related merge requests found
...@@ -9,3 +9,7 @@ data/imm_face_db: data/imm_face_db.tar.gz ...@@ -9,3 +9,7 @@ data/imm_face_db: data/imm_face_db.tar.gz
data/imm_face_db.tar.gz: data/imm_face_db.tar.gz:
(cd data; wget http://www.imm.dtu.dk/~aam/datasets/imm_face_db.tar.gz) (cd data; wget http://www.imm.dtu.dk/~aam/datasets/imm_face_db.tar.gz)
test:
py.test -f src/*_test.py
import numpy as np
def build_mean_aam(imm_points):
""" construct a mean from a matrix of x,y values
Args:
imm_points(numpy array) that follows the following structure:
observations:
0. [[x_0_0, y_0_0], ... , [x_0_m, y_0_m]],
1. [[x_1_0, y_1_0], ... , [x_1_m, y_1_m]],
2. [[x_2_0, y_2_0], ... , [x_2_m, y_2_m]],
3. [[x_3_0, y_3_0], ... , [x_3_m, y_3_m]]
.... .... .m...
n. [[x_4_0, y_4_0], ... , [x_n_m, y_n_m]]
Returns mean_values (numpy array)
This vector containts the mean values of the corresponding column, like so:
0. [[x_0_0, y_0_0], ... , [x_0_k, y_0_k]],
1. [[x_1_0, y_1_0], ... , [x_1_k, y_1_k]],
2. [[x_2_0, y_2_0], ... , [x_2_k, y_2_k]],
3. [[x_3_0, y_3_0], ... , [x_3_k, y_3_k]]
.... .... .....
n. [[x_4_0, y_4_0], ... , [x_n_k, y_n_k]]
mean. [[x_mean_0, y_mean_0], ... [x_mean_n, y_mean_n]]
"""
return np.mean(imm_points, axis=0)
import numpy as np
from aam import build_mean_aam
def test_build_mean_aan():
imm_points = np.array([
[[1, 2], [2, 4]],
[[2, 3], [3, 6]],
])
expected = np.array([
[1.5, 2.5],
[2.5, 5.]
])
mean = build_mean_aam(imm_points)
np.testing.assert_array_equal(mean, expected)
def test_zero_mean_aan():
imm_points = np.array([
[[1, 2], [2, 4]],
[[2, 3], [3, 6]],
])
expected = np.array([
[[-0.5, -0.5], [-0.5, -1.0]],
[[0.5, 0.5], [0.5, 1.0]],
])
mean = build_mean_aam(imm_points)
zero_mean = imm_points - mean
# test that zero mean has indeed zero mean
np.testing.assert_array_equal(
np.mean(zero_mean, axis=0), np.zeros((2, 2))
)
np.testing.assert_array_equal(zero_mean, expected)
...@@ -55,12 +55,11 @@ class IMMPoints(): ...@@ -55,12 +55,11 @@ class IMMPoints():
cv2.FONT_HERSHEY_SIMPLEX, .5, (100, 0, 255)) cv2.FONT_HERSHEY_SIMPLEX, .5, (100, 0, 255))
cv2.circle(img, tuple(p), 3, color=(0, 255, 100)) cv2.circle(img, tuple(p), 3, color=(0, 255, 100))
def show_on_img(self, img): def show_on_img(self, img, window_name='image'):
self.draw_triangles(img, self.points) self.draw_triangles(img, self.points)
cv2.imshow('image', img) cv2.imshow(window_name, img)
cv2.waitKey(0)
def show(self): def show(self, window_name='image'):
"""show the image and datapoints on the image""" """show the image and datapoints on the image"""
assert(len(self.points) > 0) assert(len(self.points) > 0)
assert(len(self.filename) > 0) assert(len(self.filename) > 0)
...@@ -68,9 +67,7 @@ class IMMPoints(): ...@@ -68,9 +67,7 @@ class IMMPoints():
img = cv2.imread('data/imm_face_db/' + self.filename) img = cv2.imread('data/imm_face_db/' + self.filename)
self.draw_triangles(img, self.points) self.draw_triangles(img, self.points)
cv2.imshow(window_name, img)
cv2.imshow('image', img)
cv2.waitKey(0)
def add_parser_options(): def add_parser_options():
......
from pca import pca import copy
import numpy as np
import argparse import argparse
import cv2
import numpy as np
from pca import pca, reconstruct
from aam import build_mean_aam
from imm_points import IMMPoints from imm_points import IMMPoints
def nothing(_):
pass
def add_parser_options(): def add_parser_options():
parser = argparse.ArgumentParser(description='IMMPoints tool') parser = argparse.ArgumentParser(description='IMMPoints tool')
...@@ -16,39 +24,12 @@ def add_parser_options(): ...@@ -16,39 +24,12 @@ def add_parser_options():
return parser return parser
def build_mean_aam(imm_points): def init_eigenvalue_trackbars(n_components, s):
""" construct a mean from a matrix of x,y values cv2.namedWindow('eigenvalues')
Args:
imm_points(numpy array)that follows the follwing structure:
observations:
0. [[x_0_0, y_0_0], ... , [x_0_m, y_0_m]],
1. [[x_1_0, y_1_0], ... , [x_1_m, y_1_m]],
2. [[x_2_0, y_2_0], ... , [x_2_m, y_2_m]],
3. [[x_3_0, y_3_0], ... , [x_3_m, y_3_m]]
.... .... .m...
n. [[x_4_0, y_4_0], ... , [x_n_m, y_n_m]]
Returns mean_values (numpy array)
This vector containts the mean values of the corresponding column, like so:
0. [[x_0_0, y_0_0], ... , [x_0_k, y_0_k]],
1. [[x_1_0, y_1_0], ... , [x_1_k, y_1_k]],
2. [[x_2_0, y_2_0], ... , [x_2_k, y_2_k]],
3. [[x_3_0, y_3_0], ... , [x_3_k, y_3_k]]
.... .... .....
n. [[x_4_0, y_4_0], ... , [x_n_k, y_n_k]]
mean. [[x_mean_0, y_mean_0], ... [x_mean_n, y_mean_n]] for i in range(n_components):
""" cv2.createTrackbar('{}'.format(i), 'eigenvalues', 0, 1000, nothing)
mean_values = []
for i in range(imm_points.shape[1]):
mean_values.append(np.mean(imm_points[:, i], axis=0))
return np.array(mean_values)
if __name__ == '__main__': if __name__ == '__main__':
parser = add_parser_options() parser = add_parser_options()
...@@ -63,10 +44,36 @@ if __name__ == '__main__': ...@@ -63,10 +44,36 @@ if __name__ == '__main__':
# imm.show() # imm.show()
imm_points = np.array(imm_points) imm_points = np.array(imm_points)
mean_values = build_mean_aam(np.array(imm_points)) mean_values = build_mean_aam(np.array(imm_points))
V, S = pca(imm_points, mean_values) U, s, Vt = pca(imm_points, mean_values)
index = 0
cv2.namedWindow('index')
cv2.createTrackbar('index', 'index', index, len(args.asf) - 1, nothing)
n_components = 5
init_eigenvalue_trackbars(n_components, s)
s_copy = copy.copy(s)
while True:
projection = reconstruct(U, s_copy, Vt, n_components)
X_reconstructed = projection[index].reshape((58, 2)) + mean_values
imm = IMMPoints(points=X_reconstructed)
img = np.full((480, 640, 3), 255, np.uint8)
imm.show_on_img(img)
for i in range(n_components):
s_copy[i] = s[i] * (cv2.getTrackbarPos(str(i), 'eigenvalues') / 10.0)
index = cv2.getTrackbarPos('index', 'index')
imm = IMMPoints(filename=args.asf[index])
imm.show(window_name='original')
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
# show immpoints cv2.destroyAllWindows()
imm = IMMPoints(points=mean_values)
img = np.full((480, 640, 3), 255, np.uint8)
imm.show_on_img(img)
import numpy as np import numpy as np
def preprocess(data): def flatten_feature_vectors(data):
"""
Flattens the feature vectors inside.
Example:
input:
[
[[1, 2], [3, 4], [5, 6]],
...
[[1, 2], [3, 4], [5, 6]]
]
output:
[
[1, 2, 3, 4, 5, 6],
...
[1, 2, 3, 4, 5, 6]
]
Args:
data (numpy array): array of feature vectors
return:
array: (numpy array): array flattened feature vectors
"""
flattened = [] flattened = []
y, x, dim = data.shape y, x, dim = data.shape
...@@ -12,13 +36,18 @@ def preprocess(data): ...@@ -12,13 +36,18 @@ def preprocess(data):
return np.array(flattened) return np.array(flattened)
def pca(data, mean_values, n_components): def pca(data, mean_values):
# subtract mean # subtract mean
zero_mean = data - mean_values zero_mean = data - mean_values
X = preprocess(zero_mean) X = flatten_feature_vectors(zero_mean)
observations, dims = X.shape _, dim = X.shape
U, S, V = np.linalg.svd(X) U, s, V = np.linalg.svd(X, full_matrices=False)
return V[:n_components], S[:n_components] return U, s, V
def reconstruct(U, s, Vt, n_components):
return np.dot(U[:, :n_components],
np.dot(np.diag(s[:n_components]), Vt[:n_components]))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment