Beispiel #1
0
def setup_mnist():

    scenario = data_incremental_benchmark(SplitMNIST(
        n_experiences=5, seed=1), experience_size=0, 
        custom_split_strategy=shrinking_experience_size_split_strategy)
    n_inputs = 784
    nh = 100
    nl = 2
    n_outputs = 10
    model = MLP([n_inputs] + [nh] * nl + [n_outputs])

    return model, scenario
Beispiel #2
0
def evaluate_split_mnist(
    name: str,
    plugins: List[StrategyPlugin],
    seed: int,
    tensorboard_logs_dir: Union[str, Path] = str(TB_DIR),
    verbose: bool = False,
    criterion: Any = CrossEntropyLoss(),
):

    split_mnist = SplitMNIST(n_experiences=5, seed=seed)

    model = SimpleMLP(n_classes=split_mnist.n_classes, input_size=28 * 28)
    # model = SimpleCNN(n_channels=1, n_classes=split_mnist.n_classes)

    tb_logger = TensorboardLogger(tensorboard_logs_dir + f"/split_mnist/{name}/{seed}_{create_time_id()}")

    loggers = [tb_logger]
    if verbose:
        loggers.append(InteractiveLogger())

    cl_strategy = Naive(
        model=model,
        optimizer=SGD(model.parameters(), lr=0.001, momentum=0.9),
        criterion=criterion,
        train_mb_size=32,
        train_epochs=2,
        eval_mb_size=32,
        device=device,
        plugins=plugins,
        evaluator=EvaluationPlugin(
            [
                NormalizedStreamAccuracy(),
                NormalizedExperienceAccuracy(),
                ExperienceMeanRepresentationShift(MeanL2RepresentationShift()),
                ExperienceMeanRepresentationShift(MeanCosineRepresentationShift()),
            ],
            StreamConfusionMatrix(
                num_classes=split_mnist.n_classes,
                image_creator=SortedCMImageCreator(split_mnist.classes_order),
            ),
            loggers=loggers,
        ),
    )

    for i, train_task in enumerate(split_mnist.train_stream, 1):
        cl_strategy.train(train_task, num_workers=0)
        cl_strategy.eval(split_mnist.test_stream[:i])

    tb_logger.writer.flush()
Beispiel #3
0
def main(args):
    # Device config
    device = torch.device(
        f"cuda:{args.cuda}"
        if torch.cuda.is_available() and args.cuda >= 0
        else "cpu"
    )

    # model
    model = SimpleMLP(num_classes=10)

    # Here we show all the MNIST variation we offer in the "classic" benchmarks
    if args.mnist_type == "permuted":
        scenario = PermutedMNIST(n_experiences=5, seed=1)
    elif args.mnist_type == "rotated":
        scenario = RotatedMNIST(
            n_experiences=5, rotations_list=[30, 60, 90, 120, 150], seed=1
        )
    else:
        scenario = SplitMNIST(n_experiences=5, seed=1)

    # Than we can extract the parallel train and test streams
    train_stream = scenario.train_stream
    test_stream = scenario.test_stream

    # Prepare for training & testing
    optimizer = SGD(model.parameters(), lr=0.001, momentum=0.9)
    criterion = CrossEntropyLoss()

    # Continual learning strategy with default logger
    cl_strategy = Naive(
        model,
        optimizer,
        criterion,
        train_mb_size=32,
        train_epochs=100,
        eval_mb_size=32,
        device=device,
        eval_every=1,
        plugins=[EarlyStoppingPlugin(args.patience, "test_stream")],
    )

    # train and test loop
    results = []
    for train_task, test_task in zip(train_stream, test_stream):
        print("Current Classes: ", train_task.classes_in_this_experience)
        cl_strategy.train(train_task, eval_streams=[test_task])
        results.append(cl_strategy.eval(test_stream))
Beispiel #4
0
def main(args):

    # Config
    device = torch.device(f"cuda:{args.cuda}" if torch.cuda.is_available()
                          and args.cuda >= 0 else "cpu")
    # model
    model = MTSimpleMLP()

    # CL Benchmark Creation
    scenario = SplitMNIST(n_experiences=5, return_task_id=True)
    train_stream = scenario.train_stream
    test_stream = scenario.test_stream

    # Prepare for training & testing
    optimizer = Adam(model.parameters(), lr=0.01)
    criterion = CrossEntropyLoss()

    # choose some metrics and evaluation method
    interactive_logger = InteractiveLogger()

    eval_plugin = EvaluationPlugin(
        accuracy_metrics(minibatch=False,
                         epoch=True,
                         experience=True,
                         stream=True),
        forgetting_metrics(experience=True),
        loggers=[interactive_logger],
    )

    # Choose a CL strategy
    strategy = EWC(
        model=model,
        optimizer=optimizer,
        criterion=criterion,
        train_mb_size=128,
        train_epochs=3,
        eval_mb_size=128,
        device=device,
        evaluator=eval_plugin,
        ewc_lambda=0.4,
    )

    # train and test loop
    for train_task in train_stream:
        strategy.train(train_task)
        strategy.eval(test_stream)