How can I use the T5 transformers decoder part as decoder for TrOCR ? for fine tuning task - deep-learning

#after importing lib
T5model = AutoModel.from_pretrained("t5-small")
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
# from transformers import AutoTokenizer
processor.tokenizer = AutoTokenizer.from_pretrained("t5-small")
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
model.config.decoder = T5model.decoder
model.config.pad_token_id = processor.tokenizer.pad_token_id
After uploading IAM data set and setting config and executing using trainer class from hugging face
output["decoder"] = self.decoder.to_dict()
File "/usr/lib/python3/dist-packages/torch/nn/modules/module.py", line 1130, in __getattr__
raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'T5Stack' object has no attribute 'to_dict'

Related

how to registered the log_model in MLflow?

I have tried to load the deep learning model on mlflow, it's perfectly loaded, but the model is not stored in the model registry, can any one guide me how to register the model and its dataset for inference?
Thanks
from mlflow import MlflowClient
experiment_name = "nlp_model"
try:
exp_id = mlflow.create_experiment(name=experiment_name) # set the experiment id
except Exception as e:
exp_id = mlflow.get_experiment_by_name(experiment_name).experiment_id
with mlflow.start_run(experiment_id=exp_id):
run_id = mlflow.active_run().info.run_id
print(run_id)
mlflow.sklearn.autolog(log_models=True)
print(mlflow.tracking.get_tracking_uri())
model = Demucs(**args.demucs, sample_rate=args.sample_rate)# fitting the model
"""
loaded model on mlflow
"""
mlflow.sklearn.log_model(model, "nlp_model")
"""
saved model on mlflow model registory
"""
client = MlflowClient()
model = client.create_model_version(
name="denoiser_nlp_model",
source=f"./mlruns/{exp_id}/{run_id}/artifacts/nlp_model",
run_id=mlflow.active_run().info.run_id
)
#Here is the error msg
[1]: https://i.stack.imgur.com/LNAsc.png

Save TFIDF vocab and transformation and use on new dataset

I am trying to save all the vocab words and the tfidf vectorizer from the train/test set so that I can use it on a new set of text at a later time. I got the vocab and idf dictionary using this code:
cvec_tfidf = TfidfVectorizer(analyzer="word", tokenizer=nltk.word_tokenize, strip_accents='unicode', min_df = .01, max_df = .99, ngram_range=(1,3))
cvec_tfidf.fit(X_train['answer'])
vocab_tfidf = cvec_tfidf.get_feature_names()
def tfidf (tokens, vocab, cvec):
cvec_counts = cvec.transform(tokens)
cvec_matrix = cvec_counts.toarray()
tfidf_model = pd.DataFrame(cvec_matrix, columns=cvec.vocabulary_)
idf = dict(zip(vocab, cvec.idf_))
return tfidf_model, idf
X_train, X_train_idf = tfidf(X_train['answer'], vocab_tfidf, cvec_tfidf)
X_test, X_test_idf = tfidf(X_test['answer'], vocab_tfidf, cvec_tfidf)
I think I have saved and loaded the vocab with
import pickle
pickle.dump(cvec_tfidf.vocabulary_, open("feature.pkl", "wb"))
## LOAD TFIDF
savedtfidf = pickle.load(open("feature.pkl", 'rb'))
I tried to run it on new text but got an error
## USE TFIDF ON NEW DATA
newtext = savedtfidf.fit_transform(text['newtext'])
File "<ipython-input-573-4d2aef685725>", line 1, in <module>
newtext = savedtfidf.fit_transform(text['PSW_Attention_3_cl'])
AttributeError: 'dict' object has no attribute 'fit_transform'
Any idea what I am doing wrong?
The issue is that you are serializing and deserializing only the model's vocabulary - and, as the error says, the vocabulary is simply a dictionary that doesn't have the fit_transform method.
What you want to do is to initialize a new TF-IDF model with your serialized vocabulary:
saved_vocabulary = pickle.load(open("feature.pkl", 'rb'))
cvec_tfidf = TfidfVectorizer(analyzer="word", tokenizer=nltk.word_tokenize, strip_accents='unicode', min_df = .01, max_df = .99, ngram_range=(1,3), vocabulary=saved_vocabulary)
cvec_tfidf.fit_transform(text['newtext'])

tfa.optimizers.MultiOptimizer - TypeError: 'Not JSON Serializable:'

