I'm trying to convert my Keras hdf5 file into a TensorFlow Lite file with the following code:
import tensorflow as tf
# Convert the model.
converter = tf.lite.TFLiteConverter.from_keras_model("/content/best_model_11class.hdf5")
tflite_model = converter.convert()
# Save the TF Lite model.
with tf.io.gfile.GFile('model.tflite', 'wb') as f:
f.write(tflite_model)
I'm getting this error on the from_keras_model line:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-14-26467c686751> in <module>()
2
3 # Convert the model.
----> 4 converter = tf.lite.TFLiteConverter.from_keras_model("/content/best_model_11class.hdf5")
5 tflite_model = converter.convert()
6
/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/lite.py in from_keras_model(cls, model)
426 # to None.
427 # Once we have better support for dynamic shapes, we can remove this.
--> 428 if not isinstance(model.call, _def_function.Function):
429 # Pass `keep_original_batch_size=True` will ensure that we get an input
430 # signature including the batch dimension specified by the user.
AttributeError: 'str' object has no attribute 'call'
How do I fix this? By the way, I'm using Google Colab.
I'm not sure how stuff works on Colab, but looking at the documentation for tf.lite.TFLiteConverter.from_keras_model I can see that it expects a Keras model instance as an argument but you are giving it a string. Maybe you need to load the Keras model first?
Something like:
keras_model = tf.keras.models.load_model("/content/best_model_11class.hdf5")
converter = tf.lite.TFLiteConverter.from_keras_model(keras_model)
import tensorflow as tf
model=tf.keras.models.load_model(""/content/best_model_11class.hdf5"")
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.experimental_new_converter = True
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
This worked according to https://github.com/tensorflow/tensorflow/issues/32693
This error can also appear when, by accident, you try to load only the weights of a saved model instead of the model fully.
For example, it can occur when using ModelCheckpoint() and save_weights_only = True, when only the weights are saved and not other model metadata, hence the same error.
Related
I wanted to convert my face detection model written in caffe (https://github.com/adelekuzmiakova/onnx-converter/blob/master/res10_300x300_ssd_iter_140000.caffemodel) to ONNX format. I was following this tutorial: https://github.com/onnx/onnx-docker/blob/master/onnx-ecosystem/converter_scripts/caffe_coreml_onnx.ipynb and also here is my code:
import coremltools
import onnxmltools
# Update your input name and path for your caffe model
proto_file = 'no_norm_param.deploy.prototext'
input_caffe_path = 'res10_300x300_ssd_iter_140000.caffemodel'
# Update the output name and path for intermediate coreml model, or leave as is
output_coreml_model = 'model.mlmodel'
# Change this path to the output name and path for the onnx model
output_onnx_model = 'model.onnx'
# Convert Caffe model to CoreML
coreml_model = coremltools.converters.caffe.convert((input_caffe_path, proto_file))
# Save CoreML model
coreml_model.save(output_coreml_model)
# Load a Core ML model
coreml_model = coremltools.utils.load_spec(output_coreml_model)
# Convert the Core ML model into ONNX
onnx_model = onnxmltools.convert_coreml(coreml_model)
# Save as protobuf
onnxmltools.utils.save_model(onnx_model, output_onnx_model)
However, when I run this code, I get the following error message:
[libprotobuf ERROR /Users/zach/builds/peTAVmNC/3/nn-inference/coremltools-build/deps/protobuf/src/google/protobuf/text_format.cc:287] Error parsing text-format caffe.NetParameter: 1010:17: Message type "caffe.LayerParameter" has no field named "permute_param".
Traceback (most recent call last):
File "convert-caffe-onnx.py", line 19, in <module>
coreml_model = coremltools.converters.caffe.convert((input_caffe_path, proto_file))
File "/Users/adele/Desktop/vay-sports/onnx-converter/.env/lib/python3.7/site-packages/coremltools/converters/caffe/_caffe_converter.py", line 192, in convert
predicted_feature_name)
File "/Users/adele/Desktop/vay-sports/onnx-converter/.env/lib/python3.7/site-packages/coremltools/converters/caffe/_caffe_converter.py", line 260, in _export
predicted_feature_name)
RuntimeError: Unable to load caffe network Prototxt file: no_norm_param.deploy.prototext
To me it's a bit strange because when I look at my prototext file (https://github.com/adelekuzmiakova/onnx-converter/blob/master/no_norm_param.deploy.prototext), there is no permute_param. My prototext file, caffe model, and code can be found here: https://github.com/adelekuzmiakova/onnx-converter
Did anyone else run into this problem? Do you know what might be going on? Or does it have to do somethinng with SSD? Many thanks!
Having trouble turning in Google Colab python in turning a Keras model.h5 using TFLiteConverter.from_keras_model("model.h5")
I am using TensorFlow 2.2.0
First attempt script
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model("model.h5")
tflite_model = converter.convert()
open("Robot_Eyes.tflite", "wb").write(tflite_model)
And Error gotten
AttributeError Traceback (most recent call last)
<ipython-input-22-e8e8d3a7c79e> in <module>()
13 #new_model= tf.keras.models.load_model(filepath="model.h5")
14
--->15 converter = tf.lite.TFLiteConverter.from_keras_model("model.h5")
16
17 tflite_model = converter.convert()
/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/lite.py in from_keras_model(cls, model)
426 # to None.
427 # Once we have better support for dynamic shapes, we can remove this.
-->428 if not isinstance(model.call, _def_function.Function):
429 # Pass `keep_original_batch_size=True` will ensure that we get an input
430 # signature including the batch dimension specified by the user.
AttributeError: 'str' object has no attribute 'call'
Second attempt
import tensorflow as tf
new_model= tf.keras.models.load_model(filepath="model.h5")
converter = tf.lite.TFLiteConverter.from_keras_model(new_model)
tflite_model = converter.convert()
open("model.tflite", "wb").write(tflite_model)
And 2nd Error gotten
OSError Traceback (most recent call last)
<ipython-input-23-2344f7b515a1> in <module>()
11 import tensorflow as tf
12
--->13 new_model= tf.keras.models.load_model(filepath="model.h5")
14
15 converter = tf.lite.TFLiteConverter.from_keras_model(new_model)
3 frames
/usr/local/lib/python3.6/dist-packages/h5py/_hl/files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
171 if swmr and swmr_support:
172 flags |= h5f.ACC_SWMR_READ
-->173 fid = h5f.open(name, flags, fapl=fapl)
174 elif mode == 'r+':
175 fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)
h5py/_objects.pyx in h5py._objects.with_phil.wrapper()
h5py/_objects.pyx in h5py._objects.with_phil.wrapper()
h5py/h5f.pyx in h5py.h5f.open()
OSError: Unable to open file (truncated file: eof = 40894464, sblock->base_addr = 0, stored_eof = 159686248)
I don't know what is wrong and I've been trying to solve it. Thanks for the help.
Got it to work
!pip install tensorflow==1.15.0
import tensorflow as tf
print(tf.__version__)
converter = tf.lite.TFLiteConverter.from_keras_model_file("robot_eyes2.h5")
tflite_model = converter.convert()
open("Robot_Eyes.tflite", "wb").write(tflite_model)
I keep getting the unsupported operand types 'str' and 'str' in my code.
I have created a dataset for semantic segmentation of sidewalk across a campus. I want to train this dataset but i am getting errors when trying to get the labels from the labeled images to map them with the input images with the function: 'get_y_fn' . I wabt to train this dataset with fastai library in google colab
%reload_ext autoreload
%autoreload 2
%matplotlib inline
import fastai
from fastai import *
from fastai.vision import *
import pathlib
import os
from PIL import Image
import matplotlib.pyplot as plt
fnames = get_image_files(path_img)
lbl_names = get_image_files(path_lbl)
get_y_fn = lambda x: path_lbl/f'{x.stem}.png'
data = (SegmentationItemList.from_folder(path_img)
.random_split_by_pct()
.label_from_func(get_y_fn,classes=codes)
.transform(get_transforms(),size=128,tfm_y=True)
.databunch(bs=4))
TypeError Traceback (most recent call last)
<ipython-input-18-80efbaeba6e7> in <module>()
2 data = (SegmentationItemList.from_folder(path_img)
3 .split_by_rand_pct()
----> 4 .label_from_func(get_y_fn,classes=codes)
5 .transform(get_transforms(),size=128,tfm_y=True)
6 .databunch(bs=4))
3 frames
<ipython-input-10-44f94a438cac> in <lambda>(x)
----> 1 get_y_fn = lambda x: path_lbl/f'{x.stem}.png'
TypeError: unsupported operand type(s) for /: 'str' and 'str'
Program on google colab error
beginning of program
Going through your code in google colab, I found that you've been using string as a path whereas if you're trying to reproduce fastai code then it uses path object for paths not string so you can simply replace:
get_y_fn = lambda x: path_lbl/f'{x.stem}_mask{x.suffix}'
with
get_y_fn = lambda x: path_lbl + "/" +f'{x.stem}_mask{x.suffix}'
Since path_lbl is a string object not path object.
You can also change path_lbl object from string to path using pathlib library of python.
I am working on feature selection from the NSL-KDD dataset. After preprocessing, my X-DoS has type of data like this:
type_of_target(X_newDoS)
'continuous-multioutput'
and Y_DoS as
type_of_target(Y_DoS)
'unkonwn'
I run the feature selection part as:
from sklearn.feature_selection import RFE
from sklearn.ensemble import RandomForestClassifier
clf =RandomForestClassifier( n_jobs = 2)
rfe = RFE(clf, n_features_to_select=1)
rfe.fit(X_newDoS, Y_DoS)
The error message:
ValueError Traceback (most recent call
last)
<ipython-input-31-6c22f9cc2bba> in <module>()
12 rfe = RFE(clf, n_features_to_select=1)
---> 13 rfe.fit(X_newDoS, Y_DoS)
14
4 frames
/usr/local/lib/python3.6/dist-packages/sklearn/utils/multiclass.py in
check_classification_targets(y)
167 if y_type not in ['binary', 'multiclass', 'multiclass-
multioutput',
168 'multilabel-indicator', 'multilabel-
sequences']:
--> 169 raise ValueError("Unknown label type: %r" % y_type)
170
ValueError: Unknown label type: 'unknown'
X_newDoS is a numpy array and Y_DoS is an array of dimension (125972,2). Clicking on the multiclass.py file, I saw there was no 'unknown' type in the list. I tried to convert the Y_DoS array into a numpy array with:
Y_DoS = np.array(Y_DoS)
Still it is an unknown data type and can't be recognized by the multiclass.py file. What are the ways I can solve this problem? How do I make the Y_DoS variable to another type recognizable by multiclass.py file without losing its contents and structures?
For reference I used the code from this link and have done the same steps for preprocessing. https://github.com/CynthiaKoopman/Network-Intrusion-Detection/blob/master/DecisionTree_IDS.ipynb
I am pretty new to machine learning. The program worked fine with numpy 1.11.3, sklearn 0.18.1 and pandas 1.19.2. When working with the current preinstalled libraries versions of colab (numpy 0.24.2, sklearn 1.16.3, pandas 0.21.1), it raises the error mentioned above.
Nevermind. It seems the Y_DoS variable happened to be an undefined object, so sklearn could not recognize its type. Adding
Y_DoS = Y_DoS.astype('int')
before learning step solved the problem and classified Y_DoS as 'binary' type.
I am getting an error for the following code.
Can someone please tell me where I am going wrong?
p.s. I have given the path correctly for.mrcfile
import numpy
import Mrc
a = Mrc.bindFile('somefile.mrc')
# a is a NumPy array with the image data memory mapped from
# somefile.mrc. You can use it directly with any function
# that will take a NumPy array.
hist = numpy.histogram(a, bins=200)
# a.Mrc is an instances of the Mrc class. One thing
# you can do with that class is print out key information from the header.
a.Mrc.info()
# Use a.Mrc.hdr to access the MRC header fields.
wavelength0_nm = a.Mrc.hdr.wave[0]
AttributeError Traceback (most recent call last)
in ()
3 a = Mrc.bindFile('/home/smitha/deep-image-prior/data/Falcon_2015_05_14-20_42_18.mrc')
4 hist = numpy.histogram(a, bins=200)
----> 5 a.Mrc.info()
6 wavelength0_nm = a.Mrc.hdr.wave[0]
7
AttributeError: 'NoneType' object has no attribute 'Mrc'