Visualizes a feature map using PCA. Args: features (torch.Tensor): CxHxW feature map to visualize. n_components (int): Number of PCA components to use.
(features, n_components=3)
| 63 | return img.detach().cpu().numpy() |
| 64 | |
| 65 | def pca_visualize(features, n_components=3): |
| 66 | """ |
| 67 | Visualizes a feature map using PCA. |
| 68 | |
| 69 | Args: |
| 70 | features (torch.Tensor): CxHxW feature map to visualize. |
| 71 | n_components (int): Number of PCA components to use. |
| 72 | """ |
| 73 | C, H, W = features.shape |
| 74 | features_flat = rearrange(features.float(), 'c h w -> (h w) c').detach().cpu().numpy() |
| 75 | pca = PCA(n_components=n_components) |
| 76 | img_pca = rearrange(pca.fit_transform(features_flat), '(h w) c -> h w c', h=H, w=W) |
| 77 | img_pca = (img_pca - img_pca.min()) / (img_pca.max() - img_pca.min()) |
| 78 | return img_pca |
| 79 | |
| 80 | def np_squeeze(array, axis=0): |
| 81 | """ |
no test coverage detected