TransUNet icon indicating copy to clipboard operation
TransUNet copied to clipboard

2D slices for testing

Open andife opened this issue 3 years ago • 4 comments

Hello, what do I need to change to use 2d slices for testing?

andife avatar Apr 13 '21 07:04 andife

Hello,have you solve the problem?

rameses666 avatar Apr 14 '21 08:04 rameses666

not yet, I have started the changes, but currently it does not work.

andife avatar Apr 14 '21 10:04 andife

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

liyiersan avatar Aug 04 '21 05:08 liyiersan

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!

PatrickWilliams44 avatar Sep 09 '22 02:09 PatrickWilliams44