2

as I'm learning Apple's Vision and CoreML framework but got stuck on how to use my own re-trained models. I tried training a VG16 model with Keras based on this tutorial. Everything looks OK except for some Keras version warnings. Then I tried converting the resulted model with CoreMLTools with the following code

coremlModel = coremltools.converters.keras.convert(
    kmodel,
    input_names = 'image',
    image_input_names = 'image',
    output_names = 'classLabelProbs',
    class_labels = ['cats', 'dogs'],
)

During the conversion it gave me some version compatible warnings but otherwise it was successful:

WARNING:root:Keras version 2.0.6 detected. Last version known to be fully compatible of Keras is 2.0.4 .
WARNING:root:TensorFlow version 1.2.1 detected. Last version known to be fully compatible is 1.1.1 .

So I loaded this model into Apple's Vision+ML example code, but every time I tried to classify an image it failed with errors

Vision+ML Example[2090:2012481] Error: The VNCoreMLTransform request failed
Vision+ML Example[2090:2012481] Didn't get VNClassificationObservations 
Error Domain=com.apple.vis Code=3 "The VNCoreMLTransform request failed" UserInfo={NSLocalizedDescription=The VNCoreMLTransform request failed, NSUnderlyingError=0x1c025d130 {Error Domain=com.apple.CoreML Code=0 "Dimensions of layer 'classLabelProbs' is not the same size as the number of class labels." UserInfo={NSLocalizedDescription=Dimensions of layer 'classLabelProbs' is not the same size as the number of class labels.}}}

I was guessing this is because the pre-trained VGG16 model has already had 1000 categories, so I tried 1000 categories and 1000 + 2 (cats and dogs) categories, but still got the same problem.

Did I miss anything? I'd greatly appreciate any clue and help.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
CodeBrew
  • 6,457
  • 2
  • 43
  • 48
  • 1
    Are you sure you're using the retrained model (the one trained on the cats and dogs dataset), and not a model trained on ImageNet? There should only be two categories in the model trained (or retrained) on the cats and dogs dataset, per the tutorial you link. You can't just use a model that was originally trained on ImageNet from this, you need one that was trained or retrained on the categories you're looking for. – Brad Larson Jul 29 '17 at 23:02
  • Sorry, @BradLarson, I linked the wrong page for the tutorial, and I just corrected it with the one I used (retraining from ImageNet). So if I understand your comment correctly, I can only retrain on the existing labels of a pre-trained model? But I thought that tutorial is retraining on cats and dogs based on a ImageNet model and I was also able to run it through successfully. – CodeBrew Jul 30 '17 at 16:19
  • I also tried the start from the scratch approach from the link I wrongfully put in the question originally (https://gist.github.com/fchollet/0830affa1f7f19fd47b06d4cf89ed44d), and run the same coremltools conversion with class labels (cats and dogs), but still got "Dimensions of layer 'classLabelProbs' is not the same size as the number of class labels" error. – CodeBrew Jul 30 '17 at 16:40
  • The point of retraining is to start with good feature detector layers seeded by different categories, so yes you can retrain with different target categories than the original model. The common workflow with image classification is to train against the 1000 categories of ImageNet, then take that trained model and retrain against only your smaller dataset's categories (in your case, the two classes of cats and dogs). After retraining, you should end up with a model that has two classes. If you aren't, either your retraining is set up wrong or you're accidentally using the ImageNet model. – Brad Larson Jul 30 '17 at 19:10

0 Answers0