简体   繁体   English

错误:_pickle.PicklingError:不能腌制<function <lambda>在 0x0000002F2175B048&gt;:属性查找<lambda>在 __main__ 上失败

[英]Error :_pickle.PicklingError: Can't pickle <function <lambda> at 0x0000002F2175B048>: attribute lookup <lambda> on __main__ failed

I am trying to run following code that reported running well with other users, but I found this error.我正在尝试运行以下报告与其他用户运行良好的代码,但我发现了这个错误。

- - coding: utf-8 - - - - 编码:utf-8 - -

Import the Stuff导入东西

import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils import data
from torch.utils.data import DataLoader
import torchvision.transforms as transforms

import cv2

import numpy as np

import csv

Step1: Read from the log file Step1:从日志文件中读取

samples = []
with open('data/driving_log.csv') as csvfile:
    reader = csv.reader(csvfile)
    next(reader, None)
    for line in reader:
        samples.append(line)
    

Step2: Divide the data into training set and validation set Step2:将数据划分为训练集和验证集

train_len = int(0.8*len(samples))
valid_len = len(samples) - train_len
train_samples, validation_samples = data.random_split(samples, lengths=[train_len, valid_len])

Step3a: Define the augmentation, transformation processes, parameters and dataset for dataloader Step3a:定义数据加载器的增强、转换过程、参数和数据集

def augment(imgName, angle):
  name = 'data/IMG/' + imgName.split('/')[-1]
  current_image = cv2.imread(name)
  current_image = current_image[65:-25, :, :]
  if np.random.rand() < 0.5:
    current_image = cv2.flip(current_image, 1)
    angle = angle * -1.0  
  return current_image, angle

class Dataset(data.Dataset):

    def __init__(self, samples, transform=None):

        self.samples = samples
        self.transform = transform

    def __getitem__(self, index):
      
        batch_samples = self.samples[index]
        
        steering_angle = float(batch_samples[3])
        
        center_img, steering_angle_center = augment(batch_samples[0], steering_angle)
        left_img, steering_angle_left = augment(batch_samples[1], steering_angle + 0.4)
        right_img, steering_angle_right = augment(batch_samples[2], steering_angle - 0.4)

        center_img = self.transform(center_img)
        left_img = self.transform(left_img)
        right_img = self.transform(right_img)

        return (center_img, steering_angle_center), (left_img, steering_angle_left), (right_img, steering_angle_right)
      
    def __len__(self):
        return len(self.samples)

Step3b: Creating generator using the dataloader to parallasize the process Step3b:使用数据加载器创建生成器来并行化进程

transformations = transforms.Compose([transforms.Lambda(lambda x: (x / 255.0) - 0.5)])

params = {'batch_size': 32,
          'shuffle': True,
          'num_workers': 4}

training_set = Dataset(train_samples, transformations)
training_generator = data.DataLoader(training_set, **params)

validation_set = Dataset(validation_samples, transformations)
validation_generator = data.DataLoader(validation_set, **params)

Step4: Define the network Step4:定义网络

class NetworkDense(nn.Module):类 NetworkDense(nn.Module):

def __init__(self):
    super(NetworkDense, self).__init__()
    self.conv_layers = nn.Sequential(
        nn.Conv2d(3, 24, 5, stride=2),
        nn.ELU(),
        nn.Conv2d(24, 36, 5, stride=2),
        nn.ELU(),
        nn.Conv2d(36, 48, 5, stride=2),
        nn.ELU(),
        nn.Conv2d(48, 64, 3),
        nn.ELU(),
        nn.Conv2d(64, 64, 3),
        nn.Dropout(0.25)
    )
    self.linear_layers = nn.Sequential(
        nn.Linear(in_features=64 * 2 * 33, out_features=100),
        nn.ELU(),
        nn.Linear(in_features=100, out_features=50),
        nn.ELU(),
        nn.Linear(in_features=50, out_features=10),
        nn.Linear(in_features=10, out_features=1)
    )
    
