bigmb / Unet-Segmentation-Pytorch-Nest-of-Unets

Implementation of different kinds of Unet Models for Image Segmentation - Unet , RCNN-Unet, Attention Unet, RCNN-Attention Unet, Nested Unet
MIT License
1.87k stars 345 forks source link

Caught RuntimeError in DataLoader worker process 0& The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0 #56

Closed findlayr closed 1 year ago

findlayr commented 2 years ago

thank you for your great work. let me show my errors first. Traceback (most recent call last): File "/root/autodl-tmp/Unet/pytorch_run.py", line 243, in for x, y in train_loader: File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 521, in next data = self._next_data() File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1203, in _next_data return self._process_data(data) File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1229, in _process_data data.reraise() File "/root/miniconda3/lib/python3.8/site-packages/torch/_utils.py", line 434, in reraise raise exception RuntimeError: Caught RuntimeError in DataLoader worker process 0. Original Traceback (most recent call last): File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/_utils/worker.py", line 287, in _worker_loop data = fetcher.fetch(index) File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 49, in fetch data = [self.dataset[idx] for idx in possibly_batched_index] File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 49, in data = [self.dataset[idx] for idx in possibly_batched_index] File "/root/autodl-tmp/Unet/Data_Loader.py", line 104, in getitem img = self.tx(i1) File "/root/miniconda3/lib/python3.8/site-packages/torchvision/transforms/transforms.py", line 61, in call img = t(img) File "/root/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl return forwardcall(*input, **kwargs) File "/root/miniconda3/lib/python3.8/site-packages/torchvision/transforms/transforms.py", line 226, in forward return F.normalize(tensor, self.mean, self.std, self.inplace) File "/root/miniconda3/lib/python3.8/site-packages/torchvision/transforms/functional.py", line 351, in normalize tensor.sub(mean).div_(std) RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0 I run your code with my dataset, the data(brain CT files changed into PNG files) and labels are PNG files, and then errors cam up. I am a freshman , I don't know how to solve these problems. perhaps you can help me . looking for your reply asap, thank you.

bigmb commented 2 years ago

Thanks. You are having issues while loading the data. ''for x, y in train_loader:'' The pytorch dataloader is not able to access the next batch or data in your input data. Check the input shape. Have a look at https://pytorch.org/tutorials/beginner/basics/data_tutorial.html

findlayr commented 2 years ago

Thanks. While waiting for your reply, I tried to solve the problems, I changed num_workers=0 batch_size =1 as some blogs said. This may works in someway, but the code still can't run Once again, there is an error like below: RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0 I already read your link about dataset and dataloader, I understand what the link said , I understand what the link said ,but I don't know how I can solve the problem. How or Where should I change the code? If it is possible,Please don't close this issue now. Maybe I still need your help. Thank you.

bigmb commented 2 years ago

Check your input data shape. I think this error points to the 1st channel input data. Check how the data I have provided and try to replicate it to run pytorch_run.py file.

findlayr commented 2 years ago

Hi, thank you for your suggestion. For my dataset, it is brain CT file(.IMA format), I changed them into PNG file(perhaps still Gray Scale Image)with the help of Dicom Viewer,.then, I made labels with the help of labelme. However, the code still can't run. I changed gray scale image(except label files) to rgb image,like below CHEN_YE MR WUXI_HEAD 0023 0014 2022 01 10 13 52 28 719451 24164064 then there is an error: RuntimeError: output with shape [1, 320, 320] doesn't match the broadcast shape [3,320, 320] In order to solve this problem, I changed the code as some blogs siad: torchvision.transforms.Normalize(mean=[0.5], std=[0.5]) and it runs,but another error shows: Successfully created the testing directory ./model/gen_images Successfully created the testing directory ./model/pred_threshold Successfully created the testing directory ./model/label_threshold Traceback (most recent call last): File "/root/autodl-tmp/Unet/pytorch_run.py", line 546, in total = dice_coeff(s, s3) File "/root/autodl-tmp/Unet/Metrics.py", line 12, in dice_coeff raise ValueError("Shape mismatch: im1 and im2 must have the same shape.") ValueError: Shape mismatch: im1 and im2 must have the same shape. What is the problem? Once again , thank you for your help

bigmb commented 2 years ago

Check the shape of the Input and the Broadcast shape. They need to be same. You are using a Gray scale image with 1 channel, change it to 3 channel and provide that as an input or change the input channels to 1 and then provide your input.

findlayr commented 2 years ago

Sorry. I am still confused with this error. You mean the pic I showed above is Gray scale image with 1channel not 3 channel? The data originally is .IMA format. I used matplotlib to plot it, and then ,save as png files. I thought there were already 3 channel,becasue there were color with these files. As you said, The pic above is still 1 channel? How can I change it to 3channels? I also tried change input channels to 1 as other issue said, it still can't work.

bigmb commented 2 years ago

Look into the Dice Score Metric:

def dice_coeff(im1, im2, empty_score=1.0):

im1 = np.asarray(im1).astype(np.bool)
im2 = np.asarray(im2).astype(np.bool)

if im1.shape != im2.shape:
    raise ValueError("Shape mismatch: im1 and im2 must have the same shape.")

im1 = im1 > 0.5
im2 = im2 > 0.5

im_sum = im1.sum() + im2.sum()
if im_sum == 0:
    return empty_score

# Compute Dice coefficient
intersection = np.logical_and(im1, im2)
#print(im_sum)

return 2. * intersection.sum() / im_sum 

So the s (im1) and s3(im2) doesnt have the same shape. Can you check the shape of those input and see try again?

findlayr commented 2 years ago

Thank you for your reply. I just checked the shape. im1(480,640) im2(320,320) How can I change the shape?

bigmb commented 2 years ago

if you are using cv resized = cv2.resize(img, shape)

findlayr commented 2 years ago

Thank you .Sorry to bother you again. Now , the code can run. And I trained 50 epochs. The dice score is 0.02092386608205167,loss is 0.43 The result is not good Perhaps I should train more epoch? And ,one more question about the result. The model file folder is generated by the code. It contains some folders. I am confused with some folders like the pic shows below. qu The label_threshold、pred、predthreshold folders cnotain many pictures. what do these mean? Can I visualize the result with tensorboard?

bigmb commented 2 years ago

I think 50 epochs should be good enough to check if the results are coming in the right direction by checking the gen_images after each epoch. There is early stopping implemented too if thats required.

You can visualize the results on TB. I think the code is already there in pytorch_run.py Pred_threshold and label_threhold are the images with threshold kept in loses to make it binary image. Default:150