TransUNet
TransUNet copied to clipboard
2D slices for testing
Hello, what do I need to change to use 2d slices for testing?
Hello,have you solve the problem?
not yet, I have started the changes, but currently it does not work.
My codes to test 2D slice with 3channels:
def test_single_volume(image, label, net, classes, patch_size=[256, 256], test_save_path=None, case=None, z_spacing=1):
image, label = image.squeeze(0).cpu().detach().numpy(), label.squeeze(0).cpu().detach().numpy()
slice = image[:] # 3, h, w
prediction = np.zeros_like(label) # h,w
x, y = slice.shape[1], slice.shape[2]
if x != patch_size[0] or y != patch_size[2]:
slice = zoom(slice, (1,patch_size[0] / x, patch_size[1] / y), order=3) # previous using 0
input = torch.from_numpy(slice).unsqueeze(0).float().cuda() # 3,h,w -> n,3,h,w
net.eval()
with torch.no_grad():
outputs = net(input)
out = torch.argmax(torch.softmax(outputs, dim=1), dim=1).squeeze(0)
out = out.cpu().detach().numpy()
if x != patch_size[0] or y != patch_size[1]:
prediction = zoom(out, (x / patch_size[0], y / patch_size[1]), order=0)
else:
prediction = out
metric_list = []
for i in range(1, classes):
metric_list.append(calculate_metric_percase(prediction == i, label == i))
if test_save_path is not None:
im = Image.fromarray(prediction.astype(np.float32)).convert('L')
im.save(test_save_path + '/'+ case + "_pred.nii.gz", quality = 95)
return metric_list
Thank you for your sharing@liyiersan. I would like to know how your test results are? Mean_dice and mean_HD95 in my test results are not good. In addition, there is a small error in this code: if x! = patch_size[0] or y ! = patch_size[2] --> if x ! = patch_size[0] or y ! = patch_size[1]
Thank you again!