mateuszbuda / brain-segmentation-pytorch

U-Net implementation in PyTorch for FLAIR abnormality segmentation in brain MRI
https://mateuszbuda.github.io/2017/12/01/brainseg.html
MIT License
722 stars 187 forks source link

ValueError: Sample larger than population or is negative #18

Open BaseMax opened 4 years ago

BaseMax commented 4 years ago

Hello,

Running python script cause to an error:

reading validation images...
Traceback (most recent call last):
  File "brain-segmentation-pytorch/inference.py", line 184, in <module>
    main(args)
  File "brain-segmentation-pytorch/inference.py", line 22, in main
    loader = data_loader(args)
  File "brain-segmentation-pytorch/inference.py", line 80, in data_loader
    random_sampling=True,
  File "/content/brain-segmentation-pytorch/dataset.py", line 56, in _init_
    validation_patients = random.sample(self.patients, k=validation_cases)
  File "/usr/lib/python3.6/random.py", line 320, in sample
    raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative

History of my commands in a Arch Linux system:

cd ~/OpenSource/
git clone https://github.com/mateuszbuda/brain-segmentation-pytorch
cd brain-segmentation-pytorch/
ls
pip3 install numpy
pip3 install torch
pip3 install matplotlib
python inference.py
pip3 install medpy
python inference.py
pip3 install skimage
pip3 install skit-image
pip3 install scikit-image
pip3 install tqdm
mkdir data
wget https://www.kaggle.com/kmader/mias-mammography && unzip...
python inference.py --images data --weights weights/unet.pt
python3 inference.py --images data --weights weights/unet.pt

[max@base brain-segmentation-pytorch]$ python -v

Python 3.8.5 (default, Sep  5 2020, 10:50:12) 

[max@base brain-segmentation-pytorch]$ python inference.py --images data --weights weights/unet.pt

reading validation images...
Traceback (most recent call last):
  File "inference.py", line 184, in <module>
    main(args)
  File "inference.py", line 22, in main
    loader = data_loader(args)
  File "inference.py", line 76, in data_loader
    dataset = Dataset(
  File "/home/max/OpenSource/brain-segmentation-pytorch/dataset.py", line 56, in __init__
    validation_patients = random.sample(self.patients, k=validation_cases)
  File "/usr/lib/python3.8/random.py", line 363, in sample
    raise ValueError("Sample larger than population or is negative")
ValueError: Sample larger than population or is negative

We face to same problem in Google CoLab: Screenshot_2020-10-19_19-07-41


Can you guide?

I change 56th line of dataset.py, from:

validation_patients = random.sample(self.patients, k=validation_cases)

to:

validation_patients = random.choices(self.patients, k=validation_cases)

Still, there is an error in the program:

[max@base brain-segmentation-pytorch]$ python3 inference.py --images data --weights weights/unet.pt

reading validation images...
Traceback (most recent call last):
  File "inference.py", line 184, in <module>
    main(args)
  File "inference.py", line 22, in main
    loader = data_loader(args)
  File "inference.py", line 76, in data_loader
    dataset = Dataset(
  File "/home/max/OpenSource/brain-segmentation-pytorch/dataset.py", line 57, in __init__
    validation_patients = random.choices(self.patients, k=validation_cases-1)
  File "/usr/lib/python3.8/random.py", line 399, in choices
    return [population[_int(random() * n)] for i in _repeat(None, k)]
  File "/usr/lib/python3.8/random.py", line 399, in <listcomp>
    return [population[_int(random() * n)] for i in _repeat(None, k)]
IndexError: list index out of range

Regards, Max

mateuszbuda commented 3 years ago

It simply means that validation_cases is larger than len(self.patients). patients is a dictionary mapping patient IDs to volumes. Did you run it on your own dataset? You can add this assert after setting self.patients variable: https://github.com/mateuszbuda/brain-segmentation-pytorch/blob/8ef2e2d423b67b53ec8113fc71a9b968bb0f66e7/dataset.py#L51

assert len(patients) >= validation_cases, f'The number of patients ({len(self.patients)}) is lower than the validation sample size ({validation_cases})'
fidhaph commented 3 years ago

I have solved " validation_patients = random.sample(self.patients, k=validation_cases) File "/usr/lib/python3.6/random.py", line 320, in sample raise ValueError("Sample larger than population or is negative") ValueError: Sample larger than population or is negative" issue by adding --images 'root folder with images' argument while running inference.py

LIULittleblack commented 1 week ago

我也遇到过相同的问题,可能是您的路径超参数设置与你代码的真实目录有出入 parser.add_argument( "--images", type=str, default="./kaggle_3m", help="root folder with images" ) 请将这一部分进行修改