2017-11-04 12 views
0

私は、CoreMLとVisionを使って簡単にコンピュータビジョンアプリケーションを作成しようと努力してきました。 64x64解像度のカラー画像を取得し、アルファベットの文字を教えてくれる自分のKerasネットワークを訓練しました。VN分類が機能していませんか?

//send a request to the network to identify the image 
      let request = VNCoreMLRequest(model: model) { (request, error) in 
       guard let results = request.results as? [VNClassificationObservation] else { 
        fatalError("Model failed to load image") 
      } 

私は過去3時間と希望のために、このエラーに引っかかって頂いております:私は自分の携帯電話でこのアプリケーションを実行し、画像を撮影し、ヒットすると、コードのこの部分では、コードがクラッシュ「は、この画像を使用して」あなたたちは何が間違っているのか理解する手助けをすることができます!以下は、私が使用したコードの残りの部分です。

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

    @IBOutlet weak var imageView: UIImageView! 

    let imagePicker = UIImagePickerController() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     imagePicker.delegate = self 
     imagePicker.sourceType = .camera 
     imagePicker.allowsEditing = false 

    } 

    //function to chose an image from your library or take one with your camera 
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 

     if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 

      imageView.image = userPickedImage 

      //transform the image from UIImage to a CIIMage type 
      guard let ciImage = CIImage(image: userPickedImage) else { 
       fatalError("Couldn't transform image to CIImage type") 
      } 

      //call the detect function to pass the new ciImage into the network 
      detect(image: ciImage) 

     } 

     imagePicker.dismiss(animated: true, completion: nil) 

    } 

    //function to classify the image that is taken with the camera or chosen from the library 
    func detect(image: CIImage) { 

     //try to load the model, if not throw an error 
     guard let model = try? VNCoreMLModel(for: chars74k().model) else { 
      fatalError("Loading coreML model failed") 
     } 

     //send a request to the network to identify the image 
     let request = VNCoreMLRequest(model: model) { (request, error) in 
      guard let results = request.results as? [VNClassificationObservation] else { 
       fatalError("Model failed to load image") 
     } 

      print(results) 

    } 
     //create handler for image 
     let handler = VNImageRequestHandler(ciImage: image) 

     do{ 
      try handler.perform([request]) 
     } 
     catch { 
      print(error) 
     } 
+0

どのラインで正確にクラッシュしますか?あなたの 'fatalError'や' VMCoreMLRequest() 'や' try handler.perform() 'の呼び出しで? –

+0

結果をVNClassificationObservationとして要求した後にfatalError行がクラッシュする –

+0

InceptionV3モデルでテストした結果、このコードが正しく動作することがわかっていますが、自分のKeras CNNでコードをテストすると64x64カラーの画像を入力すると、そのfatalErrorでコードがクラッシュします。 –

答えて

1

結果のタイプが間違っています。結果のタイプを確認するには、fatalError行にブレークポイントを置き、デバッガで実行します。しかし、私はあなたが初心者だと思うので、これを試してみてください。あなたがそれらを読んでください

print("your results are \(type(of: results))") 

if let results = request.results as? [VNClassificationObservation] { 
    print("your results are of type VNClassificationObservation") 
} 

if let results = request.results as? [VNPixelBufferObservation] { 
    print("your results are of type VNPixelBufferObservation") 
} 

if let results = request.results as? [VNCoreMLFeatureValueObservation] { 
    print("your results are of type VNCoreMLFeatureValueObservation") 
} 

docsVNCoreMLRequestの可能な結果にはかなりはっきりしている(私は非常にこれをお勧めしますが、それなしで成功したとしても、それはあまりないですし、彼ら:

guard let results = request.results as? [VNClassificationObservation] else { 
    fatalError("Model failed to load image") 
} 

と:交換してください簡単です)。

チャンスはあなたの失敗閉鎖に交換する必要があることですので、あなたの要求の出力は、モデルの他の出力と分類を組み合わせるものになるだろう例のほとんど:

guard let results = request.results as? [VNCoreMLFeatureValueObservation] else { 
    fatalError("Model failed to load results") 
} 

は、参照するコードを変更することを忘れないでくださいそれに応じてresultsになります。 VNCoreMLFeatureValueObservationプロトコルには、考えられる値に関する情報が含まれています。これは、あなたが期待すべきかにいくつかの光を当てる必要があります

print(model.modelDescription.outputDescriptionsByName) 

:あなたはまた、とあなたのモデルからデバッガにそれらを印刷することができます。

+0

これは、CNN自体にあるように見えるこの問題の原因を見つけるのに役立ちました。 –

関連する問題