cannot load the model in Azure ML - azure

when I try to load a model in Azure ML with below code I get an error.
anyone know how to fix the issue with Azure?
from tensorflow import keras
keras.models.load_model('model.h5')
AttributeError Traceback (most recent call last)
Input In [24], in <cell line: 2>()
1 from tensorflow import keras
----> 2 keras.models.load_model('model_base-3.h5')
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/tensorflow/python/keras/saving/save.py:184, in load_model(filepath, custom_objects, compile)
181 with generic_utils.CustomObjectScope(custom_objects or {}):
182 if (h5py is not None and (
183 isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))):
--> 184 return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
186 if sys.version_info >= (3, 4) and isinstance(filepath, pathlib.Path):
187 filepath = str(filepath)
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/tensorflow/python/keras/saving/hdf5_format.py:176, in load_model_from_hdf5(filepath, custom_objects, compile)
174 if model_config is None:
175 raise ValueError('No model found in config file.')
--> 176 model_config = json.loads(model_config.decode('utf-8'))
177 model = model_config_lib.model_from_config(model_config,
178 custom_objects=custom_objects)
180 # set weights
AttributeError: 'str' object has no attribute 'decode'

When we are calling the model we first need to consider tensorflow library and before that OS library.
import os
import tensorflow as tf
from tensorflow import keras
In the given question, tensorflow was not called and even if it was imported, to load the H5 model, we need to use tensorflow. Replace the existing code block of loading with the below code block.
import os
import tensorflow as tf
from tensorflow import keras
tf.keras.models.load_model(filepath, custom_objects=None, compile=True)
this is the procedure to call the model to solve the current issue.

Related

load/convert onnx model into pytorch

I need to use GNNExplainer which requires pytorch model.
However, my GNN model is written in keras libraries.
I tried to use this library to load my onnx model and convert into pytorch. Can Anyone help me to resolve this errors?
Here are the errors i got from it.
`import tf2onnx
#from keras2onnx import convert_keras
#Load the Keras model
keras_model = model
#Convert the Keras model to ONNX format
onnx_model, _ = tf2onnx.convert.from_keras(model)
#Save the ONNX model to a file
with open('./my_onnx_model.onnx', 'wb') as f:
f.write(onnx_model.SerializeToString())
import onnx
onnx_model = onnx.load('./my_onnx_model.onnx')
import onnx
from onnx2torch import convert
torch_model_1 = convert(onnx_model)`
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-64-b734fd4e372a> in <module>
2 from onnx2torch import convert
3
----> 4 torch_model_1 = convert(onnx_model)
5
6 # from onnx2pytorch import ConvertModel
/working/Ali_code/custom_model_env/GNN/lib/python3.6/site-packages/onnx2torch/converter.py in convert(onnx_model_or_path, save_input_names, attach_onnx_mapping)
105 domain=onnx_node.domain,
106 operation_type=onnx_node.operation_type,
--> 107 version=version,
108 )
109
/working/Ali_code/custom_model_env/GNN/lib/python3.6/site-packages/onnx2torch/node_converters/registry.py in get_converter(operation_type, version, domain)
67 converter = _CONVERTER_REGISTRY.get(description, None)
68 if converter is None:
---> 69 raise NotImplementedError(f'Converter is not implemented ({description})')
70
71 return converter
NotImplementedError: Converter is not implemented (OperationDescription(domain='', operation_type='Loop', version=13))

Error in importing MobilenetV2 model in Azure ML Studio notebook

