Exemple #1
0
def trainSamples(primaryDatasets, primaryClasses, primaryInstances,
                 helperInstances):

    for dataSet in primaryDataSets:
        for cls in primaryClasses:
            for instance in primaryInstances:
                for helperInstance in helperInstances:

                    # if the number of primary instances are larger than the number of helper
                    # instances, no need to calculate MMD
                    if instance > helperInstance:
                        continue

                    # get a fixed helper class for a particular dataset and primary class
                    helperClass = getHelperClass(dataSet, cls)

                    if helperClass == -1:
                        continue
                    print(
                        'Primary Class: {} Helper Class: {} Primary Instances: {} Helper Instance {}'
                        .format(cls, helperClass, instance, helperInstance))

                    primaryFileName = dataSet + '_' + str(cls) + '_' + str(
                        instance)
                    helperFileName = dataSet + '_' + str(
                        helperClass) + '_' + str(helperInstance)
                    dataFolder = rootDir + str(dataSet)

                    x = loadDataset(dataSet, cls, instance)
                    y = loadDataset(dataSet, helperClass, helperInstance)
                    primaryTrainLoader = torch.utils.data.DataLoader(
                        x,
                        batch_size=batchSize,
                        shuffle=True,
                        num_workers=4,
                        drop_last=True)
                    helperTrainLoader = torch.utils.data.DataLoader(
                        y,
                        batch_size=batchSize,
                        shuffle=True,
                        num_workers=4,
                        drop_last=True)
                    numOutputChannels = getChannels(dataSet)
                    epochs = getEpochs(dataSet, instance)

                    print epochs
                    train(primaryFileName,
                          helperFileName,
                          primaryTrainLoader,
                          helperTrainLoader,
                          instance,
                          numOutputChannels,
                          epochs=epochs)
def trainSamples(primaryDataSet, helperDataSet, primaryClasses,
                 primaryInstances, helperInstances):

    for cls in primaryClasses:
        for instance in primaryInstances:
            for helperInstance in helperInstances:

                # if the number of primary instances are larger than the number of helper
                # instances, no need to calculate MMD
                if instance > helperInstance:
                    continue

                # the primary class is same as helper class in this case
                helperClass = cls

                print(
                    'Primary Class: {} Helper Class: {} Primary Instances: {} Helper Instance {}'
                    .format(cls, helperClass, instance, helperInstance))

                primaryFileName = primaryDataSet + '_' + str(cls) + '_' + str(
                    instance)
                helperFileName = helperDataSet + '_' + str(
                    helperClass) + '_' + str(helperInstance)

                x = loadDataset(primaryDataSet, cls, instance)
                y = loadDataset(helperDataSet, helperClass, helperInstance)

                primaryTrainLoader = torch.utils.data.DataLoader(
                    x,
                    batch_size=batchSize,
                    shuffle=True,
                    num_workers=4,
                    drop_last=False)
                helperTrainLoader = torch.utils.data.DataLoader(
                    y,
                    batch_size=batchSize,
                    shuffle=True,
                    num_workers=4,
                    drop_last=True)
                numOutputChannels = getChannels(primaryDataSet)
                epochs = getEpochs(primaryDataSet, instance)

                train(primaryFileName,
                      helperFileName,
                      primaryTrainLoader,
                      helperTrainLoader,
                      instance,
                      numOutputChannels,
                      epochs=epochs)
Exemple #3
0
def trainSamples(primaryDatasets, primaryClasses, primaryInstances,
                 helperInstances):

    for dataSet in primaryDataSets:
        for cls in primaryClasses:
            for instance in primaryInstances:
                for helperInstance in helperInstances:

                    # if the number of primary instances are larger than the number of helper
                    # instances, no need to calculate MMD
                    if instance > helperInstance:
                        continue

                    primaryFileName = dataSet + '_' + str(cls) + '_' + str(
                        instance)
                    helperFileName = dataSet + '_' + 'all' + '_' + str(
                        helperInstance)
                    dataFolder = rootDir + str(dataSet)

                    x = loadDataset(dataSet, cls, instance, except_=0)
                    y = loadDataset(dataSet, cls, instance, except_=1)

                    primaryTrainLoader = torch.utils.data.DataLoader(
                        x,
                        batch_size=batchSize,
                        shuffle=True,
                        num_workers=4,
                        drop_last=True)
                    helperTrainLoader = torch.utils.data.DataLoader(
                        y,
                        batch_size=batchSize,
                        shuffle=True,
                        num_workers=4,
                        drop_last=True)
                    numOutputChannels = getChannels(dataSet)
                    epochs = getEpochs(dataSet, instance)

                    train(primaryFileName,
                          helperFileName,
                          primaryTrainLoader,
                          helperTrainLoader,
                          instance,
                          numOutputChannels,
                          epochs=epochs)
