コード例 #1
0
def split_preprocess_Data(degree1, degree2, strength1, strength2, percentage =1):     
    #   1. load the data
    
    x_original_train, train_target, x_original_test, val_target = assign2_utils.load_dataset()
    #train_pairs, val_pairs = transform_dataset(x_original_train, x_original_test, 30, 0.15)
    # 2. split the datset.
    x_original_train = x_original_train.astype('float32')
    x_original_test = x_original_test.astype('float32')
    
    normaliseValue = 255
    x_original_train /= normaliseValue # normalized the entries between 0 and 1
    x_original_test /= normaliseValue
    
    x_original_train1, x_original_train2, train_target1, train_target2  = split_Data(x_original_train,train_target, percentage)
    #x_original_test1, x_original_test2 , val_target1, val_target2  = split_Data(x_original_test,val_target, percentage)

    # 3. transform the dataset.
    warped_pairs1 = np.array([assign2_utils.random_deform(x, degree1, strength1) for x in x_original_train1])
    warped_pairs2 = np.array([assign2_utils.random_deform(x, degree2, strength2) for x in x_original_train2])
    # train_pairs2 = np.array([assign2_utils.random_deform(x, degree2, strength2) for x in x_original_train2]
    

    digit_indicesTrain1 = [np.where(train_target[train_target1] == i)[0] for i in range(NumberOfClass)]
    train_pairs1, train_y1 = create_pairs(warped_pairs1, digit_indicesTrain1, percentage)
    digit_indicesTrain2 = [np.where(train_target[train_target2] == i)[0] for i in range(NumberOfClass)]
    train_pairs2, train_y2 = create_pairs(warped_pairs2, digit_indicesTrain2,  percentage)
    

    #   4. reshape the pairs for 2D  convolutional layer
    
    train_twin1_input1, train_twin2_input1 = reshape_input(image_width, image_height, num_channels, train_pairs1)
    train_twin1_input2, train_twin2_input2 = reshape_input(image_width, image_height, num_channels, train_pairs2)

    return train_twin1_input1, train_twin2_input1, train_twin1_input2, train_twin2_input2, train_y1, train_y2
