yifanjiang19 / sppnet-pytorch

A simple Spatial Pyramid Pooling layer which could be added in CNN
Apache License 2.0
329 stars 133 forks source link

DataLoader not support image with different size #2

Closed mrka124 closed 6 years ago

mrka124 commented 6 years ago

Hi, I have used your SPP layer implement but I found that pytorch not support tensor with different size. So if I want to forward image to the net, I need to to set batch_size=1 like this: dataLoader = DataLoader(dataSet, batch_size=1, shuffle=True) So the training speed is really slow, because we backprop for each image. Do you have any idea for this?

Many thanks

yifanjiang19 commented 6 years ago

I don't know what "pytorch not support tensor with different size." means. Did you mean that you want to input multiple size images at the same time? If so, you should calculate the gradient for each image separately then backward together. You should further read how mini-batch gradient descent is implemented on pytorch.

Angus1996 commented 3 years ago

I have met the same problem. SPP make it easy for us to train images with different size. But if we want to train with a mini batch, we cannot stack two images having different size to be a batch

Traceback (most recent call last): File "main.py", line 54, in for data in train_loader: File "C:\ProgramData\Anaconda3\envs\medical\lib\site-packages\torch\utils\data\dataloader.py", line 560, in next batch = self.collate_fn([self.dataset[i] for i in indices]) File "C:\ProgramData\Anaconda3\envs\medical\lib\site-packages\torch\utils\data_utils\collate.py", line 68, in default_collate return [default_collate(samples) for samples in transposed] File "C:\ProgramData\Anaconda3\envs\medical\lib\site-packages\torch\utils\data_utils\collate.py", line 68, in return [default_collate(samples) for samples in transposed] File "C:\ProgramData\Anaconda3\envs\medical\lib\site-packages\torch\utils\data_utils\collate.py", line 43, in default_collate return torch.stack(batch, 0, out=out) RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 1171 and 573 in dimension 2 at ..\aten\src\TH/generic/THTensor.cpp:711