How to calculate eigenfaces in python?
I'm trying to calculate eigenfaces for a set of images using python.
First I turn each image into a vector using:
list(map(lambda x:x.flatten(), x))
Then I calculate covariance matrix (after removing mean from all data):
# x is a numpy array
x = x - mean_image
cov_matrix = np.cov(x.T)
Then I calculate eigenvalues and eigenevtors:
eigen_values, eigen_vecotrs = np.linalg.eig(cov_matrix)
The results are vectors with complex numbers, so I only keep the real part to be able to show them:
eigen_vectors = np.real(eigen_vectors)
After trying to show eigenfaces (eigenvectors), the result is not even close to how an eigenface looks like:
I have managed to get a list of eigenfaces using np.linalg.svd()
however I'm curious why my code does not work and how can I change it so it work as expected.
To fix the np.linalg.eig
returning complex results I reduced the size of images, so it doesn't return complex numbers anymore however still my eigenvectors doesn't look like an eigenface:
python machine-learning pattern-recognition eigenvector
add a comment |
I'm trying to calculate eigenfaces for a set of images using python.
First I turn each image into a vector using:
list(map(lambda x:x.flatten(), x))
Then I calculate covariance matrix (after removing mean from all data):
# x is a numpy array
x = x - mean_image
cov_matrix = np.cov(x.T)
Then I calculate eigenvalues and eigenevtors:
eigen_values, eigen_vecotrs = np.linalg.eig(cov_matrix)
The results are vectors with complex numbers, so I only keep the real part to be able to show them:
eigen_vectors = np.real(eigen_vectors)
After trying to show eigenfaces (eigenvectors), the result is not even close to how an eigenface looks like:
I have managed to get a list of eigenfaces using np.linalg.svd()
however I'm curious why my code does not work and how can I change it so it work as expected.
To fix the np.linalg.eig
returning complex results I reduced the size of images, so it doesn't return complex numbers anymore however still my eigenvectors doesn't look like an eigenface:
python machine-learning pattern-recognition eigenvector
I'm not complete sure what you're trying to do. Are you trying to obtain a small rank approximation of your images by keeping the top k eigenvectors?
– yatu
Nov 12 '18 at 14:22
I just want to show eigenfaces to make sure they are same as eigenvectors and if they are not I should find out how can I convert them to eigenfaces. Wikipedia states that they are same thing so my results should be wrong and I'm looking to find out why my code does not work as it should.
– Ravexina
Nov 12 '18 at 14:30
@Ravexina, can you show a complete example code that includes the plotting function. Also for debugging, show code where you flatten and reconstruct single images and an average image, to see that at least that part works well.
– Martijn Weterings
Nov 18 '18 at 11:26
Why do you take the covariance matrix of the transposex.T
? It is difficult to see how you construct the matrix x (is each column a picture or each row?) and the mean-image. But... is the dimension of your covariance matrix k x k with k the number of pixels or k the number of images? It should be k the number of pixels.
– Martijn Weterings
Nov 18 '18 at 11:39
@MartijnWeterings Thank you for taking the time, I will add the requested details as soon as possible, I'm a little bit busy now.
– Ravexina
Nov 18 '18 at 19:26
add a comment |
I'm trying to calculate eigenfaces for a set of images using python.
First I turn each image into a vector using:
list(map(lambda x:x.flatten(), x))
Then I calculate covariance matrix (after removing mean from all data):
# x is a numpy array
x = x - mean_image
cov_matrix = np.cov(x.T)
Then I calculate eigenvalues and eigenevtors:
eigen_values, eigen_vecotrs = np.linalg.eig(cov_matrix)
The results are vectors with complex numbers, so I only keep the real part to be able to show them:
eigen_vectors = np.real(eigen_vectors)
After trying to show eigenfaces (eigenvectors), the result is not even close to how an eigenface looks like:
I have managed to get a list of eigenfaces using np.linalg.svd()
however I'm curious why my code does not work and how can I change it so it work as expected.
To fix the np.linalg.eig
returning complex results I reduced the size of images, so it doesn't return complex numbers anymore however still my eigenvectors doesn't look like an eigenface:
python machine-learning pattern-recognition eigenvector
I'm trying to calculate eigenfaces for a set of images using python.
First I turn each image into a vector using:
list(map(lambda x:x.flatten(), x))
Then I calculate covariance matrix (after removing mean from all data):
# x is a numpy array
x = x - mean_image
cov_matrix = np.cov(x.T)
Then I calculate eigenvalues and eigenevtors:
eigen_values, eigen_vecotrs = np.linalg.eig(cov_matrix)
The results are vectors with complex numbers, so I only keep the real part to be able to show them:
eigen_vectors = np.real(eigen_vectors)
After trying to show eigenfaces (eigenvectors), the result is not even close to how an eigenface looks like:
I have managed to get a list of eigenfaces using np.linalg.svd()
however I'm curious why my code does not work and how can I change it so it work as expected.
To fix the np.linalg.eig
returning complex results I reduced the size of images, so it doesn't return complex numbers anymore however still my eigenvectors doesn't look like an eigenface:
python machine-learning pattern-recognition eigenvector
python machine-learning pattern-recognition eigenvector
asked Nov 12 '18 at 14:06
RavexinaRavexina
4911621
4911621
I'm not complete sure what you're trying to do. Are you trying to obtain a small rank approximation of your images by keeping the top k eigenvectors?
– yatu
Nov 12 '18 at 14:22
I just want to show eigenfaces to make sure they are same as eigenvectors and if they are not I should find out how can I convert them to eigenfaces. Wikipedia states that they are same thing so my results should be wrong and I'm looking to find out why my code does not work as it should.
– Ravexina
Nov 12 '18 at 14:30
@Ravexina, can you show a complete example code that includes the plotting function. Also for debugging, show code where you flatten and reconstruct single images and an average image, to see that at least that part works well.
– Martijn Weterings
Nov 18 '18 at 11:26
Why do you take the covariance matrix of the transposex.T
? It is difficult to see how you construct the matrix x (is each column a picture or each row?) and the mean-image. But... is the dimension of your covariance matrix k x k with k the number of pixels or k the number of images? It should be k the number of pixels.
– Martijn Weterings
Nov 18 '18 at 11:39
@MartijnWeterings Thank you for taking the time, I will add the requested details as soon as possible, I'm a little bit busy now.
– Ravexina
Nov 18 '18 at 19:26
add a comment |
I'm not complete sure what you're trying to do. Are you trying to obtain a small rank approximation of your images by keeping the top k eigenvectors?
– yatu
Nov 12 '18 at 14:22
I just want to show eigenfaces to make sure they are same as eigenvectors and if they are not I should find out how can I convert them to eigenfaces. Wikipedia states that they are same thing so my results should be wrong and I'm looking to find out why my code does not work as it should.
– Ravexina
Nov 12 '18 at 14:30
@Ravexina, can you show a complete example code that includes the plotting function. Also for debugging, show code where you flatten and reconstruct single images and an average image, to see that at least that part works well.
– Martijn Weterings
Nov 18 '18 at 11:26
Why do you take the covariance matrix of the transposex.T
? It is difficult to see how you construct the matrix x (is each column a picture or each row?) and the mean-image. But... is the dimension of your covariance matrix k x k with k the number of pixels or k the number of images? It should be k the number of pixels.
– Martijn Weterings
Nov 18 '18 at 11:39
@MartijnWeterings Thank you for taking the time, I will add the requested details as soon as possible, I'm a little bit busy now.
– Ravexina
Nov 18 '18 at 19:26
I'm not complete sure what you're trying to do. Are you trying to obtain a small rank approximation of your images by keeping the top k eigenvectors?
– yatu
Nov 12 '18 at 14:22
I'm not complete sure what you're trying to do. Are you trying to obtain a small rank approximation of your images by keeping the top k eigenvectors?
– yatu
Nov 12 '18 at 14:22
I just want to show eigenfaces to make sure they are same as eigenvectors and if they are not I should find out how can I convert them to eigenfaces. Wikipedia states that they are same thing so my results should be wrong and I'm looking to find out why my code does not work as it should.
– Ravexina
Nov 12 '18 at 14:30
I just want to show eigenfaces to make sure they are same as eigenvectors and if they are not I should find out how can I convert them to eigenfaces. Wikipedia states that they are same thing so my results should be wrong and I'm looking to find out why my code does not work as it should.
– Ravexina
Nov 12 '18 at 14:30
@Ravexina, can you show a complete example code that includes the plotting function. Also for debugging, show code where you flatten and reconstruct single images and an average image, to see that at least that part works well.
– Martijn Weterings
Nov 18 '18 at 11:26
@Ravexina, can you show a complete example code that includes the plotting function. Also for debugging, show code where you flatten and reconstruct single images and an average image, to see that at least that part works well.
– Martijn Weterings
Nov 18 '18 at 11:26
Why do you take the covariance matrix of the transpose
x.T
? It is difficult to see how you construct the matrix x (is each column a picture or each row?) and the mean-image. But... is the dimension of your covariance matrix k x k with k the number of pixels or k the number of images? It should be k the number of pixels.– Martijn Weterings
Nov 18 '18 at 11:39
Why do you take the covariance matrix of the transpose
x.T
? It is difficult to see how you construct the matrix x (is each column a picture or each row?) and the mean-image. But... is the dimension of your covariance matrix k x k with k the number of pixels or k the number of images? It should be k the number of pixels.– Martijn Weterings
Nov 18 '18 at 11:39
@MartijnWeterings Thank you for taking the time, I will add the requested details as soon as possible, I'm a little bit busy now.
– Ravexina
Nov 18 '18 at 19:26
@MartijnWeterings Thank you for taking the time, I will add the requested details as soon as possible, I'm a little bit busy now.
– Ravexina
Nov 18 '18 at 19:26
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263842%2fhow-to-calculate-eigenfaces-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263842%2fhow-to-calculate-eigenfaces-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm not complete sure what you're trying to do. Are you trying to obtain a small rank approximation of your images by keeping the top k eigenvectors?
– yatu
Nov 12 '18 at 14:22
I just want to show eigenfaces to make sure they are same as eigenvectors and if they are not I should find out how can I convert them to eigenfaces. Wikipedia states that they are same thing so my results should be wrong and I'm looking to find out why my code does not work as it should.
– Ravexina
Nov 12 '18 at 14:30
@Ravexina, can you show a complete example code that includes the plotting function. Also for debugging, show code where you flatten and reconstruct single images and an average image, to see that at least that part works well.
– Martijn Weterings
Nov 18 '18 at 11:26
Why do you take the covariance matrix of the transpose
x.T
? It is difficult to see how you construct the matrix x (is each column a picture or each row?) and the mean-image. But... is the dimension of your covariance matrix k x k with k the number of pixels or k the number of images? It should be k the number of pixels.– Martijn Weterings
Nov 18 '18 at 11:39
@MartijnWeterings Thank you for taking the time, I will add the requested details as soon as possible, I'm a little bit busy now.
– Ravexina
Nov 18 '18 at 19:26