title: Keras model to Coreml and using OpenCV
body: <p>I have a Keras model and have converted it to Coreml successfully. I am passing a colored 50x50 image in RGB format to the model, everything works in my Keras model in Python. However I am really struggling to get the same results in from the Coreml model. I am using OpenCV in my iOS app and need to convert a cv::Mat to a CVPixelBufferRef. I am positive something is not right with my input, but I cannot figure out what it is. The preprocessing for the input that I send into the Python model looks like this </p>
  
  <p><code>image = cv2.resize(image, (50, 50))
   image = image.astype("float") / 255.0
   image = img_to_array(image)
   image = np.expand_dims(image, axis=0)</code></p>
  
  <p>Any help would be appreciated. Below is the conversion from Keras to Coreml along with its output, and the function to convert a cv::Mat to CVPixelBufferRef (the image here is already resized to 50x50).</p>
  
  <p><strong>Keras to Coreml conversion</strong></p>
  
  <p><code>coreml_model = coremltools.converters.keras.convert(model, input_names='image', image_input_names='image', output_names='output', class_labels=output_labels, image_scale=1/255.0)</code></p>
  
  <p><a href="https://i.stack.imgur.com/vxJLr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vxJLr.png" alt="Output of the Keras to Coreml python script"></a></p>
  
  <p><strong>OpenCV Mat to CVPixelBufferRef</strong></p>
  
  <pre><code>int width = 50;//frame.cols;
  int height = 50;//frame.rows;
  
  NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                           kCVPixelBufferCGBitmapContextCompatibilityKey,
                           [NSNumber numberWithInt:width], kCVPixelBufferWidthKey,
                           [NSNumber numberWithInt:height], kCVPixelBufferHeightKey,
                           nil];
  
  CVPixelBufferRef imageBuffer;
  CVReturn status = CVPixelBufferCreate(kCFAllocatorMalloc, width, height, kCVPixelFormatType_32BGRA, (CFDictionaryRef) CFBridgingRetain(options), &amp;imageBuffer);
  
  NSParameterAssert(status == kCVReturnSuccess &amp;&amp; imageBuffer != NULL);
  
  CVPixelBufferLockBaseAddress(imageBuffer, 0);
  void *base = CVPixelBufferGetBaseAddress(imageBuffer) ;
  memcpy(base, frame.data, frame.total()*4);
  CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
  
  return imageBuffer;
  </code></pre>