Skip to content

Commit

Permalink
All unittests added
Browse files Browse the repository at this point in the history
  • Loading branch information
jan authored and jan committed Apr 12, 2017
1 parent 6215193 commit fbcc37c
Show file tree
Hide file tree
Showing 23 changed files with 4,672 additions and 107 deletions.
2 changes: 1 addition & 1 deletion docs/installation.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Installation
##################################

To install PyDeep, first download it from `GitHub <https://github.com/MelJan/PyDeep>`_.
To install PyDeep, first download it from `GitHub/MelJan <https://github.com/MelJan/PyDeep>`_.
Then simply change to the PyDeep folder and run the setup script:

.. code-block:: bash
Expand Down
6 changes: 3 additions & 3 deletions docs/welcome.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ its extensive use of unittests assures a high level of reliability and correctne

News
''''''''''''''''''''''''''''''''''''''''''''''''''''
- The documentation is updated to restructured text
- Documentation hosted
- Next the unit tests will be added
- The documentation updated to restructured text (DONE)
- Documentation hosted (DONE)
- Next the unit tests added (DONE)
- Upcoming: Tutorials will be added
- Upcoming: Auto encoders will be added
- Upcoming: MDP integration will be added
Expand Down
86 changes: 86 additions & 0 deletions pydeep/examples/eigenfaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
""" Example for Eigenfaces (Principal component analysis on face images).
:Version:
1.1.0
:Date:
08.04.2017
:Author:
Jan Melchior
:Contact:
JanMelchior@gmx.de
:License:
Copyright (C) 2017 Jan Melchior
This file is part of the Python library PyDeep.
PyDeep is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
# Import PCA, numpy, input output functions, and visualization functions
import numpy as numx
from pydeep.preprocessing import PCA
import pydeep.misc.io as io
import pydeep.misc.visualization as vis

# Set the random seed (optional, if stochastic processes are involved we always get the same results)
numx.random.seed(42)

# Load the data
data = io.load_olivetti_faces(path='../../../data/olivettifaces.mat')

# Specify image width and height for displaying
width = height = 64

# Create a PCA node and train it
pca = PCA(input_dim=width * height)
pca.train(data=data)

# Show the first 100 eigenvectors of the covariance matrix
eigenvectors = vis.tile_matrix_rows(matrix=pca.projection_matrix,
tile_width=width,
tile_height=height,
num_tiles_x=10,
num_tiles_y=10,
border_size=1,
normalized=True)
vis.imshow_matrix(matrix=eigenvectors,
windowtitle='First 100 Eigenvectors of the covariance matrix')

# Show the first 100 images
images = vis.tile_matrix_rows(matrix=data[0:100].T,
tile_width=width,
tile_height=height,
num_tiles_x=10,
num_tiles_y=10,
border_size=1,
normalized=True)
vis.imshow_matrix(matrix=images,
windowtitle='First 100 Face images')

# Plot the cumulative sum of teh Eigenvalues.
eigenvalue_sum = numx.cumsum(pca.eigen_values / numx.sum(pca.eigen_values))
vis.imshow_plot(matrix=eigenvalue_sum,
windowtitle="Cumulative sum of Eigenvalues")
vis.xlabel("Eigenvalue index")
vis.ylabel("Sum of Eigenvalues 0 to index")
vis.ylim(0, 1)
vis.xlim(0, 400)

# Show all windows.
vis.show()
117 changes: 117 additions & 0 deletions pydeep/examples/ica_on_natural_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
""" Example for the Independent component analysis on natural image patches.
:Version:
1.1.0
:Date:
08.04.2017
:Author:
Jan Melchior
:Contact:
JanMelchior@gmx.de
:License:
Copyright (C) 2017 Jan Melchior
This file is part of the Python library PyDeep.
PyDeep is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
# Import PCA, numpy, input output functions, and visualization functions
import numpy as numx
from pydeep.preprocessing import ICA, ZCA
import pydeep.misc.io as io
import pydeep.misc.visualization as vis

# Set the random seed (optional, if stochastic processes are involved we always get the same results)
numx.random.seed(42)

# Load the data
data = io.load_natural_image_patches('../../../data/NaturalImage.mat')

# Specify image width and height for displaying
width = height = 14

# Create a ZCA node to whiten the data and train it (you could also use PCA whitened=True)
zca = ZCA(input_dim=width * height)
zca.train(data=data)

# ZCA projects the whitened data back to the original space, thus does not perform a
# dimensionality reduction but a whitening in the original space
whitened_data = zca.project(data)

# Create a ZCA node and train it (you could also use PCA whitened=True)
ica = ICA(input_dim=width * height)
ica.train(data=whitened_data,
iterations=100,
convergence=1.0,
status=True)

# Show eigenvectors of the covariance matrix
eigenvectors = vis.tile_matrix_rows(matrix=zca.projection_matrix,
tile_width=width,
tile_height=height,
num_tiles_x=width,
num_tiles_y=height,
border_size=1,
normalized=True)
vis.imshow_matrix(matrix=eigenvectors,
windowtitle='Eigenvectors of the covariance matrix')

# Show whitened images
images = vis.tile_matrix_rows(matrix=data[0:width*height].T,
tile_width=width,
tile_height=height,
num_tiles_x=width,
num_tiles_y=height,
border_size=1,
normalized=True)
vis.imshow_matrix(matrix=images,
windowtitle='Some image patches')

# Show some whitened images
images = vis.tile_matrix_rows(matrix=whitened_data[0:width*height].T,
tile_width=width,
tile_height=height,
num_tiles_x=width,
num_tiles_y=height,
border_size=1,
normalized=True)
vis.imshow_matrix(matrix=images,
windowtitle='Some whitened image patches')

# Plot the cumulative sum of teh Eigenvalues.
eigenvalue_sum = numx.cumsum(zca.eigen_values)
vis.imshow_plot(matrix=eigenvalue_sum,
windowtitle="Cumulative sum of Eigenvalues")
vis.xlabel("Eigenvalue index")
vis.ylabel("Sum of Eigenvalues 0 to index")

# Show the ICA filters/bases
ica_filters = vis.tile_matrix_rows(matrix=ica.projection_matrix,
tile_width=width,
tile_height=height,
num_tiles_x=width,
num_tiles_y=height,
border_size=1,
normalized=True)
vis.imshow_matrix(matrix=ica_filters,
windowtitle='Filters learned by ICA')

# Show all windows.
vis.show()
43 changes: 41 additions & 2 deletions pydeep/misc/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
- Save/Load arbitrary objects.
- Save/Load images.
- Load MNIST.
- Load CIFAR.
- Load Caltech.
- Load CIFAR.
- load Natural image patches
- load olivetti face images
:Version:
1.1.0
Expand Down Expand Up @@ -344,9 +346,46 @@ def load_natural_image_patches(path):
print('-> loading data ... ')
try:
# https://zenodo.org/record/167823/files/NaturalImage.mat
data = numx.random.permutation(scipy.io.loadmat(path)['rawImages'].T)
data = scipy.io.loadmat(path)['rawImages'].T
print('-> done!')
except:
raise Exception('-> File reading Error: ')
data = numx.array(data, dtype=numx.double)
return data

def load_olivetti_faces(path, correct_orientation=True):
""" Loads the Olivetti face dataset 400 images, size 64x64
:param path: Path and name of the file to load.
:type path: string
:param correct_orientation: Corrects the orientation of the images.
:type correct_orientation: bool
:return: Olivetti face dataset
:rtype: numpy array
"""
if not os.path.isfile(path):
print('-> File not existing: ' + path)
try:
download_file('http://www.cs.nyu.edu/~roweis/data/olivettifaces.mat', path, buffer_size=10 * 1024 ** 2)
except:
try:
download_file('https://github.com/probml/pmtk3/tree/master/bigData/facesOlivetti/facesOlivetti.mat',
path, buffer_size=10 * 1024 ** 2)
except:
raise Exception('Download failed, make sure you have internet connection!')
print('-> loading data ... ')
try:
data = scipy.io.loadmat(path)['faces'].T
if correct_orientation:
import pydeep.base.numpyextension as npext
for i in range(data.shape[0]):
data[i] = npext.rotate(data[i].reshape(64,64),270).reshape(64*64)
print('-> orientation corrected!')
print('-> done!')
except:
raise Exception('-> File reading Error: ')
data = numx.array(data, dtype=numx.double)
return data

15 changes: 15 additions & 0 deletions pydeep/misc/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Tile a matrix rows
- Tile a matrix columns
- Show a matrix
- Show plot
- Show a histogram
- Plot data
Expand Down Expand Up @@ -171,6 +172,20 @@ def imshow_matrix(matrix, windowtitle, interpolation='nearest'):
imshow(np.array(matrix, np.float64), interpolation=interpolation)


def imshow_plot(matrix, windowtitle):
""" Plots the colums of a matrix.
:param matrix: Data to plot
:type matrix: numpy array
:param windowtitle: Figure title
:type windowtitle: string
"""
figure().suptitle(windowtitle)
gray()
plot(np.array(matrix, np.float64))


def imshow_histogram(matrix,
windowtitle,
num_bins=10,
Expand Down
4 changes: 2 additions & 2 deletions pydeep/rbm/dbn.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def forward_propagate(self, input_data, sample=False):
:return: Output of the network.
:rtype: numpy array [batchsize x output dim]
"""
if input_data.shape[1] != self._input_dim:
if input_data.shape[1] != self.input_dim:
raise Exception("Input dimensionality has to match dbn.input_dim!")
self.states[0] = input_data
for l in range(len(self._layers)):
Expand All @@ -80,7 +80,7 @@ def backward_propagate(self, output_data, sample=False):
:return: Input of the network.
:rtype: numpy array [batchsize x input dim]
"""
if output_data.shape[1] != self._output_dim:
if output_data.shape[1] != self.output_dim:
raise Exception("Output dimensionality has to match dbn.output_dim!")
self.states[len(self._layers)] = output_data
for l in range(len(self._layers), 0, -1):
Expand Down

0 comments on commit fbcc37c

Please sign in to comment.