'AttributeError: 'collections.OrderedDict' object has no attribute 'size'

I done a model which make segmentation of image. Downloaded a pretrained model, but when I want to submit an image (256, 256, 3) and see the dimensions of tensors, an error pops up. What can be wrong?

resnet = torchvision.models.segmentation.segmentation.deeplabv3_resnet50(pretrained=True).eval().to(device)
model = resnet.to(device)
summary(model,(3,256,256))



Solution 1:[1]

As explained in the documentation page (and source code) DeepLabV3 returns an OrderedDict[Tensor]:

The model returns an OrderedDict with two Tensors that are of the same height and width as the input Tensor, but with 21 classes. output['out'] contains the semantic masks, and output['aux'] contains the auxillary loss values per-pixel. In inference mode, output['aux'] is not useful. So, output['out'] is of shape (N, 21, H, W).

However, in torchsummary's source code, they only check whether the output for a given layer is a tuple, a list or else. The latter case is where where Tensor and OrderedDict[Tensor] falls into, leading to an error.

For the purpose of 'solving' this question, you can monkey patch torchsummary since it's only a hundred or so lines long. A quick - and bad - fix would be to handle this special case and use output['out'] instead of output:

        if isinstance(output, OrderedDict):
            output = output['out']

The resulting summary for this model is:

