Пример #1
0
def load_dynamic_modules():
    dir_path = get_dir(__file__)
    transformer_module = tf.load_op_library(dir_path + '/./CPAB_ops.so')
    transformer_op = transformer_module.calc_trans
    grad_op = transformer_module.calc_grad

    return transformer_op, grad_op
Пример #2
0
def get_mnist():
    """ Downloads mnist from internet """
    url = "https://s3.amazonaws.com/img-datasets/mnist.npz"
    direc = get_dir(__file__)
    file_name = url.split('/')[-1]
    if not os.path.isfile(direc + '/' + file_name):
        print('Downloading the mnist dataset (11.5MB)')
        urllib.request.urlretrieve(url, direc + '/' + file_name)

    data = np.load(direc + '/' + file_name)
    X_train, y_train = data['x_train'], data['y_train']
    X_test, y_test = data['x_test'], data['y_test']
    X_train = np.reshape(X_train, (X_train.shape[0], 28, 28, 1))
    X_test = np.reshape(X_test, (X_test.shape[0], 28, 28, 1))
    return X_train, y_train, X_test, y_test
Пример #3
0
def get_mnist_distorted():
    """ Downloads distorted mnist from internet """
    url = "https://s3.amazonaws.com/lasagne/recipes/datasets/mnist_cluttered_60x60_6distortions.npz"
    direc = get_dir(__file__)
    file_name = url.split('/')[-1]
    if not os.path.isfile(direc + '/' + file_name):
        print('Downloading the distorted mnist dataset (43MB)')
        urllib.request.urlretrieve(url, direc + '/' + file_name)

    data = np.load(direc + '/' + file_name)
    X_train, y_train = data['x_train'], data['y_train']
    X_test, y_test = data['x_test'], data['y_test']
    X_train = np.reshape(X_train, (X_train.shape[0], 60, 60, 1))
    X_test = np.reshape(X_test, (X_test.shape[0], 60, 60, 1))
    return X_train, y_train, X_test, y_test
Пример #4
0
 def __init__(self,   ncx = 2, 
                      ncy = 2, 
                      valid_outside = True,
                      zero_trace = False, 
                      zero_boundary = False,
                      name = 'cpab_basis',
                      override = False):
     """
     Main class for setting up cpab_transformer object. The main purpose of
     calling this class is to produce a file "cbap_basis.pkl" that contains
     all information needed for the transformation.
     
     Arguments:
         ncx:            number of rectangular cells in x direction
         ncy:            number of rectangular cells in y direction
         valid_outside:  boolean, determines if transformation is valid
                         outside the image region
         zero_trace:     boolean, if true the transformation is area 
                         preserving <--> each affine transformation have
                         zero trace
         zero_boundary:  boolean, if true the velocity at the image boundary
                         is constrained to be zero. NOTE: zero_boundary and
                         valid_outside cannot both be True or False at the
                         same time
         name:           str, name for the created bases file. Default is
                         'cpab_basis', but can be used to create multiple
                         basis files for easy switch between them
         override:       if True, then a new basis will be saved to 
                         'cbap_basis.pkl' even if it already exists
     """
     
     # We cannot have zero boundary and valid_outside at the same time
     assert valid_outside != zero_boundary, '''valid_outside and zero_boundary
         cannot both be active or deactive at the same time, CHOOSE'''
     
     # Domain information
     self.valid_outside = valid_outside
     self.zero_trace = zero_trace
     self.zero_boundary = zero_boundary
     self.minbound = [-1, -1]
     self.maxbound = [1, 1]
     self.ncx = ncx
     self.ncy = ncy
     self.nC = 4*ncx*ncy
     self.inc_x = (self.maxbound[0] - self.minbound[0]) / self.ncx
     self.inc_y = (self.maxbound[1] - self.minbound[1]) / self.ncy
     self.Ashape = [2,3]
     self.Asize = np.prod(self.Ashape)
     dir_loc = get_dir(__file__)
     self.filename = dir_loc + '/../' + name
     
     # Try to load file with basis and vertices
     try:    
         file = load_obj(self.filename)
         if override:
             raise print('File ' + name + '.pkl already exist, ' \
                         'but override == True, ' \
                         'so updating basis with new settings')
         # File found -> load information
         self.valid_outside = file['valid_outside']
         self.zero_trace = file['zero_trace']
         self.zero_boundary = file['zero_boundary']
         self.B = file['B']
         self.nConstrains = file['nConstrains']
         self.cells_multiidx = file['cells_multiidx']
         self.cells_verts = file['cells_verts']
         self.ncx = file['ncx']
         self.ncy = file['ncy']
         self.nC = 4*self.ncx*self.ncy
         self.inc_x = (self.maxbound[0] - self.minbound[0]) / self.ncx
         self.inc_y = (self.maxbound[1] - self.minbound[1]) / self.ncy
         loaded = True
     except: # Else create it
         # Call tessalation and get vertices of cells
         self.cells_multiidx, self.cells_verts  = self.tessalation()
         
         # Find shared vertices (edges) where a continuity constrain needs to hold
         self.shared_v, self.shared_v_idx = self.find_shared_verts()
         
         # If the transformation should be valid outside of the image domain, 
         # calculate the auxiliary points and add them to the edges where a 
         # continuity constrain should be
         if self.valid_outside:
             shared_v_outside, shared_v_idx_outside = self.find_shared_verts_outside()
             if shared_v_outside.size != 0:
                 self.shared_v = np.concatenate((self.shared_v, shared_v_outside))
                 self.shared_v_idx = np.concatenate((self.shared_v_idx, shared_v_idx_outside))
         
         # Create L
         L = self.create_continuity_constrains()
         
         # Update L with extra constrains if needed
         if self.zero_trace:
             Ltemp = self.create_zero_trace_constrains()
             L = np.vstack((L, Ltemp))
         
         if self.zero_boundary:
             Ltemp = self.create_zero_boundary_constrains()
             L = np.vstack((L, Ltemp))
         
         # Number of constrains
         self.nConstrains = L.shape[0]
         
         # Find the null space of L, which is the basis B
         self.B = null(L)
         
         # Save all information
         save_obj({
                   'B': self.B,
                   'D': self.B.shape[0],
                   'd': self.B.shape[1],
                   'nConstrains': self.nConstrains, 
                   'cells_multiidx': self.cells_multiidx,
                   'cells_verts': self.cells_verts,
                   'nC': self.nC,
                   'ncx': self.ncx,
                   'ncy': self.ncy,
                   'inc_x': self.inc_x,
                   'inc_y': self.inc_y,
                   'minbound': self.minbound, 
                   'maxbound': self.maxbound,
                   'valid_outside': self.valid_outside,
                   'zero_trace': self.zero_trace,
                   'zero_boundary': self.zero_boundary
                  }, self.filename)
         loaded = False
     
     # Get shapes of PA space and CPA space
     self.D, self.d = self.B.shape
     
     # Print information about basis
     print(70*'-')
     if loaded:
         print('Loaded file ' + name + '.pkl, ' \
               'containing tessalation with settings:')
     else:
         print('Creating file ' + name +'.pkl, ' \
               'containing tessalation with settings:')
     print('    nx = {0}, ny = {1}'.format(self.ncx, self.ncy))
     print('    valid outside     = {0}'.format(self.valid_outside))
     print('    zero boundary     = {0}'.format(self.zero_boundary))
     print('    volume preserving = {0}'.format(self.zero_trace))
     print('With these settings, theta.shape = {0}x1'.format(self.B.shape[1]))
     print(70*'-')