-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathFaceClusterAnalyzer.php
226 lines (185 loc) · 7.29 KB
/
FaceClusterAnalyzer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
/*
* Copyright (c) 2022 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/
namespace OCA\Recognize\Service;
use OCA\Recognize\Clustering\HDBSCAN;
use OCA\Recognize\Db\FaceCluster;
use OCA\Recognize\Db\FaceClusterMapper;
use OCA\Recognize\Db\FaceDetection;
use OCA\Recognize\Db\FaceDetectionMapper;
use Rubix\ML\Datasets\Labeled;
use Rubix\ML\Kernels\Distance\Euclidean;
class FaceClusterAnalyzer {
public const MIN_DATASET_SIZE = 30;
public const MIN_SAMPLE_SIZE = 4; // Conservative value: 10
public const MIN_CLUSTER_SIZE = 5; // Conservative value: 10
public const MIN_DETECTION_SIZE = 0.03;
public const DIMENSIONS = 128;
public const SAMPLE_SIZE_EXISTING_CLUSTERS = 42;
private FaceDetectionMapper $faceDetections;
private FaceClusterMapper $faceClusters;
private Logger $logger;
public function __construct(FaceDetectionMapper $faceDetections, FaceClusterMapper $faceClusters, Logger $logger) {
$this->faceDetections = $faceDetections;
$this->faceClusters = $faceClusters;
$this->logger = $logger;
}
/**
* @throws \OCP\DB\Exception
* @throws \JsonException
*/
public function calculateClusters(string $userId, int $batchSize = -1): void {
$this->logger->debug('ClusterDebug: Retrieving face detections for user ' . $userId);
$unclusteredDetections = $this->faceDetections->findUnclusteredByUserId($userId);
$unclusteredDetections = array_values(array_filter($unclusteredDetections, fn ($detection) =>
$detection->getHeight() > self::MIN_DETECTION_SIZE && $detection->getWidth() > self::MIN_DETECTION_SIZE
));
if (count($unclusteredDetections) < self::MIN_DATASET_SIZE) {
$this->logger->debug('ClusterDebug: Not enough face detections found');
return;
}
if ($batchSize > 0 && count($unclusteredDetections) > $batchSize) {
$unclusteredDetections = array_slice($unclusteredDetections, 0, $batchSize);
}
$this->logger->debug('ClusterDebug: Found ' . count($unclusteredDetections) . " unclustered detections. Calculating clusters.");
$sampledDetections = [];
$existingClusters = $this->faceClusters->findByUserId($userId);
foreach ($existingClusters as $existingCluster) {
$sampled = $this->faceDetections->findClusterSample($existingCluster->getId(), self::SAMPLE_SIZE_EXISTING_CLUSTERS);
$sampledDetections = array_merge($sampledDetections, $sampled);
}
$detections = array_merge($unclusteredDetections, $sampledDetections);
$dataset = new Labeled(array_map(static function (FaceDetection $detection): array {
return $detection->getVector();
}, $detections), array_combine(array_keys($detections), array_keys($detections)), false);
$hdbscan = new HDBSCAN($dataset, self::MIN_CLUSTER_SIZE, self::MIN_SAMPLE_SIZE);
$numberOfClusteredDetections = 0;
$clusters = $hdbscan->predict();
foreach ($clusters as $flatCluster) {
$detectionKeys = array_keys($flatCluster->getClusterVertices());
$clusterCentroid = self::calculateCentroidOfDetections(array_map(static fn ($key) => $detections[$key], $detectionKeys));
/**
* @var int|false $detection
*/
$detection = current(array_filter($detectionKeys, static fn ($key) => $detections[$key]->getClusterId() !== null));
if ($detection !== false) {
$clusterId = $detections[$detection]->getClusterId();
$cluster = $this->faceClusters->find($clusterId);
} else {
$cluster = new FaceCluster();
$cluster->setTitle('');
$cluster->setUserId($userId);
$this->faceClusters->insert($cluster);
}
foreach ($detectionKeys as $detectionKey) {
if ($detectionKey >= count($unclusteredDetections)) {
// This is a sampled, already clustered detection, ignore.
continue;
}
// If threshold is larger than 0 and $clusterCentroid is not the null vector
if ($unclusteredDetections[$detectionKey]->getThreshold() > 0.0 && count(array_filter($clusterCentroid, fn ($el) => $el !== 0.0)) > 0) {
// If a threshold is set for this detection and its vector is farther away from the centroid
// than the threshold, skip assigning this detection to the cluster
$distanceValue = self::distance($clusterCentroid, $unclusteredDetections[$detectionKey]->getVector());
if ($distanceValue >= $unclusteredDetections[$detectionKey]->getThreshold()) {
continue;
}
}
$this->faceDetections->assocWithCluster($unclusteredDetections[$detectionKey], $cluster);
$numberOfClusteredDetections += 1;
}
}
$this->logger->debug('ClusterDebug: Clustering complete. Total num of clustered detections: ' . $numberOfClusteredDetections);
$this->pruneClusters($userId);
}
/**
* @throws \OCP\DB\Exception
*/
public function pruneClusters(string $userId): void {
$clusters = $this->faceClusters->findByUserId($userId);
if (count($clusters) === 0) {
$this->logger->debug('No face clusters found');
return;
}
foreach ($clusters as $cluster) {
$detections = $this->faceDetections->findByClusterId($cluster->getId());
$filesWithDuplicateFaces = $this->findFilesWithDuplicateFaces($detections);
if (count($filesWithDuplicateFaces) === 0) {
continue;
}
$centroid = self::calculateCentroidOfDetections($detections);
foreach ($filesWithDuplicateFaces as $fileDetections) {
$detectionsByDistance = [];
foreach ($fileDetections as $detection) {
$distance = new Euclidean();
$detectionsByDistance[$detection->getId()] = $distance->compute($centroid, $detection->getVector());
}
asort($detectionsByDistance);
$bestMatchingDetectionId = array_keys($detectionsByDistance)[0];
foreach ($fileDetections as $detection) {
if ($detection->getId() === $bestMatchingDetectionId) {
continue;
}
$detection->setClusterId(null);
$this->faceDetections->update($detection);
}
}
}
}
/**
* @param FaceDetection[] $detections
* @return list<float>
*/
public static function calculateCentroidOfDetections(array $detections): array {
// init 128 dimensional vector
/** @var list<float> $sum */
$sum = [];
for ($i = 0; $i < self::DIMENSIONS; $i++) {
$sum[] = 0;
}
if (count($detections) === 0) {
return $sum;
}
foreach ($detections as $detection) {
$sum = array_map(static function ($el, $el2) {
return $el + $el2;
}, $detection->getVector(), $sum);
}
$centroid = array_map(static function ($el) use ($detections) {
return $el / count($detections);
}, $sum);
return $centroid;
}
/**
* @param array<FaceDetection> $detections
* @return array<int,FaceDetection[]>
*/
private function findFilesWithDuplicateFaces(array $detections): array {
$files = [];
foreach ($detections as $detection) {
if (!isset($files[$detection->getFileId()])) {
$files[$detection->getFileId()] = [];
}
$files[$detection->getFileId()][] = $detection;
}
/** @var array<int,FaceDetection[]> $filesWithDuplicateFaces */
$filesWithDuplicateFaces = array_filter($files, static function ($detections) {
return count($detections) > 1;
});
return $filesWithDuplicateFaces;
}
private static ?Euclidean $distance;
/**
* @param list<int|float> $v1
* @param list<int|float> $v2
* @return float
*/
private static function distance(array $v1, array $v2): float {
if (!isset(self::$distance)) {
self::$distance = new Euclidean();
}
return self::$distance->compute($v1, $v2);
}
}