Skip to content

Instantly share code, notes, and snippets.

View backnotprop's full-sized avatar
📟
E.Alderson

Michael Ramos backnotprop

📟
E.Alderson
View GitHub Profile
private func stylizeImage(cgImage: CGImage, model: MLModel) -> CGImage {
// size can change here if you want, remember to run right sizes in the fst evaluating script
let input = StyleTransferInput(input: pixelBuffer(cgImage: cgImage, width: 883, height: 720))
// model.prediction will run the style model on input image
let outFeatures = try! model.prediction(from: input)
// we get the image buffer after
let output = outFeatures.featureValue(for: "add_37__0")!.imageBufferValue!
//
// StyleTransferInput.swift
// StyleTransfer
//
import CoreML
class StyleTransferInput : MLFeatureProvider {
var input: CVPixelBuffer
private let models = [
wave().model,
udnie().model,
rain_princess().model,
la_muse().model
]
import coremltools
def convert_multiarray_output_to_image(spec, feature_name, is_bgr=False):
"""
Convert an output multiarray to be represented as an image
This will modify the Model_pb spec passed in.
Example:
model = coremltools.models.MLModel('MyNeuralNetwork.mlmodel')
spec = model.get_spec()
convert_multiarray_output_to_image(spec,'imageOutput',is_bgr=False)
import tfcoreml as tf_converter
tf_converter.convert(tf_model_path = 'output_graph.pb',
mlmodel_path = 'model_name.mlmodel',
output_feature_names = ['add_37:0'],
## Note found this after running a conversion the first time
image_input_names = ['img_placeholder__0'])
# tfcoreml src
# file1 : _interpret_shapes.py
#
# in the _SHAPE_TRANSLATOR_REGISTRY we need to add the Pow operation
_SHAPE_TRANSLATOR_REGISTRY = {
... previous keys ...
# add this:
'Pow': _identity,
}
# function ffwd, line 98
# https://github.com/lengstrom/fast-style-transfer/blob/master/evaluate.py#L98
if os.path.isdir(checkpoint_dir):
ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
########## add this for pre-trained models ###########
frozen_graph_def = tf.graph_util.convert_variables_to_constants(sess,sess.graph_def,['add_37'])
with open('output_graph.pb', 'wb') as f:
f.write(frozen_graph_def.SerializeToString())
# function ffwd,line 93
# https://github.com/lengstrom/fast-style-transfer/blob/master/evaluate.py#L93
preds = transform.net(img_placeholder)
# !! Add This !!
print(preds)
/**
* Cyclic Elimination Stage 3
* - description needed
*/
function _cycleReduceStage() {
let stable = false;
// all or nothing phase
while(!stable) {
// there is a start if any one person has more than 1 preference remaining
let start = indexWithMultipleRemain();
/**
* Elimination Stage 2
* - removes all preferences in a list who can not be possibly matched
*/
function _eliminateStage() {
_.forIn(_DB, (person,id) => {
let keepLast = _.findIndex(person.choices, function(p) { return p.id == person.acceptedReceivedID; });
for(let i = keepLast + 1; i < person.choices.length; i++) {
// each the rejected and rejecter can remove each other from choices list
eliminateChoices(person, _DB[person.choices[i].id]);