Beispiel #1
0
    def test(self, test_case_description, params, input, features, expected):

        # Create input tensors
        input_tensor = torch.from_numpy(np.array(input)).to(
            dtype=torch.float, device=torch.device("cuda"))
        feature_tensor = torch.from_numpy(np.array(features)).to(
            dtype=torch.float, device=torch.device("cuda"))

        params[-1] = None if params[-1] is None else params[-1].cuda()

        # apply filter
        crf = CRF(*params)
        output = crf(input_tensor, feature_tensor).cpu().numpy()

        # Ensure result are as expected
        # np.testing.assert_allclose(output, expected, atol=1e-4)

        # Temporarily allowing some (10%) mismatched elements due to non determinism.
        absolute_diff_tolerance = 5e-2
        mismatch_ratio_tolerance = 0.1

        output = np.array(output).flatten()
        expected = np.array(expected).flatten()

        abs_diff = abs(output - expected)
        mismatch_count = sum(np.where(abs_diff > absolute_diff_tolerance, 1,
                                      0))

        self.assertLessEqual(mismatch_count / len(output),
                             mismatch_ratio_tolerance)
Beispiel #2
0
    def __call__(self, data):
        d = dict(data)

        # attempt to fetch algorithmic parameters from app if present
        self.iterations = d.get("iterations", self.iterations)
        self.bilateral_weight = d.get("bilateral_weight",
                                      self.bilateral_weight)
        self.gaussian_weight = d.get("gaussian_weight", self.gaussian_weight)
        self.bilateral_spatial_sigma = d.get("bilateral_spatial_sigma",
                                             self.bilateral_spatial_sigma)
        self.bilateral_color_sigma = d.get("bilateral_color_sigma",
                                           self.bilateral_color_sigma)
        self.gaussian_spatial_sigma = d.get("gaussian_spatial_sigma",
                                            self.gaussian_spatial_sigma)
        self.update_factor = d.get("update_factor", self.update_factor)
        self.compatibility_matrix = d.get("compatibility_matrix",
                                          self.compatibility_matrix)
        self.device = d.get("device", self.device)

        # copy affine meta data from pairwise input
        self._copy_affine(d, self.pairwise, self.post_proc_label)

        # read relevant terms from data
        unary_term = self._fetch_data(d, self.unary)
        pairwise_term = self._fetch_data(d, self.pairwise)

        # initialise MONAI's CRF layer
        crf_layer = CRF(
            iterations=self.iterations,
            bilateral_weight=self.bilateral_weight,
            gaussian_weight=self.gaussian_weight,
            bilateral_spatial_sigma=self.bilateral_spatial_sigma,
            bilateral_color_sigma=self.bilateral_color_sigma,
            gaussian_spatial_sigma=self.gaussian_spatial_sigma,
            update_factor=self.update_factor,
            compatibility_matrix=self.compatibility_matrix,
        )

        # add batch dimension for MONAI's CRF so it is in format [B, ?, X, Y, [Z]]
        unary_term = np.expand_dims(unary_term, axis=0)
        pairwise_term = np.expand_dims(pairwise_term, axis=0)

        # numpy to torch
        unary_term = torch.from_numpy(unary_term.astype(np.float32)).to(
            self.device)
        pairwise_term = torch.from_numpy(pairwise_term.astype(np.float32)).to(
            self.device)

        # run MONAI's CRF without any gradients
        with torch.no_grad():
            d[self.post_proc_label] = (torch.argmax(
                crf_layer(unary_term, pairwise_term), dim=1,
                keepdim=True).squeeze_(dim=0).detach().cpu().numpy())

        return d
Beispiel #3
0
    def test(self, test_case_description, params, input, features, expected):

        # Create input tensors
        input_tensor = torch.from_numpy(np.array(input)).to(dtype=torch.float, device=torch.device("cuda"))
        feature_tensor = torch.from_numpy(np.array(features)).to(dtype=torch.float, device=torch.device("cuda"))

        # apply filter
        crf = CRF(*params)
        output = crf(input_tensor, feature_tensor).cpu().numpy()

        # Ensure result are as expected
        np.testing.assert_allclose(output, expected, atol=1e-4, rtol=1e-4)
Beispiel #4
0
if not run_in_3d:
    ct = ct[..., slice_index]
    logits = logits[..., slice_index]

ct_tensor = torch.from_numpy(ct).type(torch.FloatTensor).cuda()
logits_tensor = torch.from_numpy(logits).type(torch.FloatTensor).cuda()

# Adding noise
logits_tensor *= (torch.rand_like(logits_tensor) + 1) * 0.5

# CRF paramters
crf = CRF(
    iterations=5,
    bilateral_weight=3.0,
    gaussian_weight=1.0,
    bilateral_spatial_sigma=5.0,
    bilateral_color_sigma=0.5,
    gaussian_spatial_sigma=5.0,
    compatability_kernel_range=1,
)

# Run CRF and take labelsfrom input and output tensors
logits_smoothed_tensor = run_timed(crf, (logits_tensor, ct_tensor))
labels_tensor = torch.argmax(logits_tensor, dim=1, keepdim=True)
labels_smoothed_tensor = torch.argmax(logits_smoothed_tensor,
                                      dim=1,
                                      keepdim=True)

# Reading back data
ct = ct_tensor.squeeze(0).movedim(0, -1).cpu()
labels = labels_tensor.squeeze(0).movedim(0, -1).cpu()