SushantGautam commited on
Commit
0e0c3ae
Β·
verified Β·
1 Parent(s): 6f03b67

Update 2_visualize_tensorboard.py

Browse files
Files changed (1) hide show
  1. 2_visualize_tensorboard.py +61 -42
2_visualize_tensorboard.py CHANGED
@@ -1,29 +1,36 @@
 
 
 
 
 
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"]
@@ -31,16 +38,16 @@ 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 = []
@@ -52,34 +59,44 @@ def create_sprite_image(dataset, save_path='sprite.png', image_column='image', s
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"])
@@ -93,20 +110,22 @@ 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()
@@ -117,4 +136,4 @@ projector.visualize_embeddings(log_dir, config)
117
 
118
  # === Step 6: Launch TensorBoard Command ===
119
  print("βœ… All done! Launch TensorBoard using:")
120
- print(f"tensorboard --logdir={log_dir}")
 
1
+ from tensorboard.plugins import projector
2
+ import tensorflow as tf
3
+ from PIL import Image
4
+ import numpy as np
5
+ import math
6
  from datasets import load_dataset
7
+ from transformers import CLIPProcessor, CLIPModel, AutoModel, AutoProcessor
8
+ import torch
9
+ import numpy as np
10
+ import os
11
  from collections import defaultdict
12
 
13
  rename_qsn = {
14
+ "Are there any abnormalities in the image? Check all that are present.": "🧬 Abnorm",
15
+ "Are there any anatomical landmarks in the image? Check all that are present.": "πŸ“ Landmark",
16
+ "Are there any instruments in the image? Check all that are present.": "πŸ› οΈ Instrum",
17
+ "Have all polyps been removed?": "❌ Polyps_Removed",
18
+ "Is this finding easy to detect?": "πŸ” Easy_Detect",
19
+ "Is there a green/black box artefact?": "🟩 Box_Artifact",
20
+ "Is there text?": "πŸ”€ Has_Text",
21
+ "What type of polyp is present?": "πŸ”¬ Polyp_Type",
22
+ "What type of procedure is the image taken from?": "πŸ₯ Proc_Type",
23
+ "What is the size of the polyp?": "πŸ“ Polyp_Size",
24
+ "How many findings are present?": "🧾 Find_Count",
25
+ "How many polyps are in the image?": "πŸ”’ Polyp_Count",
26
+ "Where in the image is the instrument?": "πŸ“Œ Instrum_Loc",
27
+ "Where in the image is the abnormality?": "πŸ“Œ Abnorm_Loc",
28
+ "Where in the image is the anatomical landmark?": "πŸ“Œ Landmark_Loc",
29
+ "How many instrumnets are in the image?": "πŸ”’ Instrum_Count",
30
+ "What color is the abnormality? If more than one separate with ;": "🎨 Abnorm_Color",
31
+ "What color is the anatomical landmark? If more than one separate with ;": "🎨 Landmark_Color",
32
+ "Does this image contain any finding?": "πŸ“Έ Has_Finding",
33
+ "none": "🚫 Nan",
34
  }
35
 
36
  ds = load_dataset("SimulaMet-HOST/Kvasir-VQA")["raw"]
 
38
  for q, a, img_id in zip(ds["question"], ds["answer"], ds["img_id"]):
39
  qas[img_id][rename_qsn[q]] = a
40
 
41
+ sorted_qas = {
42
+ img_id: dict(sorted(questions.items()))
43
+ for img_id, questions in qas.items()
44
+ }
45
 
46
 
47
  # === Step 2: Prepare Log Directory ===
48
+ log_dir = "logs/projector1"
49
  os.makedirs(log_dir, exist_ok=True)
50
 
 
 
 
51
 
52
  def create_sprite_image(dataset, save_path='sprite.png', image_column='image', size=(100, 100), max_images=6500):
53
  imgs = []
 
59
 
60
  imgs = np.array(imgs)
61
  n = math.ceil(math.sqrt(len(imgs)))
62
+ pad = ((0, n**2 - len(imgs)), (0, 0), (0, 0), (0, 0))
63
  imgs = np.pad(imgs, pad, constant_values=1)
64
+ imgs = imgs.reshape((n, n, size[1], size[0], 3)).transpose(
65
+ 0, 2, 1, 3, 4).reshape(n*size[1], n*size[0], 3)
66
  Image.fromarray((imgs * 255).astype(np.uint8)).save(save_path)
67
 
68
+
69
  dsx = ds.select({v: k for k, v in enumerate(ds['img_id'])}.values())
70
  # dsx = dsx.select(range(10))
 
71
 
72
  device = "cuda" if torch.cuda.is_available() else "cpu"
73
  model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32").to(device)
74
  processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
75
+ # checkpoint = "ikim-uk-essen/BiomedCLIP_ViT_patch16_224"
76
+ # model = AutoModel.from_pretrained(checkpoint).to(device)
77
+ # processor = AutoProcessor.from_pretrained(checkpoint)
78
+
79
+ # create_sprite_image(dsx, save_path=f"{log_dir}/{checkpoint.replace('/', '__')}_sprite.png")
80
+
81
 
82
  def get_emb(batch):
83
+ inputs = processor(images=batch["image"],
84
+ return_tensors="pt", padding=True).to(device)
85
  with torch.no_grad():
86
+ # feats = model(**inputs).last_hidden_state[:, 0, :] # for BiomedCLIP
87
+ feats = model.get_image_features(**inputs) # for CLIP
88
+ return {"emb": feats.cpu().numpy()}
89
+
90
 
91
  dsx = dsx.map(get_emb, batched=True, batch_size=512)
92
 
93
+ np.savez_compressed(os.path.join(log_dir, "all_embeddings.npz"),
94
  embeddings=np.array(dsx["emb"]),
95
  metadata=np.array(list(zip(dsx["img_id"], dsx["source"], dsx["question"], dsx["answer"]))))
96
+ np.savetxt(os.path.join(log_dir, "vectors.tsv"),
97
+ np.array(dsx["emb"]), delimiter="\t")
98
  # breakpoint()
99
 
 
100
 
101
  # === Step 3: Save Embeddings to TensorFlow Variable ===
102
  embeddings_np = np.array(dsx["emb"])
 
110
  f.write("source\tQ/A\timg_hash\n") # header row
111
  for img_id, source, question, answer in zip(dsx["img_id"], dsx["source"], dsx["question"], dsx["answer"]):
112
  img_hash = str(img_id).replace("\t", " ").replace("\n", " ")
113
+ img_id = " | ".join(f"{k}: {v}" for k,
114
+ v in qas.get(img_id, {}).items())
115
  source = str(source).replace("\t", " ").replace("\n", " ")
116
  question = str(question).replace("\t", " ").replace("\n", " ")
117
  answer = str(answer).replace("\t", " ").replace("\n", " ")
118
  f.write(f"{source}\t{img_id}\t{img_hash}\n")
119
+
 
120
  # === Step 5: Projector Config ===
121
  config = projector.ProjectorConfig()
122
  embedding = config.embeddings.add()
123
  embedding.tensor_name = embedding_tensor.name # should be 'image_embeddings'
124
  embedding.metadata_path = "metadata.tsv" # relative to log_dir
125
+ # relative to log_dir
126
+ embedding.sprite.image_path = "openai__clip-vit-large-patch14-336_sprite.png"
127
+ embedding.sprite.single_image_dim.extend(
128
+ [100, 100]) # size of each image in the sprite
129
  projector.visualize_embeddings(log_dir, config)
130
 
131
  # tf.compat.v1.disable_eager_execution()
 
136
 
137
  # === Step 6: Launch TensorBoard Command ===
138
  print("βœ… All done! Launch TensorBoard using:")
139
+ print(f"tensorboard --logdir={log_dir}")