From 63cd8b5bb18150b1457e8659186b347f1a47e046 Mon Sep 17 00:00:00 2001 From: travisdriver Date: Tue, 14 Nov 2023 19:50:59 -0800 Subject: [PATCH 1/3] Initialize NetVLAD in constructor --- .../global_descriptor/netvlad_global_descriptor.py | 7 +++---- thirdparty/hloc/netvlad.py | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/gtsfm/frontend/global_descriptor/netvlad_global_descriptor.py b/gtsfm/frontend/global_descriptor/netvlad_global_descriptor.py index 0bb9d2b59..8aa7344ba 100644 --- a/gtsfm/frontend/global_descriptor/netvlad_global_descriptor.py +++ b/gtsfm/frontend/global_descriptor/netvlad_global_descriptor.py @@ -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. @@ -36,13 +36,12 @@ def describe(self, image: Image) -> np.ndarray: # 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() diff --git a/thirdparty/hloc/netvlad.py b/thirdparty/hloc/netvlad.py index 2cc07fe2c..756a4263b 100644 --- a/thirdparty/hloc/netvlad.py +++ b/thirdparty/hloc/netvlad.py @@ -72,7 +72,7 @@ def forward(self, x: Tensor) -> Tensor: class NetVLAD(nn.Module): - default_conf = {"model_name": "VGG16-NetVLAD-Pitts30K", "checkpoint_dir": netvlad_path, "whiten": True} + default_conf = {"model_name": "VGG16-NetVLAD-Pitts30K", "checkpoint_dir": netvlad_path, "whiten": False} required_inputs = ["image"] # Models exported using From 61b454d3add0155733fe90c1ad9eddd82ad736f1 Mon Sep 17 00:00:00 2001 From: travisdriver Date: Tue, 14 Nov 2023 19:51:55 -0800 Subject: [PATCH 2/3] Cleanup --- gtsfm/frontend/global_descriptor/netvlad_global_descriptor.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/gtsfm/frontend/global_descriptor/netvlad_global_descriptor.py b/gtsfm/frontend/global_descriptor/netvlad_global_descriptor.py index 8aa7344ba..956cfcb95 100644 --- a/gtsfm/frontend/global_descriptor/netvlad_global_descriptor.py +++ b/gtsfm/frontend/global_descriptor/netvlad_global_descriptor.py @@ -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 @@ -33,8 +33,6 @@ 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") self._model.to(device) From fbafd3ca561ea5cb6b3c1712e4592baec773ee82 Mon Sep 17 00:00:00 2001 From: travisdriver Date: Tue, 14 Nov 2023 19:54:45 -0800 Subject: [PATCH 3/3] Turn whitening back on --- thirdparty/hloc/netvlad.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thirdparty/hloc/netvlad.py b/thirdparty/hloc/netvlad.py index 756a4263b..2cc07fe2c 100644 --- a/thirdparty/hloc/netvlad.py +++ b/thirdparty/hloc/netvlad.py @@ -72,7 +72,7 @@ def forward(self, x: Tensor) -> Tensor: class NetVLAD(nn.Module): - default_conf = {"model_name": "VGG16-NetVLAD-Pitts30K", "checkpoint_dir": netvlad_path, "whiten": False} + default_conf = {"model_name": "VGG16-NetVLAD-Pitts30K", "checkpoint_dir": netvlad_path, "whiten": True} required_inputs = ["image"] # Models exported using