kazuto1011 / deeplab-pytorch

PyTorch re-implementation of DeepLab v2 on COCO-Stuff / PASCAL VOC datasets
MIT License
1.09k stars 282 forks source link

Support customize input for DeeplabV3+? #7

Closed QiaoranC closed 6 years ago

QiaoranC commented 6 years ago

Hi: Thanks for the DeepLabV3+ in pytorch implementation, it may the first DeepLabV3+ pytorch implementation in github and i wait for this for a while.
My problem is it works fine in the default input size (513,513), But when i tried to implement the DeepLabV3+ with a different input size, like (256,513). it given the following error:

>>> model = DeepLabV3Plus(n_classes=21, n_blocks=[3, 4, 23, 3], pyramids=[6, 12, 18])
>>> image = torch.autograd.Variable(torch.randn(1, 3, 256, 513), volatile=True)
>>> print model(image)[0].size()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 357, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/ubuntu/skin_demo/Tooth/deeplabV3/deeplab-pytorch-master/libs/models/msc.py", line 37, in forward
    self.interp100(output050),
  File "/usr/local/lib/python2.7/dist-packages/torch/functional.py", line 64, in stack
    return torch.cat(inputs, dim)
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 260 and 132 in dimension 4 at /pytorch/torch/lib/TH/generic/THTensorMath.c:2897

My pytorch version is '0.3.1' Any suggestion to support customize input?

kazuto1011 commented 6 years ago

The MSC class now refers only height size (see this block). Please modify the part like:

input_size = x.shape[2:]  # returns (height, width)
size100 = output100.shape[2:]
size075 = [int(s * 0.75) for s in input_size]
size050 = [int(s * 0.5) for s in input_size]

self.interp075 = nn.Upsample(size=size075, mode='bilinear')
self.interp050 = nn.Upsample(size=size050, mode='bilinear')
self.interp100 = nn.Upsample(size=size100, mode='bilinear')