Unet-Segmentation-Pytorch-Nest-of-Unets icon indicating copy to clipboard operation
Unet-Segmentation-Pytorch-Nest-of-Unets copied to clipboard

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

Open findlayr opened this issue 2 years ago • 11 comments

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 forward_call(*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.

findlayr avatar Sep 02 '22 05:09 findlayr

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

bigmb avatar Sep 02 '22 09:09 bigmb

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.

findlayr avatar Sep 02 '22 10:09 findlayr

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.

bigmb avatar Sep 02 '22 10:09 bigmb

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

findlayr avatar Sep 04 '22 14:09 findlayr

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.

bigmb avatar Sep 04 '22 21:09 bigmb

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.

findlayr avatar Sep 14 '22 01:09 findlayr

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?

bigmb avatar Sep 14 '22 01:09 bigmb

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

findlayr avatar Sep 14 '22 02:09 findlayr

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

bigmb avatar Sep 14 '22 22:09 bigmb

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?

findlayr avatar Sep 18 '22 14:09 findlayr

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

bigmb avatar Sep 18 '22 22:09 bigmb