コード例 #2
0
def transform_dataset(train_dataset, test_dataset, degree, strength):
    '''       
        Data preprocessing
        Transform the original data set
        
       @param
        dataset: The source dataset that will be transformed
        degree: The max degree of the transformation
        strength: The strength of the transformation
       @return
        x_Transformed_train: 
        x_Transformed_test: 
         
    '''
    '''
    3.2 larger transfomred datasets
    
    '''

    x_Transformed_train = np.array([
        assign2_utils.random_deform(x, degree, strength) for x in train_dataset
    ])
    x_Transformed_test = np.array([
        assign2_utils.random_deform(x, degree, strength) for x in test_dataset
    ])
    return x_Transformed_train, x_Transformed_test
    '''
コード例 #3
0
def transform_dataset(train_dataset, test_dataset, degree, strength):

    '''       
    Data preprocessing:
        Transform the original data set

       @param
        dataset: The source dataset that will be transformed
        degree: The max degree of the transformation
        strength: The strength of the transformation

       @return
        x_Transformed_train: 
        x_Transformed_test: 

    '''
    x_Transformed_train = np.array([assign2_utils.random_deform(x,degree,strength) for x in train_dataset])
    x_Transformed_test = np.array([assign2_utils.random_deform(x,degree,strength) for x in test_dataset])
    
    return x_Transformed_train, x_Transformed_test
コード例 #4
0
def warp_original_data():
    with np.load('mnist_dataset.npz') as npzfile:
        x_train = npzfile['x_train']
        y_train = npzfile['y_train']
        x_test = npzfile['x_test']
        y_test = npzfile['y_test']

    warped_x_train = np.zeros(x_train.shape)
    warped_x_test = np.zeros(x_test.shape)
    for i in range(x_train.shape[0]):
        warped_x_train[i] = assign2_utils.random_deform(
            x_train[i] / 255, 45, 0.3)
    for i in range(x_test.shape[0]):
        warped_x_test[i] = assign2_utils.random_deform(x_test[i] / 255, 45,
                                                       0.3)

    np.savez('mnist_warped_dataset.npz',
             x_train=warped_x_train,
             y_train=y_train,
             x_test=warped_x_test,
             y_test=y_test)
コード例 #5
0
def generate_testset(original_file_name, number, rotation, variation):
    x_train, y_train, x_test, y_test = load_original_data(original_file_name)

    x_te = np.zeros((number, x_test.shape[1], x_test.shape[2]))
    y_te = np.zeros((number))

    for i in range(number):
        index = np.random.randint(0, x_test.shape[0])
        x_te[i] = assign2_utils.random_deform(x_test[index], rotation,
                                              variation)
        y_te[i] = y_train[index]

    np.savez('test_dataset.npz', x_test=x_te, y_test=y_te)
    print('Generated test with training shape: {0}'.format(x_te.shape))
コード例 #6
0
    def warped_images(x_train, x_test, degree, strength):
        """
        warped dataset with degree and strength.
        
        @param: expanded x_train,x_test.
                degree: warped degree
                strength: warped strength
        @return: warped x_train, x_test
        """
        print("This time is warped data with {} degree and {} strength".format(
            degree, strength))

        for i in range(len(x_train)):

            x_train[i] = assign2_utils.random_deform(x_train[i], degree,
                                                     strength)

        for i in range(len(x_test)):

            x_test[i] = assign2_utils.random_deform(x_test[i], degree,
                                                    strength)

        return x_train, x_test
コード例 #7
0
def generate_warped_dataset(original_file_name, file_name, number, rotation,
                            variation):
    '''
        Need comment
            
    '''
    x_train, y_train, x_test, y_test = load_original_data(original_file_name)

    warped_x_train = np.zeros((number, x_train.shape[1], x_train.shape[2]))
    warped_y_train = np.zeros((number))

    for i in range(number):
        index = np.random.randint(0, x_train.shape[0])
        warped_x_train[i] = assign2_utils.random_deform(
            x_train[index],  #[index]/255,
            rotation,
            variation)
        warped_y_train[i] = y_train[index]

    np.savez(file_name, x_train=warped_x_train, y_train=warped_y_train)
    print('Generated file {0} with training shape: {1}'.format(
        file_name, warped_x_train.shape))
コード例 #8
0
def create_warped_dataset(input_array,input_lables, rotation, warp_strength, output_size = 100000, return_y = True):
    '''
    Create an array of warped images of size output_size
    
    @param
        input_array : Nnumpy array of image inputs
        input_labels : Numpy array of lables where input_labels[i] is
            The label for input_array[i]
        rotation : Maximum rotation value
        warp_strength : Maximum intensity of the warp
        output_size : Size of the output dataset (100K images by default)
        return_y : determines if the function should return labels with the dataset
    
    @return 
        if return_y is true
            warped_array warped version of input_array
        else
            warped_array
    '''
    # create an output array with the same shape as input_array but of size output_size
    warped_array = np.zeros((output_size, len(input_array[1]), len(input_array[2])))
    output_labels = np.zeros((output_size,),dtype=input_lables.dtype)
    print('Warping images...')
    
    # randomly warp images from input_array by rotation and warp_strenth and insert them into warped_array
    for i in range(output_size):
        warped_array[i] = assign2_utils.random_deform(input_array[i % len(input_array)], rotation, warp_strength)
        output_labels[i] = int(input_lables[i% len(input_lables)])
        
#    # Return Warped Dataset
#    if len(input_array) != output_size:
#        return warped_array, output_labels #, warped_array[split_range:],output_labels[split_range:]
#    else:
    if not return_y:
        return warped_array
    else:
        return warped_array, output_labels
コード例 #9
0
def generate_warped_dataset(dataset):
    '''
    Generate warped dataset based on parameter from SETTINGS.
    This will generate images randomly from x_train of mnist dataset
    and generate test images randomly from x_test of mnist dataset
        Args:
            dataset['warped_dataset_records']: total warped images will be generated
            dataset['hard_pair']: proportion of significant deformation of images in the dataset
                                  for example, dataset['hard_pair'] = 0.6 means 60% of warped 
                                  images in the dataset are deformed significantly
            dataset['test_set']: proportion of test set.
                                 for example, dataset['test_set'] = 0.1 means the number of
                                 test images will be 10% of warped images.
                                 dataset['test_set'] = 0.1 and 
                                 dataset['warped_dataset_records']= 100000
                                 There are 10000 test images and 100000 warped images
                                 Total is 110000 images
            
    '''
    x_train, y_train, x_test, y_test = load_original_data()

    # hard_pair is percentage of H dataset in total warped dataset
    hard_pair_records = int(dataset['hard_pair'] *
                            dataset['warped_dataset_records'])
    easy_pair_records = int(
        (1 - dataset['hard_pair']) * dataset['warped_dataset_records'])
    test_records = int(dataset['test_set'] * dataset['warped_dataset_records'])

    print('Warped images: ' + str(dataset['warped_dataset_records']))

    h_x_train = np.zeros(
        (hard_pair_records, x_train.shape[1], x_train.shape[2]))
    h_y_train = np.zeros((hard_pair_records))
    h_x_test = np.zeros((int(dataset['hard_pair'] * test_records),
                         x_train.shape[1], x_train.shape[2]))
    h_y_test = np.zeros((int(dataset['hard_pair'] * test_records)))
    for i in range(hard_pair_records):
        index = np.random.randint(0, x_train.shape[0])
        h_x_train[i] = assign2_utils.random_deform(
            x_train[index],  # x_train[index]/255
            dataset['hard_pair_rotation'],
            dataset['hard_pair_variation'])
        h_y_train[i] = y_train[index]
    for i in range(int(dataset['hard_pair'] * test_records)):
        index = np.random.randint(0, x_test.shape[0])
        h_x_test[i] = assign2_utils.random_deform(
            x_test[index],  # x_test[index]/255
            dataset['hard_pair_rotation'],
            dataset['hard_pair_variation'])
        h_y_test[i] = y_test[index]

    print('Hard (H) dataset: ' + str(h_x_train.shape))

    e_x_train = np.zeros(
        (easy_pair_records, x_train.shape[1], x_train.shape[2]))
    e_y_train = np.zeros((easy_pair_records))
    e_x_test = np.zeros((int((1 - dataset['hard_pair']) * test_records),
                         x_train.shape[1], x_train.shape[2]))
    e_y_test = np.zeros((int((1 - dataset['hard_pair']) * test_records)))
    for i in range(easy_pair_records):
        index = np.random.randint(0, x_train.shape[0])
        e_x_train[i] = assign2_utils.random_deform(
            x_train[index],  # x_train[index]/255
            dataset['easy_pair_rotation'],
            dataset['easy_pair_variation'])
        e_y_train[i] = y_train[i]
    for i in range(int((1 - dataset['hard_pair']) * test_records)):
        index = np.random.randint(0, x_test.shape[0])
        e_x_test[i] = assign2_utils.random_deform(
            x_test[index],  # x_test[index]/255
            dataset['easy_pair_rotation'],
            dataset['easy_pair_variation'])
        e_y_test[i] = y_test[i]

    print('Easy (E) dataset: ' + str(e_x_train.shape))

    warped_x_train = np.concatenate((h_x_train, e_x_train), axis=0)
    warped_y_train = np.concatenate((h_y_train, e_y_train), axis=0)
    warped_x_test = np.concatenate((h_x_test, e_x_test), axis=0)
    warped_y_test = np.concatenate((h_y_test, e_y_test), axis=0)

    print('Test dataset: ' + str(warped_x_test.shape))
    np.savez('mnist_warped_dataset.npz',
             x_train=warped_x_train,
             y_train=warped_y_train,
             x_test=warped_x_test,
             y_test=warped_y_test)
コード例 #10
0
def generate_warped_dataset(dataset):
    '''
    Generate warped dataset based on parameters from SETTINGS.
    This will generate images randomly from x_train of mnist dataset
    and generate test images randomly from x_test of mnist dataset
        Args:
            dataset['warped_dataset_records']: total warped images will be generated
            dataset['hard_pair']: proportion of significant deformation of images in the dataset
                                  for example, dataset['hard_pair'] = 0.6 means 60% of warped 
                                  images in the dataset are deformed significantly
            dataset['test_set']: proportion of test set.
                                 for example, dataset['test_set'] = 0.1 means the number of
                                 test images will be 10% of warped images.
                                 dataset['test_set'] = 0.1 and 
                                 dataset['warped_dataset_records']= 100000
                                 There are 10000 test images and 100000 warped images
                                 Total is 110000 images
            
    '''
    x_train, y_train, x_test, y_test = load_original_data()
    
    # hard_pair is percentage of H dataset in total warped dataset
    hard_pair_records = int(dataset['hard_pair'] * dataset['warped_dataset_records'])
    easy_pair_records = int((1 - dataset['hard_pair']) * dataset['warped_dataset_records'])
    test_records = int(dataset['test_set'] * dataset['warped_dataset_records'])
    
    print('Warped images: ' + str(dataset['warped_dataset_records']))
    
    if network_configs['train_dataset'] == 'EH'
        h_x_train = np.zeros((hard_pair_records, x_train.shape[1], x_train.shape[2]))
        h_y_train = np.zeros((hard_pair_records))
        h_x_test = np.zeros((int(dataset['hard_pair'] * test_records), x_train.shape[1], x_train.shape[2]))
        h_y_test = np.zeros((int(dataset['hard_pair'] * test_records)))
        for i in range(hard_pair_records):
            index = np.random.randint(0, x_train.shape[0])
            h_x_train[i] = assign2_utils.random_deform(#x_train[index], 
                                 x_train[index]/255,
                                 dataset['hard_pair_rotation'], 
                                 dataset['hard_pair_variation'])
            h_y_train[i] = y_train[index]
        for i in range(int(dataset['hard_pair'] * test_records)):
            index = np.random.randint(0, x_test.shape[0])
            h_x_test[i] = assign2_utils.random_deform(#x_test[index], 
                                 x_test[index]/255,
                                 dataset['hard_pair_rotation'], 
                                 dataset['hard_pair_variation'])
            h_y_test[i] = y_test[index]
            
        print('Hard (H) dataset: ' + str(h_x_train.shape))
        
        e_x_train = np.zeros((easy_pair_records, x_train.shape[1], x_train.shape[2]))
        e_y_train = np.zeros((easy_pair_records))
        e_x_test = np.zeros((int((1 - dataset['hard_pair']) * test_records), x_train.shape[1], x_train.shape[2]))
        e_y_test = np.zeros((int((1 - dataset['hard_pair']) * test_records)))
        for i in range(easy_pair_records):
            index = np.random.randint(0, x_train.shape[0])
            e_x_train[i] = assign2_utils.random_deform(#x_train[index], #
                                 x_train[index]/255,
                                 dataset['easy_pair_rotation'], 
                                 dataset['easy_pair_variation'])
            e_y_train[i] = y_train[i]
        for i in range(int((1 - dataset['hard_pair']) * test_records)):
            index = np.random.randint(0, x_test.shape[0])
            e_x_test[i] = assign2_utils.random_deform(#x_test[index], # 
                                 x_test[index]/255,
                                 dataset['easy_pair_rotation'], 
                                 dataset['easy_pair_variation'])
            e_y_test[i] = y_test[i]
    elif network_configs['train_dataset'] == 'H'
        #Warp the daataset with H setting same size of E+H 
    print('Easy (E) dataset: ' + str(e_x_train.shape))
    
    warped_x_train = np.concatenate((h_x_train, e_x_train), axis=0)
    warped_y_train = np.concatenate((h_y_train, e_y_train), axis=0)
    warped_x_test  = np.concatenate((h_x_test, e_x_test), axis=0)
    warped_y_test = np.concatenate((h_y_test, e_y_test), axis=0)    
    
    print('Test dataset: ' + str(warped_x_test.shape))
    np.savez('mnist_warped_dataset.npz', x_train=warped_x_train, y_train=warped_y_train,
             x_test=warped_x_test, y_test=warped_y_test)
    
def load_original_data():
    '''
    Load the dataset, shuffled and split between train and test sets
    and return the numpy arrays  x_train, y_train, x_test, y_test in original dataset
    The dtype of all returned array is uint8
    
    '''
    assert os.path.isfile('./mnist_dataset.npz')
    with np.load('mnist_dataset.npz') as npzfile:
#        x_train = npzfile['x_train']/255
        x_train = npzfile['x_train']
        y_train = npzfile['y_train']
#        x_test = npzfile['x_test']/255
        x_test = npzfile['x_test']
        y_test = npzfile['y_test']
    
    return x_train, y_train, x_test, y_test

def load_warped_dataset(dataset_type):
    '''
    Load the dataset, shuffled and split between train and test sets
    and return the numpy arrays  x_train, y_train, x_test, y_test in warped dataset
    The dtype of all returned array is uint8
    
    '''
    if dataset_type == 'E'
        assert os.path.isfile('./mnist_easy_warped_dataset.npz')
        datasetname = './mnist_easy_warped_dataset.npz'
    if dataset_type == 'H'
        assert os.path.isfile('./mnist_partialhard_warped_dataset.npz')
        datasetname = './mnist_partialhard_warped_dataset.npz'
    if dataset_type == 'HH'
        assert os.path.isfile('./mnist_fullhard_warped_dataset.npz')
        datasetname = './mnist_fullhard_warped_dataset.npz'
    with np.load(datasetname) as npzfile:
        warped_x_train = npzfile['x_train']
        warped_y_train = npzfile['y_train']
        warped_x_test = npzfile['x_test']
        warped_y_test = npzfile['y_test']
    
    return warped_x_train, warped_y_train, warped_x_test, warped_y_test


#------------------------------------------------------------------------------
# CONFIGURATION AND SETTING

SETTINGS = {
    'dataset_generation': {
        'create_original_dataset': False,   # Donwload the dataset and save to the file named
                                            # mnist_dataset.npz. By doing this, we don't have
                                            # to download the dataset every run the experiment
                                            
        'create_warped_dataset': True,      # Using mnist_dataset.npz, we generate warped dataset
                                            # and save the dataset to mnist_warped_dataset.npz
                                            # When we want to generate warped dataset again,
                                            # we just need to set it to True. 
                                            
        'warped_dataset_records': 100000,   # number of records in warped dataset (D dataset)
        'hard_pair': 0.8,                   # in percentage from 0 to 1. for example, 
                                            # if hard_pair=0.6 and warped_dataset_records=100000,  
                                            # the hard pairs will be 60% of warped_dataset_records which is
                                            # 60000 and the easy_pair will be 40000
                                            
        'test_set': 0.1,                    # proportion of test dataset.  
                                            # test_set = 0.2 means extra 20% of the warped 
                                            # images in the dataset will be used for test dataset
                                            
        'hard_pair_rotation': 45,           # rotation of deformed the dataset (H dataset)
        'hard_pair_variation': 0.3,         # rotation of deformed the dataset (H dataset)
        
        'easy_pair_rotation': 5,            # rotation of deformed the dataset (E dataset)
        'easy_pair_variation': 0.01,        # variation of deformed the dataset (E dataset)

    },
    'network_configs': {                    # experiment on original dataset
        'epochs': 2,
        'train_dataset': 'O'                # original_dataset can take values from ['O', 'H', 'EH', A]. 
                                            # O: Using original dataset
                                            # H: Using Hard dataset equal to the size of E+H
                                            # EH: Using Easy and then Hard dataset
                                            # to train the network
    }
}
    
#------------------------------------------------------------------------------        

if __name__=='__main__':
    SiameseExperiment.run_experiments(**SETTINGS)
コード例 #11
0
'''

