Skip to content

Commit

Permalink
Update code style of some files
Browse files Browse the repository at this point in the history
  • Loading branch information
vnmsklnk committed Nov 4, 2023
1 parent 43767a6 commit 526babe
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 56 deletions.
18 changes: 9 additions & 9 deletions aero_vloc/vpr_systems/anyloc/models/dinov2.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class DinoV2ExtractFeatures:
"""

def __init__(
self,
dino_model: _DINO_V2_MODELS,
layer: int,
facet: _DINO_FACETS = "token",
use_cls=False,
norm_descs=True,
device: str = "cpu",
self,
dino_model: _DINO_V2_MODELS,
layer: int,
facet: _DINO_FACETS = "token",
use_cls=False,
norm_descs=True,
device: str = "cpu",
) -> None:
"""
Parameters:
Expand Down Expand Up @@ -94,9 +94,9 @@ def __call__(self, img: torch.Tensor) -> torch.Tensor:
if self.facet == "query":
res = res[:, :, :d_len]
elif self.facet == "key":
res = res[:, :, d_len : 2 * d_len]
res = res[:, :, d_len: 2 * d_len]
else:
res = res[:, :, 2 * d_len :]
res = res[:, :, 2 * d_len:]
if self.norm_descs:
res = F.normalize(res, dim=-1)
self._hook_out = None # Reset the hook
Expand Down
24 changes: 12 additions & 12 deletions aero_vloc/vpr_systems/anyloc/models/vlad.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ class VLAD:
"""

def __init__(
self,
num_clusters: int,
c_centers_path: Path,
desc_dim: Union[int, None] = None,
intra_norm: bool = True,
norm_descs: bool = True,
dist_mode: str = "cosine",
vlad_mode: str = "hard",
soft_temp: float = 1.0,
self,
num_clusters: int,
c_centers_path: Path,
desc_dim: Union[int, None] = None,
intra_norm: bool = True,
norm_descs: bool = True,
dist_mode: str = "cosine",
vlad_mode: str = "hard",
soft_temp: float = 1.0,
) -> None:
self.num_clusters = num_clusters
self.desc_dim = desc_dim
Expand Down Expand Up @@ -107,7 +107,7 @@ def generate(self, query_descs: Union[np.ndarray, torch.Tensor]) -> torch.Tensor
cd_sum = residuals[labels == k, k].sum(dim=0)
if self.intra_norm:
cd_sum = F.normalize(cd_sum, dim=0)
un_vlad[k * self.desc_dim : (k + 1) * self.desc_dim] = cd_sum
un_vlad[k * self.desc_dim: (k + 1) * self.desc_dim] = cd_sum
else: # Soft cluster assignment
# Cosine similarity: 1 = close, -1 = away
cos_sims = F.cosine_similarity( # [q, c]
Expand All @@ -125,13 +125,13 @@ def generate(self, query_descs: Union[np.ndarray, torch.Tensor]) -> torch.Tensor
) # [d]
if self.intra_norm:
cd_sum = F.normalize(cd_sum, dim=0)
un_vlad[k * self.desc_dim : (k + 1) * self.desc_dim] = cd_sum
un_vlad[k * self.desc_dim: (k + 1) * self.desc_dim] = cd_sum
# Normalize the VLAD vector
n_vlad = F.normalize(un_vlad, dim=0)
return n_vlad

def generate_res_vec(
self, query_descs: Union[np.ndarray, torch.Tensor]
self, query_descs: Union[np.ndarray, torch.Tensor]
) -> torch.Tensor:
assert self.kmeans is not None
assert self.c_centers is not None
Expand Down
2 changes: 1 addition & 1 deletion aero_vloc/vpr_systems/cosplace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from aero_vloc.vpr_systems.cosplace.cosplace import CosPlace
from aero_vloc.vpr_systems.cosplace.cosplace import CosPlace
2 changes: 1 addition & 1 deletion aero_vloc/vpr_systems/cosplace/cosplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CosPlace(VPRSystem):
"""

