Beispiel #1
0
def train_models(configs, project_dir):
    """Run all the training experiments specified in configs."""
    # download dataset
    data_dir = os.path.join(project_dir, "data")
    datasets.CIFAR10(data_dir, download=True, train=True)

    # Run all experiments in serial
    if len(configs) == 0:
        print("No experiments to run!")

    for exp in configs:
        config = configs[exp]
        if "name" not in config:
            config["name"] = exp

        # Make sure local directories are relative to the project location
        path = config.get("path", "results")
        if not os.path.isabs(path):
            config["path"] = os.path.join(project_dir, path)

        data_dir = config.get("data_dir", "data")
        if not os.path.isabs(data_dir):
            config["data_dir"] = os.path.join(project_dir, data_dir)

        model = TinyCIFAR()
        model.model_setup(config)
        for epoch in range(config["iterations"]):
            ret = model.train_epoch(epoch)
            print("epoch=", epoch, ":", ret)
            # if ret['stop'] == 1:
            #   print("Stopping early!")
            #   break

        if config.get("checkpoint_at_end", False):
            model.model_save(path)
Beispiel #2
0
def run_noise_tests(config, options, project_dir):
    """
    Test a pretrained network, specified in this config against noisy vectors.
    """
    # download dataset
    data_dir = os.path.join(project_dir, "data")
    _ = datasets.CIFAR10(data_dir, download=True, train=False)

    # Make sure local directories are relative to the project location
    path = config.get("path", "results")
    if not os.path.isabs(path):
        config["path"] = os.path.join(project_dir, path)

    data_dir = config.get("data_dir", "data")
    if not os.path.isabs(data_dir):
        config["data_dir"] = os.path.join(project_dir, data_dir)

    # Load the pretrained model
    tiny_cifar = TinyCIFAR()
    tiny_cifar.model_setup(config)
    tiny_cifar.model_restore(options.checkpoint_path
                             or os.path.join(path, tiny_cifar.model_filename))

    # Create new loaders with batch size 1
    noise_values = [0.0, 0.025, 0.05, 0.075, 0.1, 0.125, 0.15, 0.175]
    loaders = create_test_loaders(noise_values,
                                  batch_size=64,
                                  data_dir=tiny_cifar.data_dir)

    print("Running full noise tests using noise values", noise_values)

    ret = tiny_cifar.run_noise_tests(noise_values, loaders, 300)

    print(ret)
Beispiel #3
0
def layer_noise_sensitivities(config, options, project_dir):
    """
    Test a pretrained network, specified in this config against noisy vectors.
    """
    # download dataset
    data_dir = os.path.join(project_dir, "data")
    _ = datasets.CIFAR10(data_dir, download=True, train=False)

    # Make sure local directories are relative to the project location
    path = config.get("path", "results")
    if not os.path.isabs(path):
        config["path"] = os.path.join(project_dir, path)

    data_dir = config.get("data_dir", "data")
    if not os.path.isabs(data_dir):
        config["data_dir"] = os.path.join(project_dir, data_dir)

    # Load the pretrained model
    tiny_cifar = TinyCIFAR()
    tiny_cifar.model_setup(config)
    tiny_cifar.model_restore(options.checkpoint_path
                             or os.path.join(path, tiny_cifar.model_filename))

    # Create new loaders with batch size 1
    noise_values = [0.0, 0.05, 0.1, 0.15, 0.2]
    loaders = create_test_loaders(noise_values,
                                  batch_size=1,
                                  data_dir=tiny_cifar.data_dir)
    noise_datasets = [l.dataset for l in loaders]

    # Test noise sensitivity for different layers
    model = tiny_cifar.model

    one_layer_noise_sensitivity(
        model=tiny_cifar.model,
        noise_datasets=noise_datasets,
        layer_name="image",
        num_images=options.num_images,
    )

    modules_to_check = [
        "cnn_0_0",
        "avgpool_0_0",
        "kwinners_2d_0_0",
        "cnn_1_0",
        "avgpool_1_0",
        "kwinners_2d_1_0",
        "flatten",
        "linear_0",
        "kwinners_linear",
    ]
    for m in modules_to_check:
        if m in model._modules.keys():
            one_layer_noise_sensitivity(
                model=tiny_cifar.model,
                noise_datasets=noise_datasets,
                layer_name=m,
                num_images=options.num_images,
            )
Beispiel #4
0
def train_models(configs, project_dir):
    """Run all the training experiments specified in configs."""
    # Default data dir if not specified
    data_dir = os.path.join(os.environ["HOME"], "nta", "data")

    # Run all experiments in serial
    if len(configs) == 0:
        print("No experiments to run!")
        sys.exit()

    for exp in configs:
        config = configs[exp]
        if "name" not in config:
            config["name"] = exp

        # Make sure local directories are relative to the project location
        path = config.get("path", "results")
        path = Path(path).expanduser().resolve()
        config["path"] = path

        data_dir = config.get("data_dir", data_dir)
        data_dir = Path(data_dir).expanduser().resolve()
        config["data_dir"] = data_dir

        datasets.CIFAR10(data_dir, download=True, train=True)

        model = TinyCIFAR()
        model.model_setup(config)
        for epoch in range(config["iterations"]):
            ret = model.train_epoch(epoch)
            print("epoch=", epoch, ":", ret)
            # if ret['stop'] == 1:
            #   print("Stopping early!")
            #   break

        if config.get("checkpoint_at_end", False):
            model.model_save(path)
Beispiel #5
0
 def __init__(self, config=None, logger_creator=None):
     TinyCIFAR.__init__(self)
     tune.Trainable.__init__(self,
                             config=config,
                             logger_creator=logger_creator)
Beispiel #6
0
 def __init__(self, config=None, logger_creator=None):
   TinyCIFAR.__init__(self)
   tune.Trainable.__init__(self, config=config, logger_creator=logger_creator)