ConsoleBuffer' object has no attribute 'isatty' - apache-spark

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.

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

cannot load the model in Azure ML

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.

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

coremltools.converters.sklearn.convert causes an error: NameError: name '_tree' is not defined

TLDR: I can't convert my linear regression model into a model I can save like below:
model = coremltools.converters.sklearn.convert(regr, input_features, output_feature)
model.save("Advertising.mlmodel")
I was working on a Raywenderlich tutorial Beginning Machine Learning with SciKit Learn and I stumbled upon an error at the end of Jupyter Notebook when I convert my linear regression to a model I can save, it gives me the following error.
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-82-da16b7baefa4> in <module>
12 # tree.export_graphviz(model)
13
---> 14 coreml_model = coremltools.converters.sklearn.convert(model, inputs, output)
15 coreml_model.save('Advertising.mlmodel')
/usr/local/lib/python3.8/site-packages/coremltools/converters/sklearn/_converter.py in convert(sk_obj, input_features, output_feature_names)
146 # several issues with the ordering of the classes are worked out. For now,
147 # to use custom class labels, directly import the internal function below.
--> 148 from ._converter_internal import _convert_sklearn_model
149
150 spec = _convert_sklearn_model(
/usr/local/lib/python3.8/site-packages/coremltools/converters/sklearn/_converter_internal.py in <module>
34 from . import _LinearSVR
35 from . import _linear_regression
---> 36 from . import _decision_tree_classifier
37 from . import _decision_tree_regressor
38 from . import _gradient_boosting_classifier
/usr/local/lib/python3.8/site-packages/coremltools/converters/sklearn/_decision_tree_classifier.py in <module>
14
15 model_type = "classifier"
---> 16 sklearn_class = _tree.DecisionTreeClassifier
17
18
NameError: name '_tree' is not defined
It's weird because according to Apple's official documentation at github.io/coremltools, their implementation is the same as Raywenderlich and still doesnt work for me.
Here is the link to my notebook
CoreMlTools is suited for scikit-learn version of 19.2 and below.
Probably you have the greater version.
Try to downgrade scikit-learn to 19.2 this way:
!pip install --force-reinstall 'scikit-learn==0.19.2'

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)

Resources