I'm trying to use tfa.optimizers.MultiOptimizer(). I did everything according to the docs (https://www.tensorflow.org/addons/api_docs/python/tfa/optimizers/MultiOptimizer) yet I'm getting the following error:
TypeError: ('Not JSON Serializable:', <tf.Tensor 'gradient_tape/model_80/dense_3/Tensordot/MatMul/MatMul:0' shape=(1, 1) dtype=float32>)
Below is a minimal, working example that reproduces the error, just copy and paste it. The error occurs when the first epoch is finished and the callback trys to save the model.
##############################################################################
import tensorflow as tf
import tensorflow_addons as tfa
import tensorflow.keras.layers as l
import tensorflow_addons.layers as la
import tensorflow.keras as ke
import numpy as np
##############################################################################
def build_model_1():
model_input = l.Input(shape=(32,1))
x = l.Dense(1)(model_input)
model = ke.Model(inputs=model_input, outputs=x)
##########
optimizers = [tf.keras.optimizers.Adam(),
tf.keras.optimizers.Adam()]
optimizers_and_layers = [(optimizers[0], model.layers[:5]), (optimizers[1], model.layers[5:])]
optimizer = tfa.optimizers.MultiOptimizer(optimizers_and_layers)
model.compile(optimizer=optimizer, loss='mse', metrics='mse')
test = tf.keras.optimizers.serialize(optimizer)
return model
##############################################################################
input_data = np.arange( 0, 10000, 1).reshape(10000,1)
target_data = np.arange(-10000, 0, 1).reshape(10000,1)
model = build_model_1()
model_checkpoint = ke.callbacks.ModelCheckpoint('best_model.h5',
monitor='val_mse',
mode='min',
save_best_only=True,
verbose=1)
training_history = model.fit(x = input_data,
y = target_data,
validation_split = 0.2,
epochs = 5,
verbose = 1,
callbacks = [model_checkpoint])
##############################################################################
When saving a complete Keras model (with its own structure in the .h5 file) the tf.keras.Model object is completely serialized as a JSON: this means that every property of the model should be JSON serializable.
NOTE: tf.Tensor are NOT JSON serializable.
When using this multi optimizer from tfa you're adding properties to the model that the JSON serializer will try (and fail) to serialize.
In particular there's this attribute gv that I think it comes from the custom optimizer used.
'gv': [(<tf.Tensor 'gradient_tape/model/dense/Tensordot/MatMul/MatMul:0' shape=(1, 1) dtype=float32>, <tf.Variable 'dense/kernel:0' shape=(1, 1) dtype=float32, numpy=array([[-0.55191684]], dtype=float32)>), (<tf.Tensor 'gradient_tape/model/dense/BiasAdd/BiasAddGrad:0' shape=(1,) dtype=float32>, <tf.Variable 'dense/bias:0' shape=(1,) dtype=float32, numpy=array([-0.23444518], dtype=float32)>)]},
All this tf.Tensor are not JSON serializable, that's why it fails.
The only option is to do not save the model completely (with all its attributes, which should be defined as Keras layers, but in this case is not possible) but saving only the model parameters.
In short, if you add the save_weights_only=True to the callback your training (and checkpoint of the weights) will work fine.
model_checkpoint = ke.callbacks.ModelCheckpoint(
"best_model.h5",
monitor="val_mse",
mode="min",
save_best_only=True,
verbose=1,
save_weights_only=True,
)

How to save and load models of custom dataset in Detectron2?

I have tried to save and load the model using:
All keys are mapped but there is no prediction in output
#1
from detectron2.modeling import build_model
model = build_model(cfg)
torch.save(model.state_dict(), 'checkpoint.pth')
model.load_state_dict(torch.load(checkpoint_path,map_location='cpu'))
I also tried doing it using the official doc but can't understand the input format part
from detectron2.checkpoint import DetectionCheckpointer
DetectionCheckpointer(model).load(file_path_or_url) # load a file, usually from cfg.MODEL.WEIGHTS
checkpointer = DetectionCheckpointer(model, save_dir="output")
checkpointer.save("model_999") # save to output/model_999.pth
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file('COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml'))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # Set threshold for this model
cfg.MODEL.WEIGHTS = '/content/model_final.pth' # Set path model .pth
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1
predictor = DefaultPredictor(cfg)
My code to load custom model works.

Problem uploading an sklearn model to S3 bucket using s3fs

I am trying to upload an SVR model (created with sklearn) to S3 bucket using s3fs, but I get an error saying "TypeError: a bytes-like object is required, not 'SVR'". Can anyone suggest how to transform SVR into the right format?
My code is
model = SVR_model
fs = s3fs.S3FileSystem()
with fs.open('s3://bucket/SVR_model', 'wb') as f:
f.write(model)
Use pickle to turn model into a bytes object:
model = pickle.dumps(SVR_model)
fs = s3fs.S3FileSystem()
with fs.open('s3://bucket/SVR_model', 'wb') as f:
f.write(model)