nextBatch method
Implementation
Map<String, Float32List> nextBatch(int batchSize) {
final int features = imageSize * imageSize * 3;
final anchorBatch = Float32List(batchSize * features);
final positiveBatch = Float32List(batchSize * features);
final negativeBatch = Float32List(batchSize * features);
final people = _identityMap.keys.toList();
for (int i = 0; i < batchSize; i++) {
// 1. Pick Anchor/Positive
final personA = people[_random.nextInt(people.length)];
final imagesA = _identityMap[personA]!;
final idx1 = _random.nextInt(imagesA.length);
int idx2 = _random.nextInt(imagesA.length);
if (imagesA.length > 1) {
while (idx1 == idx2) {
idx2 = _random.nextInt(imagesA.length);
}
}
// 2. Pick Negative
String personB;
do {
personB = people[_random.nextInt(people.length)];
} while (personA == personB);
final imagesB = _identityMap[personB]!;
final idx3 = _random.nextInt(imagesB.length);
// 3. Set pixel data into the batch lists (Types now match: Float32List to Float32List)
anchorBatch.setAll(i * features, imagesA[idx1]);
positiveBatch.setAll(i * features, imagesA[idx2]);
negativeBatch.setAll(i * features, imagesB[idx3]);
}
return {
'anchor': anchorBatch,
'positive': positiveBatch,
'negative': negativeBatch,
};
}