def forward(self, input):  
    input = input.view(input.size(0), 3, 70, 320)
    output = self.conv_layers(input)
    output = output.view(output.size(0), -1)
    output = self.linear_layers(output)
    return output


class NetworkLight(nn.Module):

def __init__(self):
    super(NetworkLight, self).__init__()
    self.conv_layers = nn.Sequential(
        nn.Conv2d(3, 24, 3, stride=2),
        nn.ELU(),
        nn.Conv2d(24, 48, 3, stride=2),
        nn.MaxPool2d(4, stride=4),
        nn.Dropout(p=0.25)
    )
    self.linear_layers = nn.Sequential(
        nn.Linear(in_features=48*4*19, out_features=50),
        nn.ELU(),
        nn.Linear(in_features=50, out_features=10),
        nn.Linear(in_features=10, out_features=1)
    )
    

def forward(self, input):
    input = input.view(input.size(0), 3, 70, 320)
    output = self.conv_layers(input)
    output = output.view(output.size(0), -1)
    output = self.linear_layers(output)
    return output

Step5: Define optimizer Step5:定义优化器

model = NetworkLight()
optimizer = optim.Adam(model.parameters(), lr=0.0001)

criterion = nn.MSELoss()

Step6: Check the device and define function to move tensors to that device Step6:检查设备并定义函数以将张量移动到该设备

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') 
print('device is: ', device)

def toDevice(datas, device):
  
  imgs, angles = datas
  return imgs.float().to(device), angles.float().to(device)

Step7: Train and validate network based on maximum epochs defined Step7:根据定义的最大时期数训练和验证网络

max_epochs = 22

for epoch in range(max_epochs):
    
    model.to(device)
    
    # Training
    train_loss = 0
    model.train()
    for local_batch, (centers, lefts, rights) in enumerate(training_generator):
        # Transfer to GPU
        centers, lefts, rights = toDevice(centers, device), toDevice(lefts, device), toDevice(rights, device)
        
        # Model computations
        optimizer.zero_grad()
        datas = [centers, lefts, rights]        
        for data in datas:
            imgs, angles = data
#             print("training image: ", imgs.shape)
            outputs = model(imgs)
            loss = criterion(outputs, angles.unsqueeze(1))
            loss.backward()
            optimizer.step()

            train_loss += loss.data[0].item()
            
        if local_batch % 100 == 0:
            print('Loss: %.3f '
                 % (train_loss/(local_batch+1)))

    
    # Validation
    model.eval()
    valid_loss = 0
    with torch.set_grad_enabled(False):
        for local_batch, (centers, lefts, rights) in enumerate(validation_generator):
            # Transfer to GPU
            centers, lefts, rights = toDevice(centers, device), toDevice(lefts, device), toDevice(rights, device)
        
            # Model computations
            optimizer.zero_grad()
            datas = [centers, lefts, rights]        
            for data in datas:
                imgs, angles = data
#                 print("Validation image: ", imgs.shape)
                outputs = model(imgs)
                loss = criterion(outputs, angles.unsqueeze(1))
                
                valid_loss += loss.data[0].item()

            if local_batch % 100 == 0:
                print('Valid Loss: %.3f '
                     % (valid_loss/(local_batch+1)))

Step8: Define state and save the model wrt to state Step8:定义状态并将模型wrt保存到状态

state = {
        'model': model.module if device == 'cuda' else model,
        }

torch.save(state, 'model.h5')

this is the error message:这是错误消息:

"D:\VICO\Back up\venv\Scripts\python.exe" "D:/VICO/Back up/venv/Scripts/self_driving_car.py"
device is:  cpu
Traceback (most recent call last):
  File "D:/VICO/Back up/venv/Scripts/self_driving_car.py", line 163, in <module>
    for local_batch, (centers, lefts, rights) in enumerate(training_generator):
  File "D:\VICO\Back up\venv\lib\site-packages\torch\utils\data\dataloader.py", line 291, in __iter__
    return _MultiProcessingDataLoaderIter(self)
  File "D:\VICO\Back up\venv\lib\site-packages\torch\utils\data\dataloader.py", line 737, in __init__
    w.start()
  File "C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Users\isonata\AppData\Local\Programs\Python\Python37\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function <lambda> at 0x0000002F2175B048>: attribute lookup <lambda> on __main__ failed

