Spaces:
Running
on
Zero
Running
on
Zero
derektan
commited on
Commit
·
0a3d091
1
Parent(s):
a3c38a0
[NEW] Able to interface with TestWorker with minimal changes to VLM-Search related files. Able to visualize robot movement gif on gradio
Browse files- .vscode/launch.json +15 -0
- Taxabind/TaxaBind/SatBind/clip_seg_tta.py +7 -3
- Taxabind/TaxaBind/SatBind/dataset.py +37 -36
- app.py +74 -30
- env.py +72 -0
- test_multi_robot_worker.py +29 -25
.vscode/launch.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"version": "0.2.0",
|
| 3 |
+
"configurations": [
|
| 4 |
+
{
|
| 5 |
+
"name": "Debug app.py",
|
| 6 |
+
"type": "debugpy",
|
| 7 |
+
"request": "launch",
|
| 8 |
+
"program": "${workspaceFolder}/app.py",
|
| 9 |
+
"cwd": "${workspaceFolder}",
|
| 10 |
+
"console": "integratedTerminal",
|
| 11 |
+
"justMyCode": false,
|
| 12 |
+
"python": "/home/user/anaconda3/envs/vlm-search/bin/python3"
|
| 13 |
+
}
|
| 14 |
+
]
|
| 15 |
+
}
|
Taxabind/TaxaBind/SatBind/clip_seg_tta.py
CHANGED
|
@@ -116,7 +116,9 @@ class ClipSegTTA:
|
|
| 116 |
self.sound_model.eval()
|
| 117 |
|
| 118 |
# Params
|
| 119 |
-
|
|
|
|
|
|
|
| 120 |
|
| 121 |
# for idx, related_img_path in enumerate(self.related_imgs_paths):
|
| 122 |
# self.visualize_heatmap(
|
|
@@ -153,8 +155,10 @@ class ClipSegTTA:
|
|
| 153 |
if self.load_model:
|
| 154 |
self.reset_local_model() # Reset to global weights as init
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
| 158 |
# self.img_path = self.img_paths[0] # Select 1st img as query
|
| 159 |
# self.img = self.imgs[0] # Select 1st img as query
|
| 160 |
# self.img = self.img.to(self.device)
|
|
|
|
| 116 |
self.sound_model.eval()
|
| 117 |
|
| 118 |
# Params
|
| 119 |
+
# NOTE: ADDED CONDITIONAL
|
| 120 |
+
if sample_index >= 0:
|
| 121 |
+
self.reset(sample_idx=self.sample_index)
|
| 122 |
|
| 123 |
# for idx, related_img_path in enumerate(self.related_imgs_paths):
|
| 124 |
# self.visualize_heatmap(
|
|
|
|
| 155 |
if self.load_model:
|
| 156 |
self.reset_local_model() # Reset to global weights as init
|
| 157 |
|
| 158 |
+
# NOTE: Added 'sample_idx' conditional
|
| 159 |
+
if sample_idx >= 0:
|
| 160 |
+
self.img_paths, self.imo_path, self.imgs, self.imo, self.sounds, self.sound_ids, self.species_name, self.target_positions, self.gt_mask_name = self.dataset.get_search_ds_data(sample_idx)
|
| 161 |
+
self.imgs = self.imgs.to(self.device)
|
| 162 |
# self.img_path = self.img_paths[0] # Select 1st img as query
|
| 163 |
# self.img = self.imgs[0] # Select 1st img as query
|
| 164 |
# self.img = self.img.to(self.device)
|
Taxabind/TaxaBind/SatBind/dataset.py
CHANGED
|
@@ -32,42 +32,43 @@ class SatNatDataset(Dataset):
|
|
| 32 |
# ADDED
|
| 33 |
self.current_epoch = 0
|
| 34 |
|
| 35 |
-
|
| 36 |
-
# self.
|
| 37 |
-
self.
|
| 38 |
-
self.
|
| 39 |
-
self.
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
self.
|
| 44 |
-
self.
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
# self.
|
| 48 |
-
self.
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
if mode == 'train':
|
| 73 |
self.img_transform = transforms.Compose([
|
|
|
|
| 32 |
# ADDED
|
| 33 |
self.current_epoch = 0
|
| 34 |
|
| 35 |
+
## NOTE: Removed as unnecessary for app.py
|
| 36 |
+
# self.json = json.load(open(json_path, 'r'))
|
| 37 |
+
# # self.sat_filt_json = json.load(open(sat_filtered_json_path, 'r'))
|
| 38 |
+
# self.sat_to_img_ids_json_path = json.load(open(sat_to_img_ids_json_path, 'r'))
|
| 39 |
+
# self.images = self.json['images']
|
| 40 |
+
# self.annot = self.json['annotations']
|
| 41 |
+
# for i in range(len(self.images)):
|
| 42 |
+
# assert self.images[i]['id'] == self.annot[i]['id']
|
| 43 |
+
# self.images[i]['label'] = self.annot[i]['category_id']
|
| 44 |
+
# self.filtered_json = [d for d in self.images if d['latitude'] is not None and d['longitude'] is not None]
|
| 45 |
+
# self.species_text = list(set([" ".join(d['file_name'].split("/")[1].split("_")[1:]) for d in self.filtered_json]))
|
| 46 |
+
|
| 47 |
+
# # self.sat_filtered_json = [d for d in self.sat_filt_json['images'] if d['latitude'] is not None and d['longitude'] is not None]
|
| 48 |
+
# # self.sat_paths = {d['id']: str(d['id'])+'_'+str(d['latitude'])+'_'+str(d['longitude'])+'.jpg' for d in self.sat_filtered_json}
|
| 49 |
+
# self.inat_json_dict = {
|
| 50 |
+
# "images": {img["id"]: img for img in self.images},
|
| 51 |
+
# "annotations": {ann["id"]: ann for ann in self.annot},
|
| 52 |
+
# }
|
| 53 |
+
|
| 54 |
+
# # Expand dict
|
| 55 |
+
# self.sat_to_img_ids_tuples = []
|
| 56 |
+
# if self.sat_to_img_ids_json_is_train_dict:
|
| 57 |
+
# # for i in range(len(self.sat_filtered_json)):
|
| 58 |
+
# # id = self.sat_filtered_json[i]['id']
|
| 59 |
+
# # sat_id = Path(self.sat_paths[id]).stem # remove file extension
|
| 60 |
+
# # img_ids = self.sat_to_img_ids_json_path[sat_id]["img_ids"]
|
| 61 |
+
# for sat_key, sat_sample in self.sat_to_img_ids_json_path.items():
|
| 62 |
+
# id = sat_sample["id"] # int(sat_key.split("_")[0]) # sat_sample['id']
|
| 63 |
+
# sat_path = sat_sample["sat_path"]
|
| 64 |
+
# img_ids = sat_sample["img_ids"]
|
| 65 |
+
# # img_locs = sat_sample["target_positions"] # NOTE: Not accurate after resize
|
| 66 |
+
# for img_id in img_ids:
|
| 67 |
+
# self.sat_to_img_ids_tuples.append((id, sat_path, img_id))
|
| 68 |
+
# print("len(self.sat_to_img_ids_json_path): ", len(self.sat_to_img_ids_json_path))
|
| 69 |
+
# print("len(self.sat_to_img_ids_tuples): ", len(self.sat_to_img_ids_tuples))
|
| 70 |
+
# else:
|
| 71 |
+
# self.filtered_val_ds_by_tax = [d for d in self.sat_to_img_ids_json_path if self.tax_to_filter_val in d['taxonomy']]
|
| 72 |
|
| 73 |
if mode == 'train':
|
| 74 |
self.img_transform = transforms.Compose([
|
app.py
CHANGED
|
@@ -11,7 +11,12 @@ This version mirrors the layout of `app_BACKUP.py` but:
|
|
| 11 |
# ────────────────────────── imports ───────────────────────────────────
|
| 12 |
from pathlib import Path
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
import gradio as gr
|
|
|
|
| 15 |
import torch
|
| 16 |
from PIL import Image
|
| 17 |
|
|
@@ -26,7 +31,7 @@ from Taxabind.TaxaBind.SatBind.clip_seg_tta import ClipSegTTA # noqa: E402
|
|
| 26 |
|
| 27 |
|
| 28 |
# CHANGE ME!
|
| 29 |
-
|
| 30 |
|
| 31 |
# Prepare the model
|
| 32 |
# device = torch.device('cpu') #if USE_GPU_TRAINING else torch.device('cpu')
|
|
@@ -51,7 +56,7 @@ if TAXABIND_TTA:
|
|
| 51 |
sat_to_img_ids_json_path=TAXABIND_SAT_TO_IMG_IDS_JSON_PATH,
|
| 52 |
patch_size=TAXABIND_PATCH_SIZE,
|
| 53 |
sat_checkpoint_path=TAXABIND_SAT_CHECKPOINT_PATH,
|
| 54 |
-
sample_index =
|
| 55 |
blur_kernel = TAXABIND_GAUSSIAN_BLUR_KERNEL,
|
| 56 |
device=device,
|
| 57 |
sat_to_img_ids_json_is_train_dict=False, # for search ds val
|
|
@@ -66,23 +71,12 @@ if TAXABIND_TTA:
|
|
| 66 |
else:
|
| 67 |
clip_seg_tta = None
|
| 68 |
|
| 69 |
-
# Define TestWorker
|
| 70 |
-
planner = TestWorker(
|
| 71 |
-
meta_agent_id=0,
|
| 72 |
-
n_agent=1,
|
| 73 |
-
policy_net=policy_net,
|
| 74 |
-
global_step=3,
|
| 75 |
-
device='cuda',
|
| 76 |
-
greedy=True,
|
| 77 |
-
save_image=SAVE_GIFS,
|
| 78 |
-
clip_seg_tta=clip_seg_tta
|
| 79 |
-
)
|
| 80 |
|
| 81 |
# ────────────────────────── Gradio process fn ─────────────────────────
|
| 82 |
|
| 83 |
def process(
|
| 84 |
-
|
| 85 |
-
|
| 86 |
taxonomy: str | None = None,
|
| 87 |
):
|
| 88 |
"""Callback executed when the user presses **Run** in the UI.
|
|
@@ -93,17 +87,67 @@ def process(
|
|
| 93 |
retained to conform to the requested interface.
|
| 94 |
"""
|
| 95 |
# If no satellite image is provided we bail out early.
|
| 96 |
-
if
|
| 97 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
# Optionally you may want to reset episode index or make it configurable.
|
| 100 |
# For now we hard-code episode 0, mirroring the snippet.
|
| 101 |
-
planner.run_episode(
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
#
|
| 106 |
-
|
|
|
|
|
|
|
| 107 |
|
| 108 |
|
| 109 |
# ────────────────────────── Gradio UI ─────────────────────────────────
|
|
@@ -121,7 +165,7 @@ with gr.Blocks(title="Search-TTA (Simplified)", theme=gr.themes.Base()) as demo:
|
|
| 121 |
sat_input = gr.Image(
|
| 122 |
label="Satellite Image",
|
| 123 |
sources=["upload"],
|
| 124 |
-
type="
|
| 125 |
height=320,
|
| 126 |
)
|
| 127 |
taxonomy_input = gr.Textbox(
|
|
@@ -131,14 +175,14 @@ with gr.Blocks(title="Search-TTA (Simplified)", theme=gr.themes.Base()) as demo:
|
|
| 131 |
ground_input = gr.Image(
|
| 132 |
label="Ground-level Image (optional)",
|
| 133 |
sources=["upload"],
|
| 134 |
-
type="
|
| 135 |
height=320,
|
| 136 |
)
|
| 137 |
run_btn = gr.Button("Run", variant="primary")
|
| 138 |
|
| 139 |
with gr.Column():
|
| 140 |
-
gr.Markdown("###
|
| 141 |
-
|
| 142 |
|
| 143 |
# Bind callback
|
| 144 |
|
|
@@ -175,8 +219,8 @@ with gr.Blocks(title="Search-TTA (Simplified)", theme=gr.themes.Base()) as demo:
|
|
| 175 |
],
|
| 176 |
],
|
| 177 |
inputs=[sat_input, ground_input, taxonomy_input],
|
| 178 |
-
outputs=[
|
| 179 |
-
fn=lambda sat, grd, tax: process(sat, grd),
|
| 180 |
cache_examples=False,
|
| 181 |
)
|
| 182 |
|
|
@@ -207,8 +251,8 @@ with gr.Blocks(title="Search-TTA (Simplified)", theme=gr.themes.Base()) as demo:
|
|
| 207 |
],
|
| 208 |
],
|
| 209 |
inputs=[sat_input, ground_input, taxonomy_input],
|
| 210 |
-
outputs=[
|
| 211 |
-
fn=lambda sat, grd, tax: process(sat, grd),
|
| 212 |
cache_examples=False,
|
| 213 |
)
|
| 214 |
|
|
@@ -216,7 +260,7 @@ with gr.Blocks(title="Search-TTA (Simplified)", theme=gr.themes.Base()) as demo:
|
|
| 216 |
run_btn.click(
|
| 217 |
fn=process,
|
| 218 |
inputs=[sat_input, ground_input, taxonomy_input],
|
| 219 |
-
outputs=
|
| 220 |
)
|
| 221 |
|
| 222 |
# ────────────────────────── unchanged worker initialisation ───────────
|
|
|
|
| 11 |
# ────────────────────────── imports ───────────────────────────────────
|
| 12 |
from pathlib import Path
|
| 13 |
|
| 14 |
+
# Use non-GUI backend to avoid Tkinter errors in background threads
|
| 15 |
+
import matplotlib
|
| 16 |
+
matplotlib.use("Agg", force=True)
|
| 17 |
+
|
| 18 |
import gradio as gr
|
| 19 |
+
import os, glob, threading, time
|
| 20 |
import torch
|
| 21 |
from PIL import Image
|
| 22 |
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
# CHANGE ME!
|
| 34 |
+
POLL_INTERVAL = 0.1 # For visualization
|
| 35 |
|
| 36 |
# Prepare the model
|
| 37 |
# device = torch.device('cpu') #if USE_GPU_TRAINING else torch.device('cpu')
|
|
|
|
| 56 |
sat_to_img_ids_json_path=TAXABIND_SAT_TO_IMG_IDS_JSON_PATH,
|
| 57 |
patch_size=TAXABIND_PATCH_SIZE,
|
| 58 |
sat_checkpoint_path=TAXABIND_SAT_CHECKPOINT_PATH,
|
| 59 |
+
sample_index = -1, # Set using 'reset' in worker
|
| 60 |
blur_kernel = TAXABIND_GAUSSIAN_BLUR_KERNEL,
|
| 61 |
device=device,
|
| 62 |
sat_to_img_ids_json_is_train_dict=False, # for search ds val
|
|
|
|
| 71 |
else:
|
| 72 |
clip_seg_tta = None
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
# ────────────────────────── Gradio process fn ─────────────────────────
|
| 76 |
|
| 77 |
def process(
|
| 78 |
+
sat_path: str | None,
|
| 79 |
+
ground_path: str | None,
|
| 80 |
taxonomy: str | None = None,
|
| 81 |
):
|
| 82 |
"""Callback executed when the user presses **Run** in the UI.
|
|
|
|
| 87 |
retained to conform to the requested interface.
|
| 88 |
"""
|
| 89 |
# If no satellite image is provided we bail out early.
|
| 90 |
+
if sat_path is None:
|
| 91 |
+
return None
|
| 92 |
+
|
| 93 |
+
# ------------------------------------------------------------------
|
| 94 |
+
# Load images from paths and configure ClipSegTTA inputs
|
| 95 |
+
sat_img = Image.open(sat_path).convert("RGB")
|
| 96 |
+
ground_img_pil = Image.open(ground_path).convert("RGB") if ground_path else None
|
| 97 |
+
|
| 98 |
+
clip_seg_tta.img_paths = [ground_path] if ground_path else []
|
| 99 |
+
clip_seg_tta.imo_path = sat_path
|
| 100 |
+
clip_seg_tta.imgs = ([clip_seg_tta.dataset.img_transform(ground_img_pil).to(device)]
|
| 101 |
+
if ground_img_pil else [])
|
| 102 |
+
clip_seg_tta.imo = clip_seg_tta.dataset.imo_transform(sat_img).to(device)
|
| 103 |
+
clip_seg_tta.sounds = []
|
| 104 |
+
clip_seg_tta.sound_ids = [] # None
|
| 105 |
+
clip_seg_tta.species_name = taxonomy or ""
|
| 106 |
+
clip_seg_tta.target_positions = [(0,0)] # PLACEHOLDERS
|
| 107 |
+
clip_seg_tta.gt_mask_name = taxonomy.replace(" ", "_") # None
|
| 108 |
+
|
| 109 |
+
# Define TestWorker
|
| 110 |
+
planner = TestWorker(
|
| 111 |
+
meta_agent_id=0,
|
| 112 |
+
n_agent=1,
|
| 113 |
+
policy_net=policy_net,
|
| 114 |
+
global_step=-1,
|
| 115 |
+
device='cuda',
|
| 116 |
+
greedy=True,
|
| 117 |
+
save_image=SAVE_GIFS,
|
| 118 |
+
clip_seg_tta=clip_seg_tta
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
# ------------------------------------------------------------------
|
| 122 |
+
|
| 123 |
+
# Empty gifs_path folder
|
| 124 |
+
if os.path.exists(gifs_path):
|
| 125 |
+
for file in os.listdir(gifs_path):
|
| 126 |
+
os.remove(os.path.join(gifs_path, file))
|
| 127 |
|
| 128 |
# Optionally you may want to reset episode index or make it configurable.
|
| 129 |
# For now we hard-code episode 0, mirroring the snippet.
|
| 130 |
+
t = threading.Thread(target=planner.run_episode, args=(0,), daemon=True)
|
| 131 |
+
t.start()
|
| 132 |
+
# planner.run_episode(0)
|
| 133 |
|
| 134 |
+
sent = set()
|
| 135 |
+
last_img = None
|
| 136 |
+
while t.is_alive():
|
| 137 |
+
# discover any new pngs written by TestWorker
|
| 138 |
+
pngs = glob.glob(os.path.join(gifs_path, "*.png"))
|
| 139 |
+
pngs.sort(key=lambda p: int(os.path.splitext(os.path.basename(p))[0]))
|
| 140 |
+
for fp in pngs:
|
| 141 |
+
if fp not in sent:
|
| 142 |
+
sent.add(fp)
|
| 143 |
+
last_img = fp
|
| 144 |
+
yield fp # stream update
|
| 145 |
+
time.sleep(POLL_INTERVAL)
|
| 146 |
|
| 147 |
+
# one final yield after the loop finishes
|
| 148 |
+
yield last_img
|
| 149 |
+
|
| 150 |
+
print("planner.perf_metrics: ", planner.perf_metrics)
|
| 151 |
|
| 152 |
|
| 153 |
# ────────────────────────── Gradio UI ─────────────────────────────────
|
|
|
|
| 165 |
sat_input = gr.Image(
|
| 166 |
label="Satellite Image",
|
| 167 |
sources=["upload"],
|
| 168 |
+
type="filepath",
|
| 169 |
height=320,
|
| 170 |
)
|
| 171 |
taxonomy_input = gr.Textbox(
|
|
|
|
| 175 |
ground_input = gr.Image(
|
| 176 |
label="Ground-level Image (optional)",
|
| 177 |
sources=["upload"],
|
| 178 |
+
type="filepath",
|
| 179 |
height=320,
|
| 180 |
)
|
| 181 |
run_btn = gr.Button("Run", variant="primary")
|
| 182 |
|
| 183 |
with gr.Column():
|
| 184 |
+
gr.Markdown("### Live Heatmap")
|
| 185 |
+
display_img = gr.Image(label="Current Heatmap", type="filepath", height=512)
|
| 186 |
|
| 187 |
# Bind callback
|
| 188 |
|
|
|
|
| 219 |
],
|
| 220 |
],
|
| 221 |
inputs=[sat_input, ground_input, taxonomy_input],
|
| 222 |
+
outputs=[display_img],
|
| 223 |
+
fn=lambda sat, grd, tax: process(sat, grd, tax),
|
| 224 |
cache_examples=False,
|
| 225 |
)
|
| 226 |
|
|
|
|
| 251 |
],
|
| 252 |
],
|
| 253 |
inputs=[sat_input, ground_input, taxonomy_input],
|
| 254 |
+
outputs=[display_img],
|
| 255 |
+
fn=lambda sat, grd, tax: process(sat, grd, tax),
|
| 256 |
cache_examples=False,
|
| 257 |
)
|
| 258 |
|
|
|
|
| 260 |
run_btn.click(
|
| 261 |
fn=process,
|
| 262 |
inputs=[sat_input, ground_input, taxonomy_input],
|
| 263 |
+
outputs=display_img,
|
| 264 |
)
|
| 265 |
|
| 266 |
# ────────────────────────── unchanged worker initialisation ───────────
|
env.py
CHANGED
|
@@ -739,6 +739,78 @@ class Env():
|
|
| 739 |
self.frame_files.append(frame)
|
| 740 |
plt.close()
|
| 741 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 742 |
|
| 743 |
####################
|
| 744 |
|
|
|
|
| 739 |
self.frame_files.append(frame)
|
| 740 |
plt.close()
|
| 741 |
|
| 742 |
+
####################
|
| 743 |
+
# ADDED: For app.py
|
| 744 |
+
####################
|
| 745 |
+
|
| 746 |
+
def plot_heatmap(self, save_dir, step, travel_dist, robots_route=None):
|
| 747 |
+
"""Plot only the segmentation heatmap and save it as ``{step}.png`` in
|
| 748 |
+
``save_dir``. This lightweight helper is meant for asynchronous
|
| 749 |
+
streaming in the Gradio demo when full `plot_env` is too heavy.
|
| 750 |
+
|
| 751 |
+
Parameters
|
| 752 |
+
----------
|
| 753 |
+
save_dir : str
|
| 754 |
+
Directory to save the generated PNG file.
|
| 755 |
+
step : int
|
| 756 |
+
Current timestep; becomes the filename ``{step}.png``.
|
| 757 |
+
robots_route : list | None
|
| 758 |
+
Optional list of routes (xPoints, yPoints) to overlay.
|
| 759 |
+
Returns
|
| 760 |
+
-------
|
| 761 |
+
str
|
| 762 |
+
Full path to the generated PNG file.
|
| 763 |
+
"""
|
| 764 |
+
import os
|
| 765 |
+
plt.switch_backend('agg')
|
| 766 |
+
plt.cla()
|
| 767 |
+
|
| 768 |
+
color_list = ["r", "g", "c", "m", "y", "k"]
|
| 769 |
+
fig, ax = plt.subplots(1, 1, figsize=(6, 6))
|
| 770 |
+
|
| 771 |
+
# Select the mask to visualise
|
| 772 |
+
if TAXABIND_TTA and USE_CLIP_PREDS:
|
| 773 |
+
side_dim = int(np.sqrt(self.segmentation_info_mask.shape[0]))
|
| 774 |
+
mask_viz = self.segmentation_info_mask.squeeze().reshape((side_dim, side_dim)).T
|
| 775 |
+
scale_y = math.ceil(self.ground_truth_size[1] / side_dim)
|
| 776 |
+
scale_x = math.ceil(self.ground_truth_size[0] / side_dim)
|
| 777 |
+
upscaled_mask_viz = np.kron(mask_viz, np.ones((scale_y, scale_x)))
|
| 778 |
+
upscaled_mask_viz = upscaled_mask_viz[:self.ground_truth_size[1], :self.ground_truth_size[0]]
|
| 779 |
+
im = ax.imshow(upscaled_mask_viz, cmap="viridis")
|
| 780 |
+
ax.axis("off")
|
| 781 |
+
else:
|
| 782 |
+
im = ax.imshow(self.segmentation_mask.mean(axis=-1), cmap='viridis', vmin=0, vmax=100)
|
| 783 |
+
ax.axis((0, self.ground_truth_size[1], self.ground_truth_size[0], 0))
|
| 784 |
+
|
| 785 |
+
# Optionally overlay robot paths
|
| 786 |
+
if robots_route is not None:
|
| 787 |
+
for i, route in enumerate(robots_route):
|
| 788 |
+
robot_marker_color = color_list[i % len(color_list)]
|
| 789 |
+
xPoints, yPoints = route
|
| 790 |
+
ax.plot(xPoints, yPoints, c=robot_marker_color, linewidth=2)
|
| 791 |
+
ax.plot(xPoints[-1], yPoints[-1], markersize=12, zorder=99, marker="^", ls="-", c=robot_marker_color, mec="black")
|
| 792 |
+
|
| 793 |
+
# Plot target positions
|
| 794 |
+
for target in self.target_positions:
|
| 795 |
+
if self.coverage_belief[target[1], target[0]] == 255:
|
| 796 |
+
# ax.plot(target[0], target[1], 'go', markersize=8, zorder=99)
|
| 797 |
+
ax.plot(target[0], target[1], color='g', marker='x', linestyle='-', markersize=12, markeredgewidth=4, zorder=99)
|
| 798 |
+
else:
|
| 799 |
+
# ax.plot(target[0], target[1], 'ro', markersize=8, zorder=99)
|
| 800 |
+
ax.plot(target[0], target[1], color='r', marker='x', linestyle='-', markersize=12, markeredgewidth=4, zorder=99)
|
| 801 |
+
|
| 802 |
+
# Color bar
|
| 803 |
+
cbar = fig.colorbar(im, ax=ax, fraction=0.046, pad=0.04)
|
| 804 |
+
cbar.set_label("Normalized Probs")
|
| 805 |
+
|
| 806 |
+
plt.suptitle('Targets Found: {}/{} Coverage ratio: {:.4g} Travel Dist: {:.4g}'.format(self.num_targets_found, \
|
| 807 |
+
len(self.target_positions), self.explored_rate, travel_dist))
|
| 808 |
+
|
| 809 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 810 |
+
out_path = os.path.join(save_dir, f"{step}.png")
|
| 811 |
+
fig.savefig(out_path, dpi=100)
|
| 812 |
+
plt.close(fig)
|
| 813 |
+
return out_path
|
| 814 |
|
| 815 |
####################
|
| 816 |
|
test_multi_robot_worker.py
CHANGED
|
@@ -142,7 +142,7 @@ class TestWorker:
|
|
| 142 |
min_patch_size=5, # smoothing parameter
|
| 143 |
n_smooth_iter=2, # smoothing parameter
|
| 144 |
ignore_label=-1,
|
| 145 |
-
plot=
|
| 146 |
gifs_dir = gifs_path
|
| 147 |
)
|
| 148 |
# Fit & predict (this will also plot the clusters before & after smoothing)
|
|
@@ -277,31 +277,35 @@ class TestWorker:
|
|
| 277 |
if not os.path.exists(gifs_path):
|
| 278 |
os.makedirs(gifs_path)
|
| 279 |
sound_id_override = None if self.clip_seg_tta.sound_ids == [] else self.clip_seg_tta.sound_ids[0]
|
|
|
|
|
|
|
| 280 |
if TAXABIND_TTA and USE_CLIP_PREDS:
|
| 281 |
-
self.env.
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
|
|
|
|
|
|
| 305 |
|
| 306 |
if done:
|
| 307 |
break
|
|
|
|
| 142 |
min_patch_size=5, # smoothing parameter
|
| 143 |
n_smooth_iter=2, # smoothing parameter
|
| 144 |
ignore_label=-1,
|
| 145 |
+
plot=False, # NOTE: Set to false since using app.py
|
| 146 |
gifs_dir = gifs_path
|
| 147 |
)
|
| 148 |
# Fit & predict (this will also plot the clusters before & after smoothing)
|
|
|
|
| 277 |
if not os.path.exists(gifs_path):
|
| 278 |
os.makedirs(gifs_path)
|
| 279 |
sound_id_override = None if self.clip_seg_tta.sound_ids == [] else self.clip_seg_tta.sound_ids[0]
|
| 280 |
+
|
| 281 |
+
## NOTE: Replaced since using app.py
|
| 282 |
if TAXABIND_TTA and USE_CLIP_PREDS:
|
| 283 |
+
self.env.plot_heatmap(gifs_path, step, max(travel_dist_list), robots_route)
|
| 284 |
+
# if TAXABIND_TTA and USE_CLIP_PREDS:
|
| 285 |
+
# self.env.plot_env(
|
| 286 |
+
# self.global_step,
|
| 287 |
+
# gifs_path,
|
| 288 |
+
# step,
|
| 289 |
+
# max(travel_dist_list),
|
| 290 |
+
# robots_route,
|
| 291 |
+
# img_path_override=self.clip_seg_tta.img_paths[0], # Viz 1st
|
| 292 |
+
# sat_path_override=self.clip_seg_tta.imo_path,
|
| 293 |
+
# msk_name_override=self.clip_seg_tta.species_name,
|
| 294 |
+
# sound_id_override=sound_id_override,
|
| 295 |
+
# colormap_mid_val=np.max(self.clip_seg_tta.heatmap_unnormalized_initial)
|
| 296 |
+
# )
|
| 297 |
+
# else:
|
| 298 |
+
# self.env.plot_env(
|
| 299 |
+
# self.global_step,
|
| 300 |
+
# gifs_path,
|
| 301 |
+
# step,
|
| 302 |
+
# max(travel_dist_list),
|
| 303 |
+
# robots_route,
|
| 304 |
+
# img_path_override=self.clip_seg_tta.img_paths[0], # Viz 1st
|
| 305 |
+
# sat_path_override=self.clip_seg_tta.imo_path,
|
| 306 |
+
# msk_name_override=self.clip_seg_tta.species_name,
|
| 307 |
+
# sound_id_override=sound_id_override,
|
| 308 |
+
# )
|
| 309 |
|
| 310 |
if done:
|
| 311 |
break
|