Skip to content

Commit

Permalink
Get face descriptors only when needed in batches.
Browse files Browse the repository at this point in the history
In my tests with 37128 faces and a batch size of 5000 it improves
memory consumption by 38,81%.

I guess we can celebrate a little. 🎉 😉
  • Loading branch information
matiasdelellis committed Jul 30, 2024
1 parent 201b8c8 commit 221ea52
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
14 changes: 10 additions & 4 deletions lib/BackgroundJob/Tasks/CreateClustersTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,20 @@ private function createClusterIfNeeded(string $userId) {
$sliceSize = ceil($facesCount / $noSlices);
}

$this->logDebug('We will cluster with ' . $noSlices . ' batch(es) of ' . $sliceSize . ' faces');
$this->logDebug('We will cluster these with ' . $noSlices . ' batch(es) of ' . $sliceSize . ' faces.');

$newClusters = [];

// Obtain the clusters in batches and append them.
for ($i = 0; $i < $noSlices ; $i++) {
// Get the batches.
$facesSliced = array_slice($faces, $i * $sliceSize, $sliceSize);
$newClusters = array_merge($newClusters, $this->getNewClusters($facesSliced));
// Get the indices, obtain the partial clusters and incorporate them.
$faceIds = array_map(function ($face) { return $face->getId(); }, $facesSliced);
$facesDescripted = $this->faceMapper->findDescriptorsBathed($faceIds);
$newClusters = array_merge($newClusters, $this->getNewClusters($facesDescripted));
// Discard variables aggressively to improve memory consumption.
unset($facesDescripted);
unset($facesSliced);
}

// Append non groupable faces on a single step.
Expand All @@ -184,7 +190,7 @@ private function createClusterIfNeeded(string $userId) {
// Value is array of face IDs. For old clusters, person IDs are some existing person IDs,
// and for new clusters is whatever chinese whispers decides to identify them.
//
$currentClusters = $this->getCurrentClusters($faces);
$currentClusters = $this->getCurrentClusters(array_merge($faces, $nonGroupables));

$this->logInfo(count($newClusters) . ' clusters found after clustering');

Expand Down
13 changes: 11 additions & 2 deletions lib/Db/FaceMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,23 @@ public function find (int $faceId): ?Face {
$qb = $this->db->getQueryBuilder();
$qb->select('id', 'image', 'person', 'x', 'y', 'width', 'height', 'landmarks', 'descriptor', 'confidence')
->from($this->getTableName(), 'f')
->andWhere($qb->expr()->eq('id', $qb->createNamedParameter($faceId)));
->where($qb->expr()->eq('id', $qb->createNamedParameter($faceId)));
try {
return $this->findEntity($qb);
} catch (DoesNotExistException $e) {
return null;
}
}

public function findDescriptorsBathed (array $faceIds): array {
$qb = $this->db->getQueryBuilder();
$qb->select('id', 'descriptor')
->from($this->getTableName(), 'f')
->where($qb->expr()->in('id', $qb->createParameter('face_ids')));
$qb->setParameter('face_ids', $faceIds, IQueryBuilder::PARAM_INT_ARRAY);
return $this->findEntities($qb);
}

/**
* Based on a given fileId, takes all faces that belong to that file
* and return an array with that.
Expand Down Expand Up @@ -147,7 +156,7 @@ public function getFaces(string $userId, int $model): array {

public function getGroupableFaces(string $userId, int $model, int $minSize, float $minConfidence): array {
$qb = $this->db->getQueryBuilder();
$qb->select('f.id', 'f.person', 'f.descriptor')
$qb->select('f.id', 'f.person')
->from($this->getTableName(), 'f')
->innerJoin('f', 'facerecog_images' ,'i', $qb->expr()->eq('f.image', 'i.id'))
->where($qb->expr()->eq('user', $qb->createParameter('user')))
Expand Down

0 comments on commit 221ea52

Please sign in to comment.