Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Guasanization #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 54 additions & 32 deletions gaussianisation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File containing classes for Gaussianisation
#File containing classes for Gaussianisation

from typing import Tuple, List
import numpy as np
Expand All @@ -12,7 +12,6 @@

class HistogramGaussianisation:
"""A class for Gaussianisation via histograms.

Attributes
----------
transformations : List[object]
Expand All @@ -27,11 +26,9 @@ def __init__(self):

def fit(self,
images : np.ndarray,
n_samples : int = 10000) -> np.ndarray:
n_samples : int = int(1e6)) -> np.ndarray:
"""Fits the transformations to the data.

Fits the marginal Gaussianisation transform to the data.

Parameters
----------
images : numpy.ndarray
Expand All @@ -44,24 +41,24 @@ def fit(self,
Z_G : numpy.ndarray
The marginally Gaussianised data.
"""
self.im_shape = images.shape
n_samples = int(np.min([n_samples,np.prod(images.shape[0:3])]))
aux = np.reshape(images,(np.prod(images.shape[0:-1]),images.shape[3]))
self.Z = np.random.permutation(aux)[0:n_samples,:]
del images
Z = np.random.permutation(aux)[0:n_samples,:]

Z_G, self.transformations, self.log_pZ_1, self.MI_1 = \
self.marginal_gaussianization(self.Z)
self.marginal_gaussianization(Z)

self.im_shape = images.shape

return Z_G

def predict(self,
images : np.ndarray,
batch : int = 100):
"""Performs marginal Gaussianisation on the data.

Marginally Gaussianises the data according to the trained
transformations.

Parameters
----------
images : numpy.ndarray
Expand All @@ -82,25 +79,31 @@ def predict(self,
Nim = aux.shape[0]

BATCH_loop = self.im_shape[1]*self.im_shape[2]*batch
images_G = np.zeros(images.shape)
images_G = np.zeros(images.shape, dtype=np.float32)
self.val_log_pZ = np.zeros(images.shape[0], dtype=np.float32)

cada = 0
for ii in range(0,Nim,BATCH_loop):
Z_U2 = self.transformations[0].forward(aux[ii:ii+BATCH_loop,:])
log_pZ_aux = self.transformations[0].gradient(aux[ii:ii+BATCH_loop,:])
Z_G2 = self.transformations[1].forward(Z_U2)
log_pZ_aux += self.transformations[1].gradient(Z_U2)
images_G[cada:cada+batch,:,:,:] = np.reshape(
Z_G2,(batch,*images.shape[1:]))
Z_G2,(batch,*images.shape[1:]))
log_pZ_aux = np.reshape(log_pZ_aux,(batch,*images.shape[1:-1]))
self.val_log_pZ[cada:cada+batch] = np.mean(log_pZ_aux,axis=(1,2))
cada = cada+batch

self.val_MI = information_reduction(aux,np.reshape(images_G,aux.shape))

return images_G

def inverse(self,
images : np.ndarray,
batch : int = 100):
batch : int = 1000000,
synthesis_flag: int = 0):
"""Applies inverse transform.

Applies the inverse transform according to the learned transformations.

Parameters
----------
images : numpy.ndarray
Expand All @@ -113,34 +116,53 @@ def inverse(self,
images_I : numpy.ndarray
The images with the inverse transform applied.
"""
#
aux = np.reshape(
images,
(images.shape[0]*np.prod(self.im_shape[1:-1]),self.im_shape[3]))
images.copy(),
(np.prod(images.shape[0:-1]),images.shape[3]))

# To apply
Nim = aux.shape[0]

BATCH_loop = self.im_shape[1]*self.im_shape[2]*batch
images_I = np.zeros(images.shape)
Nim = images.shape[0]

but = np.mod(Nim,batch)
Nim_in_loop = Nim-but

BATCH_loop = np.prod(images.shape[1:3])*batch
ALL_samples_loop = np.prod(images.shape[1:3])*Nim_in_loop

images_I = np.zeros(images.shape, dtype=np.float32)

# samples in the main loop
cada = 0
for ii in range(0,Nim,BATCH_loop):
Z_Ui = self.transformations[1].inverse(aux[ii:ii+BATCH_loop,:])
for ii in range(0,ALL_samples_loop,BATCH_loop):
# for synthesis
if synthesis_flag == 1:
aux[ii:(ii+BATCH_loop),:],_,_,_ = self.marginal_gaussianization(aux[ii:(ii+BATCH_loop),:])

Z_Ui = self.transformations[1].inverse(aux[ii:(ii+BATCH_loop),:])
Z_i = self.transformations[0].inverse(Z_Ui)
images_I[cada:cada+batch,:,:,:] = np.reshape(
Z_i,(batch,*images.shape[1:]))
images_I[cada:cada+batch,:,:,:] = np.reshape(Z_i,(batch,*images.shape[1:]))
cada = cada+batch


# left samples

# for synthesis
if synthesis_flag == 1:
#aux[ALL_samples_loop:ALL_samples_loop+np.prod(images.shape[1:3])*but,:],_,_,_ = self.marginal_gaussianization(aux[ALL_samples_loop:ALL_samples_loop+np.prod(images.shape[1:3])*but,:])
aux[ALL_samples_loop:,:],_,_,_ = self.marginal_gaussianization(aux[ALL_samples_loop:,:])

Z_Ui = self.transformations[1].inverse(aux[ALL_samples_loop:,:])
Z_i = self.transformations[0].inverse(Z_Ui)
images_I[cada:,:,:,:] = np.reshape(Z_i,(but,*images.shape[1:]))

return images_I

def marginal_gaussianization(self,
Z : np.ndarray
) -> Tuple[np.ndarray, List[object], np.ndarray, float]:
"""Gets the marginal Gaussianisation transformations.

Gets the marginal Gaussianisation transformations using the functions
from the RBIG package https://github.com/IPL-UV/rbig/

Parameters
----------
Z : numpy.ndarray
Expand All @@ -162,19 +184,19 @@ def marginal_gaussianization(self,
bins = "auto"
alpha = 1e-10
bound_ext = 0.3
eps = 1e-10
eps = 1e-5

ibijector = MarginalHistogramUniformization(Z, bound_ext=bound_ext, bins=bins, alpha=alpha)
Z_U = ibijector.forward(Z)
Z_U = ibijector.forward(Z).astype(np.float32)
log_pZ = ibijector.gradient(Z)
transformations.append(ibijector)

# Inverse Gauss CDF
ibijector = InverseGaussCDF(eps=eps)
Z_G = ibijector.forward(Z_U)
Z_G = ibijector.forward(Z_U).astype(np.float32)
log_pZ += ibijector.gradient(Z_U)
transformations.append(ibijector)

MI = information_reduction(Z_G,Z)
MI = information_reduction(Z,Z_G)

return Z_G, transformations, log_pZ, MI