text stringlengths 0 184 |
|---|
Args: |
image_dir (str): Path to the directory containing images. |
weights_path (str): Path to the AsymmetricMASt3R model weights. |
retrieval_model_path (str): Path to the retrieval model (e.g., "trainingfree.pth"). |
scene_graph (str, optional): String defining the scene graph construction strategy. |
Defaults to 'retrieval-20-1-10-1'. |
device (str, optional): PyTorch device to use ('cuda' or 'cpu'). Defaults to 'cuda'. |
Returns: |
sorted_pairs: List[Tuple[str, str]], where each tuple contains |
the relative paths of the paired images (img1, img2). |
""" |
print("🖼️ Scanning images...") |
imgs = get_image_list(image_dir) |
imgs_fp = [os.path.join(image_dir, f) for f in imgs] |
if not imgs: |
print("⚠️ No valid images found in the directory. Returning empty pairs.") |
return [] |
print(f"⚙️ Loading backbone model from {weights_path}...") |
backbone = AsymmetricMASt3R.from_pretrained(weights_path).to(device).eval() |
# print("🔍 Running ASMK retrieval...") |
retriever = Retriever(retrieval_model_path, backbone=backbone) |
with torch.no_grad(): |
sim_matrix_np = retriever(imgs_fp) |
# Cleanup GPU cache |
del retriever |
del backbone |
torch.cuda.empty_cache() |
# print("🧮 Generating image pairs...") |
# make_pairs 应该期望一个 PyTorch 张量作为 sim_mat |
raw_pairs = make_pairs(imgs, scene_graph, prefilter=None, symmetrize=True, sim_mat=sim_matrix_np) |
# print(raw_pairs) |
sorted_pairs = sorted(set(tuple(sorted([a, b])) for a, b in raw_pairs)) |
print(f"✅ Generated {len(sorted_pairs)} unique image pairs.") |
return sorted_pairs |
import kornia as K |
import kornia.feature as KF |
# --- Helper function for image loading (if not already defined) --- |
def load_torch_image(fname, device=torch.device('cpu')): |
img = K.io.load_image(fname, K.io.ImageLoadType.RGB32, device=device)[None, ...] |
return img |
# Must Use efficientnet global descriptor to get matching shortlists. |
def get_global_desc(fnames, device = torch.device('cpu')): |
processor = AutoImageProcessor.from_pretrained('/kaggle/input/dinov2/pytorch/base/1') |
model = AutoModel.from_pretrained('/kaggle/input/dinov2/pytorch/base/1') |
model = model.eval() |
model = model.to(device) |
global_descs_dinov2 = [] |
for i, img_fname_full in tqdm(enumerate(fnames),total= len(fnames)): |
key = os.path.splitext(os.path.basename(img_fname_full))[0] |
timg = load_torch_image(img_fname_full) |
with torch.inference_mode(): |
inputs = processor(images=timg, return_tensors="pt", do_rescale=False).to(device) |
outputs = model(**inputs) |
dino_mac = F.normalize(outputs.last_hidden_state[:,1:].max(dim=1)[0], dim=1, p=2) |
global_descs_dinov2.append(dino_mac.detach().cpu()) |
global_descs_dinov2 = torch.cat(global_descs_dinov2, dim=0) |
return global_descs_dinov2 |
def get_img_pairs_exhaustive(img_fnames): |
index_pairs = [] |
for i in range(len(img_fnames)): |
for j in range(i+1, len(img_fnames)): |
index_pairs.append((i,j)) |
return index_pairs |
def get_image_pairs_shortlist_org(fnames, |
sim_th = 0.6, # should be strict |
min_pairs = 60, |
exhaustive_if_less = 20, |
device=torch.device('cpu')): |
num_imgs = len(fnames) |
if num_imgs <= exhaustive_if_less: |
return get_img_pairs_exhaustive(fnames) |
descs = get_global_desc(fnames, device=device) |
dm = torch.cdist(descs, descs, p=2).detach().cpu().numpy() |
mask = dm <= sim_th |
total = 0 |
matching_list = [] |
ar = np.arange(num_imgs) |
already_there_set = [] |
for st_idx in range(num_imgs-1): |
mask_idx = mask[st_idx] |
to_match = ar[mask_idx] |
if len(to_match) < min_pairs: |
to_match = np.argsort(dm[st_idx])[:min_pairs] |
for idx in to_match: |
if st_idx == idx: |
continue |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.