Multilabel text classification using BERT and Tensorflow 2 - python

I am trying to build a simple multilabel text classification pipeline using BERT; the goal is to classify the content of social media posts and any post can have more than one label (i.e., a post can be labeled both "Medications" and "Physical and Mental Health"). I am very new to BERT and was trying to follow this example I found: https://towardsdatascience.com/building-a-multi-label-text-classifier-using-bert-and-tensorflow-f188e0ecdc5d
I have some questions on how to set it up for this task.
In my Anaconda system I have previously installed Tensorflow version 2.0. I have ran the command "pip install bert-tensorflow" and then ran the following:
import tensorflow as tf
import tensorflow_hub as hub
import bert
from bert import run_classifier
from bert import optimization
from bert import tokenization
from bert import modeling
And got this error at the step "from bert import run_classifier":
ModuleNotFoundError: No module named 'tensorflow.contrib'
I did some research and found out that Tensorflow 2.0 indeed does not have the contrib module, but earlier versions do. I just wanted some ideas on how to resolve this issue? I do not want to downgrade my Tensorflow version. And, if anyone can please point me to other examples of multilabel text classification and BERT I would greatly appreciate it (so far the ones I've seen are not very easy to follow).
Thank you!

You can avoid this error by selecting Tensorflow version 1.x before your code:
%tensorflow_version 1.x
import tensorflow as tf
tf.__version__ # to check the tensorflow version
Output:
TensorFlow 1.x selected.
'1.15.2'
This code line will convert Tensorflow version to 1.15 for your kernel runtime and now you can import the libraries and run your code without error:
import tensorflow as tf
import tensorflow_hub as hub
import bert
from bert import run_classifier
from bert import optimization
from bert import tokenization
from bert import modeling

Related

Is there any way to get CRF loss in using Tensorflow addons

I have used "from tensorflow_addons.layers import CRF" for the CRF implementation in TensorFlow. I am familiar with the way around using Keras contrib library but I was wondering how did tf didn't make any implementation of crf loss in tf 2?

YOLOv3 ModuleNotFoundError: No module named 'tensorflow.python.keras.engine.base_layer_v1'

I need to train a model using YOLO. I am using the Kaggle kernel and I am training custom images.
When I execute the train module, I get this error.
ModuleNotFoundError: No module named 'tensorflow.python.keras.engine.base_layer_v1'
preinstalled is Tensorflow 2.+. I read somewhere that Yolov3 does not work with TensorFlow 2.+ so I installed Tensor 1.14.
I also tried using ImageAi and got exactly the same error.
Does anyone understand this basekayer_v1? Is it the tensorflow version that give the error?
Thanks
I found the solution
Well, it looks that I am doing things wrong or more difficult since no one encounter this error. I found the solution.
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K
to:
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras import backend as K
If you have the same problem.
YOLO files are outdated. They are said that they are not compatible with Tensorflow 1.14 but for me it worked. I needed to fork the git and edit the model.py. As shown above the important thing is add tensorflow.keras
I also needed to edit Change K.controlflowops.whileloop to tf.whileloop in the code or will give you an error.
I used Kaggle kernel. Using Tensorflow 2.0.2
It took me 4 days to solve this, now it is finally training. I hope it helps.

How to convert Tensorflow 2.* trained with Keras model to .onnx format?

I use the Python 3.7.4 with TensorFlow 2.0 and Keras 2.2.4-tf to train my own CNN model. Everything goes fine. I can use e.g. model.save(my_model), and then use it in other Python scripts. Problem appears when I want to use trained model in OpenCV with its DNN module in C++. cv::dnn:readNetFromTensorflow(model.pb, model.pbtxt), takes as you can see two arguments, and I can't get the second .pbtxt file. So I decide to use .onnx format, because of its flexibility. The problem is that existing libraries keras2onnx takes only model from TensorFlow 1.*, and I want to avoid working with it. Example of code to convert it is presented below:
import tensorflow as tf
import onnx
import keras2onnx
model = tf.keras.models.load_model(my_model_folder_path)
onnx_model = keras2onnx.convert_keras(model, model.name)
onnx.save_model(onnx_model, model_name_onnx)
Is there some other ways to convert such model to onnx format?
The latest version of keras2onnx (in github master) supports TensorFlow 2.
You can install it like this:
pip install git+https://github.com/microsoft/onnxconverter-common
pip install git+https://github.com/onnx/keras-onnx
You need to create a file which can hold ONNX object. Visit https://github.com/onnx/tutorials/blob/master/tutorials/OnnxTensorflowExport.ipynb
import tensorflow as tf
import onnx
import keras2onnx
model = tf.keras.models.load_model('Model.h5')
onnx_model = keras2onnx.convert_keras(model, model.name)
file = open("Sample_model.onnx", "wb")
file.write(onnx_model.SerializeToString())
file.close()

Keras create dataset from CSV without TensorFlow

On every question and tutorial I have found, tf.data.Dataset is used for CSV files, but I am not using tensorflow, I am using PlaidML because my AMD GPU is not supported in ROCm. I have tried using the same code by doing
os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
from tensorflow import keras
but that still does not use the plaidml backend. How do I load this dataset: https://www.kaggle.com/keplersmachines/kepler-labelled-time-series-data into keras without tensorflow? Thank you. I check if the plaidml backend is used by looking at the output. If it says "Using plaidml.keras.backend backend", then it is using plaidml. Also, only plaidml recognizes my GPU, so tensorflow will use my CPU.

No module named 'tensorflow.python._impl

I want to run code for try neural network with python but
No module named 'tensorflow.python._impl
part of my code:
import tensorflow as tf
from tensorflow import keras
from tensorflow.python._impl import activations
I did all solutions that some sites said such as
- Uninstall and install package like tensorflow,..
also:
- Upgrade tensorflow
- Upgrade keras
but it doesn't solve yet. I have this error on co lab too.
please help me if you have any solution
thanks
from keras.layers.recurrent import ...
Official TensorFlow Support discourages use of tf.python.* as it is private and intended for development purposes only. While it may work in some cases, it will "break unannounced" in many others.
Instead, try importing tensorflow with the .python portion removed, e.g.:
from tensorflow._impl import activations

Categories