예제 #1
0
image_size = 20
N = image_size
num_nodes = image_size**2
dtype = torch.FloatTensor

model_name = [
    'modelBlock_prop07.pth.tar', 'modelBlock_prop10.pth.tar',
    'modelBlock_prop12.pth.tar'
]
layers_list = [7, 10, 12]

for layers in layers_list:
    # Loop over models
    loss_fn = nn.MSELoss()
    testDict = generateSamples(image_size, 10000, layers)

    for i in range(3):

        # Load in the relevant model and extract the propagation weights
        modelBlock_State = torch.load(model_name[i],
                                      map_location=torch.device('cpu'))
        modelBlock = loadStateDict(modelBlock_State)
        modelProp = modelBlock[modelBlock['Meta']['BestModel']]["Model"]

        w = modelProp.iteratedLayer_Pred.w.data
        a = modelProp.iteratedLayer_Pred.a.data
        b = modelProp.iteratedLayer_Pred.bias.data

        # Set up model with given number of step sizes using these weights
        model = NF.PropagationOnly_FixedWeights(num_nodes, layers,
예제 #2
0
def trainModel_Exp(modelBlock, resultBlock, n_epochs, log_file, result_file,
                   model_file):
    # Function TRAIN_MODEL
    # Trains all models in modelList for n_epochs
    # Parameters:
    # 		* modelBlock: Nested dictionary of models
    #       * n_epochs: Number of epochs for which to train
    #       * N: size of image to generate
    # Parameters to add
    #		* exp flag that tells whehter or not we are hyperopt or running experiments
    #		* resultBlock
    #		* Number of previously executed epochs

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)

    formatter = logging.Formatter('[%(asctime)s:%(name)s]:%(message)s')

    if not len(logger.handlers):
        file_handler = logging.FileHandler(log_file)
        file_handler.setFormatter(formatter)

        stream_handler = logging.StreamHandler()
        stream_handler.setFormatter(formatter)

        logger.addHandler(file_handler)
        logger.addHandler(stream_handler)

    epochs_trained = modelBlock["Meta"]["Epochs_Trained"]
    epochs_total = epochs_trained + n_epochs
    print(epochs_total)

    for epoch in range(n_epochs):

        epoch_real = epoch + epochs_trained
        # Generate training samples and iterate through all models in modelList
        print('Starting epoch %d / %d' % (epoch_real + 1, epochs_total))
        sampleDict = generateSamples(modelBlock["Meta"]["N"], 20000,
                                     modelBlock["Meta"]["Layers"])

        for key, val in modelBlock.items():
            if (key != "Meta"):
                runEpoch(modelBlock[key]["Model"],
                         modelBlock["Meta"]["Loss_Function"],
                         modelBlock[key]["Optimizer"],
                         modelBlock["Meta"]["Type"], modelBlock[key]["Batch"],
                         sampleDict)
        print('Finishing epoch %d / %d' % (epoch_real + 1, epochs_total))

        # Want to record test error if the total number of epochs is a multiple 50 or this is the final epoch
        if (((epoch_real % 10) == 0) or (epoch == (n_epochs - 1))):

            # Every 50 epochs, evaluate the performance of all the models and print summary statistics
            testDict = generateSamples(modelBlock["Meta"]["N"], 40000,
                                       modelBlock["Meta"]["Layers"])

            print('')
            logger.info('Finishing epoch %d / %d' %
                        (epoch_real + 1, epochs_total))

            loss = []
            accAll = []
            accPath = []
            accDistract = []

            for key, val in modelBlock.items():
                if (key != "Meta"):
                    model_accAll, model_accPath, model_accDistract, model_loss = checkAccuracy(
                        modelBlock[key]["Model"],
                        modelBlock["Meta"]["Loss_Function"],
                        modelBlock["Meta"]["Type"], modelBlock[key]["Batch"],
                        testDict)
                    modelBlock[key]["Loss"] = model_loss
                    modelBlock[key]["Acc_All"] = model_accAll
                    modelBlock[key]["Acc_Path"] = model_accPath
                    modelBlock[key]["Acc_Distract"] = model_accDistract

                    resultBlock[key][epoch_real] = {
                        "Loss": model_loss,
                        "Acc_All": model_accAll,
                        "Acc_Path": model_accPath,
                        "Acc_Distract": model_accDistract
                    }

                    loss.append(model_loss)
                    accAll.append(model_accAll)
                    accPath.append(model_accPath)
                    accDistract.append(model_accDistract)

            loss_array = np.asarray(loss)
            accAll_array = np.asarray(accAll)
            accPath_array = np.asarray(accPath)
            accDistract_array = np.asarray(accDistract)
            print(loss_array)

            logger.info('[Loss] Mean:%.6f, Median:%.6f, Best:%.6f' %
                        (np.mean(loss_array), np.median(loss_array),
                         np.min(loss_array)))
            logger.info(
                '[Accuracy (All pixels)] Mean:%.6f, Median:%.6f, Best:%.6f ' %
                (np.mean(accAll_array), np.median(accAll_array),
                 np.min(accAll_array)))
            logger.info(
                '[Accuracy (Edge-Connected Paths)] Mean:%.6f, Median:%.6f, Best:%.6f '
                % (np.mean(accPath_array), np.median(accPath_array),
                   np.min(accPath_array)))
            logger.info(
                '[Accuracy (Distractors)] Mean:%.6f, Median:%.6f, Best:%.6f ' %
                (np.mean(accDistract_array), np.median(accDistract_array),
                 np.min(accDistract_array)))
            logger.info('')
            print('')

        # Update the total number of epochs trained
        modelBlock["Meta"]["Epochs_Trained"] = epoch_real
        torch.save(resultBlock, result_file)

        modelBlock_State = convertStateDict(modelBlock)
        torch.save(modelBlock_State, model_file)
