Skip to content

Instantly share code, notes, and snippets.

View boaerosuke's full-sized avatar

Eridy Lukau boaerosuke

  • Fraunhofer Institut Fokus
  • Berlin
View GitHub Profile
#import <CoreML/CoreML.h>
#import <Vision/Vision.h>
#import "keras_mnist_cnn.h"
//scaled image to 28x28
UIImage *scaledCanvasImage = [self imageWithImage:self.drawingCanvas.image scaledToSize:CGSizeMake(28, 28)];
self.imageToDetect = [[CIImage alloc]initWithImage:scaledCanvasImage];
MLModel *ml_model = [[[keras_mnist_cnn alloc] init] model];
VNCoreMLModel *vnc_core_ml_model = [VNCoreMLModel modelForMLModel: ml_model error:nil];
@boaerosuke
boaerosuke / imagerequest.m
Created August 16, 2017 16:27
Perform a VNCoreMLRequest with VNImageRequestHandler
NSDictionary *options_dict = [[NSDictionary alloc] init];
NSArray *request_array = @[request];
VNImageRequestHandler *handler = [[VNImageRequestHandler alloc] initWithCIImage:self.imageToDetect options:options_dict];
dispatch_queue_t myCustomQueue;
myCustomQueue = dispatch_queue_create("com.aname.VNImageRequestHandlerQueue", NULL);
self.resultLabel.text = @"Predicting...";
dispatch_sync(myCustomQueue, ^{
[handler performRequests:request_array error:nil];
@boaerosuke
boaerosuke / model.m
Created August 16, 2017 16:20
Initializing a keras model as MLModel and VNCoreMLModel
MLModel *ml_model = [[[keras_mnist_cnn alloc] init] model];
VNCoreMLModel *vnc_core_ml_model = [VNCoreMLModel modelForMLModel: ml_model error:nil];
@boaerosuke
boaerosuke / VNCoreMLRequest.m
Created August 16, 2017 16:20
How to set up a VNCoreMLRequest with VNRequestCompletitionHandler
VNCoreMLRequest *request = [[VNCoreMLRequest alloc] initWithModel: vnc_core_ml_model completionHandler: (VNRequestCompletionHandler) ^(VNRequest *request, NSError *error){
NSArray *results = [request.results copy];
VNCoreMLFeatureValueObservation *res = ((VNCoreMLFeatureValueObservation *)(results[0]));
NSNumber *prediction = [NSNumber numberWithFloat:0];
NSNumber *compare= [NSNumber numberWithFloat:0];
int atIndex = 0;
@boaerosuke
boaerosuke / scaleimage.m
Created August 16, 2017 16:18
Helper method to scale an uiimage
//helper method to scale uiimage
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 1.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
@boaerosuke
boaerosuke / convert_model.py
Last active July 29, 2023 21:50
Using coremltools to convert a Keras model into mlmodel for iOS
import coremltools
import numpy
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.utils import np_utils
from keras.models import load_model
@boaerosuke
boaerosuke / save_model.py
Created August 16, 2017 13:33
Keras call to save a model after training
model.save('my_mnist_keras_cnn_model.h5')
@boaerosuke
boaerosuke / cnn_example_mnist.py
Created August 16, 2017 13:32
Tweak the code that it only sets up channels last
#if K.image_data_format() == 'channels_first':
# x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
# x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
# input_shape = (1, img_rows, img_cols)
#changed this to be always channels_last!
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
@boaerosuke
boaerosuke / predictDigit.m
Created August 15, 2017 15:57
Predict Digit takes a 28x28 image and predicts the digit
-(void)predictDigit{
//unscaled image
//self.imageToDetect = [[CIImage alloc]initWithImage:self.drawingCanvas.image];
//scaled image to 28x28
UIImage *scaledCanvasImage = [self imageWithImage:self.drawingCanvas.image scaledToSize:CGSizeMake(28, 28)];
self.imageToDetect = [[CIImage alloc]initWithImage:scaledCanvasImage];
MLModel *ml_model = [[[keras_mnist_cnn alloc] init] model];