From 526babef65f496016fed2587634d1656cc3e2106 Mon Sep 17 00:00:00 2001 From: Ivan Date: Sat, 4 Nov 2023 23:47:41 +0300 Subject: [PATCH] Update code style of some files --- aero_vloc/vpr_systems/anyloc/models/dinov2.py | 18 ++++++------- aero_vloc/vpr_systems/anyloc/models/vlad.py | 24 ++++++++--------- aero_vloc/vpr_systems/cosplace/__init__.py | 2 +- aero_vloc/vpr_systems/cosplace/cosplace.py | 2 +- aero_vloc/vpr_systems/eigenplaces/__init__.py | 2 +- .../vpr_systems/eigenplaces/eigenplaces.py | 2 +- aero_vloc/vpr_systems/mixvpr/__init__.py | 2 +- .../mixvpr/model/mixvpr_aggregator.py | 2 +- .../vpr_systems/mixvpr/model/mixvpr_model.py | 2 +- aero_vloc/vpr_systems/mixvpr/model/resnet.py | 10 +++---- aero_vloc/vpr_systems/mixvpr/model/utils.py | 2 +- aero_vloc/vpr_systems/netvlad/__init__.py | 2 +- aero_vloc/vpr_systems/netvlad/model/layers.py | 26 ++++++++++--------- .../netvlad/model/models_generic.py | 18 ++++++++----- aero_vloc/vpr_systems/netvlad/netvlad.py | 4 +-- 15 files changed, 62 insertions(+), 56 deletions(-) diff --git a/aero_vloc/vpr_systems/anyloc/models/dinov2.py b/aero_vloc/vpr_systems/anyloc/models/dinov2.py index 84cd411..a596502 100644 --- a/aero_vloc/vpr_systems/anyloc/models/dinov2.py +++ b/aero_vloc/vpr_systems/anyloc/models/dinov2.py @@ -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: @@ -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 diff --git a/aero_vloc/vpr_systems/anyloc/models/vlad.py b/aero_vloc/vpr_systems/anyloc/models/vlad.py index 698668a..73c288f 100644 --- a/aero_vloc/vpr_systems/anyloc/models/vlad.py +++ b/aero_vloc/vpr_systems/anyloc/models/vlad.py @@ -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 @@ -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] @@ -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 diff --git a/aero_vloc/vpr_systems/cosplace/__init__.py b/aero_vloc/vpr_systems/cosplace/__init__.py index ebc32b6..ed9224d 100644 --- a/aero_vloc/vpr_systems/cosplace/__init__.py +++ b/aero_vloc/vpr_systems/cosplace/__init__.py @@ -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 \ No newline at end of file +from aero_vloc.vpr_systems.cosplace.cosplace import CosPlace diff --git a/aero_vloc/vpr_systems/cosplace/cosplace.py b/aero_vloc/vpr_systems/cosplace/cosplace.py index 1ebbfac..c72670e 100644 --- a/aero_vloc/vpr_systems/cosplace/cosplace.py +++ b/aero_vloc/vpr_systems/cosplace/cosplace.py @@ -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 diff --git a/aero_vloc/vpr_systems/eigenplaces/__init__.py b/aero_vloc/vpr_systems/eigenplaces/__init__.py index cfa32eb..a98650f 100644 --- a/aero_vloc/vpr_systems/eigenplaces/__init__.py +++ b/aero_vloc/vpr_systems/eigenplaces/__init__.py @@ -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 \ No newline at end of file +from aero_vloc.vpr_systems.eigenplaces.eigenplaces import EigenPlaces diff --git a/aero_vloc/vpr_systems/eigenplaces/eigenplaces.py b/aero_vloc/vpr_systems/eigenplaces/eigenplaces.py index b49b00d..1f47a99 100644 --- a/aero_vloc/vpr_systems/eigenplaces/eigenplaces.py +++ b/aero_vloc/vpr_systems/eigenplaces/eigenplaces.py @@ -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 diff --git a/aero_vloc/vpr_systems/mixvpr/__init__.py b/aero_vloc/vpr_systems/mixvpr/__init__.py index 82a267e..c38860b 100644 --- a/aero_vloc/vpr_systems/mixvpr/__init__.py +++ b/aero_vloc/vpr_systems/mixvpr/__init__.py @@ -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 \ No newline at end of file +from aero_vloc.vpr_systems.mixvpr.mixvpr import MixVPR diff --git a/aero_vloc/vpr_systems/mixvpr/model/mixvpr_aggregator.py b/aero_vloc/vpr_systems/mixvpr/model/mixvpr_aggregator.py index 9183ccf..ae52b93 100644 --- a/aero_vloc/vpr_systems/mixvpr/model/mixvpr_aggregator.py +++ b/aero_vloc/vpr_systems/mixvpr/model/mixvpr_aggregator.py @@ -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 \ No newline at end of file + return x diff --git a/aero_vloc/vpr_systems/mixvpr/model/mixvpr_model.py b/aero_vloc/vpr_systems/mixvpr/model/mixvpr_model.py index 9258cb0..4ef08a4 100644 --- a/aero_vloc/vpr_systems/mixvpr/model/mixvpr_model.py +++ b/aero_vloc/vpr_systems/mixvpr/model/mixvpr_model.py @@ -47,4 +47,4 @@ def __init__(self, def forward(self, x): x = self.backbone(x) x = self.aggregator(x) - return x \ No newline at end of file + return x diff --git a/aero_vloc/vpr_systems/mixvpr/model/resnet.py b/aero_vloc/vpr_systems/mixvpr/model/resnet.py index ec5c2c4..00acb4d 100644 --- a/aero_vloc/vpr_systems/mixvpr/model/resnet.py +++ b/aero_vloc/vpr_systems/mixvpr/model/resnet.py @@ -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), diff --git a/aero_vloc/vpr_systems/mixvpr/model/utils.py b/aero_vloc/vpr_systems/mixvpr/model/utils.py index 61b908b..dd29d30 100644 --- a/aero_vloc/vpr_systems/mixvpr/model/utils.py +++ b/aero_vloc/vpr_systems/mixvpr/model/utils.py @@ -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) \ No newline at end of file + return MixVPR(**agg_config) diff --git a/aero_vloc/vpr_systems/netvlad/__init__.py b/aero_vloc/vpr_systems/netvlad/__init__.py index d3deb3a..aff9748 100644 --- a/aero_vloc/vpr_systems/netvlad/__init__.py +++ b/aero_vloc/vpr_systems/netvlad/__init__.py @@ -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 \ No newline at end of file +from aero_vloc.vpr_systems.netvlad.netvlad import NetVLAD diff --git a/aero_vloc/vpr_systems/netvlad/model/layers.py b/aero_vloc/vpr_systems/netvlad/model/layers.py index 03e1f17..8b012c9 100644 --- a/aero_vloc/vpr_systems/netvlad/model/layers.py +++ b/aero_vloc/vpr_systems/netvlad/model/layers.py @@ -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: @@ -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 \ No newline at end of file + return vlad diff --git a/aero_vloc/vpr_systems/netvlad/model/models_generic.py b/aero_vloc/vpr_systems/netvlad/model/models_generic.py index 52ddcd4..5cf95bf 100644 --- a/aero_vloc/vpr_systems/netvlad/model/models_generic.py +++ b/aero_vloc/vpr_systems/netvlad/model/models_generic.py @@ -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) @@ -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") @@ -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) @@ -73,4 +77,4 @@ def get_model( nn_model.add_module( "WPCA", nn.Sequential(*[pca_conv, Flatten(), L2Norm(dim=-1)]) ) - return nn_model \ No newline at end of file + return nn_model diff --git a/aero_vloc/vpr_systems/netvlad/netvlad.py b/aero_vloc/vpr_systems/netvlad/netvlad.py index 5e0410f..7e03862 100644 --- a/aero_vloc/vpr_systems/netvlad/netvlad.py +++ b/aero_vloc/vpr_systems/netvlad/netvlad.py @@ -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)