Got error parsing message when import pretrain caffe model to chainer - 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.

Related

How to convert Tensorflow Object Detection API model to TFLite?

I am trying to convert a Tensorflow Object Detection model(ssd-mobilenet-v2-fpnlite, from TensorFlow 2 Detection Model Zoo) to TFLite. First of all, I train the model using the model_main_tf2.py and then I use the export_tflite_graph_tf2.py to export a saved model(.pb). However, when it comes to convert the .pb file to .tflite it throws this error:
OSError: SavedModel file does not exist at: /content/gdrive/My Drive/models/research/object_detection/fine_tuned_model/saved_model/saved_model.pb/{saved_model.pbtxt|saved_model.pb}
To convert the .pb file I used:
import tensorflow as tf
SAVED_MODEL_PATH = os.path.join(os.getcwd(),'object_detection', 'fine_tuned_model', 'saved_model', 'saved_model.pb')
# SAVED_MODEL_PATH: '/content/gdrive/My Drive/models/research/object_detection/exported_model/saved_model/saved_model.pb'
converter = tf.lite.TFLiteConverter.from_saved_model(SAVED_MODEL_PATH)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.experimental_new_converter = True
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()
open("detect.tflite", "wb").write(tflite_model)
or "tflite_convert" from command line, but with the same error. I also tried to run it with the latest tf-nightly version as it suggests here, but the outcome is the same. I tried to pass the path with various ways, it seems like the .pd is not well written (not the right file). Is there a way to manage to convert the model to tflite so as to implement it to android? Thank you!
Your saved_model path should be "/content/gdrive/My Drive/models/research/object_detection/fine_tuned_model/saved_model/". It is the folder instead of files in that folder
For quick test, try to type in terminal
tflite_convert \
--saved_model_dir="path to saved_folder" \
--output_file="path to tflite file u want to save"
I don't have enough reputation to just comment but the problem here seems to be your SAVED_MODEL_PATH.
You could try to hardcode the path and remove the .pb file. I don't remember exactly what's the trick here but it's definitively due to the path

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')

Use Azure custom-vision trained model with tensorflow.js

I've trained a model with Azure Custom Vision and downloaded the TensorFlow files for Android
(see: https://learn.microsoft.com/en-au/azure/cognitive-services/custom-vision-service/export-your-model). How can I use this with tensorflow.js?
I need a model (pb file) and weights (json file). However Azure gives me a .pb and a textfile with tags.
From my research I also understand that there are also different pb files, but I can't find which type Azure Custom Vision exports.
I found the tfjs converter. This is to convert a TensorFlow SavedModel (is the *.pb file from Azure a SavedModel?) or Keras model to a web-friendly format. However I need to fill in "output_node_names" (how do I get these?). I'm also not 100% sure if my pb file for Android is equal to a "tf_saved_model".
I hope someone has a tip or a starting point.
Just parroting what I said here to save you a click. I do hope that the option to export directly to tfjs is available soon.
These are the steps I did to get an exported TensorFlow model working for me:
Replace PadV2 operations with Pad. This python function should do it. input_filepath is the path to the .pb model file and output_filepath is the full path of the updated .pb file that will be created.
import tensorflow as tf
def ReplacePadV2(input_filepath, output_filepath):
graph_def = tf.GraphDef()
with open(input_filepath, 'rb') as f:
graph_def.ParseFromString(f.read())
for node in graph_def.node:
if node.op == 'PadV2':
node.op = 'Pad'
del node.input[-1]
print("Replaced PadV2 node: {}".format(node.name))
with open(output_filepath, 'wb') as f:
f.write(graph_def.SerializeToString())
Install tensorflowjs 0.8.6 or earlier. Converting frozen models is deprecated in later versions.
When calling the convertor, set --input_format as tf_frozen_model and set output_node_names as model_outputs. This is the command I used.
tensorflowjs_converter --input_format=tf_frozen_model --output_json=true --output_node_names='model_outputs' --saved_model_tags=serve path\to\modified\model.pb folder\to\save\converted\output
Ideally, tf.loadGraphModel('path/to/converted/model.json') should now work (tested for tfjs 1.0.0 and above).
Partial answer:
Trying to achieve the same thing - here is the start of an answer - to make use of the output_node_names:
tensorflowjs_converter --input_format=tf_frozen_model --output_node_names='model_outputs' model.pb web_model
I am not yet sure how to incorporate this into same code - do you have anything #Kasper Kamperman?

Unable to read an SBML file in SBMLR

I'm trying to read a SBML file (Test.xml) using the R package SBMLR. Below is the code I executed.
library(SBMLR)
file <- system.file("models","Test.xml",package = "SBMLR")
doc <- readSBML(file)
When I execute the 3rd line I get an error message saying:
Error in xmlEventParse(filename, handlers = sbmlHandler(),
ignoreBlanks = TRUE) : File does not exist
I have tried to read the file using rsbml library as well.. But still I'm getting an error saying
Error: File unreadable.
I'm following this guide at the moment. Any help regarding the issue is highly appreciated!

I get error when I use zip.au3 and console.au3 together in an autoit script

I get the following error when I use zip.au3 and console.au3 together. To simulate this error please create a new script in SciTE script editor and include zip.au3 and console.au3 and then run it (just two include lines are sufficient to simulate). You will get two pop up messages.
Here are the error messages:
First Popup message:
AutoIt Error:
Line 456 (File "C:\Program Files (z86)|AutoIt3\Include\zip.au3:):
$ZipFile=#ZipSplit[2]
$ZipFile=^Error
Error: Array variable has incorrect number of subscripts or subscript dimension range exceeded.
Second popup message:
Line 455 (File "C:\Program Files (z86)|AutoIt3\Include\Console.au3:):
If $_Amount_Startup_COnsole Then If^Error
Error: Variable used without being declared.
(I would like to attach zip.au3 and console.au3. How can I do it? They are available for download rom Autoit Forum - Example scripts)
Regards,
Nazir
As far as i can see, the Problem is that the Zip.au3 starts a function at startup which it should not do.
If you delete the first Lines in Zip.au3 then it should work fine:
If UBound($CMDLine) > 1 Then
If $CMDLine[1] <> "" Then _Zip_VirtualZipOpen()
EndIf
So my AutoIt dont gives back any Error Messages anymore. But I'm not sure if the other functions will work now. Try it out.
Teifun2
PS: sorry for my bad english!

Resources