Skip to content

Instantly share code, notes, and snippets.

View kirualex's full-sized avatar
🚀
Making new stuff

Alexis Creuzot kirualex

🚀
Making new stuff
View GitHub Profile
@kirualex
kirualex / quantizer.py
Last active September 10, 2019 14:23
CoreML quantization
import sys
import coremltools
from coremltools.models.neural_network.quantization_utils import *
def quantize(file, bits, functions):
"""
Processes a file to quantize it for each bit-per-weight
and function listed.
file : Core ML file to process (example : mymodel.mlmodel)
bits : Array of bit per weight (example : [16,8,6,4,2,1])
import sys
from onnx import onnx_pb
from onnx_coreml import convert
model_in = sys.argv[1]
model_out = sys.argv[2]
model_file = open(model_in, 'rb')
model_proto = onnx_pb.ModelProto()
model_proto.ParseFromString(model_file.read())
func process(input: UIImage, completion: @escaping FilteringCompletion) {
// Initialize the NST model
let model = StarryNight()
// Next steps are pretty heavy, better process them on a background thread
DispatchQueue.global().async {
// 1 - Transform our UIImage to a PixelBuffer of appropriate size
guard let cvBufferInput = input.pixelBuffer(width: 720, height: 720) else {
@kirualex
kirualex / export_coreml.sh
Created August 20, 2018 15:02
Building a Style Transfer app on iOS using CoreML (2)
python ./neural_style/neural_style.py eval \
--content-image ~/Documents/data/images/original.jpg \
--output-image ~/Documents/data/images/stylized-test.jpg \
--model ~/Documents/checkpoints/checkpoint_5000.pth \
--cuda 0 \
--export_onnx ~/Documents/models/pytorch_model.onnx \
&& \
python ./onnx_to_coreml.py \
~/Documents/models/pytorch_model.onnx \
~/Documents/models/final_model.mlmodel
@kirualex
kirualex / stylize.sh
Created August 20, 2018 15:01
Building a Style Transfer app on iOS using CoreML (2)
python neural_style/neural_style.py eval \
--model ~/Documents/checkpoint_5000.pth \
--content-image ~/Documents/images/original.jpg \
--output-image ~/Documents/images/stylized-test.jpg \
--cuda 0
@kirualex
kirualex / train.sh
Last active August 23, 2018 15:18
Building a Style Transfer app on iOS using CoreML
python neural_style/neural_style.py train \
--dataset ~/Documents/dataset \
--style-image ~/Documents/images/styles/starry_night.jpg \
--style-size 512 \
--batch-size 2 \
--style-weight 1e10 \
--content-weight 1e5 \
--checkpoint-model-dir ~/Documents/checkpoints \
--save-model-dir ~/Documents/models \
--log-interval 10 \
API.request(.Colors("Sunny")) { response in
if (response.result.error) != nil {
// ERROR HANDLING
} else {
// PARSING...
}
}
public static func request(
endpoint: API.Endpoints,
completionHandler: Response<AnyObject, NSError> -> Void)
-> Request {
let request = Manager.sharedInstance.request(
endpoint.method,
endpoint.path,
parameters: endpoint.parameters,
encoding: .URL,
headers: nil
public var method: Alamofire.Method {
switch self {
case .Colors:
return Alamofire.Method.GET
case .AddColor:
return Alamofire.Method.POST
}
}
public var path: String {
public enum Endpoints {
case Colors(String)
case AddColor(Color)
}