This file contains hidden or 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
func predict(image: UIImage) -> Species? { | |
do { | |
if let resizedImage = resize(image: image, newSize: trainedImageSize), let pixelBuffer = resizedImage.toCVPixelBuffer() { | |
let model = DogAndCatCNN() | |
let prediction = try model.prediction(data: pixelBuffer) | |
if prediction.species[0].intValue == 1 { | |
return .Dog | |
} else { | |
return .Cat | |
} |
This file contains hidden or 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
func resize(image: UIImage, newSize: CGSize) -> UIImage? { | |
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0) | |
image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)) | |
let newImage = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return newImage | |
} |
This file contains hidden or 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
func takePhotoTouched() { | |
let imagePicker = UIImagePickerController() | |
imagePicker.sourceType = .camera | |
imagePicker.delegate = self | |
imagePicker.allowsEditing = true | |
present(imagePicker, animated: true, completion: nil) | |
} | |
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { | |
dismiss(animated: true, completion: nil) |
This file contains hidden or 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 UIKit | |
extension UIImage { | |
func toCVPixelBuffer() -> CVPixelBuffer? { | |
let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue, kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue] as CFDictionary | |
var pixelBuffer : CVPixelBuffer? | |
let status = CVPixelBufferCreate(kCFAllocatorDefault, Int(self.size.width), Int(self.size.height), kCVPixelFormatType_32ARGB, attrs, &pixelBuffer) | |
guard status == kCVReturnSuccess else { | |
return nil | |
} |
This file contains hidden or 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 | |
coreml_model = coremltools.converters.keras.convert('model.h5', input_names='data', image_input_names='data', is_bgr=True, output_names='species') | |
coreml_model.save('model.mlmodel') |
This file contains hidden or 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
def predict(data): | |
# Process your data, create a dataframe/vector and make your predictions | |
final_formatted_data = [] | |
return load_model().predict(final_formatted_data) |
This file contains hidden or 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
from sklearn.externals import joblib | |
from boto.s3.key import Key | |
from boto.s3.connection import S3Connection | |
from flask import Flask | |
from flask import request | |
from flask import json | |
BUCKET_NAME = 'your-s3-bucket-name' | |
MODEL_FILE_NAME = 'your-model-name.pkl' | |
MODEL_LOCAL_PATH = '/tmp/' + MODEL_FILE_NAME |
This file contains hidden or 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
def load_model(): | |
conn = S3Connection() | |
bucket = conn.create_bucket(BUCKET_NAME) | |
key_obj = Key(bucket) | |
key_obj.key = MODEL_FILE_NAME | |
contents = key_obj.get_contents_to_filename(MODEL_LOCAL_PATH) | |
return joblib.load(MODEL_LOCAL_PATH) |
This file contains hidden or 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
from sklearn.externals import joblib | |
from boto.s3.key import Key | |
from boto.s3.connection import S3Connection | |
from flask import Flask | |
from flask import request | |
from flask import json | |
BUCKET_NAME = 'your-s3-bucket-name' | |
MODEL_FILE_NAME = 'your-model-name.pkl' | |
MODEL_LOCAL_PATH = '/tmp/' + MODEL_FILE_NAME |
This file contains hidden or 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
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |