【问题标题】:PyTorch AttributeError: 'UNet3D' object has no attribute 'size'PyTorch AttributeError:“UNet3D”对象没有属性“大小”
【发布时间】:2020-11-09 03:31:57
【问题描述】:

我正在使用 Pytorch 制作图像分割迁移学习项目。我正在使用这个预训练模型和 UNet3D 类的权重。 https://github.com/MrGiovanni/ModelsGenesis

当我运行以下代码时,我在调用 MSELoss 的行中收到此错误:“AttributeError: 'DataParallel' object has no attribute 'size'”。

当我删除第一行时,我得到一个类似的错误:“AttributeError: 'UNet3D' object has no attribute 'size'

"

如何将 DataParallel 或 UNet3D 类转换为 MSELoss 可以使用的对象?我现在不需要 DataParallel。我需要运行 UNet3D() 类进行迁移学习。

model = nn.DataParallel(model, device_ids = [i for i in range(torch.cuda.device_count())])
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), conf.lr, momentum=0.9, weight_decay=0.0, nesterov=False)
scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)
initial_epoch=10
for epoch in range(initial_epoch, conf.nb_epoch):
    scheduler.step(epoch)
    model.train()
    for batch_ndx, (x,y) in enumerate(train_loader):
        x, y = x.float().to(device), y.float().to(device)
        pred = model
        loss = criterion(pred, y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-46-20d1943b3498> in <module>
     25         x, y = x.float().to(device), y.float().to(device)
     26         pred = model
---> 27         loss = criterion(pred, y)
     28         optimizer.zero_grad()
     29         loss.backward()

/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--> 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
    430 
    431     def forward(self, input, target):
--> 432         return F.mse_loss(input, target, reduction=self.reduction)
    433 
    434 

/opt/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py in mse_loss(input, target, size_average, reduce, reduction)
   2528                 mse_loss, tens_ops, input, target, size_average=size_average, reduce=reduce,
   2529                 reduction=reduction)
-> 2530     if not (target.size() == input.size()):
   2531         warnings.warn("Using a target size ({}) that is different to the input size ({}). "
   2532                       "This will likely lead to incorrect results due to broadcasting. "

/opt/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in __getattr__(self, name)
    592                 return modules[name]
    593         raise AttributeError("'{}' object has no attribute '{}'".format(
--> 594             type(self).__name__, name))
    595 
    596     def __setattr__(self, name, value):

AttributeError: 'UNet3D' object has no attribute 'size'

【问题讨论】:

  • 我认为从描述来看,错误很可能是完全不同的东西。 (a) 如果您在没有 DataParallel 的情况下运行相同的程序,您会收到错误吗? (b) 你能发布确切的错误吗? (完整的堆栈跟踪)(c)您能否发布产生错误的完整可运行代码?
  • (a) 当我删除 DataParallel 行时,我得到与类 UNet3D() 相同的错误(我在问题中写了这个。)
  • 好的。那样的话,DataParallel 和问题无关吧?
  • 是的,我只需要运行它。我不需要将函数包装到 DataParallel。

标签: deep-learning pytorch transfer-learning


【解决方案1】:

您在这一行有错字:

pred = model

应该是

pred = model(x)

model 是描述网络的 nn.Module 对象。 x, y, pred 是(应该是)火炬张量。

除了这个特殊情况,我认为最好考虑一下如何解决这类问题。

您在某行看到错误(异常)。问题出在哪里,还是更早?你能隔离问题吗?

例如,如果您在调用之前打印出要传递给标准(pred,y)的参数,它们看起来正确吗? (他们没有)

如果您在调用之前创建几个正确形状的张量并传递它们会发生什么? (工作正常)

错误真的在说什么? “AttributeError: 'UNet3D' object has no attribute 'size'” - 好吧,当然它不应该有大小,但为什么代码试图访问它的大小?实际上,为什么代码甚至能够访问该行上的那个对象? (因为模型不应该传递给标准函数 - 对吧?)

也许有用的进一步阅读:https://ericlippert.com/2014/03/05/how-to-debug-small-programs/

【讨论】:

  • 感谢您的回答。我联系了 Models Genesis 的创建者,他们更正了这部分代码。
猜你喜欢
  • 2021-10-29
  • 1970-01-01
  • 2021-10-26
  • 1970-01-01
  • 2021-04-09
  • 1970-01-01
  • 2021-03-12
  • 1970-01-01
相关资源
最近更新 更多