Skip to content

Commit

Permalink
formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
B1ueber2y committed Oct 19, 2024
1 parent 2f81b57 commit 43caaa7
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 64 deletions.
4 changes: 2 additions & 2 deletions limap/base/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ def report_error(imagecols_pred, imagecols):
R_error = (
imagecols_pred.camimage(img_id).R() - imagecols.camimage(img_id).R()
)
R_error = np.sqrt(np.sum(R_error ** 2))
R_error = np.sqrt(np.sum(R_error**2))
T_error = (
imagecols_pred.camimage(img_id).T() - imagecols.camimage(img_id).T()
)
T_error = np.sqrt(np.sum(T_error ** 2))
T_error = np.sqrt(np.sum(T_error**2))
pose_errors.append(np.array([R_error, T_error]))
print("pose_error: (R, T)", np.array(pose_errors).mean(0))
15 changes: 6 additions & 9 deletions limap/estimators/absolute_pose/_pl_estimate_absolute_pose.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,12 @@ def _pl_estimate_absolute_pose(
ransac_options.data_type_weights_ = np.array(
[ransac_cfg["weight_point"], ransac_cfg["weight_line"]]
)
ransac_options.data_type_weights_ *= (
np.array(
[
ransac_options.squared_inlier_thresholds_[1],
ransac_options.squared_inlier_thresholds_[0],
]
)
/ np.sum(ransac_options.squared_inlier_thresholds_)
)
ransac_options.data_type_weights_ *= np.array(
[
ransac_options.squared_inlier_thresholds_[1],
ransac_options.squared_inlier_thresholds_[0],
]
) / np.sum(ransac_options.squared_inlier_thresholds_)
ransac_options.min_num_iterations_ = ransac_cfg["min_num_iterations"]
ransac_options.final_least_squares_ = ransac_cfg["final_least_squares"]

Expand Down
4 changes: 2 additions & 2 deletions limap/features/models/s2dnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def print_gpu_memory():
a = torch.cuda.memory_allocated(0)
f = r - a # free inside reserved

print(np.array([t, r, a, f]) / 2 ** 30)
print(np.array([t, r, a, f]) / 2**30)


class AdapLayers(nn.Module):
Expand Down Expand Up @@ -130,7 +130,7 @@ def _init(self, conf):
if isinstance(layer, torch.nn.MaxPool2d):
current_scale += 1
if i in self.hypercolumn_indices:
self.scales.append(2 ** current_scale)
self.scales.append(2**current_scale)

self.adaptation_layers = AdapLayers(
conf.hypercolumn_layers, conf.output_dim
Expand Down
2 changes: 1 addition & 1 deletion limap/features/models/vggnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _init(self, conf=default_conf):
if isinstance(layer, torch.nn.MaxPool2d):
current_scale += 1
if i in self.hypercolumn_indices:
self.scales.append(2 ** current_scale)
self.scales.append(2**current_scale)

def _forward(self, data):
image = data # data['image']
Expand Down
2 changes: 1 addition & 1 deletion limap/line2d/LineTR/line_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, n_heads: int, d_feature: int, dropout=0.1):
self.w_vs = nn.Linear(d_feature, n_heads * dim, bias=True)
self.fc = nn.Linear(n_heads * dim, d_feature, bias=True)

self.attention = ScaledDotProduct(scale=dim ** 0.5)
self.attention = ScaledDotProduct(scale=dim**0.5)

self.dropout = nn.Dropout(dropout)
self.layer_norm = nn.LayerNorm(d_feature, eps=1e-6)
Expand Down
10 changes: 4 additions & 6 deletions limap/line2d/LineTR/line_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def point_on_line(line, dist_px):
vec = ep - sp
if vec[0] != 0:
m = vec[1] / vec[0]
x = np.sqrt(dist_px ** 2 / (1 + m ** 2))
x = np.sqrt(dist_px**2 / (1 + m**2))
y = m * x
else:
x = 0
Expand Down Expand Up @@ -94,10 +94,8 @@ def remove_borders(
sp = np.floor(klines[:, 0]).astype(int)
ep = np.floor(klines[:, 1]).astype(int)
valid_mask_given = (
(
valid_mask_given[sp[:, 1], sp[:, 0]]
+ valid_mask_given[ep[:, 1], ep[:, 0]]
)
valid_mask_given[sp[:, 1], sp[:, 0]]
+ valid_mask_given[ep[:, 1], ep[:, 0]]
).astype(bool)
valid_mask = valid_mask & valid_mask_given

Expand Down Expand Up @@ -275,7 +273,7 @@ def change_cv2_T_np(klines_cv):
kline_ep = [sp_x, sp_y]

# linelength = math.sqrt((kline_ep[0]-kline_sp[0])**2 +(kline_ep[1]-kline_sp[1])**2)
linelength = line.lineLength * (2 ** line.octave)
linelength = line.lineLength * (2**line.octave)

klines_sp.append(kline_sp)
klines_ep.append(kline_ep)
Expand Down
2 changes: 1 addition & 1 deletion limap/line2d/LineTR/line_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def forward(
def attention(query, key, value):
dim = query.shape[1]
scores = (
torch.einsum("bdhn,bdhm->bhnm", query, key) / dim ** 0.5
torch.einsum("bdhn,bdhm->bhnm", query, key) / dim**0.5
) # [3, 64, 4, 512] -> [3, 4, 512, 512]
prob = torch.nn.functional.softmax(scores, dim=-1)
return torch.einsum("bhnm,bdhm->bdhn", prob, value), prob
Expand Down
24 changes: 11 additions & 13 deletions limap/line2d/LineTR/linetr_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def process_siamese(data, i):
pred["keypoints0"],
pred["keypoints1"],
**data,
pos_th=self.conf.ground_truth.th_positive
pos_th=self.conf.ground_truth.th_positive,
)
pred["gt_assignment"] = assignment
pred["gt_matches0"], pred["gt_matches1"] = m0, m1
Expand All @@ -188,7 +188,7 @@ def process_siamese(data, i):
pred["lines0"].reshape(b_size, -1, 2),
pred["lines1"].reshape(b_size, -1, 2),
**data,
pos_th=self.conf.ground_truth.th_positive
pos_th=self.conf.ground_truth.th_positive,
)
pred["samples_gt_assignment"] = samples_assignment
pred["samples_gt_matches0"] = samples_m0
Expand Down Expand Up @@ -219,7 +219,7 @@ def process_siamese(data, i):
pred["keypoints1"],
**data,
pos_th=self.conf.ground_truth.th_positive,
neg_th=self.conf.ground_truth.th_negative
neg_th=self.conf.ground_truth.th_negative,
)
pred["gt_assignment"] = assignment
pred["gt_matches0"], pred["gt_matches1"] = m0, m1
Expand All @@ -244,10 +244,8 @@ def process_siamese(data, i):
pred["lines1"].reshape(b_size, -1, 2),
**data,
pos_th=self.conf.ground_truth.th_positive,
neg_th=self.conf.ground_truth.th_negative
)[
:3
]
neg_th=self.conf.ground_truth.th_negative,
)[:3]
pred["samples_gt_assignment"] = samples_assignment
pred["samples_gt_matches0"] = samples_m0
pred["samples_gt_matches1"] = samples_m1
Expand Down Expand Up @@ -308,13 +306,13 @@ def process_siamese(data, i):
assert match_mat.shape[0] == 1
bool_match_mat = match_mat[0] > 0
pred["line_matches0"] = np.argmax(bool_match_mat, axis=1)
pred["line_matches0"][
~np.any(bool_match_mat, axis=1)
] = UNMATCHED_FEATURE
pred["line_matches0"][~np.any(bool_match_mat, axis=1)] = (
UNMATCHED_FEATURE
)
pred["line_matches1"] = np.argmax(bool_match_mat, axis=0)
pred["line_matches1"][
~np.any(bool_match_mat, axis=0)
] = UNMATCHED_FEATURE
pred["line_matches1"][~np.any(bool_match_mat, axis=0)] = (
UNMATCHED_FEATURE
)
pred["line_matches0"] = torch.from_numpy(pred["line_matches0"])[None]
pred["line_matches1"] = torch.from_numpy(pred["line_matches1"])[None]
lmatch_scores = torch.from_numpy(
Expand Down
2 changes: 1 addition & 1 deletion limap/line2d/SOLD2/misc/visualize_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Organize some frequently used visualization functions. """
"""Organize some frequently used visualization functions."""

import cv2
import numpy as np
Expand Down
6 changes: 2 additions & 4 deletions limap/line2d/SOLD2/model/line_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ def detect(self, junctions, heatmap, device=torch.device("cpu")):
dim=-1,
)
)
normalized_seg_length = segments_length / (
((H ** 2) + (W ** 2)) ** 0.5
)
normalized_seg_length = segments_length / (((H**2) + (W**2)) ** 0.5)

# Perform local max search
num_cand = cand_h.shape[0]
Expand Down Expand Up @@ -552,7 +550,7 @@ def detect_local_max(
"""Detection by local maximum search."""
# Compute the distance threshold
dist_thresh = (
0.5 * (2 ** 0.5) + self.lambda_radius * normalized_seg_length
0.5 * (2**0.5) + self.lambda_radius * normalized_seg_length
)
# Make it N x 64
dist_thresh = torch.repeat_interleave(
Expand Down
2 changes: 1 addition & 1 deletion limap/line2d/SOLD2/model/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def space_to_depth(input_tensor, grid_size):
# (N, bs, bs, C, H//bs, W//bs)
x = x.permute(0, 3, 5, 1, 2, 4).contiguous()
# (N, C*bs^2, H//bs, W//bs)
x = x.view(N, C * (grid_size ** 2), H // grid_size, W // grid_size)
x = x.view(N, C * (grid_size**2), H // grid_size, W // grid_size)
return x


Expand Down
4 changes: 2 additions & 2 deletions limap/line2d/SOLD2/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def train_single_epoch(
results = metric_func.metric_results
average = average_meter.average()
# Get gpu memory usage in GB
gpu_mem_usage = torch.cuda.max_memory_allocated() / (1024 ** 3)
gpu_mem_usage = torch.cuda.max_memory_allocated() / (1024**3)
if compute_descriptors:
print(
"Epoch [%d / %d] Iter [%d / %d] loss=%.4f (%.4f), junc_loss=%.4f (%.4f), heatmap_loss=%.4f (%.4f), descriptor_loss=%.4f (%.4f), gpu_mem=%.4fGB"
Expand Down Expand Up @@ -734,7 +734,7 @@ def record_train_summaries(writer, global_step, scalars, images):

# GPU memory part
# Get gpu memory usage in GB
gpu_mem_usage = torch.cuda.max_memory_allocated() / (1024 ** 3)
gpu_mem_usage = torch.cuda.max_memory_allocated() / (1024**3)
writer.add_scalar("GPU/GPU_memory_usage", gpu_mem_usage, global_step)

# Loss part
Expand Down
4 changes: 2 additions & 2 deletions limap/line2d/line_utils/merge_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def merge_line_cluster(lines):
if b == 0:
u = np.array([1, 0]) if a >= c else np.array([0, 1])
else:
m = (c - a + np.sqrt((a - c) ** 2 + 4 * b ** 2)) / (2 * b)
u = np.array([1, m]) / np.sqrt(1 + m ** 2)
m = (c - a + np.sqrt((a - c) ** 2 + 4 * b**2)) / (2 * b)
u = np.array([1, m]) / np.sqrt(1 + m**2)

# Get the center of gravity of all endpoints
cross = np.mean(points, axis=0)
Expand Down
7 changes: 4 additions & 3 deletions limap/optimize/line_localization/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ def reprojection_filter_matches_2to3(
best_id = None
for id in track_ids:
l3d = linetracks[id].line
l2d_start, l2d_end = ref_camview.projection(
l3d.start
), ref_camview.projection(l3d.end)
l2d_start, l2d_end = (
ref_camview.projection(l3d.start),
ref_camview.projection(l3d.end),
)
l2d = _base.Line2d(l2d_start, l2d_end)

dist = dist_func(ref_line, l2d)
Expand Down
9 changes: 5 additions & 4 deletions limap/point2d/superglue/superglue.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def attention(
query: torch.Tensor, key: torch.Tensor, value: torch.Tensor
) -> Tuple[torch.Tensor, torch.Tensor]:
dim = query.shape[1]
scores = torch.einsum("bdhn,bdhm->bhnm", query, key) / dim ** 0.5
scores = torch.einsum("bdhn,bdhm->bhnm", query, key) / dim**0.5
prob = torch.nn.functional.softmax(scores, dim=-1)
return torch.einsum("bhnm,bdhm->bdhn", prob, value), prob

Expand Down Expand Up @@ -336,9 +336,10 @@ def log_optimal_transport(
return Z

def _get_matches(self, scores_mat):
max0, max1 = scores_mat[:, :-1, :-1].max(2), scores_mat[
:, :-1, :-1
].max(1)
max0, max1 = (
scores_mat[:, :-1, :-1].max(2),
scores_mat[:, :-1, :-1].max(1),
)
m0, m1 = max0.indices, max1.indices
mutual0 = arange_like(m0, 1)[None] == m1.gather(1, m0)
mutual1 = arange_like(m1, 1)[None] == m0.gather(1, m1)
Expand Down
6 changes: 2 additions & 4 deletions limap/pointsfm/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

IS_PYTHON3 = sys.version_info[0] >= 3

MAX_IMAGE_ID = 2 ** 31 - 1
MAX_IMAGE_ID = 2**31 - 1

CREATE_CAMERAS_TABLE = """CREATE TABLE IF NOT EXISTS cameras (
camera_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Expand Down Expand Up @@ -68,9 +68,7 @@
prior_tz REAL,
CONSTRAINT image_id_check CHECK(image_id >= 0 and image_id < {}),
FOREIGN KEY(camera_id) REFERENCES cameras(camera_id))
""".format(
MAX_IMAGE_ID
)
""".format(MAX_IMAGE_ID)

CREATE_TWO_VIEW_GEOMETRIES_TABLE = """
CREATE TABLE IF NOT EXISTS two_view_geometries (
Expand Down
6 changes: 3 additions & 3 deletions limap/util/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ def rotation_from_quaternion(quad):
quad = quad / norm
qr, qi, qj, qk = quad[0], quad[1], quad[2], quad[3]
rot_mat = np.zeros((3, 3))
rot_mat[0, 0] = 1 - 2 * (qj ** 2 + qk ** 2)
rot_mat[0, 0] = 1 - 2 * (qj**2 + qk**2)
rot_mat[0, 1] = 2 * (qi * qj - qk * qr)
rot_mat[0, 2] = 2 * (qi * qk + qj * qr)
rot_mat[1, 0] = 2 * (qi * qj + qk * qr)
rot_mat[1, 1] = 1 - 2 * (qi ** 2 + qk ** 2)
rot_mat[1, 1] = 1 - 2 * (qi**2 + qk**2)
rot_mat[1, 2] = 2 * (qj * qk - qi * qr)
rot_mat[2, 0] = 2 * (qi * qk - qj * qr)
rot_mat[2, 1] = 2 * (qj * qk + qi * qr)
rot_mat[2, 2] = 1 - 2 * (qi ** 2 + qj ** 2)
rot_mat[2, 2] = 1 - 2 * (qi**2 + qj**2)
return rot_mat
5 changes: 3 additions & 2 deletions runners/hypersim/Hypersim.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ def set_max_dim(cls, max_dim):

@classmethod
def set_resize_ratio(cls, ratio):
cls.h, cls.w = int(round(cls.default_h * ratio)), int(
round(cls.default_w * ratio)
cls.h, cls.w = (
int(round(cls.default_h * ratio)),
int(round(cls.default_w * ratio)),
)
cls.K[0, :] = cls.default_K[0, :] * cls.w / cls.default_w
cls.K[1, :] = cls.default_K[1, :] * cls.h / cls.default_h
Expand Down
4 changes: 1 addition & 3 deletions runners/inloc/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ def parse_config():
# Output folder for LIMAP linetracks (in tmp)
if cfg["output_folder"] is None:
cfg["output_folder"] = "finaltracks"
cfg[
"inloc_dataset"
] = (
cfg["inloc_dataset"] = (
args.dataset
) # For reading camera poses for estimating 3D lines fron depth
return cfg, args
Expand Down

0 comments on commit 43caaa7

Please sign in to comment.