G-U-N / PyCIL

PyCIL: A Python Toolbox for Class-Incremental Learning
Other
815 stars 138 forks source link

Implementation on Custom Dataset #55

Closed abhishekdey001 closed 1 year ago

abhishekdey001 commented 1 year ago

Hi I would like to know what changes are required for implementing the different CIL methods on custom dataset. I tried to add a custom dataset class in utils/data.py . However I am getting assertion error.

class customdataset(iData):
    use_path = True

    train_trsf = [
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
    ]
    test_trsf = [
        transforms.Resize(256),
        transforms.CenterCrop(224),
    ]
    common_trsf = [
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ]

    class_order = np.arange(15).tolist()

    def download_data(self):
        assert 0, "You should specify the folder of your dataset"
        train_dir = "./data/customdata/train/"
        test_dir = "./data/customdata/val/"

        train_dset = datasets.ImageFolder(train_dir)
        test_dset = datasets.ImageFolder(test_dir)

        self.train_data, self.train_targets = split_images_labels(train_dset.imgs)
        self.test_data, self.test_targets = split_images_labels(test_dset.imgs)

AssertionError:

Traceback (most recent call last):
  File "main.py", line 31, in <module>
    main()
  File "main.py", line 12, in main
    train(args)
  File "/home/developer/trainer.py", line 18, in train
    _train(args)
  File "/home/developer/trainer.py", line 50, in _train
    data_manager = DataManager(
  File "/home/developer/utils/data_manager.py", line 12, in __init__
    self._setup_data(dataset_name, shuffle, seed)
  File "/home/developer/utils/data_manager.py", line 188, in _setup_data
    idata.download_data()
  File "/home/developer/utils/data.py", line 151, in download_data
    assert 0, "You should specify the folder of your dataset"
AssertionError: You should specify the folder of your dataset
G-U-N commented 1 year ago

Hi, simply comment out the assert line should work.

abhishekdey001 commented 1 year ago

Upon commenting the assert line, I am getting the following error

2023-07-12 21:05:19,749 [trainer.py] => init_epochs: 5
2023-07-12 21:05:19,749 [trainer.py] => init_lr: 0.1
2023-07-12 21:05:19,749 [trainer.py] => init_weight_decay: 0.0005
2023-07-12 21:05:19,749 [trainer.py] => expansion_epochs: 3
2023-07-12 21:05:19,749 [trainer.py] => fusion_epochs: 2
2023-07-12 21:05:19,749 [trainer.py] => lr: 0.1
2023-07-12 21:05:19,749 [trainer.py] => batch_size: 4
2023-07-12 21:05:19,749 [trainer.py] => weight_decay: 0.0005
2023-07-12 21:05:19,749 [trainer.py] => num_workers: 8
2023-07-12 21:05:19,749 [trainer.py] => input_size: 256
2023-07-12 21:05:19,749 [trainer.py] => T: 2
2023-07-12 21:05:19,822 [data_manager.py] => [6, 10, 8, 2, 4, 13, 11, 5, 7, 0, 3, 12, 14, 9, 1]
2023-07-12 21:05:19,841 [trainer.py] => All params: 0
2023-07-12 21:05:19,841 [trainer.py] => Trainable params: 0
2023-07-12 21:05:19,866 [beef_iso.py] => Learning on 0-9
2023-07-12 21:05:19,867 [beef_iso.py] => All params: 465324
2023-07-12 21:05:19,867 [beef_iso.py] => Trainable params: 465324
  0%|                                                                                                                                           | 0/5 [00:03<?, ?it/s]
Traceback (most recent call last):
  File "main.py", line 31, in <module>
    main()
  File "main.py", line 12, in main
    train(args)
  File "/home/developer/trainer.py", line 18, in train
    _train(args)
  File "/home/developer/trainer.py", line 65, in _train
    model.incremental_train(data_manager)
  File "/home/developer/models/beef_iso.py", line 105, in incremental_train
    self._train(self.train_loader, self.test_loader,self.val_loader)
  File "/home/developer/models/beef_iso.py", line 134, in _train
    self._init_train(train_loader, test_loader, optimizer, scheduler)
  File "/home/developer/models/beef_iso.py", line 193, in _init_train
    logits = self._network(inputs)["logits"]
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1131, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/developer/utils/inc_net.py", line 676, in forward
    out = fc(features)
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1131, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/developer/convs/linears.py", line 32, in forward
    return {'logits': F.linear(input, self.weight, self.bias)}
RuntimeError: mat1 and mat2 shapes cannot be multiplied (4x3136 and 64x9)
root@5a323a380665:/home/developer# 
zhoudw-zdw commented 1 year ago

Upon commenting the assert line, I am getting the following error

2023-07-12 21:05:19,749 [trainer.py] => init_epochs: 5
2023-07-12 21:05:19,749 [trainer.py] => init_lr: 0.1
2023-07-12 21:05:19,749 [trainer.py] => init_weight_decay: 0.0005
2023-07-12 21:05:19,749 [trainer.py] => expansion_epochs: 3
2023-07-12 21:05:19,749 [trainer.py] => fusion_epochs: 2
2023-07-12 21:05:19,749 [trainer.py] => lr: 0.1
2023-07-12 21:05:19,749 [trainer.py] => batch_size: 4
2023-07-12 21:05:19,749 [trainer.py] => weight_decay: 0.0005
2023-07-12 21:05:19,749 [trainer.py] => num_workers: 8
2023-07-12 21:05:19,749 [trainer.py] => input_size: 256
2023-07-12 21:05:19,749 [trainer.py] => T: 2
2023-07-12 21:05:19,822 [data_manager.py] => [6, 10, 8, 2, 4, 13, 11, 5, 7, 0, 3, 12, 14, 9, 1]
2023-07-12 21:05:19,841 [trainer.py] => All params: 0
2023-07-12 21:05:19,841 [trainer.py] => Trainable params: 0
2023-07-12 21:05:19,866 [beef_iso.py] => Learning on 0-9
2023-07-12 21:05:19,867 [beef_iso.py] => All params: 465324
2023-07-12 21:05:19,867 [beef_iso.py] => Trainable params: 465324
  0%|                                                                                                                                           | 0/5 [00:03<?, ?it/s]
Traceback (most recent call last):
  File "main.py", line 31, in <module>
    main()
  File "main.py", line 12, in main
    train(args)
  File "/home/developer/trainer.py", line 18, in train
    _train(args)
  File "/home/developer/trainer.py", line 65, in _train
    model.incremental_train(data_manager)
  File "/home/developer/models/beef_iso.py", line 105, in incremental_train
    self._train(self.train_loader, self.test_loader,self.val_loader)
  File "/home/developer/models/beef_iso.py", line 134, in _train
    self._init_train(train_loader, test_loader, optimizer, scheduler)
  File "/home/developer/models/beef_iso.py", line 193, in _init_train
    logits = self._network(inputs)["logits"]
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1131, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/developer/utils/inc_net.py", line 676, in forward
    out = fc(features)
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1131, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/developer/convs/linears.py", line 32, in forward
    return {'logits': F.linear(input, self.weight, self.bias)}
RuntimeError: mat1 and mat2 shapes cannot be multiplied (4x3136 and 64x9)
root@5a323a380665:/home/developer# 

If you are training the model with your specific data, I think the encoder should also be redesigned. This error seems to indicate that the embedding dimension cannot be matched between the encoder and the fully connected layer (fc).