Post

Replies

Boosts

Views

Activity

CoreML Model spec - change output type to dictionary [Double : String]
Hello everybody, For the past week I have been struggling to run inference on a classifier I built using Google's AutoML Vision tool. At first I thought everything would go smoothly because Google allows to export a CoreML version of the final model. I assumed I would only need to use Apple's CoreML library to make it work. When I export the model Google provides a .mlmodel file and a dict.txt file with the classification labels. For the current model I have 100 labels. This is my Swift code to run inference on the model. private lazy var classificationRequest: VNCoreMLRequest = { 				do { 						let classificationModel = try VNCoreMLModel(for: NewGenusModel().model) 						let request = VNCoreMLRequest(model: classificationModel, completionHandler: { [weak self] request, error in 								self?.processClassifications(for: request, error: error) 						}) 						request.imageCropAndScaleOption = .scaleFit 						return request 				} 				catch { 						fatalError("Error! Can't use Model.") 				} 		}() 		func classifyImage(receivedImage: UIImage) { 				let orientation = CGImagePropertyOrientation(rawValue: UInt32(receivedImage.imageOrientation.rawValue)) 				if let image = CIImage(image: receivedImage) { 						DispatchQueue.global(qos: .userInitiated).async { 								let handler = VNImageRequestHandler(ciImage: image, orientation: orientation!) 								do { 										try handler.perform([self.classificationRequest]) 								} 								catch { 										fatalError("Error classifying image!") 								} 						} 				} 		} The problem started when I tried to pass a UIImage to run inference on the model. The input type of the original model was MultiArray (Float32 1 x 224 x 224 x 3). Using Coremltools library I was able to convert the input type to Image (Color 224 x 224) using Python. This worked and here is my code: import coremltools import coremltools.proto.FeatureTypes_pb2 as ft spec = coremltools.utils.load_spec("model.mlmodel") input = spec.description.input[0] input.type.imageType.colorSpace = ft.ImageFeatureType.RGB input.type.imageType.height = 224 input.type.imageType.width = 224 coremltools.utils.save_spec(spec, "newModel.mlmodel") My problem now is with the output type. I want to be able to access the confidence of the classification as well as the result label of the classification. Again using coremltools I was able to to access the output description and I got this. name: "scores" type { 	multiArrayType { 		dataType: FLOAT32 	} } I am trying to change it this way: f = open("dict.txt", "r") labels = f.read() class_labels = labels.splitlines() print(class_labels) class_labels = class_labels[1:] assert len(class_labels) == 57 for i, label in enumerate(class_labels): 	if isinstance(label, bytes): 		class_labels[i] = label.decode("utf8") classifier_config = ct.ClassifierConfig(class_labels) output = spec.description.output[0] output.type = ft.DictionaryFeatureType Unfortunately this is not working and I can't find information only that can help me... This I don't know what to do next. Thank you for your help!
1
0
2.2k
Sep ’20
CoreML - Label presented as random string
Hello everybody, I used Google Cloud Platform to create a Machine learning model to perform computer vision. I downloaded the CoreML model from the cloud platform website and followed the instructions in the Google Tutorial for iOS model deployment. This is my code currently. class Classification {          private lazy var classificationRequest: VNCoreMLRequest = {         do {             let model = try VNCoreMLModel(for: AutoML().model)             let request = VNCoreMLRequest(model: model, completionHandler: { [weak self] request, error in                 if let classifications = request.results as? [VNClassificationObservation] {                     print(classifications.first ?? "No classification!")                 }                          })                          request.imageCropAndScaleOption = .scaleFit             return request         }         catch {             fatalError("Error! Can't use Model.")         }     }()          func classifyImage(receivedImage: UIImage) {                  let orientation = CGImagePropertyOrientation(rawValue: UInt32(receivedImage.imageOrientation.rawValue))                  if let image = CIImage(image: receivedImage) {             DispatchQueue.global(qos: .userInitiated).async {                                  let handler = VNImageRequestHandler(ciImage: image, orientation: orientation!)                 do {                     try handler.perform([self.classificationRequest])                 }                 catch {                     fatalError("Error classifying image!")                 }             }         }     } My code executes and I receive this: <VNClassificationObservation: 0x600002091d40> A7DBD70C-541C-4112-84A4-C6B4ED2EB7E2 requestRevision=1 confidence=0.332127 "CICAgICAwPmveRIJQWdsYWlzX2lv" I receive a confidence value but I don't receive a label string. Is there any step I am not taking? With the model there is also a dict.txt file. Is there anything I have to do with that and that I am not doing? Thank you!
3
0
1.6k
Sep ’20
Swift - Show Empty map view with error message iOS
Hello everybody, I am building a Map and I am using a map view that I want to be always present in the interface. However, the user can share to show its location or not. The Map view requires a region. But I want to show an empty map view with just the grid on with a message. Do you know if it is possible to achieve that? Thank you! let span = MKCoordinateSpan(latitudeDelta: CLLOcationDegrees, longitudeDelta: CLLOcationDegrees) let region = MKCoordinateRegion(center: observationCoordinates, span: span) &#9;&#9;&#9;&#9; let annotation =&#9;MKPointAnnotation() annotation.coordinate = observationCoordinates &#9;&#9;&#9;&#9; uiView.setRegion(region, animated: true) uiView.addAnnotation(annotation)
2
0
1.5k
Sep ’20
NavigationLink initializer unused
Hello everybody, I am trying to navigate from one view to another using NavigationView and NavigationLink. I can't figure out what I am doing wrong, but the way I am doing it I get a warning saying: "Result of NavigationLink<Label, Destination> is unused". In fact, I can't navigate to the view I want to open. Here is my code so far: NavigationView { VStack { Button(action: { let observation = self.createObservation() self.records.addObservation(observation) self.isPresented.toggle() NavigationLink(destination: ObservationDetails(observation: observation).environmentObject(self.records)) { EmptyView() } }) {   Text("Classify") } } Thank you for your help!
4
0
5.4k
Sep ’20
Display annotation Map View with offset
Hello everyone, I want to use a map view to display an annotation but when I place it on the view it always stays in at the center of the map view. I want to give it some offset so that it goes a little bit upwards. This is the code I have so far. Could you please help me? let span = MKCoordinateSpan(latitudeDelta: CLLOcationDegrees, longitudeDelta: CLLOcationDegrees) let region = MKCoordinateRegion(center: observationCoordinates, span: span) let annotation =  MKPointAnnotation() annotation.coordinate = observationCoordinates          uiView.setRegion(region, animated: true) uiView.addAnnotation(annotation) Thank you for your help.
0
0
452
Aug ’20
Label.text Error! - Unexpectedly found nil while implicitly unwrapping an Optional value: file...
Hello everybody,I am new to iOS development and I found an error that I can not get past. I have read a lot online and on Stackoverflow but I don't understand why this error keeps coming up.I have a table view controller and I want to write text to other view controller using:navigationController?.pushViewController(viewController, animated: true)I have this class where I already have an outlet for the label.import UIKit class PetitionDetailsViewController: UIViewController { @IBOutlet weak var PetitionDetailsOutlet: UILabel! override func viewDidLoad() { super.viewDidLoad() } }On the other class I have this code where I try to add text to the label in the PetitionDetailsViewController after tapping one of the rows in the table view.override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let viewController = PetitionDetailsViewController() print(petitions[indexPath.row].body) viewController.PetitionDetailsOutlet.text = petitions[indexPath.row].body navigationController?.pushViewController(viewController, animated: true) }I don't understand why this error keeps coming up. I have the outlet and initially the label is empty.Why is it nil?
10
0
5.5k
Feb ’20
Metadata rejected - what should I do?
Hello everybody ,Two days ago I submitted my first iOS app. I was super excited. Yesterday my app came back rejected with “metadata rejected”. I am very inexperienced and I kind of panic and tried everything to solve it. I replied in the resolution center and then I made the mistake of clicking submit for review. And now I am waiting.Sorry to bother you but I really would like to hear from somebody with experience what is going to happen now...- Since I clicked submit for review will my app be again in the beginning of the queue?- what happens to my response in the resolution center? Will my reviewer still read it?- How long, in the worst case scenario will it take to get some feedback back?Thank you so much.Best regards,
3
0
10k
Sep ’18