[Technical Questions] How to deal with a problem when integrating ML models #145
-
In what area do you have a technical challenge?Other (non-StanfordBDHG-related) area DescriptionWhen I tried to integrate an ML model into my code to recognize images, I encountered an issue:
I couldn't find an answer, and I'm not sure if anyone knows how to handle this issue. I did not pass or request the Here is the relevant part of my code: @MainActor
class ImageClassifier: ObservableObject {
// MARK: - Published Properties
@Published var image: UIImage?
@Published var classificationOptions: [String] = []
@Published var isProcessing: Bool = false
@Published var errorMessage: String?
// MARK: - Internal Properties
private let confidenceThreshold: Float = 0.7
private var cancellables = Set<AnyCancellable>()
// MARK: - Initialization
init() {
setupBindings()
}
private func setupBindings() {
$image
.dropFirst()
.sink { [weak self] newImage in
if let image = newImage {
self?.classifyImage(image)
}
}
.store(in: &cancellables)
}
// MARK: - Main Classification Method
func classifyImage(_ image: UIImage) {
DispatchQueue.main.async {
self.isProcessing = true
self.errorMessage = nil
}
guard let ciImage = CIImage(image: image) else {
self.handleError("Could not create CIImage from UIImage")
return
}
guard let model = try? VNCoreMLModel(for: SeeFood(configuration: MLModelConfiguration()).model) else {
self.handleError("Failed to load SeeFood model")
return
}
let request = VNCoreMLRequest(model: model) { [weak self] request, error in
guard let self = self else { return }
if let error = error {
self.handleError("Classification failed: \(error.localizedDescription)")
return
}
guard let results = request.results as? [VNClassificationObservation] else {
self.handleError("Could not process classification results")
return
}
let validResults = results.filter { $0.confidence >= self.confidenceThreshold }
let topResults = validResults.prefix(3)
DispatchQueue.main.async {
self.classificationOptions = topResults.map {
"\($0.identifier) (\(String(format: "%.1f", $0.confidence * 100))%)"
}
self.isProcessing = false
}
}
let handler = VNImageRequestHandler(ciImage: ciImage)
DispatchQueue.global(qos: .userInitiated).async {
do {
try handler.perform([request])
} catch {
print("Failed to perform classification: \(error.localizedDescription)")
}
}
}
// MARK: - Helper Methods
private func handleError(_ message: String) {
DispatchQueue.main.async {
self.errorMessage = message
self.isProcessing = false
}
}
}```
The error occurs in the following block:
```swift
let handler = VNImageRequestHandler(ciImage: ciImage)
DispatchQueue.global(qos: .userInitiated).async {
do {
try handler.perform([request])
} catch {
print("Failed to perform classification: \(error.localizedDescription)")
}
}```
### Reproduction
None
### Expected behavior
have expected output for the model.(I tried MobileNetV2 and seeFood)
### Additional context
_No response_
### Code of Conduct
- [X] I agree to follow this course's Code of Conduct and Contributing Guidelines |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
@felixschlegel Do you mind taking a look at this? |
Beta Was this translation helpful? Give feedback.
-
Can you please provide a repo + branch for running this code? |
Beta Was this translation helpful? Give feedback.
Hi,
This problem was solved on Tuesday's class. Thanks your all for the kind help!