A short script to illustrate the warping functions of 'assign2_utils'

'''

#import numpy as np
import matplotlib.pyplot as plt

from tensorflow.contrib import keras
#from tensorflow.contrib.keras import backend as K

import assign2_utils

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

im1 = x_train[20]

plt.imshow(im1, cmap='gray')

im2 = assign2_utils.random_deform(im1, 45, 0.3)

plt.figure()

plt.imshow(im2, cmap='gray')

plt.show()
コード例 #12
0
im2: A wraped image
the maximum degree: 45 
strength: 0.3

'''
'''
3.1  Orignal datasets.

'''
#x_original_train, y_train, x_original_test, y_test = assign2_utils.load_dataset()
'''
3.2 larger transfomred datasets

'''
x_largeTransformed_train = np.array(
    [assign2_utils.random_deform(x, 45, 0.3) for x in x_original_train])
x_largeTransformed_test = np.array(
    [assign2_utils.random_deform(x, 45, 0.3) for x in x_original_test])
'''
3.3 smaller transformed datasets.

'''
#x_smallTransformed_train = np.array([assign2_utils.random_deform(x,30,0.2) for x in x_original_train])
#x_smallTransformed_test = np.array([assign2_utils.random_deform(x,30,0.2) for x in x_original_test])
'''
4. Reshape the datasets and nomalised them.
(the total number of pairs, one pair(2 numbers) in the same or not same classification, 28*28 )
'''
x_train = x_original_train.reshape(60000, 784)
x_test = x_original_test.reshape(10000, 784)
x_train = x_train.astype('float32')
コード例 #13
0
import matplotlib.pyplot as plt