----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1         [-1, 64, 128, 128]           9,408
       BatchNorm2d-2         [-1, 64, 128, 128]             128
              ReLU-3         [-1, 64, 128, 128]               0
         MaxPool2d-4           [-1, 64, 64, 64]               0
            Conv2d-5           [-1, 64, 64, 64]           4,096
       BatchNorm2d-6           [-1, 64, 64, 64]             128
              ReLU-7           [-1, 64, 64, 64]               0
            Conv2d-8           [-1, 64, 64, 64]          36,864
       BatchNorm2d-9           [-1, 64, 64, 64]             128
             ReLU-10           [-1, 64, 64, 64]               0
           Conv2d-11          [-1, 256, 64, 64]          16,384
      BatchNorm2d-12          [-1, 256, 64, 64]             512
           Conv2d-13          [-1, 256, 64, 64]          16,384
      BatchNorm2d-14          [-1, 256, 64, 64]             512
             ReLU-15          [-1, 256, 64, 64]               0
       Bottleneck-16          [-1, 256, 64, 64]               0
           Conv2d-17           [-1, 64, 64, 64]          16,384
      BatchNorm2d-18           [-1, 64, 64, 64]             128
             ReLU-19           [-1, 64, 64, 64]               0
           Conv2d-20           [-1, 64, 64, 64]          36,864
      BatchNorm2d-21           [-1, 64, 64, 64]             128
             ReLU-22           [-1, 64, 64, 64]               0
           Conv2d-23          [-1, 256, 64, 64]          16,384
      BatchNorm2d-24          [-1, 256, 64, 64]             512
             ReLU-25          [-1, 256, 64, 64]               0
       Bottleneck-26          [-1, 256, 64, 64]               0
           Conv2d-27           [-1, 64, 64, 64]          16,384
      BatchNorm2d-28           [-1, 64, 64, 64]             128
             ReLU-29           [-1, 64, 64, 64]               0
           Conv2d-30           [-1, 64, 64, 64]          36,864
      BatchNorm2d-31           [-1, 64, 64, 64]             128
             ReLU-32           [-1, 64, 64, 64]               0
           Conv2d-33          [-1, 256, 64, 64]          16,384
      BatchNorm2d-34          [-1, 256, 64, 64]             512
             ReLU-35          [-1, 256, 64, 64]               0
       Bottleneck-36          [-1, 256, 64, 64]               0
           Conv2d-37          [-1, 128, 64, 64]          32,768
      BatchNorm2d-38          [-1, 128, 64, 64]             256
             ReLU-39          [-1, 128, 64, 64]               0
           Conv2d-40          [-1, 128, 32, 32]         147,456
      BatchNorm2d-41          [-1, 128, 32, 32]             256
             ReLU-42          [-1, 128, 32, 32]               0
           Conv2d-43          [-1, 512, 32, 32]          65,536
      BatchNorm2d-44          [-1, 512, 32, 32]           1,024
           Conv2d-45          [-1, 512, 32, 32]         131,072
      BatchNorm2d-46          [-1, 512, 32, 32]           1,024
             ReLU-47          [-1, 512, 32, 32]               0
       Bottleneck-48          [-1, 512, 32, 32]               0
           Conv2d-49          [-1, 128, 32, 32]          65,536
      BatchNorm2d-50          [-1, 128, 32, 32]             256
             ReLU-51          [-1, 128, 32, 32]               0
           Conv2d-52          [-1, 128, 32, 32]         147,456
      BatchNorm2d-53          [-1, 128, 32, 32]             256
             ReLU-54          [-1, 128, 32, 32]               0
           Conv2d-55          [-1, 512, 32, 32]          65,536
      BatchNorm2d-56          [-1, 512, 32, 32]           1,024
             ReLU-57          [-1, 512, 32, 32]               0
       Bottleneck-58          [-1, 512, 32, 32]               0
           Conv2d-59          [-1, 128, 32, 32]          65,536
      BatchNorm2d-60          [-1, 128, 32, 32]             256
             ReLU-61          [-1, 128, 32, 32]               0
           Conv2d-62          [-1, 128, 32, 32]         147,456
      BatchNorm2d-63          [-1, 128, 32, 32]             256
             ReLU-64          [-1, 128, 32, 32]               0
           Conv2d-65          [-1, 512, 32, 32]          65,536
      BatchNorm2d-66          [-1, 512, 32, 32]           1,024
             ReLU-67          [-1, 512, 32, 32]               0
       Bottleneck-68          [-1, 512, 32, 32]               0
           Conv2d-69          [-1, 128, 32, 32]          65,536
      BatchNorm2d-70          [-1, 128, 32, 32]             256
             ReLU-71          [-1, 128, 32, 32]               0
           Conv2d-72          [-1, 128, 32, 32]         147,456
      BatchNorm2d-73          [-1, 128, 32, 32]             256
             ReLU-74          [-1, 128, 32, 32]               0
           Conv2d-75          [-1, 512, 32, 32]          65,536
      BatchNorm2d-76          [-1, 512, 32, 32]           1,024
             ReLU-77          [-1, 512, 32, 32]               0
       Bottleneck-78          [-1, 512, 32, 32]               0
           Conv2d-79          [-1, 256, 32, 32]         131,072
      BatchNorm2d-80          [-1, 256, 32, 32]             512
             ReLU-81          [-1, 256, 32, 32]               0
           Conv2d-82          [-1, 256, 32, 32]         589,824
      BatchNorm2d-83          [-1, 256, 32, 32]             512
             ReLU-84          [-1, 256, 32, 32]               0
           Conv2d-85         [-1, 1024, 32, 32]         262,144
      BatchNorm2d-86         [-1, 1024, 32, 32]           2,048
           Conv2d-87         [-1, 1024, 32, 32]         524,288
      BatchNorm2d-88         [-1, 1024, 32, 32]           2,048
             ReLU-89         [-1, 1024, 32, 32]               0
       Bottleneck-90         [-1, 1024, 32, 32]               0
           Conv2d-91          [-1, 256, 32, 32]         262,144
      BatchNorm2d-92          [-1, 256, 32, 32]             512
             ReLU-93          [-1, 256, 32, 32]               0
           Conv2d-94          [-1, 256, 32, 32]         589,824
      BatchNorm2d-95          [-1, 256, 32, 32]             512
             ReLU-96          [-1, 256, 32, 32]               0
           Conv2d-97         [-1, 1024, 32, 32]         262,144
      BatchNorm2d-98         [-1, 1024, 32, 32]           2,048
             ReLU-99         [-1, 1024, 32, 32]               0
      Bottleneck-100         [-1, 1024, 32, 32]               0
          Conv2d-101          [-1, 256, 32, 32]         262,144
     BatchNorm2d-102          [-1, 256, 32, 32]             512
            ReLU-103          [-1, 256, 32, 32]               0
          Conv2d-104          [-1, 256, 32, 32]         589,824
     BatchNorm2d-105          [-1, 256, 32, 32]             512
            ReLU-106          [-1, 256, 32, 32]               0
          Conv2d-107         [-1, 1024, 32, 32]         262,144
     BatchNorm2d-108         [-1, 1024, 32, 32]           2,048
            ReLU-109         [-1, 1024, 32, 32]               0
      Bottleneck-110         [-1, 1024, 32, 32]               0
          Conv2d-111          [-1, 256, 32, 32]         262,144
     BatchNorm2d-112          [-1, 256, 32, 32]             512
            ReLU-113          [-1, 256, 32, 32]               0
          Conv2d-114          [-1, 256, 32, 32]         589,824
     BatchNorm2d-115          [-1, 256, 32, 32]             512
            ReLU-116          [-1, 256, 32, 32]               0
          Conv2d-117         [-1, 1024, 32, 32]         262,144
     BatchNorm2d-118         [-1, 1024, 32, 32]           2,048
            ReLU-119         [-1, 1024, 32, 32]               0
      Bottleneck-120         [-1, 1024, 32, 32]               0
          Conv2d-121          [-1, 256, 32, 32]         262,144
     BatchNorm2d-122          [-1, 256, 32, 32]             512
            ReLU-123          [-1, 256, 32, 32]               0
          Conv2d-124          [-1, 256, 32, 32]         589,824
     BatchNorm2d-125          [-1, 256, 32, 32]             512
            ReLU-126          [-1, 256, 32, 32]               0
          Conv2d-127         [-1, 1024, 32, 32]         262,144
     BatchNorm2d-128         [-1, 1024, 32, 32]           2,048
            ReLU-129         [-1, 1024, 32, 32]               0
      Bottleneck-130         [-1, 1024, 32, 32]               0
          Conv2d-131          [-1, 256, 32, 32]         262,144
     BatchNorm2d-132          [-1, 256, 32, 32]             512
            ReLU-133          [-1, 256, 32, 32]               0
          Conv2d-134          [-1, 256, 32, 32]         589,824
     BatchNorm2d-135          [-1, 256, 32, 32]             512
            ReLU-136          [-1, 256, 32, 32]               0
          Conv2d-137         [-1, 1024, 32, 32]         262,144
     BatchNorm2d-138         [-1, 1024, 32, 32]           2,048
            ReLU-139         [-1, 1024, 32, 32]               0
      Bottleneck-140         [-1, 1024, 32, 32]               0
          Conv2d-141          [-1, 512, 32, 32]         524,288
     BatchNorm2d-142          [-1, 512, 32, 32]           1,024
            ReLU-143          [-1, 512, 32, 32]               0
          Conv2d-144          [-1, 512, 32, 32]       2,359,296
     BatchNorm2d-145          [-1, 512, 32, 32]           1,024
            ReLU-146          [-1, 512, 32, 32]               0
          Conv2d-147         [-1, 2048, 32, 32]       1,048,576
     BatchNorm2d-148         [-1, 2048, 32, 32]           4,096
          Conv2d-149         [-1, 2048, 32, 32]       2,097,152
     BatchNorm2d-150         [-1, 2048, 32, 32]           4,096
            ReLU-151         [-1, 2048, 32, 32]               0
      Bottleneck-152         [-1, 2048, 32, 32]               0
          Conv2d-153          [-1, 512, 32, 32]       1,048,576
     BatchNorm2d-154          [-1, 512, 32, 32]           1,024
            ReLU-155          [-1, 512, 32, 32]               0
          Conv2d-156          [-1, 512, 32, 32]       2,359,296
     BatchNorm2d-157          [-1, 512, 32, 32]           1,024
            ReLU-158          [-1, 512, 32, 32]               0
          Conv2d-159         [-1, 2048, 32, 32]       1,048,576
     BatchNorm2d-160         [-1, 2048, 32, 32]           4,096
            ReLU-161         [-1, 2048, 32, 32]               0
      Bottleneck-162         [-1, 2048, 32, 32]               0
          Conv2d-163          [-1, 512, 32, 32]       1,048,576
     BatchNorm2d-164          [-1, 512, 32, 32]           1,024
            ReLU-165          [-1, 512, 32, 32]               0
          Conv2d-166          [-1, 512, 32, 32]       2,359,296
     BatchNorm2d-167          [-1, 512, 32, 32]           1,024
            ReLU-168          [-1, 512, 32, 32]               0
          Conv2d-169         [-1, 2048, 32, 32]       1,048,576
     BatchNorm2d-170         [-1, 2048, 32, 32]           4,096
            ReLU-171         [-1, 2048, 32, 32]               0
      Bottleneck-172         [-1, 2048, 32, 32]               0
