Unet-Segmentation-Pytorch-Nest-of-Unets
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
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
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
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.
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.
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
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
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.
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.
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?
Thank you for your reply. I just checked the shape. im1(480,640) im2(320,320) How can I change the shape?
if you are using cv resized = cv2.resize(img, shape)
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.
The label_threshold、pred、predthreshold folders cnotain many pictures.
what do these mean?
Can I visualize the result with tensorboard?
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