Upload 2_visualize_tensorboard.py
Browse files- 2_visualize_tensorboard.py +120 -0
2_visualize_tensorboard.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datasets import load_dataset
|
2 |
+
from transformers import CLIPProcessor, CLIPModel
|
3 |
+
import torch, numpy as np, os
|
4 |
+
from collections import defaultdict
|
5 |
+
|
6 |
+
rename_qsn = {
|
7 |
+
"Are there any abnormalities in the image? Check all that are present.": "𧬠Abnorm",
|
8 |
+
"Are there any anatomical landmarks in the image? Check all that are present.": "π Landmark",
|
9 |
+
"Are there any instruments in the image? Check all that are present.": "π οΈ Instrum",
|
10 |
+
"Have all polyps been removed?": "β Polyps_Removed",
|
11 |
+
"Is this finding easy to detect?": "π Easy_Detect",
|
12 |
+
"Is there a green/black box artefact?": "π© Box_Artifact",
|
13 |
+
"Is there text?": "π€ Has_Text",
|
14 |
+
"What type of polyp is present?": "π¬ Polyp_Type",
|
15 |
+
"What type of procedure is the image taken from?": "π₯ Proc_Type",
|
16 |
+
"What is the size of the polyp?": "π Polyp_Size",
|
17 |
+
"How many findings are present?": "π§Ύ Find_Count",
|
18 |
+
"How many polyps are in the image?": "π’ Polyp_Count",
|
19 |
+
"Where in the image is the instrument?": "π Instrum_Loc",
|
20 |
+
"Where in the image is the abnormality?": "π Abnorm_Loc",
|
21 |
+
"Where in the image is the anatomical landmark?": "π Landmark_Loc",
|
22 |
+
"How many instrumnets are in the image?": "π’ Instrum_Count",
|
23 |
+
"What color is the abnormality? If more than one separate with ;": "π¨ Abnorm_Color",
|
24 |
+
"What color is the anatomical landmark? If more than one separate with ;": "π¨ Landmark_Color",
|
25 |
+
"Does this image contain any finding?": "πΈ Has_Finding",
|
26 |
+
"none": "π« Nan",
|
27 |
+
}
|
28 |
+
|
29 |
+
ds = load_dataset("SimulaMet-HOST/Kvasir-VQA")["raw"]
|
30 |
+
qas = defaultdict(dict)
|
31 |
+
for q, a, img_id in zip(ds["question"], ds["answer"], ds["img_id"]):
|
32 |
+
qas[img_id][rename_qsn[q]] = a
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
# === Step 2: Prepare Log Directory ===
|
38 |
+
log_dir = "logs/projector"
|
39 |
+
os.makedirs(log_dir, exist_ok=True)
|
40 |
+
|
41 |
+
import math
|
42 |
+
import numpy as np
|
43 |
+
from PIL import Image
|
44 |
+
|
45 |
+
def create_sprite_image(dataset, save_path='sprite.png', image_column='image', size=(100, 100), max_images=6500):
|
46 |
+
imgs = []
|
47 |
+
for i, x in enumerate(dataset):
|
48 |
+
if i >= max_images:
|
49 |
+
break
|
50 |
+
img = x[image_column].resize(size).convert('RGB')
|
51 |
+
imgs.append(np.asarray(img) / 255.0)
|
52 |
+
|
53 |
+
imgs = np.array(imgs)
|
54 |
+
n = math.ceil(math.sqrt(len(imgs)))
|
55 |
+
pad = ((0, n**2 - len(imgs)), (0,0), (0,0), (0,0))
|
56 |
+
imgs = np.pad(imgs, pad, constant_values=1)
|
57 |
+
imgs = imgs.reshape((n, n, size[1], size[0], 3)).transpose(0,2,1,3,4).reshape(n*size[1], n*size[0], 3)
|
58 |
+
Image.fromarray((imgs * 255).astype(np.uint8)).save(save_path)
|
59 |
+
|
60 |
+
dsx = ds.select({v: k for k, v in enumerate(ds['img_id'])}.values())
|
61 |
+
# dsx = dsx.select(range(10))
|
62 |
+
# create_sprite_image(dsx, save_path=f"{log_dir}/openai__clip-vit-large-patch14-336_sprite.png")
|
63 |
+
|
64 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
65 |
+
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32").to(device)
|
66 |
+
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
67 |
+
|
68 |
+
def get_emb(batch):
|
69 |
+
inputs = processor(images=batch["image"], return_tensors="pt", padding=True).to(device)
|
70 |
+
with torch.no_grad():
|
71 |
+
feats = model.get_image_features(**inputs)
|
72 |
+
return {"emb": (feats / feats.norm(p=2, dim=-1, keepdim=True)).cpu().numpy()}
|
73 |
+
|
74 |
+
dsx = dsx.map(get_emb, batched=True, batch_size=512)
|
75 |
+
|
76 |
+
np.savez_compressed("all_embeddings.npz",
|
77 |
+
embeddings=np.array(dsx["emb"]),
|
78 |
+
metadata=np.array(list(zip(dsx["img_id"], dsx["source"], dsx["question"], dsx["answer"]))))
|
79 |
+
np.savetxt(os.path.join(log_dir, "vectors.tsv"), np.array(dsx["emb"]), delimiter="\t")
|
80 |
+
# breakpoint()
|
81 |
+
|
82 |
+
import tensorflow as tf
|
83 |
+
|
84 |
+
# === Step 3: Save Embeddings to TensorFlow Variable ===
|
85 |
+
embeddings_np = np.array(dsx["emb"])
|
86 |
+
embedding_tensor = tf.Variable(embeddings_np, name="image_embeddings")
|
87 |
+
checkpoint = tf.train.Checkpoint(embedding=embedding_tensor)
|
88 |
+
checkpoint.save(os.path.join(log_dir, "embedding.ckpt"))
|
89 |
+
|
90 |
+
# === Step 4: Write metadata.tsv (WITH HEADERS) ===
|
91 |
+
metadata_path = os.path.join(log_dir, "metadata.tsv")
|
92 |
+
with open(metadata_path, "w", encoding="utf-8") as f:
|
93 |
+
f.write("source\tQ/A\timg_hash\n") # header row
|
94 |
+
for img_id, source, question, answer in zip(dsx["img_id"], dsx["source"], dsx["question"], dsx["answer"]):
|
95 |
+
img_hash = str(img_id).replace("\t", " ").replace("\n", " ")
|
96 |
+
img_id = " | ".join(f"{k}: {v}" for k, v in qas.get(img_id, {}).items())
|
97 |
+
source = str(source).replace("\t", " ").replace("\n", " ")
|
98 |
+
question = str(question).replace("\t", " ").replace("\n", " ")
|
99 |
+
answer = str(answer).replace("\t", " ").replace("\n", " ")
|
100 |
+
f.write(f"{source}\t{img_id}\t{img_hash}\n")
|
101 |
+
|
102 |
+
from tensorboard.plugins import projector
|
103 |
+
# === Step 5: Projector Config ===
|
104 |
+
config = projector.ProjectorConfig()
|
105 |
+
embedding = config.embeddings.add()
|
106 |
+
embedding.tensor_name = embedding_tensor.name # should be 'image_embeddings'
|
107 |
+
embedding.metadata_path = "metadata.tsv" # relative to log_dir
|
108 |
+
embedding.sprite.image_path = "openai__clip-vit-large-patch14-336_sprite.png" # relative to log_dir
|
109 |
+
embedding.sprite.single_image_dim.extend([100, 100]) # size of each image in the sprite
|
110 |
+
projector.visualize_embeddings(log_dir, config)
|
111 |
+
|
112 |
+
# tf.compat.v1.disable_eager_execution()
|
113 |
+
# saver = tf.compat.v1.train.Saver([ tf.Variable(1.0, name="var1"), tf.Variable(2.0, name="var2")])
|
114 |
+
# with tf.compat.v1.Session() as sess:
|
115 |
+
# sess.run(tf.compat.v1.global_variables_initializer())
|
116 |
+
# saver.save(sess, os.path.join(log_dir, "model.ckpt"), 1)
|
117 |
+
|
118 |
+
# === Step 6: Launch TensorBoard Command ===
|
119 |
+
print("β
All done! Launch TensorBoard using:")
|
120 |
+
print(f"tensorboard --logdir={log_dir}")
|