Skip to content

Instantly share code, notes, and snippets.

View marcoleewow's full-sized avatar

Marco Lee marcoleewow

View GitHub Profile
@marcoleewow
marcoleewow / coremltools 4.0b1 pytorch LSTM conversion.ipynb
Last active July 7, 2020 08:24
coremltools==4.0b1, torch==1.5.0
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@marcoleewow
marcoleewow / pytorchBLSTM_to_coreML.py
Last active July 29, 2023 21:51
pytorch BLSTM model to CoreML using ONNX. Note that you must use Mac OS 10.15 Catalina and iOS 13 for it to work!
import os
import numpy as np
import coremltools
from coremltools.models.neural_network import flexible_shape_utils
import torch
from torch import nn
import onnx
@marcoleewow
marcoleewow / ErrorRateMetric.py
Last active April 30, 2019 03:45
Error rate calculation for handwriting recognition. pip install editdistance==0.4
import editdistance
def cer(y_true: str, y_pred: str) -> float:
assert isinstance(y_true, str) and isinstance(y_pred, str)
n_err = editdistance.eval(y_true.rstrip(), y_pred.rstrip())
return n_err / len(y_true)
def wer(transcript: str, reference: str) -> float:
@marcoleewow
marcoleewow / test_rnn_pytorch_to_coreml.py
Last active October 12, 2018 06:23
Test for converting a RNN model from pyTorch to ONNX to coreML
import torch
from torch import nn
import torch.nn.functional as F
import os
import onnx
from onnx_coreml import convert
import pytest
from coremltools.proto import NeuralNetwork_pb2 #type: ignore
"""

Keybase proof

I hereby claim:

  • I am marcoleewow on github.
  • I am marcoleewow (https://keybase.io/marcoleewow) on keybase.
  • I have a public key ASA5wRJ5pC3LsccRazSVrRDLjSgWk7bsSAGb09DGjtRDEQo

To claim this, I am signing this object:

import numpy as np
import tensorflow as tf
def CTCLossExactPathTest():
BIG_NEG_NUMBER = -100
logit = np.array([[BIG_NEG_NUMBER, 0., BIG_NEG_NUMBER, BIG_NEG_NUMBER],
[0., BIG_NEG_NUMBER, BIG_NEG_NUMBER, BIG_NEG_NUMBER],
[BIG_NEG_NUMBER, BIG_NEG_NUMBER, 0., BIG_NEG_NUMBER]])
"""
@marcoleewow
marcoleewow / CTCLossSimpleTest.py
Last active December 6, 2017 06:21
Tensorflow 1.4.0-rc1 CTC Loss Simple Test
import numpy as np
import tensorflow as tf
def CTCLossSimpleTest():
"""
Alex Graves Paper example:
# a -
P = [[0.3, 0.7], # t = 0
[0.4, 0.6]] # t = 1
import numpy as np
from keras import backend as K
from keras.layers import *
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, Model
from keras.optimizers import SGD, Adam
np.random.seed(1337) # for reproducibility