Exemplo n.º 1
0
def main():
  # Initialize experiment options and parameters
  suite = MNISTSparseExperiment()
  suite.parse_opt()
  suite.parse_cfg()
  experiments = suite.options.experiments or suite.cfgparser.sections()

  pool = multiprocessing.Pool()

  for expName in experiments:
    path = suite.get_exp(expName)[0]
    results = suite.get_exps(path=path)

    # Build argument list for multiprocessing pool
    args = []
    for exp in results:
      params = suite.get_params(exp)
      for i, minWeight in enumerate(np.linspace(0.0, 0.1, 21)):
        args.append((exp, params, minWeight, -1, i))

    args = np.array(args)

    # Analyze weight pruning alone. No dutycycle pruning
    args[:, 3] = -1  # set minDutycycle to -1 for all experiments
    run(pool, expName, "Weight Pruning", args)

    # Analyze dutycycle pruning units with dutycycle below 5% from target density
    args[:, 3] = 0.05  # set minDutycycle to 5% for all experiments
    run(pool, expName, "Dutycycle Pruning (5%)", args)


    # Analyze dutycycle pruning units with dutycycle below 10% from target density
    args[:, 3] = 0.10  # set minDutycycle to 10% for all experiments
    run(pool, expName, "Dutycycle Pruning (10%)", args)
Exemplo n.º 2
0
def main():
  # Initialize experiment options and parameters
  suite = MNISTSparseExperiment()
  suite.parse_opt()
  suite.parse_cfg()
  experiments = suite.options.experiments or suite.cfgparser.sections()

  pool = multiprocessing.Pool()

  for expName in experiments:
    path = suite.get_exp(expName)[0]
    results = suite.get_exps(path=path)

    # Build argument list for multiprocessing pool
    args = []
    for exp in results:
      params = suite.get_params(exp)
      for i, minWeight in enumerate(np.linspace(0.0, 0.1, 21)):
        args.append((exp, params, minWeight, -1, i))

    args = np.array(args)

    # Analyze weight pruning alone. No dutycycle pruning
    args[:, 3] = -1  # set minDutycycle to -1 for all experiments
    run(pool, expName, "weight_prunning", args)

    # Analyze dutycycle pruning units with dutycycle below 5% from target density
    args[:, 3] = 0.05  # set minDutycycle to 5% for all experiments
    run(pool, expName, "dutycycle_pruning", args)
Exemplo n.º 3
0
def main():
    # Set fixed seed. Only works on cpu
    random.seed(42)
    np.random.seed(42)
    torch.manual_seed(42)

    dataset = create_union_mnist_dataset()

    # Load experiment
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    suite = MNISTSparseExperiment()
    suite.parse_opt()
    suite.parse_cfg()
    experiments = suite.options.experiments or suite.cfgparser.sections()
    table = {}
    progress = tqdm(experiments)
    for expName in progress:
        progress.set_description(expName)
        path = suite.get_exp(expName)[0]
        results = suite.get_exps(path=path)
        for exp in results:
            model_file = os.path.join(exp, "model.pt")
            if os.path.exists(model_file):
                model = torch.load(model_file, map_location=device)
                params = suite.get_params(exp)
                test_loader = torch.utils.data.DataLoader(
                    dataset,
                    shuffle=True,
                    batch_size=params["test_batch_size"])
                table[params['name']] = evaluate(model=model,
                                                 loader=test_loader,
                                                 device=device)

    # Random model
    test_loader = torch.utils.data.DataLoader(dataset,
                                              shuffle=True,
                                              batch_size=4)
    table["random"] = evaluate(model=random_model,
                               loader=test_loader,
                               device=device)

    # Save results
    df = pd.DataFrame.from_dict(table)
    df.to_csv("union_experiment_results.csv")
    print(tabulate(df, tablefmt='fancy_grid', headers='keys',
                   numalign="right"))

    # Plot first 100 images in the dataset
    fig = plt.figure(figsize=(10, 10))
    for i in range(100):
        ax = fig.add_subplot(10, 10, i + 1)
        ax.set_axis_off()
        img, label = dataset[i]
        ax.imshow(img.numpy().reshape((28, 28)), cmap='gray')
        ax.set_title(str(label.numpy()))
    plt.tight_layout()
    plt.savefig("union_experiment_sample_images.png")
    plt.close()
Exemplo n.º 4
0
    for i, scoresForRepetition in enumerate(
            zip(results["testerror"], results["totalCorrect"])):
        maxTestScore, bestEpoch, maxNoiseScore = bestScore(scoresForRepetition)
        testScores[i] = maxTestScore
        noiseScores[i] = maxNoiseScore

    return {
        "test_score": (testScores.mean(), testScores.std()),
        "noise_score": (noiseScores.mean(), noiseScores.std())
    }


if __name__ == '__main__':
    suite = MNISTSparseExperiment()
    suite.parse_opt()
    suite.parse_cfg()
    experiments = suite.options.experiments or suite.cfgparser.sections()

    testScoresTable = [["Network", "Test Score", "Noise Score"]]
    for expName in experiments:
        results = suite.get_exp(expName)
        for exp in results:
            if not os.path.exists(exp):
                continue

            errorBars = getErrorBars(exp, suite)
            test_score = u"{0:.2f} ± {1:.2f}".format(*errorBars["test_score"])
            noise_score = u"{0:,.0f} ± {1:.2f}".format(
                *errorBars["noise_score"])

            params = suite.get_params(exp=exp)