def fit(self, images): """ Train PCANet Parameters ---------- images: np.ndarray | Color / grayscale images of shape | (n_images, height, width, n_channels) or | (n_images, height, width) """ images = self.process_input(images) # images.shape == (n_images, n_channels, y, x) for image in images: X = [] for channel in image: patches = image_to_patch_vectors(channel, self.filter_shape_l1, self.step_shape_l1) X.append(patches) patches = np.hstack(X) #print("the size of patches is ") #print(patches.shape) # patches.shape = (n_patches, n_patches * vector length) self.pca_l1.partial_fit(patches) filters_l1 = components_to_filters( self.pca_l1.components_, n_channels=images.shape[1], filter_shape=self.filter_shape_l1, ) print("Filter size in layer1") print(filters_l1) if gpu_enabled(): images = to_gpu(images) filters_l1 = to_gpu(filters_l1) images = convolution_2d(images, filters_l1, stride=self.step_shape_l1).data print("Convolution output after the 1st stage") print(images.shape) if gpu_enabled(): images = to_cpu(images) filters_l1 = to_cpu(filters_l1) # images.shape == (n_images, L1, y, x) images = images.reshape(-1, *images.shape[2:4]) print("After Reshape") print(images.shape) for image in images: patches = image_to_patch_vectors(image, self.filter_shape_l2, self.step_shape_l2) #print("Patches size in 2nd layer") #print(patches.shape) self.pca_l2.partial_fit(patches) return self
def fit(self, images): """ Train PCANet Parameters ---------- images: np.ndarray | Color / grayscale images of shape | (n_images, height, width, n_channels) or | (n_images, height, width) """ images = self.process_input(images) # images.shape == (n_images, n_channels, y, x) for image in images: X = [] #print("image shape in fit's for loop:",image.shape) for channel in image: patches = image_to_patch_vectors(channel, self.filter_shape_l1, self.step_shape_l1) X.append(patches) patches = np.hstack(X) # patches.shape = (n_patches, n_patches * vector length) self.pca_l1.partial_fit(patches) filters_l1 = components_to_filters( self.pca_l1.components_, n_channels=images.shape[1], filter_shape=self.filter_shape_l1, ) # images = images.astype(float) if gpu_enabled(): images = to_gpu(images) filters_l1 = to_gpu(filters_l1) images = convolution_2d(images, filters_l1, stride=self.step_shape_l1).data if gpu_enabled(): images = to_cpu(images) filters_l1 = to_cpu(filters_l1) # images.shape == (n_images, L1, y, x) images = images.reshape(-1, *images.shape[2:4]) for image in images: patches = image_to_patch_vectors(image, self.filter_shape_l2, self.step_shape_l2) self.pca_l2.partial_fit(patches) return self
def transform(self, images): """ Parameters ---------- images: np.ndarray | Color / grayscale images of shape | (n_images, height, width, n_channels) or | (n_images, height, width) Returns ------- X: np.ndarray A set of feature vectors of shape (n_images, n_features) where :code:`n_features` is determined by the hyperparameters """ print("Shape of the data passed to transform") print(images.shape) images = self.process_input(images) # images.shape == (n_images, n_channels, y, x) filters_l1 = components_to_filters( self.pca_l1.components_, n_channels=images.shape[1], filter_shape=self.filter_shape_l1, ) filters_l2 = components_to_filters(self.pca_l2.components_, n_channels=1, filter_shape=self.filter_shape_l2) print("Filters in layer 2") print(filters_l2) if gpu_enabled(): images = to_gpu(images) filters_l1 = to_gpu(filters_l1) filters_l2 = to_gpu(filters_l2) images = convolution_2d(images, filters_l1, stride=self.step_shape_l1).data images = xp.swapaxes(images, 0, 1) # L1.shape == (L1, n_images, y, x) # iterate over each L1 output X = [] for maps in images: n_images, h, w = maps.shape maps = convolution_2d( maps.reshape(n_images, 1, h, w), # 1 channel images filters_l2, stride=self.step_shape_l2).data # maps.shape == (n_images, L2, y, x) right here maps = binarize(maps) maps = binary_to_decimal(maps) # maps.shape == (n_images, y, x) x = self.histogram(maps) # x is a set of feature vectors. # The shape of x is (n_images, vector length) X.append(x) # concatenate over L1 X = xp.hstack(X) if gpu_enabled(): X = to_cpu(X) X = X.astype(np.float64) # The shape of X is (n_images, L1 * vector length) return X
# [the original paper](https://arxiv.org/abs/1404.3606) import itertools from chainer.cuda import to_gpu, to_cpu from chainer.functions import convolution_2d import numpy as np from sklearn.decomposition import IncrementalPCA from utils import gpu_enabled if gpu_enabled(): try: import cupy as xp except ImportError: import numpy as xp else: import numpy as xp def steps(image_shape, filter_shape, step_shape): """ Generates feature map coordinates that filters visit Parameters ---------- image_shape: tuple of ints Image height / width filter_shape: tuple of ints Filter height / width
def transform(self, images): """ Parameters ---------- image: np.ndarray | Color / grayscale image of shape | (height, width, n_channels) or | (height, width) Returns ------- X: np.ndarray A set of feature vectors of shape (n_images, n_features) where :code:`n_features` is determined by the hyperparameters NB: It is necessary to call the fit function first, to set the filters. """ #images.shape == (1, h, w[, n_channels]) images = self.process_input(images) #Now images.shape == (1, n_channels=1, y, x) #Retrieve the layer 1 filters filters_l1 = pcanet.components_to_filters( self.pca_l1.components_, n_channels=images.shape[1], filter_shape=self.filter_shape_l1, ) #Retrieve the layer 2 filters filters_l2 = pcanet.components_to_filters( self.pca_l2.components_, n_channels=1, filter_shape=self.filter_shape_l2) if gpu_enabled(): images = to_gpu(images) filters_l1 = to_gpu(filters_l1) filters_l2 = to_gpu(filters_l2) #Apply the layer 1 filters l1output = convolution_2d(images, filters_l1, stride=self.step_shape_l1, pad=1).data #The l1 output has shape (n_images, L1, y, x), swap axes l1output = xp.swapaxes(l1output, 0, 1) # Now l1output.shape == (L1, n_images, y, x) # iterate over each L1 output, applying L2 filters l2output = [] for maps in l1output: n_images, h, w = maps.shape l2maps = convolution_2d( maps.reshape(n_images, 1, h, w), # 1 channel images filters_l2, stride=self.step_shape_l2, pad=1).data # l2maps.shape == (n_images, L2, y, x) # Apply sigmoid and sum over the L2 channels Z = np.zeros((n_images, *l2maps.shape[2:])) for chan in range(l2maps.shape[1]): Z += pow(2, 8 - chan) * ( expit(self.alpha * np.abs(l2maps[:, chan, ...])) - self.S0) l2output.append(Z) #JCG comment: l2output contains L1 elements of shape (n_images, h, w) if gpu_enabled(): l2output = to_cpu(l2output) return images, l1output, l2output
def transform(self, images): """ Parameters ---------- images: np.ndarray | Color / grayscale images of shape | (n_images, height, width, n_channels) or | (n_images, height, width) Returns ------- X: np.ndarray A set of feature vectors of shape (n_images, n_features) where :code:`n_features` is determined by the hyperparameters """ images = self.process_input(images) # images.shape == (n_images, n_channels, y, x) N = images.shape[0] filters_l1 = components_to_filters( self.pca_l1.components_, n_channels=images.shape[1], filter_shape=self.filter_shape_l1, ) filters_l2 = components_to_filters(self.pca_l2.components_, n_channels=1, filter_shape=self.filter_shape_l2) filters_l3 = components_to_filters(self.pca_l3.components_, n_channels=1, filter_shape=self.filter_shape_l3) if gpu_enabled(): images = to_gpu(images) filters_l1 = to_gpu(filters_l1) filters_l2 = to_gpu(filters_l2) filters_l3 = to_gpu(filters_l3) images = convolution_2d(images.astype('float32'), filters_l1, stride=self.step_shape_l1).data print("Memory: {}Mb".format(memory_profiler.memory_usage())) images = images.reshape(-1, *images.shape[2:4]) images = convolution_2d( images.reshape( images.shape[0], 1, images.shape[1], images.shape[2]).astype('float32'), # 1 channel images filters_l2, stride=self.step_shape_l2).data print("Memory: {}Mb".format(memory_profiler.memory_usage())) images = images.reshape(N, self.n_l1_output * self.n_l2_output, images.shape[2], images.shape[3]) images = xp.swapaxes(images, 0, 1) print(images.shape) # L1*L2.shape == (L1*L2, n_images, y, x) # iterate over each L1*L2 output X = [] for maps in images: n_images, h, w = maps.shape #print(type(maps)) #print(type(filters_l3)) maps = convolution_2d( maps.reshape(n_images, 1, h, w).astype('float32'), # 1 channel images filters_l3, stride=self.step_shape_l3).data # maps.shape == (n_images, L1*L2*L3, y, x) right here maps = binarize(maps) maps = binary_to_decimal(maps) # maps.shape == (n_images, y, x) t1 = timeit.default_timer() x = self.histogram(maps) t2 = timeit.default_timer() hist_time = t2 - t1 print("hist_time: %f" % hist_time) # x is a set of feature vectors. # The shape of x is (n_images, vector length) X.append(x) # concatenate over L1 X = xp.hstack(X) if gpu_enabled(): X = to_cpu(X) X = X.astype(np.float32) print("Memory: {}Mb".format(memory_profiler.memory_usage())) # The shape of X is (n_images, L1 * vector length) return X
def fit(self, images): """ Train PCANet Parameters ---------- images: np.ndarray | Color / grayscale images of shape | (n_images, height, width, n_channels) or | (n_images, height, width) """ images_trans = images print(images_trans.shape) images = self.process_input(images) # images.shape == (n_images, n_channels, y, x) for image in images: X = [] for channel in image: patches = image_to_patch_vectors(channel, self.filter_shape_l1, self.step_shape_l1) X.append(patches) patches = np.hstack(X) # patches.shape = (n_patches, n_patches * vector length) self.pca_l1.partial_fit(patches) filters_l1 = components_to_filters( self.pca_l1.components_, n_channels=images.shape[1], filter_shape=self.filter_shape_l1, ) if gpu_enabled(): images = to_gpu(images) filters_l1 = to_gpu(filters_l1) images_l1 = convolution_2d(images.astype('float64'), filters_l1, stride=self.step_shape_l1).data if gpu_enabled(): images_l1 = to_cpu(images_l1) filters_l1 = to_cpu(filters_l1) # images.shape == (n_images, L1, y, x) images_l1 = images_l1.reshape(-1, *images_l1.shape[2:4]) images_trans = xp.resize( images_trans, (images_trans.shape[0], images_l1.shape[1], images_l1.shape[2], 1)) images_trans = images_trans.reshape(-1, *images_trans.shape[1:3]) print(images_trans.shape) print(images_l1.shape) print(type(images_trans)) print(type(images_l1)) images = xp.vstack((images_l1, images_trans)) for image in images: patches = image_to_patch_vectors(image, self.filter_shape_l2, self.step_shape_l2) self.pca_l2.partial_fit(patches) filters_l2 = components_to_filters( self.pca_l2.components_, n_channels=1, filter_shape=self.filter_shape_l1, ) if gpu_enabled(): images = to_gpu(images) filters_l2 = to_gpu(filters_l2) print("#######") print(images.shape) images_l2 = convolution_2d( images.reshape(images.shape[0], 1, images.shape[1], images.shape[2]).astype('float64'), filters_l2, stride=self.step_shape_l2).data if gpu_enabled(): images_l2 = to_cpu(images_l2) filters_l2 = to_cpu(filters_l2) images_l2 = images_l2.reshape(-1, *images_l2.shape[2:4]) images_l1_trans = xp.resize( images_l1, (images_l1.shape[0], images_l2.shape[1], images_l2.shape[2])) images = xp.vstack((images_l2, images_l1_trans)) print("################") for image in images: patches = image_to_patch_vectors(image, self.filter_shape_l3, self.step_shape_l3) self.pca_l3.partial_fit(patches) return self
def transform(self, images): """ Parameters ---------- images: np.ndarray | Color / grayscale images of shape | (n_images, height, width, n_channels) or | (n_images, height, width) Returns ------- X: np.ndarray A set of feature vectors of shape (n_images, n_features) where :code:`n_features` is determined by the hyperparameters """ images = self.process_input(images) # images.shape == (n_images, n_channels, y, x) filters_l1 = components_to_filters( self.pca_l1.components_, n_channels=images.shape[1], filter_shape=self.filter_shape_l1, ) filters_l2 = components_to_filters( self.pca_l2.components_, n_channels=1, filter_shape=self.filter_shape_l2 ) if gpu_enabled(): images = to_gpu(images) filters_l1 = to_gpu(filters_l1) filters_l2 = to_gpu(filters_l2) images = convolution_2d( images, filters_l1, stride=self.step_shape_l1 ).data images = xp.swapaxes(images, 0, 1) # L1.shape == (L1, n_images, y, x) # iterate over each L1 output X = [] for maps in images: n_images, h, w = maps.shape maps = convolution_2d( maps.reshape(n_images, 1, h, w), # 1 channel images filters_l2, stride=self.step_shape_l2 ).data # maps.shape == (n_images, L2, y, x) right here maps = binarize(maps) maps = binary_to_decimal(maps) # maps.shape == (n_images, y, x) x = self.histogram(maps) # x is a set of feature vectors. # The shape of x is (n_images, vector length) #JCG comment: histogram length is pow(2, L2), i.e. the range #of integers that can be represented by the binarized outputs of #L2 filters so we normally have shape (n_images, 256) X.append(x) #JCG comment: X contains L1 elements of shape (n_images, 256) # concatenate over L1 X = xp.hstack(X) #JCG comment: now X contains n_images elements of shape (L1*256) if gpu_enabled(): X = to_cpu(X) X = X.astype(np.float64) # The shape of X is (n_images, L1 * vector length) return X
def fit(self, images): """ Train PCANet Parameters ---------- images: np.ndarray | Color / grayscale images of shape | (n_images, height, width, n_channels) or | (n_images, height, width) """ images = self.process_input(images) # images.shape == (n_images, n_channels, y, x) for image in images: X = [] for channel in image: patches = image_to_patch_vectors( channel, self.filter_shape_l1, self.step_shape_l1 ) X.append(patches) #JCG comment: combine patches for each channel into single patch per pixel patches = np.hstack(X) # patches.shape = (n_patches, n_patches * vector length) #JCG comment: should read patches.shape = (n_patches, n_channels * vector length)? TODO: CHECK self.pca_l1.partial_fit(patches) filters_l1 = components_to_filters( self.pca_l1.components_, n_channels=images.shape[1], filter_shape=self.filter_shape_l1, ) if gpu_enabled(): images = to_gpu(images) filters_l1 = to_gpu(filters_l1) images = convolution_2d( images, filters_l1, stride=self.step_shape_l1 ).data if gpu_enabled(): images = to_cpu(images) filters_l1 = to_cpu(filters_l1) # images.shape == (n_images, L1, y, x) images = images.reshape(-1, *images.shape[2:4]) for image in images: patches = image_to_patch_vectors( image, self.filter_shape_l2, self.step_shape_l2 ) self.pca_l2.partial_fit(patches) return self