|
@@ -6,6 +6,7 @@ import cv2
|
|
|
import numpy as np
|
|
import numpy as np
|
|
|
import torch
|
|
import torch
|
|
|
from matplotlib import pyplot as plt
|
|
from matplotlib import pyplot as plt
|
|
|
|
|
+from scipy.ndimage import gaussian_filter
|
|
|
from torch.optim.lr_scheduler import ReduceLROnPlateau
|
|
from torch.optim.lr_scheduler import ReduceLROnPlateau
|
|
|
from torch.utils.tensorboard import SummaryWriter
|
|
from torch.utils.tensorboard import SummaryWriter
|
|
|
|
|
|
|
@@ -310,13 +311,46 @@ class Trainer(BaseTrainer):
|
|
|
|
|
|
|
|
# keypoint_img = draw_keypoints((img * 255).to(torch.uint8), points, colors='red', width=3)
|
|
# keypoint_img = draw_keypoints((img * 255).to(torch.uint8), points, colors='red', width=3)
|
|
|
self.writer.add_image('z-out-circle', img_tensor, global_step=epoch)
|
|
self.writer.add_image('z-out-circle', img_tensor, global_step=epoch)
|
|
|
|
|
+ features=self.apply_gaussian_blur_to_tensor(features,sigma=3)
|
|
|
self.writer.add_image('z-feature', features, global_step=epoch)
|
|
self.writer.add_image('z-feature', features, global_step=epoch)
|
|
|
|
|
|
|
|
# cv2.imshow('arc', img_rgb)
|
|
# cv2.imshow('arc', img_rgb)
|
|
|
# cv2.waitKey(1000000)
|
|
# cv2.waitKey(1000000)
|
|
|
|
|
|
|
|
|
|
+ def normalize_tensor(self,tensor):
|
|
|
|
|
+ """Normalize tensor to [0, 1]"""
|
|
|
|
|
+ min_val = tensor.min()
|
|
|
|
|
+ max_val = tensor.max()
|
|
|
|
|
+ return (tensor - min_val) / (max_val - min_val)
|
|
|
|
|
|
|
|
|
|
+ def apply_gaussian_blur_to_tensor(self,feature_map, sigma=3):
|
|
|
|
|
+ """
|
|
|
|
|
+ Apply Gaussian blur to a feature map and convert it into an RGB heatmap.
|
|
|
|
|
|
|
|
|
|
+ :param feature_map: Tensor of shape (H, W) or (1, H, W)
|
|
|
|
|
+ :param sigma: Standard deviation for Gaussian kernel
|
|
|
|
|
+ :return: Tensor of shape (3, H, W) representing the RGB heatmap
|
|
|
|
|
+ """
|
|
|
|
|
+ if feature_map.dim() == 3:
|
|
|
|
|
+ if feature_map.shape[0] != 1:
|
|
|
|
|
+ raise ValueError("Only single-channel feature map supported.")
|
|
|
|
|
+ feature_map = feature_map.squeeze(0)
|
|
|
|
|
+
|
|
|
|
|
+ # Normalize to [0, 1]
|
|
|
|
|
+ normalized_feat = self.normalize_tensor(feature_map).cpu().numpy()
|
|
|
|
|
+
|
|
|
|
|
+ # Apply Gaussian blur
|
|
|
|
|
+ blurred_feat = gaussian_filter(normalized_feat, sigma=sigma)
|
|
|
|
|
+
|
|
|
|
|
+ # Convert to colormap (e.g., 'jet')
|
|
|
|
|
+ colormap = plt.get_cmap('jet')
|
|
|
|
|
+ colored = colormap(blurred_feat) # shape: (H, W, 4) RGBA
|
|
|
|
|
+
|
|
|
|
|
+ # Convert to (3, H, W), drop alpha channel
|
|
|
|
|
+ colored_rgb = colored[:, :, :3] # (H, W, 3)
|
|
|
|
|
+ colored_tensor = torch.from_numpy(colored_rgb).permute(2, 0, 1) # (3, H, W)
|
|
|
|
|
+
|
|
|
|
|
+ return colored_tensor.float()
|
|
|
def writer_loss(self, losses, epoch, phase='train'):
|
|
def writer_loss(self, losses, epoch, phase='train'):
|
|
|
try:
|
|
try:
|
|
|
for key, value in losses.items():
|
|
for key, value in losses.items():
|