I am implementing face recognition and clustering application in iOS, For that first I have detected faces from image and cropped face images using MLKit, That works well.
Now I am giving cropped face images in face recognition machine learning models using tensorflow lite to get vectors for face.
But In this second step, I am getting same output for all face images, So far I have tried three different versions of facenet machine learning models.
Below is the code to initialise machine learning model:
init() throws {
do {
// Initialize an interpreter with the model.
interpreter = try Interpreter(modelPath: modelPath)
// Allocate memory for the model's input `Tensor`s.
try interpreter.allocateTensors()
// Get input details
let inputDetails = try interpreter.input(at: 0)
inputWidth = inputDetails.shape.dimensions[1]
inputHeight = inputDetails.shape.dimensions[2]
// Print input shape
print("Input Shape: \(inputDetails.shape), inputWidth: \(inputWidth), inputHeight: \(inputHeight)")
} catch {
throw error
}
}
Below is the code to process cropped face images:
func generateVectors(images: [UIImage]) {
do {
for image in images {
guard let resizedImage = image.resized(to: CGSize(width: inputWidth, height: inputHeight)),
let inputData = resizedImage.normalizedData() else {
print("Failed to process image")
continue
}
// Feed data to model
try interpreter.copy(inputData, toInputAt: 0)
// Run inference
try interpreter.invoke()
// Get output
let outputTensor = try interpreter.output(at: 0)
// Convert raw Data to Float32 array
let floatArray = outputTensor.data.withUnsafeBytes {
Array(UnsafeBufferPointer<Float32>(
start: $0.baseAddress?.assumingMemoryBound(to: Float32.self),
count: outputTensor.shape.dimensions[1] // 128 values
))
}
print("Feature Vector Output: \(floatArray)")
}
} catch let error {
print("Error running model: \(error)")
}
return output
}
Below is the output vectors:
[-0.009220079, 0.038770936, 0.01804376, 0.0017266792, -0.06855814, 0.07214466, -0.06875174, -0.06868738, -0.09033966, -0.01930608, -0.0195967, 0.011521962, -0.008457049, 0.03948402, -0.002224167, -0.058808904, -0.030869633, -0.009765219, 0.000973374, 0.0037677472, -0.19366269, 0.08309283, -0.047775332, 0.016188627, 0.02456835, 0.01125063, -0.05468251, 0.08173297, 0.19266018, -0.059302196, -0.01258643, 0.28976113, 0.09724123, 0.002565295, -0.0486573, 0.10080463, -0.026181312, -0.014548875, 0.0027476046, -0.010945596, 0.009463895, 0.005252799, 0.019287268, -0.009135995, 0.008196506, -0.0612358, 0.009160928, 0.004444012, -0.010797426, 0.06427721, 0.0635613, -0.00705058, -0.23729105, -0.0038941011, -0.021929592, 0.0070061935, 0.007189498, 0.0051750448, -0.1103151, 0.034162432, 0.038976744, -0.13650523, -0.050892524, 0.04492468, -0.016929258, 0.017758325, -0.007822959, 0.019677926, 0.00964872, 0.0012666954, -0.044016927, -0.034723878, -0.08498321, 0.012788337, -0.041820418, 0.009875813, 0.003703687, -0.0010015147, 0.2554124, 0.046859697, -0.008996924, 0.028291812, -0.0138691915, 0.197871, -0.08906104, -0.003602797, -0.00471391, -0.009401287, 0.040403757, -0.14449984, 0.015253428, -0.0011967644, 0.012509383, -0.029001027, -0.017686743, -0.15465711, -0.010484967, 0.006724833, -0.0010327495, 0.004064801, 0.0033183543, -0.0161573, -0.008383467, -0.00051657186, -0.0041507203, 0.0070093162, -0.17482482, 0.0017419434, -0.005793525, 0.022245506, -0.1609532, 0.012124289, 0.012564862, 0.27895862, 0.013894456, 0.06850863, -0.003990949, -0.045771595, 0.11117715, 0.11178145, 0.15556717, -0.017600833, -0.15439847, -0.0024967825, 0.002401842, -0.0032675255, 0.0061577093, -0.001042465, -0.0018741376, -0.116546564, 0.014505089, 0.030642524, -0.00013455506, -0.00610297, 0.07161295, -0.014800259, -0.18568702, -0.010711309, 0.009508076, 0.012341375, -0.005046241, 0.0009768148, -0.0007504827, -0.057355925, -0.089514, 0.07412282, -0.020803858, -0.0035957384, 0.009036027, -0.01416573, -0.011037713, -0.1024432, -0.0001064163, -0.034000482, -0.0112722535, -0.002430556, 0.005671933, 0.009195163, -0.13230675, -0.0062144296, -0.07031675, -0.0019532659, -0.015630726, 0.0007332002, 0.005727988, 0.024316145, 0.02418903, -0.09510416, -0.006006736, -0.00040619334, 0.18963154, 0.032816295, -0.0025780753, -0.013093517, 0.02123824, 0.005748838, -0.089399874, -0.0964007, -0.0134110255, 0.010199679, -0.07981146, -0.03332103, 0.00030683028, -0.0036552714, 0.16836038, 0.02386042, -0.11319512, 0.10237385, 0.11490546, -0.06683481, -0.083391674, -0.013886787]
I am not adding multiple output here, because they are same and I don't want to add them unnecessary.
I doubt there is some issue with processing cropped face images, As I have tried three different machine learning models, and I am facing same issue with all of them.
One of the facenet model I am using I took from this code:
All of the machine learning models are giving different outputs compared to each other, but they all are giving same output for every input image.
Thanks in advance!