What is "cudnn_status_internal_error" meaning Tensorflow pipeline? - tensorflow

If we start an object_detection project using TensorFlow Model API, we got an error "cudnn_status_internal_error".
I'm using TensorFlow object detection pipeline to train the model.
How to solve it?

In my case, it is because the GPU resource is not enough.
To solve this issue in the pipeline, please add a piece of code in "model_main.py"
session_config = tf.ConfigProto()
#session_config.gpu_options.allow_growth = True
session_config.gpu_options.per_process_gpu_memory_fraction = 0.8
config = tf.estimator.RunConfig(model_dir=FLAGS.model_dir, session_config=session_config)

Related

Databricks MLFlow AutoML XGBoost can't predict_proba()

I used AutoML in Databricks Notebooks for a binary classification problem and the winning model flavor was XGBoost (big surprise).
The outputted model is of this variety:
mlflow.pyfunc.loaded_model:
artifact_path: model
flavor: mlflow.sklearn
run_id: 123456789
Any idea why when I use model.predict_proba(X), I get this response?
AttributeError: 'PyFuncModel' object has no attribute 'predict_proba'
I know it is possible to get the probabilities because ROC/AUC is a metric used for tuning the model. Any help would be amazing!
I had the same issue with catboost model.
The way I solved it was by saving the artifacts in a local dir
import os
from mlflow.tracking import MlflowClient
client = MlflowClient()
local_dir = "/dbfs/FileStore/user/models"
local_path = client.download_artifacts('run_id', "model", local_dir)```
```model_path = '/dbfs/FileStore/user/models/model/model.cb'
model = CatBoostClassifier()
model = model.load_model(model_path)
model.predict_proba(test_set)```

How to use Hugging Face transfomers with spaCy 3.0

Let's say that I want to include distilbert https://huggingface.co/distilbert-base-uncased from Hugging Face into spaCy 3.0 pipeline. I think that this is possible and I found some code on how to convert this model for spaCy 2.0 but it doesn't work in v3.0. What I really want is to load this model using something like this
nlp = spacy.load('path_to_distilbert')
Is it even possible and could you please provide the exact steps to do that.
You can use spacy-transformers to this end. In spaCy v3, you can train custom pipelines using a config file, where you would define the transformer component using any HF model you like in components.transformer.model.name:
[components.transformer]
factory = "transformer"
max_batch_items = 4096
[components.transformer.model]
#architectures = "spacy-transformers.TransformerModel.v1"
name = "bert-base-cased"
tokenizer_config = {"use_fast": true}
[components.transformer.model.get_spans]
#span_getters = "spacy-transformers.doc_spans.v1"
[components.transformer.set_extra_annotations]
#annotation_setters = "spacy-transformers.null_annotation_setter.v1"
You can then train any other component (NER, textcat, ...) on top of this pretrained transformer model, and the transformer weights will be further finetuned, too.
You can read more about this in the docs here: https://spacy.io/usage/embeddings-transformers#transformers-training
It appears that the only transformer that will work out of the box is their roberta-base model. In the docs it mentions being able to connect thousands of Huggingface models but there is no mention of how to add them to a SpaCy pipeline.
In the meantime if you wanted to use the roberta model you can do the following.
# install using spacy transformers
pip install spacy[transformers]
python -m spacy download en_core_web_trf
import spacy
nlp = spacy.load("en_core_web_trf")

How to run a tensorflow model in parallel? Do I need to reprogram the model?

Is there a simple way in tensorflow to make any model run in parallel without reprogramming the model? Like in PyTorch using the DataParallel method
I am new to tensorflow so any reference or code example is very much appreciated.
maybe you can modify the config proto of tensorflow and use:
config = tf.ConfigProto(device_count={'GPU': GPU},
inter_op_parallelism_threads=NBR_THREADS,
intra_op_parallelism_threads=NBR_THREADS)
with tf.Session(config=config) as sess:
<run_your_code>

How to set session configuration in example CNN Estimator for MNIST (built with tf.layers)

I am new to TensorFlow and learning about how to implement CNN (Convolutional Neural Networks). I am using this official example (code). When I try to run it on GPU it gives cuda_error_out_of_memory, as it tries to allocate the entire GPU memory available. I ran it on CPU by setting CUDA_VISIBLE_DEVICES="" environment variable and it worked fine but took lot of time.
I looked for solution to cuda_error_out_of_memory and found it can be mitigated by setting config.gpu_options.allow_growth = True or config.gpu_options.per_process_gpu_memory_fraction = in the tf session.
Question: In the code I shared above for CNN where do I set the session configuration as I don't see any session.run() type of command. I assume its being called internally in the layer methods. So, where do it set it? Is there any way I can set session configuration globally for one file?
You can add any configuration in the Estimator's constructor:
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
config = tf.ConfigProto(gpu_options=gpu_options)
mnist_classifier = tf.estimator.Estimator(
model_fn=cnn_model_fn, model_dir="/tmp/mnist_convnet_model", session_config=config)
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
sess_config = tf.ConfigProto(gpu_options=gpu_options)
run_config = tf.estimator.RunConfig(session_config = sess_config)
mnist_classifier = tf.estimator.Estimator(
model_fn=cnn_model_fn, model_dir="/tmp/mnist_convnet_model", config=run_config)
Add this code before creating Esitimator.

Use keras with tensorflow serving

I was wondering how to use my model trained with keras on a production server.
I heard about tensorflow serving, but I can't figure out how to use it with my keras model.
I found this link : https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html
But I don't know how to initialize the sess variable, since my model is already trained and everything.
Is there any way to do this?
You can initialise your session variable as
from keras import backend as K
sess = K.get_session()
and go about exporting the model as in the tutorial (Note that import for exporter has changed)
from tensorflow.contrib.session_bundle import exporter
K.set_learning_phase(0)
export_path = ... # where to save the exported graph
export_version = ... # version number (integer)
saver = tf.train.Saver(sharded=True)
model_exporter = exporter.Exporter(saver)
signature = exporter.classification_signature(input_tensor=model.input,
scores_tensor=model.output)
model_exporter.init(sess.graph.as_graph_def(),
default_graph_signature=signature)
model_exporter.export(export_path, tf.constant(export_version), sess)
Good alternative to TensorFlow Serving can be TensorCraft - a simple HTTP server that stores models (I'm the author of this tool). Currently it supports only TensorFlow Saved Model format.
Before using the model, you need to export it using TensorFlow API, pack it to TAR and push to the server.
More details you can find in the project documentation.