This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <CoreML/CoreML.h> | |
#import <Vision/Vision.h> | |
#import "keras_mnist_cnn.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MLModel *ml_model = [[[keras_mnist_cnn alloc] init] model]; | |
VNCoreMLModel *vnc_core_ml_model = [VNCoreMLModel modelForMLModel: ml_model error:nil]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model.save('my_mnist_keras_cnn_model.h5') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(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]; |