AttributeError: Can‘t pickle local object ‘Stage.__init__.<locals>.<lambda>‘

Posted AI浩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AttributeError: Can‘t pickle local object ‘Stage.__init__.<locals>.<lambda>‘相关的知识,希望对你有一定的参考价值。

问题描述

模型里面有lambda函数,在保存模型的时候出现了如下问题:


Traceback (most recent call last):
  File "D:\\MaxViT_Demo\\train.py", line 226, in <module>
    val_list, pred_list, val_loss, val_acc = val(model_ft, DEVICE, test_loader)
  File "D:\\Users\\wh109\\anaconda3\\lib\\site-packages\\torch\\autograd\\grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "D:\\MaxViT_Demo\\train.py", line 115, in val
    torch.save(model, file_dir + "/" + 'model_' + str(epoch) + '_' + str(round(acc, 3)) + '.pth')
  File "D:\\Users\\wh109\\anaconda3\\lib\\site-packages\\torch\\serialization.py", line 380, in save
    _save(obj, opened_zipfile, pickle_module, pickle_protocol)
  File "D:\\Users\\wh109\\anaconda3\\lib\\site-packages\\torch\\serialization.py", line 590, in _save
    pickler.dump(obj)
AttributeError: Can't pickle local object 'Stage.__init__.<locals>.<lambda>'

Process finished with exit code 1

解决办法

打开serialization.py文件

import pickle

替换为:

import dill as pickle

以上是关于AttributeError: Can‘t pickle local object ‘Stage.__init__.<locals>.<lambda>‘的主要内容,如果未能解决你的问题,请参考以下文章

AttributeError: Can‘t pickle local object ‘Stage.__init__.<locals>.<lambda>‘

pickle load文件时报AttributeError: Can't get attribute 'Cours' on <module '__main__&#

AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm

metaxcan出现“AttributeError: Can only use .str accessor with string values!”报错的解决方案

Unpickling 保存的 pytorch 模型会引发 AttributeError: Can't get attribute 'Net' on <module '__main__' 尽管内联

AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas(