From 26d83249f4d1268984d31b20a19eeb2a279ec78f Mon Sep 17 00:00:00 2001 From: Mihir Paradkar Date: Tue, 9 Jan 2018 11:55:26 -0500 Subject: [PATCH 1/4] Implemented zca_whitening without forming covariance matrix or explicit call to diag for eigenvalues --- keras/preprocessing/image.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/keras/preprocessing/image.py b/keras/preprocessing/image.py index c403d896b29..c2853659b7b 100644 --- a/keras/preprocessing/image.py +++ b/keras/preprocessing/image.py @@ -748,9 +748,10 @@ def fit(self, x, if self.zca_whitening: flat_x = np.reshape(x, (x.shape[0], x.shape[1] * x.shape[2] * x.shape[3])) - sigma = np.dot(flat_x.T, flat_x) / flat_x.shape[0] - u, s, _ = linalg.svd(sigma) - self.principal_components = np.dot(np.dot(u, np.diag(1. / np.sqrt(s + self.zca_epsilon))), u.T) + n_examples = flat_x.shape[0] + u, s, vt = linalg.svd(flat_x / np.sqrt(n_examples)) + s_expand = np.hstack((s, np.zeros(vt.shape[0] - n_examples, dtype=flat_x.dtype))) + principal_components = (vt.T / np.sqrt(s_expand**2 + 1e-5)).dot(vt) class Iterator(Sequence): From 6672ac3747f9040c2343a77fd86fcd69274d6be2 Mon Sep 17 00:00:00 2001 From: Mihir Paradkar Date: Tue, 9 Jan 2018 12:01:30 -0500 Subject: [PATCH 2/4] Made principal_components self.principal_components --- keras/preprocessing/image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keras/preprocessing/image.py b/keras/preprocessing/image.py index c2853659b7b..b11d922129b 100644 --- a/keras/preprocessing/image.py +++ b/keras/preprocessing/image.py @@ -751,7 +751,7 @@ def fit(self, x, n_examples = flat_x.shape[0] u, s, vt = linalg.svd(flat_x / np.sqrt(n_examples)) s_expand = np.hstack((s, np.zeros(vt.shape[0] - n_examples, dtype=flat_x.dtype))) - principal_components = (vt.T / np.sqrt(s_expand**2 + 1e-5)).dot(vt) + self.principal_components = (vt.T / np.sqrt(s_expand**2 + 1e-5)).dot(vt) class Iterator(Sequence): From a5bf990916a3321325007c142ce9535782eedf63 Mon Sep 17 00:00:00 2001 From: Mihir Paradkar Date: Tue, 9 Jan 2018 12:47:44 -0500 Subject: [PATCH 3/4] Fixed mistake with zca_epsilon --- keras/preprocessing/image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keras/preprocessing/image.py b/keras/preprocessing/image.py index b11d922129b..850f97bd793 100644 --- a/keras/preprocessing/image.py +++ b/keras/preprocessing/image.py @@ -751,7 +751,7 @@ def fit(self, x, n_examples = flat_x.shape[0] u, s, vt = linalg.svd(flat_x / np.sqrt(n_examples)) s_expand = np.hstack((s, np.zeros(vt.shape[0] - n_examples, dtype=flat_x.dtype))) - self.principal_components = (vt.T / np.sqrt(s_expand**2 + 1e-5)).dot(vt) + self.principal_components = (vt.T / np.sqrt(s_expand**2 + self.zca_epsilon)).dot(vt) class Iterator(Sequence): From abb2f07640e2b2a2893fb8474a6630598440b9c0 Mon Sep 17 00:00:00 2001 From: Mihir Paradkar Date: Tue, 9 Jan 2018 23:52:32 -0500 Subject: [PATCH 4/4] Style fixes --- keras/preprocessing/image.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/keras/preprocessing/image.py b/keras/preprocessing/image.py index 850f97bd793..329f8151c68 100644 --- a/keras/preprocessing/image.py +++ b/keras/preprocessing/image.py @@ -748,10 +748,10 @@ def fit(self, x, if self.zca_whitening: flat_x = np.reshape(x, (x.shape[0], x.shape[1] * x.shape[2] * x.shape[3])) - n_examples = flat_x.shape[0] - u, s, vt = linalg.svd(flat_x / np.sqrt(n_examples)) - s_expand = np.hstack((s, np.zeros(vt.shape[0] - n_examples, dtype=flat_x.dtype))) - self.principal_components = (vt.T / np.sqrt(s_expand**2 + self.zca_epsilon)).dot(vt) + num_examples = flat_x.shape[0] + u, s, vt = linalg.svd(flat_x / np.sqrt(num_examples)) + s_expand = np.hstack((s, np.zeros(vt.shape[0] - num_examples, dtype=flat_x.dtype))) + self.principal_components = (vt.T / np.sqrt(s_expand ** 2 + self.zca_epsilon)).dot(vt) class Iterator(Sequence):