예제 #3
0
def trainModel(modelBlock, n_epochs, log_file):
    # Function TRAIN_MODEL
    # Trains all models in modelList for n_epochs
    # Parameters:
    # 		* modelBlock: Nested dictionary of models
    #       * n_epochs: Number of epochs for which to train
    #       * N: size of image to generate
    # Parameters to add
    #		* exp flag that tells whehter or not we are hyperopt or running experiments
    #		* resultBlock
    #		* Number of previously executed epochs

    # Setup logger
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)

    formatter = logging.Formatter('[%(asctime)s:%(name)s]:%(message)s')

    if not len(logger.handlers):
        file_handler = logging.FileHandler(log_file)
        file_handler.setFormatter(formatter)

        stream_handler = logging.StreamHandler()
        stream_handler.setFormatter(formatter)

        logger.addHandler(file_handler)
        logger.addHandler(stream_handler)

    # Read in how many epochs the model has already been trained
    epochs_trained = modelBlock["Meta"]["Epochs_Trained"]
    epochs_total = epochs_trained + n_epochs
    print(epochs_total)

    #if ((epochs_total % 50) != 0):
    #	logger.warning("Epoch total not a multiple of 50; pruning will occur at the closest multiple of 50.")
    # 50000

    for epoch in range(n_epochs):

        epoch_real = epoch + epochs_trained
        # Generate training samples and iterate through all models in modelList
        print('Starting epoch %d / %d' % (epoch_real + 1, epochs_total))
        sampleDict = generateSamples(modelBlock["Meta"]["N"], 5000,
                                     modelBlock["Meta"]["Layers"])
        for key, val in modelBlock.items():
            if (key != "Meta"):
                runEpoch(modelBlock[key]["Model"],
                         modelBlock["Meta"]["Loss_Function"],
                         modelBlock[key]["Optimizer"],
                         modelBlock["Meta"]["Type"], modelBlock[key]["Batch"],
                         sampleDict)
        print('Finishing epoch %d / %d' % (epoch_real + 1, epochs_total))

        # Want to record test error if the total number of epochs is a multiple 50 or this is the final epoch
        if (((epoch_real % 50) == 0) or (epoch == (n_epochs - 1))):

            # Every 50 epochs, evaluate the performance of all the models and print summary statistics
            testDict = generateSamples(modelBlock["Meta"]["N"], 10000,
                                       modelBlock["Meta"]["Layers"])

            ## This code was originally to check the run/hide percentages of the samples generated
            ## It now will not work because the sampler outputs one-hot encoded class labels
            #check = testDict["Label"]
            #frac = (check[check == 1]).sum()/40000
            #print(frac)

            loss = []
            accAll = []

            for key, val in modelBlock.items():
                if (key != "Meta"):
                    model_accAll, model_loss = checkAccuracy(
                        modelBlock[key]["Model"],
                        modelBlock["Meta"]["Loss_Function"],
                        modelBlock["Meta"]["Type"], modelBlock[key]["Batch"],
                        testDict)
                    modelBlock[key]["Loss"] = model_loss
                    modelBlock[key]["Acc_All"] = model_accAll

                    loss.append(model_loss)
                    accAll.append(model_accAll)

            loss_array = np.asarray(loss)
            accAll_array = np.asarray(accAll)

            print('')
            logger.info('Finishing epoch %d / %d' %
                        (epoch_real + 1, epochs_total))
            logger.info('[Loss] Mean:%.6f, Median:%.6f, Best:%.6f' %
                        (np.mean(loss_array), np.median(loss_array),
                         np.min(loss_array)))
            logger.info(
                '[Accuracy (All pixels)] Mean:%.6f, Median:%.6f, Best:%.6f ' %
                (np.mean(accAll_array), np.median(accAll_array),
                 np.min(accAll_array)))
            logger.info('')
            print('')

    # Update the total number of epochs trained
    modelBlock["Meta"]["Epochs_Trained"] = epochs_total
    print(modelBlock["Meta"]["Epochs_Trained"])
예제 #4
0
cmap3._lut[:, -1] = alphas

# General parameters that would get set in other code

image_size = 15
N = image_size
num_nodes = image_size**2

weightMask, diagMask, edgeMask, cornerMask = generateFixedWeightMask_PredPrey(
    image_size)

centerMask = ~(edgeMask + cornerMask)

dtype = torch.FloatTensor

testDict = generateSamples(image_size, 2, 10)

for i in range(3):

    modelBlock_State = torch.load(model_path + model_name[i],
                                  map_location=torch.device('cpu'))
    modelBlock = loadStateDict(modelBlock_State)

    layers = layers_list[i]

    test_dsetPath = torch.utils.data.TensorDataset(testDict["Environment"],
                                                   testDict["Predator"],
                                                   testDict["Range"])
    loader = DataLoader(test_dsetPath, batch_size=32, shuffle=False)

    loss_fn = nn.MSELoss()