CheXNet icon indicating copy to clipboard operation
CheXNet copied to clipboard

Can't pickle local object

Open AnqiZou77 opened this issue 6 years ago • 11 comments

Hi, Thank you for your sharing. When I run the code, There is an error occurred in this line "for i, (inp, target) in enumerate(test_loader)" , Somebody tell me that "python can't pickle functions. ", can you point me the right way to fix it? I run the code on win7 python 3.6 pytorch 0.1.12. Than you~

AnqiZou77 avatar Jan 16 '18 04:01 AnqiZou77

Did you modify the code? I have run the original code, too. There are no problems.

chaoyan1037 avatar Jan 16 '18 16:01 chaoyan1037

No, I just run the original code. can you tell me your environment?

AnqiZou77 avatar Jan 17 '18 02:01 AnqiZou77

Ubuntu 14.04 64-bit Anaconda 4.3.30 Python 3.6.3 pytorch 0.3.0

chaoyan1037 avatar Jan 17 '18 02:01 chaoyan1037

I met the same problem too? not so sure why the error is "AttributeError: Can't pickle local object 'main..'"

billhhh avatar Jan 17 '18 02:01 billhhh

Yes, I encountered the same error. Did you fix it?@billhhh

AnqiZou77 avatar Jan 17 '18 06:01 AnqiZou77

Not yet, @CocaKoala7

billhhh avatar Jan 18 '18 03:01 billhhh

Hi, did anybody solve the "can't pickle local objects" issue? Thanks in advance.

jubaer145 avatar Mar 21 '18 06:03 jubaer145

@omrfrkmfy This is the right issue. It seems the issue is the pickler. I think multiprocessing has to pickle the dataset to use across processes. The pickle module, unlike a better pickler like dill, can't pickle lambda functions, transforms.Lambda(lambda data_idk: do_some_stuff_here...). You could test if thats the issue by directly trying to pickle the test_dataset. If thats the problem, a simple solution would be changing the lambdas to real functions, looking something like this (untested)

# somewhere outside the "main" function
def crops_to_tensors(crops):
    to_tensor = transforms.ToTensor()
    return torch.stack([to_tensor(crop) for crop in crops])

def normalize_crops(crops):
    normalize = transforms.Normalize([0.485, 0.456, 0.406],
                                     [0.229, 0.224, 0.225])
    return torch.stack([normalize(crop)for crop in crops])

# in the "main" function
test_dataset = ChestXrayDataSet(data_dir=DATA_DIR,
                                image_list_file=TEST_IMAGE_LIST,
                                transform=transforms.Compose([
                                    transforms.Resize(256),
                                    transforms.TenCrop(224),
                                    transforms.Lambda(crops_to_tensors),
                                    transforms.Lambda(normalize_crops),
                                ]))

ghost avatar Sep 16 '18 04:09 ghost

Yes it worked. But sorry for the disturbance; Traceback (most recent call last): File "C:/Users/Nasir Isa/Documents/1Research/algortihm/CheXNet-master/CheXNet-master/m2.py", line 149, in main() File "C:/Users/Nasir Isa/Documents/1Research/algortihm/CheXNet-master/CheXNet-master/m2.py", line 76, in main transforms.Lambda(normalize_crops), File "C:/Users/Nasir Isa/Documents/1Research/algortihm/CheXNet-master/CheXNet-master\read_data.py", line 24, in init with open(image_list_file, "r") as f: FileNotFoundError: [Errno 2] No such file or directory: './ChestX-ray14/labels/test_list.txt'

pharouknucleus avatar Sep 16 '18 13:09 pharouknucleus

是的,它奏效了。但很抱歉打扰了; 回溯(最近一次通话): 文件“C:/Users/Nasir Isa/Documents/1Research/algortihm/CheXNet-master/CheXNet-master/m2.py”,第 149 行,在 main() 文件“C:/Users /Nasir Isa/Documents/1Research/algortihm/CheXNet-master/CheXNet-master/m2.py”,第 76 行,在主 转换中。Lambda (normalize_crops), 文件“C:/Users/Nasir Isa/Documents/1Research/algortihm /CheXNet-master/CheXNet-master\read_data.py", line 24, in init with open(image_list_file, "r") as f: FileNotFoundError: [Errno 2] No such file or directory: './ChestX-ray14/标签/test_list.txt'

Have you solved this problem? Can you share your ideas?

zhyl123 avatar Jul 08 '21 01:07 zhyl123