from tensorflow.contrib import keras
#from tensorflow.contrib.keras import backend as K

import assign2_utils

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

#ims = x_train[0]
#plt.imshow(ims,cmap='gray')
#ims2 = x_test

for i in range(10):
    plt.figure()
    #    print("Train "+str(i))
    plt.imshow(x_train[i], cmap='gray')
    plt.figure()
    g = assign2_utils.random_deform(x_train[i], 20, 0.1)
    #    print("Test "+str(i))
    plt.imshow(g, cmap='gray')

#
#
#im2 = assign2_utils.random_deform(im1,45,0.3)
#
#plt.figure()
#
#plt.imshow(im2,cmap='gray')
#
#plt.show()
コード例 #14
0
    '''
    x_original_train, train_target, x_original_test, val_target = assign2_utils.load_dataset()
    #train_pairs, val_pairs = transform_dataset(x_original_train, x_original_test, 30, 0.15)
    # 2. split the datset.
    x_original_train = x_original_train.astype('float32')
    x_original_test = x_original_test.astype('float32')
    
    normaliseValue = 255
    x_original_train /= normaliseValue # normalized the entries between 0 and 1
    x_original_test /= normaliseValue
    
    x_original_train1, x_original_train2, train_target1, train_target2  = split_Data(x_original_train,train_target, 0.4)
    #x_original_test1, x_original_test2 , val_target1, val_target2  = split_Data(x_original_test,val_target, percentage)

    # 3. transform the dataset.
    warped_pairs1 = np.array([assign2_utils.random_deform(x, 15, 0.1) for x in x_original_train1])
    warped_pairs2 = np.array([assign2_utils.random_deform(x, 45, 0.3) for x in x_original_train2])
    # train_pairs2 = np.array([assign2_utils.random_deform(x, degree2, strength2) for x in x_original_train2]
    

    digit_indicesTrain1 = [np.where(train_target[train_target1] == i)[0] for i in range(NumberOfClass)]
    train_pairs1, train_y1 = create_pairs(warped_pairs1, digit_indicesTrain1, 0.4)
    digit_indicesTrain2 = [np.where(train_target[train_target2] == i)[0] for i in range(NumberOfClass)]
    train_pairs2, train_y2 = create_pairs( warped_pairs2, digit_indicesTrain2,  0.6)
    
    
    #   4. reshape the pairs for 2D  convolutional layer
    
    train_twin1_input1, train_twin2_input1 = reshape_input(image_width, image_height, num_channels, train_pairs1)
    train_twin1_input2, train_twin2_input2 = reshape_input(image_width, image_height, num_channels, train_pairs2)
コード例 #15
0
def generate_warped_dataset(dataset):
    '''
    Generate warped dataset based on parameter from SETTINGS.
    This will generate images randomly from x_train of mnist dataset
    and generate test images randomly from x_test of mnist dataset
        Args:
            dataset['warped_dataset_records']: total warped images will be generated
            dataset['hard_pair']: proportion of significant deformation of images in the dataset
                                  for example, dataset['hard_pair'] = 0.6 means 60% of warped 
                                  images in the dataset are deformed significantly
            dataset['test_set']: proportion of test set.
                                 for example, dataset['test_set'] = 0.1 means the number of
                                 test images will be 10% of warped images.
                                 dataset['test_set'] = 0.1 and 
                                 dataset['warped_dataset_records']= 100000
                                 There are 10000 test images and 100000 warped images
                                 Total is 110000 images
            
    '''
    x_train, y_train, x_test, y_test = load_original_data()
    
    # hard_pair is percentage of H dataset in total warped dataset
    test_records = int(dataset['test_set'] * dataset['warped_dataset_records'])
    if dataset['train_dataset'] == 'H' and dataset['retrain']:
        w_pair_records = int(dataset['hard_pair'] * dataset['warped_dataset_records'])
        w_test_records = int(dataset['hard_pair'] * test_records)
        w_rotation = dataset['hard_pair_rotation']
        w_variation = dataset['hard_pair_variation']
    elif dataset['train_dataset'] == 'H' and not dataset['retrain']:
        w_pair_records = int(dataset['warped_dataset_records'])
        w_test_records = test_records
        w_rotation = dataset['hard_pair_rotation']
        w_variation = dataset['hard_pair_variation']
    else: 
        w_pair_records = int((1 - dataset['hard_pair']) * dataset['warped_dataset_records'])
        w_test_records = int((1 - dataset['hard_pair']) * test_records)
        w_rotation = dataset['easy_pair_rotation']
        w_variation = dataset['easy_pair_variation']
        
    print('Warped images: ' + str(dataset['warped_dataset_records']))
    
    w_x_train = np.zeros((w_pair_records, x_train.shape[1], x_train.shape[2]))
    w_y_train = np.zeros((w_pair_records))
    w_x_test = np.zeros((w_test_records, x_train.shape[1], x_train.shape[2]))
    w_y_test = np.zeros((w_test_records))
    for i in range(w_pair_records):
        index = np.random.randint(0, x_train.shape[0])
        w_x_train[i] = assign2_utils.random_deform(x_train[index], # x_train[index]/255
                             w_rotation, 
                             w_variation)
        w_y_train[i] = y_train[index]
    for i in range(w_test_records):
        index = np.random.randint(0, x_test.shape[0])
        w_x_test[i] = assign2_utils.random_deform(x_test[index], # x_test[index]/255
                             w_rotation, 
                             w_variation)
        w_y_test[i] = y_test[index]
        
    
    print('Test dataset: ' + str(w_x_test.shape))
    np.savez('mnist_warped_dataset.npz', x_train=w_x_train, y_train=w_y_train,
             x_test=w_x_test, y_test=w_y_test)
コード例 #16
0
def stage_learning():
    """
    stage learning
    training the first 20% easy warped images first,
    then training the 80% hard warped images
    test all images 
    predict the accuracy
    """
    #expand x_train,y_train into a new x_train and y_train array with 100,000
    x_train, y_train, x_test, y_test = assign2_utils.load_dataset()

    a = x_train[0:40000, :, :]
    x_train = np.concatenate((a, x_train), axis=0)

    b = y_train[0:40000]
    y_train = np.concatenate((b, y_train), axis=0)

    img_row = x_train.shape[1]
    img_col = x_train.shape[2]

    # normalized the input image
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')

    x_train /= 255
    x_test /= 255

    #reshape data
    x_train = x_train.reshape(x_train.shape[0], img_row, img_col, 1)
    x_test = x_test.reshape(x_test.shape[0], img_row, img_col, 1)

    input_dim = (img_row, img_col, 1)

    epochs = 10
    x_train_easy = x_train
    x_train_hard = x_train
    x_test_easy = x_test
    x_test_hard = x_test

    #warped images with with easy strength and easy degree
    for i in range(100000):
        x_train_easy[i] = assign2_utils.random_deform(x_train[i], 15, 0.1)
    for i in range(100000):
        x_train_hard[i] = assign2_utils.random_deform(x_train[i], 45, 0.3)

    #warped images with with hard strength and hard degree
    for i in range(10000):
        x_test_easy[i] = assign2_utils.random_deform(x_test[i], 15, 0.1)
    for i in range(10000):
        x_test_hard[i] = assign2_utils.random_deform(x_test[i], 45, 0.3)

    # create training+test positive and negative pairs
    digit_indices = [np.where(y_train == i)[0] for i in range(10)]
    tr_pairs_easy, tr_y_easy = create_pairs(x_train_easy, digit_indices)

    digit_indices = [np.where(y_train == i)[0] for i in range(10)]
    tr_pairs_hard, tr_y_hard = create_pairs(x_train_hard, digit_indices)

    digit_indices = [np.where(y_test == i)[0] for i in range(10)]
    te_pairs, te_y = create_pairs(x_test, digit_indices)

    # network definition
    base_network = create_simplistic_base_network(input_dim)

    input_a = keras.layers.Input(shape=input_dim)
    input_b = keras.layers.Input(shape=input_dim)

    # because we re-use the same instance `base_network`,
    # the weights of the network will be shared across the two branches
    processed_a = base_network(input_a)
    processed_b = base_network(input_b)

    # node to compute the distance between the two vectors
    # processed_a and processed_a
    distance = keras.layers.Lambda(euclidean_distance)(
        [processed_a, processed_b])

    # Our model take as input a pair of images input_a and input_b
    # and output the Euclidian distance of the mapped inputs

    #model to fit the first 20% easy warped images
    model = keras.models.Model([input_a, input_b], distance)
    rms = keras.optimizers.RMSprop()
    model.compile(loss=contrastive_loss, optimizer=rms)
    model.fit([tr_pairs_easy[0:36096, 0], tr_pairs_easy[0:36096, 1]],
              tr_y_easy[0:36096],
              batch_size=128,
              epochs=epochs,
              validation_data=([te_pairs[0:3564, 0],
                                te_pairs[0:3564, 1]], te_y[0:3564]))
    #model.save_weights("easy_warped.h5")

    #model to fit the last 80% hard warped images
    model2 = keras.models.Model([input_a, input_b], distance)
    rms = keras.optimizers.RMSprop()
    model2.compile(loss=contrastive_loss, optimizer=rms)

    #model2.load_weights("easy_warped.h5")

    model2.fit([tr_pairs_hard[36096:, 0], tr_pairs_hard[36096:, 1]],
               tr_y_hard[36096:],
               batch_size=128,
               epochs=epochs,
               validation_data=([te_pairs[3564:, 0],
                                 te_pairs[3564:, 1]], te_y[3564:]))

    #merge two tr_pairs dataset together
    tr_pairs = np.concatenate((tr_pairs_hard[36096:], tr_pairs_easy[0:36096]),
                              axis=0)
    tr_y = np.concatenate((tr_y_hard[36096:], tr_y_easy[0:36096]))

    pred = model.predict([tr_pairs[:, 0], tr_pairs[:, 1]])
    tr_acc = compute_accuracy(pred, tr_y)

    pred = model.predict([te_pairs[:, 0], te_pairs[:, 1]])
    te_acc = compute_accuracy(pred, te_y)

    print('when epochs is {}'.format(epochs))
    print('* Accuracy on training set: %0.2f%%' % (100 * tr_acc))
    print('* Accuracy on test set: %0.2f%% \n\n' % (100 * te_acc))