Skip to content

Instantly share code, notes, and snippets.

View francoismarceau29's full-sized avatar

François Marceau francoismarceau29

View GitHub Profile
@francoismarceau29
francoismarceau29 / prediction.swift
Last active September 12, 2017 16:55
Doing prediction with CoreML
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
}
@francoismarceau29
francoismarceau29 / resize-uiimage.swift
Created September 11, 2017 22:49
Resizing a UIImage
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
}
@francoismarceau29
francoismarceau29 / imagepickercontroller.swift
Last active September 11, 2017 22:45
Creating an UIImagePickerController
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)
@francoismarceau29
francoismarceau29 / UIImage+CVPixelBuffer.swift
Created September 9, 2017 00:08
UIImage to CVPixelBuffer
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
}
@francoismarceau29
francoismarceau29 / convert-keras-to-core-ml.py
Created July 25, 2017 23:51
Convert Convolutional Neural Network Keras model to CoreML model (.mlmodel)
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')
@francoismarceau29
francoismarceau29 / scikit-prediction.py
Last active July 11, 2017 16:51
Make prediction from Scikit model
def predict(data):
# Process your data, create a dataframe/vector and make your predictions
final_formatted_data = []
return load_model().predict(final_formatted_data)
@francoismarceau29
francoismarceau29 / flask_template_aws.py
Last active July 18, 2017 23:48
Basic Flask template for AWS Lambda
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
@francoismarceau29
francoismarceau29 / load-model-s3.py
Last active July 11, 2017 16:52
Loading Scikit model from S3 using Boto3
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)
@francoismarceau29
francoismarceau29 / aws-lambda-prediction.py
Last active March 31, 2020 08:49
Deployment of SciKit model on AWS Lambda using S3 and Boto3 (WIP)
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
@francoismarceau29
francoismarceau29 / 0_reuse_code.js
Last active August 29, 2015 14:13
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console