MCPcopy Create free account
hub / github.com/apple/ml-4m / pca_visualize

Function pca_visualize

fourm/utils/plotting_utils.py:65–78  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

63 return img.detach().cpu().numpy()
64
65def 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
80def np_squeeze(array, axis=0):
81 """

Callers 4

eval_image_logFunction · 0.90
decode_tok_clipFunction · 0.85
decode_tok_dinov2Function · 0.85
decode_tok_imagebindFunction · 0.85

Calls 1

maxMethod · 0.80

Tested by

no test coverage detected