havakv / pycox

Survival analysis with PyTorch
BSD 2-Clause "Simplified" License
803 stars 188 forks source link

Facing error for batch size that is greater than 1 #132

Open mahootiha-maryam opened 2 years ago

mahootiha-maryam commented 2 years ago

Hi Havard. I have 56, 3d MRI images(NIFTI). I preprocessed them with Monai library. I made data loader for them.

dl_train = DataLoader(dataset_train, batch_size=10, shuffle=True, collate_fn=collate_fn) dl_val = DataLoader(dataset_val, batch_size=10, shuffle=True, collate_fn=collate_fn)

when the batch_size is 1 I don't get error but when I want to change it to 2,3 and ... I face this error

File ~/anaconda3/envs/myenv/lib/python3.9/site-packages/torch/utils/data/_utils/fetch.py:52 in fetch return self.collate_fn(data)

File ~/Documents/survival_analysis/survival_estimate.py:42 in collate_fn return tt.tuplefy(batch).stack()

File ~/anaconda3/envs/myenv/lib/python3.9/site-packages/torchtuples/tupletree.py:534 in stack return stack(self, dim)

File ~/anaconda3/envs/myenv/lib/python3.9/site-packages/torchtuples/tupletree.py:251 in stack raise ValueError("Shapes of merged arrays need to be the same")

ValueError: Shapes of merged arrays need to be the same

The main error is for this line: log = model.fit_dataloader(dl_train, epochs, callbacks, verbose, val_dataloader=dl_val)

havakv commented 2 years ago

It seems to be complaining about your training samples have different sizes. It's probably either that or that you should write a different collate_fn that fits your data. Maybe try to look into that and read though #92, and if you're not getting anywhere, you should post a minimal example, so I can run it and reproduce your error