IntermediateLayerGetter-173         [-1, 2048, 32, 32]               0
          Conv2d-174          [-1, 256, 32, 32]         524,288
     BatchNorm2d-175          [-1, 256, 32, 32]             512
            ReLU-176          [-1, 256, 32, 32]               0
          Conv2d-177          [-1, 256, 32, 32]       4,718,592
     BatchNorm2d-178          [-1, 256, 32, 32]             512
            ReLU-179          [-1, 256, 32, 32]               0
          Conv2d-180          [-1, 256, 32, 32]       4,718,592
     BatchNorm2d-181          [-1, 256, 32, 32]             512
            ReLU-182          [-1, 256, 32, 32]               0
          Conv2d-183          [-1, 256, 32, 32]       4,718,592
     BatchNorm2d-184          [-1, 256, 32, 32]             512
            ReLU-185          [-1, 256, 32, 32]               0
AdaptiveAvgPool2d-186           [-1, 2048, 1, 1]               0
          Conv2d-187            [-1, 256, 1, 1]         524,288
     BatchNorm2d-188            [-1, 256, 1, 1]             512
            ReLU-189            [-1, 256, 1, 1]               0
          Conv2d-190          [-1, 256, 32, 32]         327,680
     BatchNorm2d-191          [-1, 256, 32, 32]             512
            ReLU-192          [-1, 256, 32, 32]               0
         Dropout-193          [-1, 256, 32, 32]               0
            ASPP-194          [-1, 256, 32, 32]               0
          Conv2d-195          [-1, 256, 32, 32]         589,824
     BatchNorm2d-196          [-1, 256, 32, 32]             512
            ReLU-197          [-1, 256, 32, 32]               0
          Conv2d-198           [-1, 21, 32, 32]           5,397
          Conv2d-199          [-1, 256, 32, 32]       2,359,296
     BatchNorm2d-200          [-1, 256, 32, 32]             512
            ReLU-201          [-1, 256, 32, 32]               0
         Dropout-202          [-1, 256, 32, 32]               0
          Conv2d-203           [-1, 21, 32, 32]           5,397
       DeepLabV3-204         [-1, 21, 256, 256]               0
================================================================
Total params: 42,004,074
Trainable params: 42,004,074
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.75
Forward/backward pass size (MB): 929.85
Params size (MB): 160.23
Estimated Total Size (MB): 1090.83
----------------------------------------------------------------

Solution 2:[2]

There's a high chance you're trying to get a value off an OrderedDict by doing foo.bar, replace this with foo["bar"] instead, at least this was the problem in my case.

For the exact point in your code that triggered the error, read the stack-traces and find a directory that isn't from a third party package (one that you authored).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ivan
Solution 2 xodeeq