-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpca_vis.py
More file actions
59 lines (52 loc) · 2.13 KB
/
pca_vis.py
File metadata and controls
59 lines (52 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import torch
from pathlib import Path
from PIL import Image
from sklearn.decomposition import PCA
from matplotlib import pyplot as plt
from model.dune import load_dune_encoder_from_checkpoint
from data.transform import get_test_transform
from data.utils import normalize_min_max
architecture = "vitsmall14"
image_size = 448
checkpoint_path = Path("dune_{}_{}_paper.pth".format(architecture, image_size))
device = torch.device("cpu")
print("Loading DUNE model from checkpoint:", checkpoint_path)
model = load_dune_encoder_from_checkpoint(checkpoint_path)[0]
model = model.eval()
if torch.cuda.is_available():
print(" - Using GPU")
device = torch.device("cuda")
model = model.to(device)
print("Loading the test image")
transform = get_test_transform(image_size)
image = Image.open("./assets/test_image.png").convert("RGB")
image = transform(image)
print("Making a forward pass through the model")
with torch.inference_mode():
output: dict = model(image.unsqueeze(0).to(device))
# output is compatibile to that of DINOv2
# output["x_norm_clstoken"].shape --> [1, 768]
# output["x_norm_patchtokens"].shape --> [1, num_patches, 768]
patch_emb = output["x_norm_patchtokens"].detach().cpu().squeeze()
print("Reducing the dimension of patch embeddings to 3 via PCA")
num_patches_side = int(patch_emb.shape[0] ** 0.5) # assume a square image
pca = PCA(n_components=3, random_state=22)
patch_pca = torch.from_numpy(pca.fit_transform(patch_emb.numpy())) # [num_patches, 3]
patch_pca = patch_pca.reshape([num_patches_side, num_patches_side, 3]).permute(2, 0, 1)
patch_pca = (
torch.nn.functional.interpolate(patch_pca.unsqueeze(0), image_size, mode="nearest")
.squeeze(0)
.permute(1, 2, 0)
) # [image_size, image_size, 3]
patch_pca = normalize_min_max(patch_pca)
print("Visualizing the original image and the PCA-reduced patch embeddings")
plt.close()
fig, axs = plt.subplots(1, 2, dpi=200, constrained_layout=True)
axs[0].imshow(normalize_min_max(image.permute(1, 2, 0)))
axs[1].imshow(patch_pca)
for ax in axs:
ax.axis("off")
plt.savefig(
"./assets/test_image_patch_pca_{}.png".format(checkpoint_path.stem),
bbox_inches="tight",
)