I am trying the import MobilenetV2 in the Azure ML Studio notebook, but I am getting an error. Please note that I can import the model running the same command via Jupyter notebook on my local machine.
import tensorflow as tf
from tensorflow import keras
model = tf.keras.applications.MobileNetV2()
The error log is as below
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Input In [23], in <cell line: 1>()
----> 1 model = tf.keras.applications.MobileNetV2()
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/tensorflow/python/keras/applications/mobilenet_v2.py:405, in MobileNetV2(input_shape, alpha, include_top, weights, input_tensor, pooling, classes, classifier_activation, **kwargs)
402 weight_path = BASE_WEIGHT_PATH + model_name
403 weights_path = data_utils.get_file(
404 model_name, weight_path, cache_subdir='models')
--> 405 model.load_weights(weights_path)
406 elif weights is not None:
407 model.load_weights(weights)
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:250, in Model.load_weights(self, filepath, by_name, skip_mismatch)
246 if (self._distribution_strategy.extended.steps_per_run > 1 and
247 (not network._is_hdf5_filepath(filepath))): # pylint: disable=protected-access
248 raise ValueError('Load weights is not yet supported with TPUStrategy '
249 'with steps_per_run greater than 1.')
--> 250 return super(Model, self).load_weights(filepath, by_name, skip_mismatch)
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/tensorflow/python/keras/engine/network.py:1266, in Network.load_weights(self, filepath, by_name, skip_mismatch)
1263 hdf5_format.load_weights_from_hdf5_group_by_name(
1264 f, self.layers, skip_mismatch=skip_mismatch)
1265 else:
-> 1266 hdf5_format.load_weights_from_hdf5_group(f, self.layers)
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/tensorflow/python/keras/saving/hdf5_format.py:659, in load_weights_from_hdf5_group(f, layers)
648 """Implements topological (order-based) weight loading.
649
650 Arguments:
(...)
656 and weights file.
657 """
658 if 'keras_version' in f.attrs:
--> 659 original_keras_version = f.attrs['keras_version'].decode('utf8')
660 else:
661 original_keras_version = '1'
AttributeError: 'str' object has no attribute 'decode'
According to this MSFT document, fixes for Attribute errors such as python AttributeError: 'str' object has no attribute 'decode' are based on the training version of your AutoML SDK
You require pandas == 0.25.1 and scikit-learn==0.22.1 , If your AutoML SDK training version is greater than 1.13.0
Upgrade scikit-learn and/or pandas to proper version if there is a version discrepancy with the following,
```
pip install --upgrade pandas==0.25.1
pip install --upgrade scikit-learn==0.22.1
```
You require pandas == 0.23.4 and sckit-learn==0.20.3 if your AutoML SDK training version is less than or equal to 1.12.0,
Downgrade scikit-learn and/or pandas to correct version with the following, If there is a version mismatch by the following:
pip install --upgrade pandas==0.23.4
pip install --upgrade scikit-learn==0.20.3
References:
Does Any one got "AttributeError: 'str' object has no attribute 'decode' " , while Loading a Keras Saved Model
2.azure ml update service erro AttributeError: AttributeError: 'str' object has no attribute 'id'

how to use sentence bert with transformers and torch

