AttributeError: 'tuple' object has no attribute 'dim' in inception model

my code of the model is

class Inception(nn.Module):
  def __init__(self,model,feature_dim=2048,name='inception'):
    super(Inception, self).__init__()
    model.maxpool = nn.AdaptiveMaxPool2d((1))
    del model.fc
    model.fc = lambda x:x
    self.features = model
    self.classifier = nn.Linear(feature_dim,20)

  def forward(self,x):
    features = self.features(x)
    # if not isinstance(features, Variable):
    #   pdb.set_trace()
    #   features = Variable(features).cuda()
    return self.classifier(features)

def inception_v3(pretrained=True):
  model = tv.models.inception_v3(pretrained=pretrained)
  return Inception(model)

while training, got the following error:

Traceback (most recent call last):
  File "", line 146, in <module>                                        [00:36<00:01,  1.16s/it]
  File "", line 141, in main
  File "/media/sjtu/831bebd9-c866-4ece-b878-5dbd68e5ca50/sjtu/CH/image_scene_classification/SC/model/", line 291, in train
  File "/media/sjtu/831bebd9-c866-4ece-b878-5dbd68e5ca50/sjtu/CH/image_scene_classification/SC/model/", line 228, in train_epoch
    output = self.model(inputs)
  File "/home/sjtu/anaconda2/envs/scene/lib/python3.6/site-packages/torch/nn/modules/", line 357, in __call__
    result = self.forward(*input, **kwargs)
  File "/media/sjtu/831bebd9-c866-4ece-b878-5dbd68e5ca50/sjtu/CH/image_scene_classification/SC/model/", line 30, in forward
    return self.classifier(features)
  File "/home/sjtu/anaconda2/envs/scene/lib/python3.6/site-packages/torch/nn/modules/", line 357, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/sjtu/anaconda2/envs/scene/lib/python3.6/site-packages/torch/nn/modules/", line 55, in forward
    return F.linear(input, self.weight, self.bias)
  File "/home/sjtu/anaconda2/envs/scene/lib/python3.6/site-packages/torch/nn/", line 833, in linear
    if input.dim() == 2 and bias is not None:
AttributeError: 'tuple' object has no attribute 'dim'

while set the break point, got the features is torch.Size([50, 2048]) and torch.Size([50, 1000])
anyone get the solution?

It seems aux_logits is set to True, so that your inception model will return two outputs (x and aux_logits).
Therefore features will be a tuple, which throws this error in self.classifier(features).
If you don’t need the aux_logits, set aux_logits=False when constructing the inception model.

1 Like