LeeJunHyun / Image_Segmentation

Pytorch implementation of U-Net, R2U-Net, Attention U-Net, and Attention R2U-Net.
2.72k stars 600 forks source link

about the test #8

Closed YangBai1109 closed 5 years ago

YangBai1109 commented 5 years ago

hello ,thank you very much for your contribution

could you tell me how to test the code ,because

Epoch [15/15], Loss: 11.5097, [Training] Acc: 0.9289, SE: 0.8480, SP: 0.9742, PC: 0.8885, F1: 0.8537, JS: 0.7518, DC: 0.8537 Decay learning rate to lr: 0.0. [Validation] Acc: 0.8809, SE: 0.9680, SP: 0.8651, PC: 0.6196, F1: 0.7209, JS: 0.5988, DC: 0.7209

Process finished with exit code 0

the test function ,doesn't work , and i only can see the test result

li-pengcheng commented 5 years ago

when main.py is set to test mode,something wrong as 'Solver' object has no attribute 'test',Then I found that this function does not appear in 'solver.py'. Can you provide it? And How do we test that and output the segment results, is the SR image means it? Thanks a lot! @LeeJunHyun

LeeJunHyun commented 5 years ago

@PanPan0210 , @lpengc , Thank you for your interest.

I deleted solver.test() and then inserted the test code in solver.train() , solver.py#L252.

I'm sorry to make you confuse.

You can get result images modifying this comments solver.py#L231. SR denotes Segmentation Result.

If you want to get the test results on console as well as csv file, you have to add the print code such as solver.py#L229.

li-pengcheng commented 5 years ago

@LeeJunHyun when I trained with my own data set, After modifying solver.py#L231, I found that the original image became strange in the result folder, I don't know what happend, by the way, The original picture of my training data is also three-channel, while the GT is single-channel.

LeeJunHyun commented 5 years ago

@lpengc Did you check the range of images (both input and output) ? input images are converted to Tensor in pipeline, but generally RGB images almost have integer range.

li-pengcheng commented 5 years ago

@LeeJunHyun I checked the input data format, and it seems the input image are all normalized to (0,1), so the outputs of input image looks strange, but I still don't know how to modify it. Another question is Can you explain in detail how to change solver.py to output the segmentation results and the corresponding segmentation performance of each test image when testing? I am not familiar with PyTorch & python. Thank you!

LeeJunHyun commented 5 years ago

Its okay, try to define solver.test() with below code.

self.build_model()
self.unet.load_state_dict(torch.load(unet_path))

self.unet.train(False)
self.unet.eval()

acc = 0.    # Accuracy
SE = 0.     # Sensitivity (Recall)
SP = 0.     # Specificity
PC = 0.     # Precision
F1 = 0.     # F1 Score
JS = 0.     # Jaccard Similarity
DC = 0.     # Dice Coefficient
length=0
for i, (images, GT) in enumerate(self.test_loader):

  images = images.to(self.device)
  GT = GT.to(self.device)
  SR = self.unet(images)
  acc += get_accuracy(SR,GT)
  SE += get_sensitivity(SR,GT)
  SP += get_specificity(SR,GT)
  PC += get_precision(SR,GT)
  F1 += get_F1(SR,GT)
  JS += get_JS(SR,GT)
  DC += get_DC(SR,GT)

  length += images.size(0)
  print('ACC:{}, SE:{}, SP:{},PC:{},F1:{},JS:{},DC:{}'.format(acc,SE,SP,PC,F1,JS,DC))
  torchvision.utils.save_image(images.data.cpu(), os.path.join(self.result_path,'%s_valid_%d_image.png'%(self.model_type,epoch+1)))

  torchvision.utils.save_image(SR.data.cpu(),os.path.join(self.result_path,'%s_test_%d_SR.png'%(self.model_type,epoch+1)))

  torchvision.utils.save_image(GT.data.cpu(), os.path.join(self.result_path,'%s_test_%d_GT.png'%(self.model_type,epoch+1)))

acc = acc/length
SE = SE/length
SP = SP/length
PC = PC/length
F1 = F1/length
JS = JS/length
DC = DC/length
unet_score = JS + DC

f = open(os.path.join(self.result_path,'result.csv'), 'a', encoding='utf-8', newline='')
wr = csv.writer(f)
wr.writerow([self.model_type,acc,SE,SP,PC,F1,JS,DC,self.lr,best_epoch,self.num_epochs,self.num_epochs_decay,self.augmentation_prob])
f.close()
YangBai1109 commented 5 years ago

@LeeJunHyun thank you for your reply .But may i ask how to draw the corresponding loss curve ?

LeeJunHyun commented 5 years ago

@PanPan0210 you can refer here.

Because I did not use tensorboard, you should use other package.

LeeJunHyun commented 5 years ago

If you don't have further questions, I will close the issue. I hope my answer was helpful :)