Process finished with exit code 1

I am not sure the next step to resolve the problem.我不确定解决问题的下一步。

pickle doesn't pickle function objects. pickle不pickle 函数对象。 It expects to find the function object by importing its module and looking up its name.它期望通过导入其模块并查找其名称来找到函数对象。 lambdas are anonymous functions (no name) so that doesn't work. lambdas 是匿名函数(无名称),因此不起作用。 The solution is to name the function at module level.解决方案是在模块级别命名函数。 The only lambda I found in your code is我在您的代码中找到的唯一 lambda 是

transformations = transforms.Compose([transforms.Lambda(lambda x: (x / 255.0) - 0.5)])

Assuming that's the troublesome function, you can假设这是麻烦的功能,你可以

def _my_normalization(x):
    return x/255.0 - 0.5

transformations = transforms.Compose([transforms.Lambda(_my_normalization])

You may have other problems because it looks like you are doing work at module level.您可能会遇到其他问题,因为您似乎是在模块级别进行工作。 If this is a multiprocessing thing and you are running on windows, the new process will import the file and run all of that module level code again.如果这是一个多处理的事情并且您在 Windows 上运行,则新进程将导入文件并再次运行所有模块级代码。 This isn't a problem on linux/mac where a forked process already has the modules loaded from the parent.这在 linux/mac 上不是问题,因为分叉进程已经从父进程加载了模块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pickle.PicklingError: 不能腌制<function past_match_sim at 0x7fa26e03b7b8> : __main__ 上的属性查找 past_match_sim 失败</function> - pickle.PicklingError: Can't pickle <function past_match_sim at 0x7fa26e03b7b8>: attribute lookup past_match_sim on __main__ failed Python:_pickle.PicklingError:无法腌制<function <lambda> ></function> - Python: _pickle.PicklingError: Can't pickle <function <lambda>> Python Pytorch 多处理 Pycharm PicklingError: Can't pickle<function train at 0x7f4064696e18> : __main__ 上的属性查找训练失败</function> - Python Pytorch Multiprocessing Pycharm PicklingError: Can't pickle <function train at 0x7f4064696e18>: attribute lookup train on __main__ failed pickle.PicklingError:不能腌制 <function func at 0x02B3C1B0> :找不到它为__main __。func - pickle.PicklingError: Can't pickle <function func at 0x02B3C1B0>: it's not found as __main__.func Python:多重处理-肥皂水-_pickle.PicklingError:无法腌制 <class> 属性查找失败 - Python: Multiprocessing - Suds - _pickle.PicklingError: Can't pickle <class> attribute lookup failed pickle.PicklingError:不能腌制 <class 'module'> :内建属性查找模块失败 - pickle.PicklingError: Can't pickle <class 'module'>: attribute lookup module on builtins failed 多处理酸洗错误:_pickle.PicklingError:不能酸洗<function myProcess at 0x02B2D420> :它与 __main__.myProcess 不是同一个对象 - multiprocessing pickling error: _pickle.PicklingError: Can't pickle <function myProcess at 0x02B2D420>: it's not the same object as __main__.myProcess 当我尝试运行代码时,出现错误:PickingError:不能腌制 <type 'function'> :属性查找__builtin __。function失败 - when I try to run the code, it get the error:PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed PicklingError:无法腌制 <type 'function'> :属性查找__builtin __。function失败(多处理) - PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed (multiprocessing) pickle.PicklingError: Can't pickle: 它与 object 不同 - pickle.PicklingError: Can't pickle: it's not the same object as
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM