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

dev(narugo): norm + mean #1

Merged
merged 3 commits into from
Apr 28, 2024
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
23 changes: 4 additions & 19 deletions ccip_merge/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pandas as pd
from ditk import logging
from imgutils.metrics import ccip_batch_differences, ccip_default_threshold
from scipy.optimize import minimize
from tqdm import tqdm

from .index import get_np_feats
Expand Down Expand Up @@ -35,24 +34,10 @@ def measure_tag_via_func(tag, func):


def ccip_merge_func(embs):
# TODO: just replace your function here!!!
def objective_function(x):
total_similarity = 0
for vector in embs:
total_similarity += np.dot(x, vector) / (np.linalg.norm(x) * np.linalg.norm(vector))
return -total_similarity / embs.shape[0]

initial_guess = np.random.rand(768)
constraints = ({'type': 'eq', 'fun': lambda x: np.linalg.norm(x) - 1.0})
result = minimize(
objective_function, initial_guess,
constraints=constraints,
)

mean_length = np.linalg.norm(embs, axis=1).mean()
best_vec = result.x
best_vec = best_vec * mean_length
return best_vec
lengths = np.linalg.norm(embs, axis=-1)
embs = embs / lengths.reshape(-1, 1)
ret_embedding = embs.mean(axis=0)
return ret_embedding / np.linalg.norm(ret_embedding) * lengths.mean()


def get_metrics_of_tags(n: int = 100) -> pd.DataFrame:
Expand Down
Loading