Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize NetVLAD in constructor #740

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions gtsfm/frontend/global_descriptor/netvlad_global_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import numpy as np
import torch
from torch import nn

from gtsfm.common.image import Image
from gtsfm.frontend.global_descriptor.global_descriptor_base import GlobalDescriptorBase
from thirdparty.hloc.netvlad import NetVLAD
Expand All @@ -22,7 +22,7 @@ class NetVLADGlobalDescriptor(GlobalDescriptorBase):

def __init__(self) -> None:
""" """
pass
self._model = NetVLAD().eval()

def describe(self, image: Image) -> np.ndarray:
"""Compute the NetVLAD global descriptor for a single image query.
Expand All @@ -33,16 +33,13 @@ def describe(self, image: Image) -> np.ndarray:
Returns:
img_desc: Array of shape (D,) representing global image descriptor.
"""
# Load model.
# Note: Initializing in the constructor leads to OOM.
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model: nn.Module = NetVLAD().to(device)
model.eval()
self._model.to(device)

img_tensor = (
torch.from_numpy(image.value_array).to(device).permute(2, 0, 1).unsqueeze(0).type(torch.float32) / 255
)
with torch.no_grad():
img_desc = model({"image": img_tensor})
img_desc = self._model({"image": img_tensor})

return img_desc["global_descriptor"].detach().squeeze().cpu().numpy()
Loading