I have deeppavlov fine-tuned model. Is there a way to convert to a model that transformers can work with (https://github.com/huggingface/transformers)?
Here is the way of how to get the HF Transformers model from DeepPavlov model:
from deeppavlov import build_model, configs
m = build_model(configs.classifiers.insults_kaggle_bert_torch, download=True)
m.pipe contains all elements of the pipeline:
[(([], ['x']),
['bert_features'],
<deeppavlov.models.preprocessors.torch_transformers_preprocessor.TorchTransformersPreprocessor at 0x7f9b0414e550>),
(([], ['bert_features']),
['y_pred_probas'],
<deeppavlov.models.torch_bert.torch_transformers_classifier.TorchTransformersClassifierModel at 0x7f9ae5625ac8>),
(([], ['y_pred_probas']),
['y_pred_ids'],
<deeppavlov.models.classifiers.proba2labels.Proba2Labels at 0x7f9ae56221d0>),
(([], ['y_pred_ids']),
['y_pred_labels'],
<deeppavlov.core.data.simple_vocab.SimpleVocabulary at 0x7f9abddfe470>)]
So, you can get the TorchTransformersClassifierModel with
m.pipe[1][2]
and get the HF Transformers model from it:
hf_model = m.pipe[1][2].model
hf_model is a PyTorch nn.Module and you can use it as usually.
Related
I have a NLP use case to compute semantic similarity between sentences that are very specific to my use case.
I want to use Sentence Transformers library to do this, which provides with state of the art result for this goal.
I have a BERT model specifically trained for the sBERT task and I know I can finetune the model with pair of sentences as inputs and similarity score as labels.
However, I would also like to continue BERT pretraining with Mask Language Modeling task on this model.
Does it make sense to instantiate a BertForMaskedLM object from this model already trained for sentence transformer task in order to continue its pretraining, and then load it as a SentenceTransformer model to finetune it on sentence pairs?
I would do as such, with example on Camembert French NLP model from huggingface :
For the MLM part:
from transformers import CamembertTokenizer, CamembertForMaskedLM, LineByLineTextDataset, DataCollatorForLanguageModeling, Trainer, TrainingArguments
tokenizer = CamembertTokenizer.from_pretrained("dangvantuan/sentence-camembert-large")
model = CamembertForMaskedLM.from_pretrained("dangvantuan/sentence-camembert-large")
dataset = LineByLineTextDataset(
tokenizer=tokenizer,
file_path=LOCAL_DATASET_PATH,
block_size=512
)
data_collator = DataCollatorForLanguageModeling(
tokenizer=tokenizer, mlm=True, mlm_probability=0.15
)
training_args = TrainingArguments(
output_dir=LOCAL_MODEL_PATH,
overwrite_output_dir=True,
num_train_epochs=25,
save_steps=500,
save_total_limit=2,
seed=1,
auto_find_batch_size=True
)
trainer = Trainer(
model=model,
args=training_args,
data_collator=data_collator,
train_dataset=dataset,
)
trainer.train()
trainer.save_model(LOCAL_MODEL_PATH + "/my_model")
To get it as SentenceTransformer model:
from sentence_transformers import SentenceTransformer, models
word_embedding_model = models.Transformer(
LOCAL_MODEL_PATH + "/my_model",
tokenizer_name_or_path=tokenizer_path,
max_seq_length=max_seq_length
)
pooling_model = models.Pooling(word_embedding_model.get_word_embedding_dimension())
model = SentenceTransformer(modules=[word_embedding_model, pooling_model])
Thanks !
I am trying to finetune the Detection Transformer model using this awesome tutorial. But I am unable to set a custom backbone (something other than the pretrained models and weights found on timm) in the DetrConfig
This works:
config = DetrConfig.from_pretrained(pt_model_path,
id2label=id2label,
label2id=label2id,
num_queries=num_queries,
backbone='resnet50')
model = DetrForObjectDetection.from_pretrained(
pt_model_path, config=config, ignore_mismatched_sizes=True)
But this does not:
config = DetrConfig.from_pretrained(pt_model_path,
id2label=id2label,
label2id=label2id,
num_queries=num_queries,
backbone='./my_custom_trained/cnn/backbone.pth')
model = DetrForObjectDetection.from_pretrained(
pt_model_path, config=config, ignore_mismatched_sizes=True)
I used the following tutorial program (python3) to train a model to classify images as cat or dog.
https://www.tensorflow.org/tutorials/images/classification
I could run this on my ubuntu computer but I want to save the trained model and try it again to test it with my own images.
Can you please point me to a code snippet to
1. save the trained model and
2. infer image.
Re #PSKP
I was able to save and load the model. Code is below.
import tensorflow as tf
dog = tf.keras.preprocessing.image.load_img(
"mowgli.JPG", grayscale=False, color_mode='rgb', target_size=None,
interpolation='nearest'
)
print(dog.size)
model = tf.keras.models.load_model('dog-cat.h5')
y_hat = model.predict(dog)
print(y_hat)
But got this error at model.predict...
ValueError: Failed to find data adapter that can handle input: <class 'PIL.JpegImagePlugin.JpegImageFile'>, <class 'NoneType'>
Thank you
We have number of ways of doing this. But I am showing you easiest way.
solution
import tensorflow as tf
# Train model
model.fit(...)
# Save Model
model.save("model_name.h5")
# Delete Model
del model
# Load Model
model = tf.keras.models.load_model('model_name.h5')
# Now you can use model for inference
y_hat = model.predict(test_X)
Edit
Solution to ValueError
The problem is your dog variable is not numpy array or tensorflow tensor. Before using it you should convert it into numpy array. And also model.predict(..) does not accept only single image so you should add one extra dimension.
import tensorflow as tf
dog = tf.keras.preprocessing.image.load_img(
"mowgli.JPG", grayscale=False, color_mode='rgb', target_size=None,
interpolation='nearest'
)
# Convert to numpy array
dog = np.asarray(dog)
model = tf.keras.models.load_model('dog-cat.h5')
# Add extrac Dimension (it depends on your model)
# This is because dog has only one image. But predict takes multiple
dog = np.array([dog])
y_hat = model.predict(dog)
print(y_hat)
Find Other Solutions
Here
Initially, I have a fine-tuned BERT base cased model using a text classification dataset and I have used BertforSequenceClassification class for this.
from transformers import BertForSequenceClassification, AdamW, BertConfig
# Load BertForSequenceClassification, the pretrained BERT model with a single
# linear classification layer on top.
model = BertForSequenceClassification.from_pretrained(
"bert-base-uncased", # Use the 12-layer BERT model, with an uncased vocab.
num_labels = 2, # The number of output labels--2 for binary classification.
# You can increase this for multi-class tasks.
output_attentions = False, # Whether the model returns attentions weights.
output_hidden_states = False, # Whether the model returns all hidden-states.
)
Now I want to use this fine-tuned BERT model weights for Named Entity Recognition and I have to use BertforTokenClassification class for this. I'm unable to figure out how to load the fine-tuned BERT model weights into the new model created using BertforTokenClassification.
Thanks in advance.......................
You can get weights from the bert inside the first model and load into the bert inside the second:
new_model = BertForTokenClassification(config=config)
new_model.bert.load_state_dict(model.bert.state_dict())
This worked for me
new_model = BertForTokenClassification.from_pretrained('/config path')
new_model.bert.load_state_dict(model.bert.state_dict())
I trained a model with Resnet3D and I want to extract the neurons of a layer. I plan to use them with the SVM classifier. How can I extract these weights and put them to the numpy array?
Load the weights by keras
model = Resnet3DBuilder.build_resnet_18((128, 96, 96, 3), nClass[0])
model.load_weights('drive/app/models/3d_resnet_modelq.hdf5')
extract a layer
dns = model.layers[-1].output
now what should i do?
If you just want to visualise the features, in pure Keras you can define a Model with the desired layer as output:
from keras.models import Model
model_cut = Model(inputs=model.inputs, output=model.layers[-1].output)
features = model_cut.predict(x) # Assuming you have your images in x
Note that in order for this to work, model must have been compiled at least once.