Created
June 2, 2025 10:35
-
-
Save jacobsapps/d40c0a56aca564bc5c4cf93dc226d9b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func handleCLIPPrediction(from request: VNRequest, name: String) -> CardStats? { | |
let result = request.results?.first as! VNCoreMLFeatureValueObservation | |
let imageEmbedding = result.featureValue.multiArrayValue! | |
let floatArray: [Float] = (0..<imageEmbedding.count).map { | |
Float(truncating: imageEmbedding[$0]) | |
} | |
let norm = sqrt(floatArray.reduce(0) { $0 + $1 * $1 }) | |
let normalized = floatArray.map { $0 / norm } | |
let title = predictTitle(from: normalized, name) | |
return CardStats(title: title, stats: stats) | |
} | |
private func predictTitle(from imageEmbedding: [Float], _ name: String) -> String { | |
let topTitle = titleEmbeddings | |
.map { ($0.fullNameCopy.replacingOccurrences(of: "X", with: name), dot(imageEmbedding, $0.embedding)) } | |
.sorted(by: { $0.1 > $1.1 }) | |
return topTitle.first?.0 ?? "" | |
} | |
private func dot(_ a: [Float], _ b: [Float]) -> Float { | |
zip(a, b).reduce(0) { $0 + $1.0 * $1.1 } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment