How to convert Core ML model file to other format so it can run on a server - coreml

A Core ML model (.mlmodel) was trained but I found it can only run on MacOS.
(But I want to run it on a Linux server)
I tried to convert it to ONNX format but failed.
onnxmltools and following python code was used for the conversion:
import onnxmltools
import coremltools
# Load a Core ML model
coreml_model = coremltools.utils.load_spec('example.mlmodel')
# Convert the Core ML model into ONNX
onnx_model = onnxmltools.convert_coreml(coreml_model, 'Example Model')
# Save as protobuf
onnxmltools.utils.save_model(onnx_model, 'example.onnx')
But following error message was shown after ran the program above
Traceback (most recent call last):
File "convertToOnnx.py", line 8, in <module>
onnx_model = onnxmltools.convert_coreml(coreml_model, 'Post Rating Model')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/onnxmltools/convert/main.py", line 18, in convert_coreml
custom_conversion_functions, custom_shape_calculators)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/onnxmltools/convert/coreml/convert.py", line 60, in convert
topology = parse_coreml(spec, initial_types, target_opset, custom_conversion_functions, custom_shape_calculators)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/onnxmltools/convert/coreml/_parse.py", line 467, in parse_coreml
topology.compile()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/onnxconverter_common/topology.py", line 632, in compile
self._infer_all_types()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/onnxconverter_common/topology.py", line 508, in _infer_all_types
operator.infer_types()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/onnxconverter_common/topology.py", line 110, in infer_types
registration.get_shape_calculator(self.type)(self)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/onnxmltools/convert/coreml/shape_calculators/OneHotEncoder.py", line 36, in calculate_one_hot_encoder_output_shapes
raise ValueError('Categorical indexes are missing')
ValueError: Categorical indexes are missing
How can I solve the error above?
Or how can I use the Core ML model on the Linux server? (Any other way of model conversion?)

Related

Replicating tensorflow bert model in R

I am just replicating this code based on Basic Text Classification. Until the below line, it seems alright. However, the following line:
vectorize_layer %>% adapt(train_text)
throws me an error as below. Any idea, how should I approach it in order to solve it ? My tensorflow version is TensorFlow v2.8.2, is it the main issue for this error ?
I assume that the train_text should not be like that as well, should it ?
> train_text
<MapDataset element_spec=TensorSpec(shape=(None,), dtype=tf.string, name=None)>
The thrown error:
Error in py_call_impl(callable, dots$args, dots$keywords) :
RuntimeError: in user code:
File "C:\ANACON~2\envs\R-TENS~1\lib\site-packages\keras\engine\base_preprocessing_layer.py", line 118, in adapt_step *
self.update_state(data)
File "C:\ANACON~2\envs\R-TENS~1\lib\site-packages\keras\layers\preprocessing\text_vectorization.py", line 431, in update_state **
self._lookup_layer.update_state(self._preprocess(data))
File "C:\ANACON~2\envs\R-TENS~1\lib\site-packages\keras\layers\preprocessing\text_vectorization.py", line 512, in _preprocess
inputs = self._standardize(inputs)
File "C:\Users\xxxxx\AppData\Local\R\win-library\4.2\reticulate\python\rpytools\call.py", line 21, in python_function
raise RuntimeError(res[kErrorKey])
RuntimeError: Evaluation error: attempt to apply non-function.

Import wav file in Tensorflow 2

Using Python 3.7 and Tensorflow 2.0, I'm having a hard time reading wav files from the UrbanSounds dataset. This question and answer are helpful because they explain that the input has to be a string tensor, but it seems to be having a hard time getting past the initial metadata encoded in the file, and getting to the real data. Do I have to preprocess the string before being able to load it as a float32 tensor? I already had to preprocess the data by downsampling it from 24-bit wav to 16-bit wav, so the data-input pipeline is turning out to be much more cumbersome than I would have expected. The required downsampling is particularly frustrating. Here's what I'm trying so far:
import tensorflow as tf # this is TensorFlow 2.0
path_to_wav_file = '/mnt/d/Code/UrbanSounds/audio/fold1/101415-3-0-2.wav'
# Turn the wav file into a string tensor
input_data = tf.io.read_file(path_to_wav_file)
# Convert the string tensor to a float32 tensor
audio, sampling_rate = tf.audio.decode_wav(input_data)
This is the error I get at the last step:
2019-10-08 20:56:09.124254: W tensorflow/core/framework/op_kernel.cc:1546] OP_REQUIRES failed at decode_wav_op.cc:55 : Invalid argument: Header mismatch: Expected fmt but found junk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow/python/ops/gen_audio_ops.py", line 216, in decode_wav
_six.raise_from(_core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: Header mismatch: Expected fmt but found junk [Op:DecodeWav]
And here is the beginning of that string tensor. I'm no expert on wav files, but I think the part after "fmt" is where the actual audio data starts. Before that I think it's all metadata about the file.
data.numpy()[:70]
b'RIFFhb\x05\x00WAVEjunk\x1c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00fmt \x10\x00\x00\x00\x01\x00\x01\x00D\xac\x00\x00\x88X\x01\x00\x02\x00'
It seems like your error has to do with TensorFlow expecting the fmt part as the beginning.
The code of TensorFlow for the processing can be found here: https://github.com/tensorflow/tensorflow/blob/c9cd1784bf287543d89593ca1432170cdbf694de/tensorflow/core/lib/wav/wav_io.cc#L225
There's also an open issue, awaiting response from TensorFlow's team which roughly covers the same error you've provided.
https://github.com/tensorflow/tensorflow/issues/32382
Other libraries just skip the Junk part, so it works with them.
It seems that your code fails for dual channel audio file. The code works for mono channel wav file. In your case you can try using scipy.
from scipy.io import wavfile as wav
sampling_rate, data = wav.read('101415-3-0-2.wav')

Got error parsing message when import pretrain caffe model to chainer

I want to import Resnet50 pretrain file "ResNet-50-model.caffemodel" to chainer.
Here is chainer code:
class chexnet(L.ResNet50Layers):
def __init__(self, pretrained_model="auto", out_features=2):
super(chexnet, self).__init__(pretrained_model)
with self.init_scope():
self.classifier = L.Linear(2048, out_features)
But i got the error message as below :
File "/home/tamnt27/.local/lib/python3.5/site-packages/chainer/links/model/vision/resnet.py", line 148, in convert_caffemodel_to_npz
caffemodel = CaffeFunction(path_caffemodel)
File "/home/tamnt27/.local/lib/python3.5/site-packages/chainer/links/caffe/caffe_function.py", line 151, in __init__
net.MergeFromString(model_file.read())
google.protobuf.message.DecodeError: Error parsing message
I don't know why this error happens, it should work, please help me. Thank you all.
I tried to reproduce your situation, but could not.
My environment is
python2.7
chainer4.2.0
cupy4.2.0
I downloaded a model from
https://onedrive.live.com/?authkey=%21AAFW2-FVoxeVRck&id=4006CBB8476FF777%2117887&cid=4006CBB8476FF777
and placed it on ~/.chainer/dataset/pfnet/chainer/models/ResNet-50-model.caffemodel
I think the downloaded file is corrupted, therefore I recommend you to check md5sum by
$ md5sum ~/.chainer/dataset/pfnet/chainer/models/ResNet-50-model.caffemodel
44b20660c5948391734036963e855dd2
If the md5sum is different from mine, try download the model again.

UnicodeDecodeError while importing rpy2 in Spyder

I've installed rpy2 in Anaconda. This is the code I run:
import rpy2.robjects as robjects
I encouter error (I've changed username to "myuser"):
runfile('D:/Users/myuser/Documents/Python Scripts/regression_test.py', wdir='D:/Users/myuser/Documents/Python Scripts')
Traceback (most recent call last):
File "d:\Users\myuser\AppData\Local\Continuum\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2902, in run_code
self.showtraceback()
File "d:\Users\myuser\AppData\Local\Continuum\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 1830, in showtraceback
value, tb, tb_offset=tb_offset)
File "d:\Users\myuser\AppData\Local\Continuum\Anaconda2\lib\site-packages\IPython\core\ultratb.py", line 1392, in structured_traceback
self, etype, value, tb, tb_offset, number_of_lines_of_context)
File "d:\Users\myuser\AppData\Local\Continuum\Anaconda2\lib\site-packages\IPython\core\ultratb.py", line 1309, in structured_traceback
self, etype, value, elist, tb_offset, number_of_lines_of_context
File "d:\Users\myuser\AppData\Local\Continuum\Anaconda2\lib\site-packages\IPython\core\ultratb.py", line 640, in structured_traceback
lines = ''.join(self._format_exception_only(etype, value))
File "d:\Users\myuser\AppData\Local\Continuum\Anaconda2\lib\site-packages\IPython\core\ultratb.py", line 752, in _format_exception_only
Colors.Normal, s))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xbf in position 18: ordinal not in range(128)
My software versions:
Anaconda 4.0.0 (64-bit)
Python 2.7.11
R 3.3.1
IPython 4.1.2
Does anybody know how to deal with it?
The traceback does not show rpy2 code involved, and the details of regression_test.py are not known.
I have seen reports of unicode-related issues with Spyder. Can you try with Python 3 instead of Python 2 ? Python 3 is making the handling of string a little more consistent, and it might solve the issue.

Bokeh embed_multiply example returns an error

I'm attempting to run the embed_multiple.py example from https://github.com/bokeh/bokeh/blob/master/examples/embed/embed_multiple.py
Here's the error I see when I run the script:
Traceback (most recent call last):
File "/Users/me/test/project/Test.py", line 41, in <module>
script, div = components(plots)
File "/Library/Python/2.7/site-packages/bokeh/embed.py", line 56, in components
ref = plot_object.ref
AttributeError: 'dict' object has no attribute 'ref'
Is there a workout to this?
That is a new example that requires the latest version of Bokeh (0.9.1), you are trying to run it with an older version.

Resources