Exemple #4
0
def test(primaryDataSet, helperDataSet, primaryClass, helperClass, primaryInstances, helperInstances):
    '''
    Inputs :
    
    dataSets : List : Datasets for which samples are to be genrated
    instances : List : Number of instances to be used from original dataset
    classes : List : Classes for which samples are to be generated
    
    Output :
    
    File with 1000 compressed images generated by GAN
    
    '''
    helperClass = primaryClass
    
    modelFolder = resultDir + 'models/crossDataSetMMDall'+'/'+primaryDataSet+'/'
    print (primaryDataSet, helperDataSet, primaryClass, helperClass, primaryInstances, helperInstances, getEpochs(primaryDataSet,primaryInstances)-1)
    modelFile = modelFolder + primaryDataSet + '_' + helperDataSet + '_' + \
                str(primaryClass) + '_' + str(helperClass) + '_' + \
                str(primaryInstances) + '_' + str(helperInstances)+'_'+ \
                str(getEpochs(primaryDataSet,primaryInstances)-1)+'.pt'
        

    
    print ('Generating examples for Dataset: '+primaryDataSet+
           ' Primary Class: '+str(primaryClass)+
           ' Helper Class: '+str(helperClass)+
           ' Primary Instances: '+str(primaryInstances)+
           ' Helper Instances: '+str(helperInstances)+
           ' Epochs: '+str(getEpochs(primaryDataSet,primaryInstances)))
    
    numOutputChannels = getChannels(primaryDataSet)
    
    # load the model learnt during training
    G = Generator(numInputChannels, numGenFilter, numOutputChannels)
    G.load_state_dict(torch.load(modelFile))
    genImageConcat = np.empty(1)
    
    iterations = numOfSamples/batchSize
    
    for iteration in range(iterations):
        noise = torch.FloatTensor(batchSize,
                                  numInputChannels,
                                  1,
                                  1)
        noise.normal_(0,1)

        if cuda:
            G = G.cuda()
            noise = noise.cuda()
        noiseVariable = Variable(noise)

        genImage = G(noiseVariable)
        genImage = genImage.data
        genImage = genImage.cpu()
        genImage = genImage.numpy()
        
        
        if iteration==0:
            genImageConcat = genImage
        else:
            genImageConcat = np.concatenate((genImageConcat, genImage),
                                            axis=0)
            
        if iteration==(iterations-1):
            
            # normalize sets image pixels between 0 and 1
            genImage = torchvision.utils.make_grid(torch.from_numpy(genImage[:25]), nrow=5, normalize=True)
            
            # mapping between 0 to 1 as required by imshow,
            # otherwise the images are stored in the form -1 to 1
            # done through normalize=True
            
            genImage = genImage.permute(1,2,0)
            genImage = genImage.numpy()

            plt.imshow(genImage, cmap='gray')
            
            plotFolder = resultDir+'results'+'/'+'crossDataSetMMDall/samples'+'/'+primaryDataSet+'/'
            checkAndCreateFolder(plotFolder)
            plotFile = primaryDataSet + '_' + helperDataSet + '_' + \
                str(primaryClass) + '_' + 'all' + '_' + \
                str(primaryInstances) + '_' + str(helperInstances) 
            plotPath = plotFolder + plotFile
            
            plt.axis('off')
            plt.savefig(plotPath, bbox_inches='tight')
            plt.show()
            

    resultFolder = resultDir+'results/crossDataSetMMDall'+'/'+'compressed'+'/'+primaryDataSet+'/'
    checkAndCreateFolder(resultFolder)
    resultFile = primaryDataSet + '_' + helperDataSet + '_' + \
                 str(primaryClass) + '_' + 'all' + '_' + \
                 str(primaryInstances) + '_' + str(helperInstances)  + '.npy'
            
    resultPath = resultFolder + resultFile
    
    # save the image in some format
    with open(resultPath,'wb+') as fh:
        genImageConcat = np.squeeze(genImageConcat)
        np_save(fh, genImageConcat, allow_pickle=False)
        sync(fh)