mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-02-15 07:35:27 +03:00
Only save stats for multi modal searches and only use cosine similarity for image -> image search
This commit is contained in:
parent
673394d9a1
commit
1165c42a8d
@ -481,20 +481,15 @@ def events_search(request: Request, params: EventsSearchQueryParams = Depends())
|
|||||||
else:
|
else:
|
||||||
search_types = search_type.split(",")
|
search_types = search_type.split(",")
|
||||||
|
|
||||||
# only normalize multi-modal searches
|
# only save stats for multi-modal searches
|
||||||
apply_normalization = (
|
save_stats = "thumbnail" in search_types and "description" in search_types
|
||||||
"thumbnail" in search_types and "description" in search_types
|
|
||||||
)
|
|
||||||
|
|
||||||
if "thumbnail" in search_types:
|
if "thumbnail" in search_types:
|
||||||
thumb_result = context.search_thumbnail(query)
|
thumb_result = context.search_thumbnail(query)
|
||||||
|
|
||||||
if apply_normalization:
|
thumb_distances = context.thumb_stats.normalize(
|
||||||
thumb_distances = context.thumb_stats.normalize(
|
[result[1] for result in thumb_result], save_stats
|
||||||
[result[1] for result in thumb_result]
|
)
|
||||||
)
|
|
||||||
else:
|
|
||||||
thumb_distances = [result[1] for result in thumb_result]
|
|
||||||
|
|
||||||
thumb_ids = dict(
|
thumb_ids = dict(
|
||||||
zip([result[0] for result in thumb_result], thumb_distances)
|
zip([result[0] for result in thumb_result], thumb_distances)
|
||||||
@ -508,14 +503,10 @@ def events_search(request: Request, params: EventsSearchQueryParams = Depends())
|
|||||||
|
|
||||||
if "description" in search_types:
|
if "description" in search_types:
|
||||||
desc_result = context.search_description(query)
|
desc_result = context.search_description(query)
|
||||||
if apply_normalization:
|
|
||||||
desc_distances = context.desc_stats.normalize(
|
desc_distances = context.desc_stats.normalize(
|
||||||
[result[1] for result in desc_result]
|
[result[1] for result in desc_result], save_stats
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
desc_distances = [
|
|
||||||
result[1] for result in desc_result
|
|
||||||
] # Use raw distances
|
|
||||||
|
|
||||||
desc_ids = dict(zip([result[0] for result in desc_result], desc_distances))
|
desc_ids = dict(zip([result[0] for result in desc_result], desc_distances))
|
||||||
|
|
||||||
|
|||||||
@ -22,8 +22,9 @@ class ZScoreNormalization:
|
|||||||
def stddev(self):
|
def stddev(self):
|
||||||
return math.sqrt(self.variance) if self.variance > 0 else 0.0
|
return math.sqrt(self.variance) if self.variance > 0 else 0.0
|
||||||
|
|
||||||
def normalize(self, distances: list[float]):
|
def normalize(self, distances: list[float], save_stats: bool):
|
||||||
self._update(distances)
|
if save_stats:
|
||||||
|
self._update(distances)
|
||||||
if self.stddev == 0:
|
if self.stddev == 0:
|
||||||
return distances
|
return distances
|
||||||
return [
|
return [
|
||||||
|
|||||||
@ -190,17 +190,13 @@ export default function SearchView({
|
|||||||
// confidence score
|
// confidence score
|
||||||
|
|
||||||
const zScoreToConfidence = (score: number) => {
|
const zScoreToConfidence = (score: number) => {
|
||||||
// Normalizing is needed for multi-modal searches only
|
// Normalizing is not needed for similarity searches
|
||||||
// Sigmoid function for normalized: 1 / (1 + e^x)
|
// Sigmoid function for normalized: 1 / (1 + e^x)
|
||||||
// Cosine for non-normalized
|
// Cosine for similarity
|
||||||
if (searchFilter) {
|
if (searchFilter) {
|
||||||
const normalized =
|
const notNormalized = searchFilter?.search_type?.includes("similarity");
|
||||||
!searchFilter.search_type ||
|
|
||||||
(Array.isArray(searchFilter.search_type) &&
|
const confidence = notNormalized ? 1 - score : 1 / (1 + Math.exp(score));
|
||||||
searchFilter.search_type.length === 2 &&
|
|
||||||
searchFilter.search_type.includes("thumbnail") &&
|
|
||||||
searchFilter.search_type.includes("description"));
|
|
||||||
const confidence = normalized ? 1 / (1 + Math.exp(score)) : 1 - score;
|
|
||||||
|
|
||||||
return Math.round(confidence * 100);
|
return Math.round(confidence * 100);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user