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 behavior you're observing is not unusual, but it's important to understand why it's happening and whether it's appropriate for your specific use case.
The SurfaceLoss function you've provided doesn't actually compute a "perimeter loss" in the traditional sense. Instead, it's calculating the mean of the element-wise multiplication between the probability maps (pc) and the distance maps (dc). This means that even if you pass in identical masks, you might still get a non-zero loss value. Here's why:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.