I trained the bert_model and saved it it was woking just fine. After saving the predictor it creates bert_model folder which contains tf_model.h5 and tf_model.preproc but loading the predictor and saving it gives lots of errors.
y_train = [encoding[x] for x in y_train]
y_test = [encoding[x] for x in y_test]
(x_train, y_train), (x_test, y_test), preproc = text.texts_from_array(x_train=X_train, y_train=y_train,
x_test=X_test, y_test=y_test,
class_names=class_names,
preprocess_mode='bert',
maxlen=350,
max_features=35000)
model = text.text_classifier(
'bert', train_data=(x_train, y_train), preproc=preproc)
learner = ktrain.get_learner(model, train_data=(x_train, y_train),
val_data=(x_test, y_test),
batch_size=6)
learner.fit_onecycle(2e-5, 3)
learner.validate(val_data=(x_test, y_test), class_names=class_names)
predictor = ktrain.get_predictor(learner.model, preproc)
predictor.get_classes()
import time
message = 'she left me while i was waiting for her and she isnt herself '
start_time = time.time()
prediction = predictor.predict(message)
print('predicted: {} ({:.2f})'.format(prediction, (time.time() - start_time)))
predictor.save("models/bert_model")
Now after saving it it created bert_model folder containing two files tf_model.h5 and tf_model.preproc
to load it and use i am using this:
from keras.models import load_model
import ktrain
from ktrain import text
import time
message = 'she left me while i was waiting for her and she isnt herself '
model_path = "/home/priyanshi/Desktop/folder/models/bert_model/"
predictor = ktrain.load_predictor(model_path)
start_time = time.time()
prediction = predictor.predict(message)
print('predicted: {} ({:.2f} seconds)'.format(
prediction, (time.time() - start_time)))
But it is giving this error:—
python -u “/home/priyanshi/Desktop/Array/email obsulation/test2.py”
2024-06-02 00:59:18.984734: I external/local_tsl/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-06-02 00:59:18.987632: I external/local_tsl/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-06-02 00:59:19.031606: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-06-02 00:59:19.734612: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
Call to keras.models.load_model failed. Try manually invoking this function to investigate error and report issue if necessary.
Traceback (most recent call last):
File “/home/priyanshi/.local/lib/python3.10/site-packages/ktrain/core.py”, line 2048, in _load_model
model = keras.models.load_model(
File “/home/priyanshi/.local/lib/python3.10/site-packages/tf_keras/src/saving/saving_api.py”, line 262, in load_model
return legacy_sm_saving_lib.load_model(
File “/home/priyanshi/.local/lib/python3.10/site-packages/tf_keras/src/utils/traceback_utils.py”, line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File “/home/priyanshi/.local/lib/python3.10/site-packages/tensorflow/python/saved_model/loader_impl.py”, line 119, in parse_saved_model
raise IOError(
OSError: SavedModel file does not exist at: /home/priyanshi/Desktop/Array/email obsulation/bert_model//{saved_model.pbtxt|saved_model.pb}
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/home/priyanshi/Desktop/Array/email obsulation/test2.py”, line 13, in
predictor = ktrain.load_predictor(model_path)
File “/home/priyanshi/.local/lib/python3.10/site-packages/ktrain/core.py”, line 1848, in load_predictor
model = _load_model(fpath, preproc=preproc, custom_objects=custom_objects)
File “/home/priyanshi/.local/lib/python3.10/site-packages/ktrain/core.py”, line 2055, in _load_model
raise Exception(“Error detected: %s” % (e))
Exception: Error detected: SavedModel file does not exist at: /home/priyanshi/Desktop/Array/email obsulation/bert_model//{saved_model.pbtxt|saved_model.pb}
I tried many other ways to use it but its not working and giving errors
Priyanshi Soni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.