Untitled conversation
- Usercode
class SurfaceLoss(): ''' Boundary loss implementation Inputs: @probs: probability maps provded from the output of the network @dc: distance maps computed when the dataset class is initialized outputs: @loss: boundary loss @description: the loss finetunes the probability maps by the groundtruth distance map representations. ''' def __init__(self, **kwargs): # Self.idc is used to filter out some classes of the target mask. Use fancy indexing self.idc: List[int] = kwargs["idc"] print(f"Initialized {self.__class__.__name__} with {kwargs}") def __call__(self, probs: Tensor, dist_maps: Tensor, _: Tensor) -> Tensor: # assert simplex(probs) assert not one_hot(dist_maps) pc = probs[:, self.idc, ...].type(torch.float32) dc = dist_maps[:, self.idc, ...].type(torch.float32) multipled = einsum("bcwh,bcwh->bcwh", pc, dc) loss = multipled.mean() return loss def get_distance_map(mask_tensor): """ Calculate the distance map from a binary tensor (segmentation mask). Parameters: mask_tensor (torch.Tensor): A binary tensor with shape (B, C, H, W), where B is the batch size, C is the number of channels, and non-zero pixels represent the object of interest. Returns: distance_map (torch.Tensor): A tensor of shape (B, 1, H, W) representing the distance map. Each pixel value indicates the distance to the nearest non-zero pixel in the mask. """ # Convert the tensor to numpy array and squeeze the channel dimension mask_numpy = mask_tensor.numpy().squeeze(axis=1) # Apply distance transform to each batch element distance_maps = [distance_transform_edt(mask) for mask in mask_numpy] # Convert the list of distance maps back to a tensor distance_map_tensor = torch.from_numpy(np.array(distance_maps)) # Add a channel dimension to match the expected output shape distance_map_tensor = distance_map_tensor.unsqueeze(1) return distance_map_tensor Assistant
The loss value you're observing when using the
SurfaceLosswith identical masks depends on several factors, including the nature of the distance maps and the specific characteristics of the masks. However, a few points can be made regarding the behavior you're describing:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.