lucidrains / vit-pytorch

Implementation of Vision Transformer, a simple way to achieve SOTA in vision classification with only a single transformer encoder, in Pytorch

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RuntimeError: stack expects a non-empty TensorList

maxboels opened this issue · comments

Hi,

I tried to add the Extractor and Recorder methods to access the attns and embeddings but can't make it work.

Without those new lines, my code runs well.

Any idea what might by the issue here?

Thank you

` model = model.to(device)

    # create train loop
    for epoch in range(self.epochs):
        phase_loss_running = 0
        phase_acc_running = 0
        for i_train, (images, phase_train_label, video_idx) in tqdm(enumerate(train_dataloader, start=1)):
            if torch.cuda.is_available():
                images = images.to(device)
                phase_train_label = phase_train_label.to(device)
            
            optimizer.zero_grad(set_to_none = set_grad_to_none)
            if auto_cast:
                with autocast():
                    model = Recorder(model)
                    pred_train, attns = model(images)
                    if i_train == 1:
                        print("attensions.shape: ", attns.shape)
                    model = model.eject()
                    model = Extractor(model)
                    logits, embeddings = model(images)
                    if i_train == 1:
                        print("embeddings.shape: ", embeddings.shape)`

The Error:

File "train_vit.py", line 280, in __init__ pred_train, attns = model(images) File "/home/localadmin/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl return forward_call(*input, **kwargs) File "/home/localadmin/.local/lib/python3.8/site-packages/vit_pytorch/recorder.py", line 58, in forward attns = torch.stack(recordings, dim = 1) RuntimeError: stack expects a non-empty TensorList

@maxboels hey Maxence, just put in a fix for that specific error 25b3842 are you using the base ViT class for model? i'm afraid the Extractor only works with ViT for now

@lucidrains Hey Phil, Yes using the ViT. Just fixed the error this morning. Wasn't really related to your code. So feel free to archive/delete this issue.
Thanks for the incredible work you do.