I would like to use sentence_transformers
But due to policy restrictions I cannot install the package sentence-transformers
I have transformers and torch package though.
I went to this page and tried to run the below code
Before doing that I went to the page and downloaded all the files
import os
path="/yz/sentence-transformers/multi-qa-mpnet-base-dot-v1/" #local path where I have stored files
os.listdir(path)
['.dominokeep',
'config.json',
'data_config.json',
'modules.json',
'sentence_bert_config.json',
'special_tokens_map.json',
'tokenizer_config.json',
'train_script.py',
'vocab.txt',
'tokenizer.json',
'config_sentence_transformers.json',
'README.md',
'gitattributes',
'9e1e76b7a067f72e49c7f571cd8e811f7a1567bec49f17e5eaaea899e7bc2c9e']
The code that I ran is
from transformers import AutoTokenizer, AutoModel
import torch
# Load model from HuggingFace Hub
path="/yz/sentence-transformers/multi-qa-mpnet-base-dot-v1/"
"""tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/multi-qa-mpnet-base-dot-v1")
model = AutoModel.from_pretrained("sentence-transformers/multi-qa-mpnet-base-dot-v1")"""
tokenizer = AutoTokenizer.from_pretrained(path)
model = AutoModel.from_pretrained(path)
The error that I get is as below
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-18-bb33f7c519e0> in <module>
32 model = AutoModel.from_pretrained("sentence-transformers/multi-qa-mpnet-base-dot-v1")"""
33
---> 34 tokenizer = AutoTokenizer.from_pretrained(path)
35 model = AutoModel.from_pretrained(path)
36
/usr/local/anaconda/lib/python3.6/site-packages/transformers/models/auto/tokenization_auto.py in from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs)
308 config = kwargs.pop("config", None)
309 if not isinstance(config, PretrainedConfig):
--> 310 config = AutoConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
311
312 if "bert-base-japanese" in str(pretrained_model_name_or_path):
/usr/local/anaconda/lib/python3.6/site-packages/transformers/models/auto/configuration_auto.py in from_pretrained(cls, pretrained_model_name_or_path, **kwargs)
342
343 if "model_type" in config_dict:
--> 344 config_class = CONFIG_MAPPING[config_dict["model_type"]]
345 return config_class.from_dict(config_dict, **kwargs)
346 else:
KeyError: 'mpnet'
my questions:
How should I fix this error?
is there a way to use the same method for MiniLM-L6-H384-uncased-
. I would like to use it as seems to be faster
==============================
package versions as below -
transformers - 4.0.0
torch - 1.4.0
Answer is easy, you can not use "MiniLM-L6-H384-uncased" model with pytorch 1.4.0
print(torch.__version__)
# 1.4.0
torch.load("/content/MiniLM-L6-H384-uncased/pytorch_model.bin", location="cpu")
"""RuntimeError: version_ <= kMaxSupportedFileFormatVersion INTERNAL ASSERT FAILED
at /pytorch/caffe2/serialize/inline_container.cc:132, please report a bug to
PyTorch. Attempted to read a PyTorch file with version 3, but the maximum
supported version for reading is 2. Your PyTorch installation may be too old.
(init at /pytorch/caffe2/serialize/inline_container.cc:132)"""

Having trouble turning in Google Colab python in turning a Keras "model.h5" using TFLiteConverter.from_keras_model("model.h5")

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)

ConsoleBuffer' object has no attribute 'isatty'

I'm doing image classification using sparkdl on databricks community edition.
I added all the library's.
i have created data-frame using the image data.
from pyspark.ml.classification import LogisticRegression
from pyspark.ml import Pipeline
from sparkdl import DeepImageFeaturizer
featurizer = DeepImageFeaturizer(inputCol="image", outputCol="features", modelName="InceptionV3")
lr = LogisticRegression(maxIter=20, regParam=0.05, elasticNetParam=0.3, labelCol="label")
p = Pipeline(stages=[featurizer, lr])
p_model = p.fit(train_df)
AttributeError Traceback (most recent call last)
<command-2468766328144961> in <module>()
7 p = Pipeline(stages=[featurizer, lr])
8
----> 9 p_model = p.fit(train_df)
/databricks/spark/python/pyspark/ml/base.py in fit(self, dataset, params)
62 return self.copy(params)._fit(dataset)
63 else:
---> 64 return self._fit(dataset)
65 else:
66 raise ValueError("Params must be either a param map or a list/tuple of param maps, "
/databricks/spark/python/pyspark/ml/pipeline.py in _fit(self, dataset)
104 if isinstance(stage, Transformer):
105 transformers.append(stage)
--> 106 dataset = stage.transform(dataset)
107 else: # must be an Estimator
108 model = stage.fit(dataset)
From the title of your question, it sounds like you're hitting a AttributeError: 'ConsoleBuffer' object has no attribute 'isatty' error in a Databricks Python notebook.
If you are using Databricks Runtime 3.3 or later then this bug should be fixed.
In earlier Databricks Runtime releases, you should be able to work around this problem by monkeypatching sys.stdout by running the following code snippet at the beginning of your Python notebook:
import sys
sys.stdout.isatty = lambda: False
sys.stdout.encoding = sys.getdefaultencoding()
Databricks' Python REPL overrides sys.stdout to use our own ConsoleBuffer class and prior to Databricks Runtime 3.3 this class did not implement the isatty and encoding methods.
Source: I'm a Databricks employee who worked on this bugfix.

Resources