def __init__(
self, backbone: str = "ResNet101", fc_output_dim: int = 2048, resize: int = 800
self, backbone: str = "ResNet101", fc_output_dim: int = 2048, resize: int = 800
):
super().__init__()
self.resize = resize
Expand Down
2 changes: 1 addition & 1 deletion aero_vloc/vpr_systems/eigenplaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from aero_vloc.vpr_systems.eigenplaces.eigenplaces import EigenPlaces
from aero_vloc.vpr_systems.eigenplaces.eigenplaces import EigenPlaces
2 changes: 1 addition & 1 deletion aero_vloc/vpr_systems/eigenplaces/eigenplaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class EigenPlaces(VPRSystem):
"""

def __init__(
self, backbone: str = "ResNet101", fc_output_dim: int = 2048, resize: int = 800
self, backbone: str = "ResNet101", fc_output_dim: int = 2048, resize: int = 800
):
super().__init__()
self.resize = resize
Expand Down
2 changes: 1 addition & 1 deletion aero_vloc/vpr_systems/mixvpr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from aero_vloc.vpr_systems.mixvpr.mixvpr import MixVPR
from aero_vloc.vpr_systems.mixvpr.mixvpr import MixVPR
2 changes: 1 addition & 1 deletion aero_vloc/vpr_systems/mixvpr/model/mixvpr_aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ def forward(self, x):
x = x.permute(0, 2, 1)
x = self.row_proj(x)
x = F.normalize(x.flatten(1), p=2, dim=-1)
return x
return x
2 changes: 1 addition & 1 deletion aero_vloc/vpr_systems/mixvpr/model/mixvpr_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ def __init__(self,
def forward(self, x):
x = self.backbone(x)
x = self.aggregator(x)
return x
return x
10 changes: 5 additions & 5 deletions aero_vloc/vpr_systems/mixvpr/model/resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

class ResNet(nn.Module):
def __init__(
self,
model_name="resnet50",
pretrained=True,
layers_to_freeze=2,
layers_to_crop=[],
self,
model_name="resnet50",
pretrained=True,
layers_to_freeze=2,
layers_to_crop=[],
):
"""Class representing the resnet backbone used in the pipeline
we consider resnet network as a list of 5 blocks (from 0 to 4),
Expand Down
2 changes: 1 addition & 1 deletion aero_vloc/vpr_systems/mixvpr/model/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ def get_aggregator(agg_arch='ConvAP', agg_config={}):
assert 'in_h' in agg_config
assert 'in_w' in agg_config
assert 'mix_depth' in agg_config
return MixVPR(**agg_config)
return MixVPR(**agg_config)
2 changes: 1 addition & 1 deletion aero_vloc/vpr_systems/netvlad/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from aero_vloc.vpr_systems.netvlad.netvlad import NetVLAD
from aero_vloc.vpr_systems.netvlad.netvlad import NetVLAD
26 changes: 14 additions & 12 deletions aero_vloc/vpr_systems/netvlad/model/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ class NetVLADModule(nn.Module):
"""NetVLAD layer implementation"""

def __init__(
self,
num_clusters=64,
dim=128,
normalize_input=True,
vladv2=False,
use_faiss=True,
self,
num_clusters=64,
dim=128,
normalize_input=True,
vladv2=False,
use_faiss=True,
):
"""
Args:
Expand Down Expand Up @@ -109,16 +109,18 @@ def forward(self, x):
[N, self.num_clusters, C], dtype=x.dtype, layout=x.layout, device=x.device
)
for C in range(
self.num_clusters
self.num_clusters
): # slower than non-looped, but lower memory usage
residual = x_flatten.unsqueeze(0).permute(1, 0, 2, 3) - self.centroids[
C : C + 1, :
].expand(x_flatten.size(-1), -1, -1).permute(1, 2, 0).unsqueeze(0)
residual *= soft_assign[:, C : C + 1, :].unsqueeze(2)
vlad[:, C : C + 1, :] = residual.sum(dim=-1)
C: C + 1, :
].expand(x_flatten.size(-1), -1, -1).permute(1, 2,
0).unsqueeze(
0)
residual *= soft_assign[:, C: C + 1, :].unsqueeze(2)
vlad[:, C: C + 1, :] = residual.sum(dim=-1)

vlad = F.normalize(vlad, p=2, dim=2) # intra-normalization
vlad = vlad.view(x.size(0), -1) # flatten
vlad = F.normalize(vlad, p=2, dim=1) # L2 normalize

return vlad
return vlad
18 changes: 11 additions & 7 deletions aero_vloc/vpr_systems/netvlad/model/models_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from aero_vloc.vpr_systems.netvlad.model.layers import NetVLADModule


class Flatten(nn.Module):
def forward(self, input_data):
return input_data.view(input_data.size(0), -1)
Expand All @@ -34,6 +35,7 @@ def __init__(self, dim=1):
def forward(self, input_data):
return F.normalize(input_data, p=2, dim=self.dim)


def get_backend():
enc_dim = 512
enc = models.vgg16(weights="IMAGENET1K_V1")
Expand All @@ -45,17 +47,19 @@ def get_backend():
enc = nn.Sequential(*layers)
return enc_dim, enc


def get_pca_encoding(model, vlad_encoding):
pca_encoding = model.WPCA(vlad_encoding.unsqueeze(-1).unsqueeze(-1))
return pca_encoding


def get_model(
encoder,
encoder_dim,
num_clusters,
use_vladv2=False,
append_pca_layer=False,
num_pcs=8192,
encoder,
encoder_dim,
num_clusters,
use_vladv2=False,
append_pca_layer=False,
num_pcs=8192,
):
nn_model = nn.Module()
nn_model.add_module("encoder", encoder)
Expand All @@ -73,4 +77,4 @@ def get_model(
nn_model.add_module(
"WPCA", nn.Sequential(*[pca_conv, Flatten(), L2Norm(dim=-1)])
)
return nn_model
return nn_model
4 changes: 2 additions & 2 deletions aero_vloc/vpr_systems/netvlad/netvlad.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def __init__(self, path_to_weights: str, resize: int = 800):
self.model.eval()

def get_image_descriptor(
self,
image_path: Path,
self,
image_path: Path,
):
image = Image.open(image_path).convert("RGB")
image = transform_image(image, self.resize)[None, :].to(self.device)
Expand Down

0 comments on commit 526babe

Please sign in to comment.