Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

discussion: Can we use our own Pytorch models in avalanche stragtegy? #1665

Open
akashgupta1909 opened this issue Sep 9, 2024 · 0 comments

Comments

@akashgupta1909
Copy link

I have been trying to run my own PyTorch model, which is a LightningModule Derivative, using benchmarking experience which i got from avalanche. But it gives the following error:

Training on experience 0
-- >> Start of training phase << --
Traceback (most recent call last):
  File "/Users/akashgupta/Documents/Projects/Python/binary_deepfake_detection/model.py", line 387, in <module>
    strategy.train(experience)  # Train on the current experience
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/avalanche/training/templates/base_sgd.py", line 211, in train
    super().train(experiences, eval_streams, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/avalanche/training/templates/base.py", line 162, in train
    self._before_training_exp(**kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/avalanche/training/templates/base_sgd.py", line 291, in _before_training_exp
    self.make_train_dataloader(**kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/avalanche/training/templates/base_sgd.py", line 456, in make_train_dataloader
    self.dataloader = TaskBalancedDataLoader(
                      ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/avalanche/benchmarks/utils/data_loader.py", line 404, in __init__
    task_labels_field = getattr(data, "targets_task_labels")
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'ClassificationDataset' object has no attribute 'targets_task_labels'
(base) MacBook-Pro:binary_deepfake_detection akashgupta$ ;2B

The code which I've been using:

    train_dataset_wrapped = as_classification_dataset(train_dataset)
    test_dataset_wrapped = as_classification_dataset(val_dataset)
    train_experiences = [train_dataset_wrapped]  # List of training experiences
    test_experiences = [test_dataset_wrapped]    # List of testing experiences

    # Create a benchmark scenario from the experiences
    benchmark = benchmark_from_datasets(
        train=train_experiences,
        test=test_experiences
    )

    # Initialize model, optimizer, and criterion
    model = BNext4DFR(num_classes=2)  # Your model
    optimizer = Adam(model.parameters())
    criterion = nn.CrossEntropyLoss()

    # Setup the strategy
    strategy = Naive(
        model=model,
        optimizer=optimizer,
        criterion=criterion
    )

    # Training loop
    for experience in benchmark.train_stream:
        print(f"Training on experience {experience.current_experience}")
        strategy.train(experience)  # Train on the current experience

    # Evaluation loop
    for experience in benchmark.test_stream:
        print(f"Testing on experience {experience.current_experience}")
        strategy.eval(experience)

Can someone suggest how to make it work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant