Fairseq without dictionary - python

I used a Hugging face tokenizer and encoder and preprocessed the data, and now I want to use Fairseq's transformer model for the translation task, but I don't have a dict.txt. What should I do, please
Can only give input and output data fit, or how to make dict.txt

The dict.txt file is attached within the pre-trained model. For transformer models, see Pre-trained models
Downloading, and extracting transformer_lm.wmt19.en gives the following file structure
wmt19.en
|- bpecodes
|- dict.txt
|- model.pt
Also from the docs, the model uses Byte Pair Encoding (BPE). So it you want to train a new model, you might need to pre-process the text first

Related

Convert Tensorflow Saved Model format to Keras .h5 model format

I'm trying to use a pre-trained object detection network (TridentNet) to be able to perform object detection on the images that interest me; the model was previously saved (not by me) in the Tensorflow's SavedModel format.
The TridenNet SavedModel folder I downloaded has a format like:
├── assets
├── saved_model.pb
└── variables
├── variables.data-00000-of-00001
└── variables.index
I need to convert the network from Tensorflow's SavedModel format to Keras .h5 format in order to use Keras basic functions .summary() and .predict()
I've tried various load and save combinations from both Keras and Tensorflow, but the error I get is always the same:
AttributeError: '_UserObject' object has no attribute '_is_graph_network'
Unfortunately I need to change format having never approached the models saved through the SavedModel format, I hope you can help me, thanks in advance.
Though you can use Keras basic functions .summary() and .predict() after loading the SavedModel.
Try with this below code to convert the saved model into h5 format:
new_model = tf.keras.models.load_model('Path of the saved model along with the model name')
# Check its architecture
new_model.summary()
#Save the model into h5 format
new_model.save('New_Model1.h5')
Please check this link for more details.

How to create a Keras model from saved weights without a config JSON (Mask-RCNN)

I'm trying to use the TACO dataset GitHub Repository to get a functional neural network, and I downloaded pre-trained weights from here. I understand that the .h5 file contains only the weights and not the architecture of the model itself. I am interested in getting a .hdf5 file that contains both the weights and the model architecture to test on sample images.
I tried the solution shown in the first answer to this question. The code below just prints "none."
`
from tensorflow import keras
import h5py
f = h5py.File('mask_rcnn_taco_0100.h5', 'r')
print(f.attrs.get('model_config'))
I'm able to print a list of keys, values, and items with the following code, but I'm not sure how this translates to the actual model architecture.
`
print('KEYS-------------------------------------')
print(list(f.keys()))
print('VALUES-------------------------------------')
print(list(f.values()))
print('ITEMS-------------------------------------')
print(list(f.items()))
I think the issue is that I'm missing the config.json file, and I'm not sure where to find that or how to produce it.
A few specific questions:
Is there somewhere I can find a config.json file for a generic Mask-RCNN architecture and somehow apply the pre-trained TACO weights to it?
Is there a way to extract the model architecture from the weights file other than what I've already tried?

Where can I find the label map file(pbtxt) of ssd_mobilenet_v1_coco when using tensorflow?

I learn object detection on windows 10 with tensorflow object detection.
I download ssd_mobilenet_v1_coco_2018_01_28.tar.gz from https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md
After I unzipped the ssd_mobilenet_v1_coco_2018_01_28.tar.gz file, I didn't find the pbtxt file.
Where can I find the related pbtxt file of ssd_mobilenet_v1_coco?
I know that there some pbtxt files in models-master\research\object_detection\data folder, but which file is related to ssd_mobilenet_v1_coco?
The label map is not specific to an architecture, but rather to a dataset (which classes you support, and accordingly you should set the number of channels of the classification layer).
Therefore, you simply need the label map which corresponds to coco, which is object_detection/data/mscoco_label_map.pbtxt.
Dataset is implemented in the model. Model is delivered by tar.gz or .zip. If you use pretrained basic model, then label map can be found code tree githup object_detection/data/mscoco_label_map.pbtxt as netanel-sam explains.
But if you start to train your pretrained model and add items to be detected to your dataset and start to deliver your modified model, then your must offer your label map also and there is no better way than include it to the .tar.gz or .zip. Same situation is with lite-model, because conversion from trainable model to lite often loses items from dataset. Lite-model uses also other format to labelmap than basic model. Confusing?

How to load the pre-trained doc2vec model and use it's vectors

Does anyone know which function should I use if I want to use the pre-trained doc2vec models in this website https://github.com/jhlau/doc2vec?
I know we can use the Keyvectors.load_word2vec_format()to laod the word vectors from pre-trained word2vec models, but do we have a similar function to load pre-trained doc2vec models as well in gensim?
Thanks a lot.
When a model like Doc2Vec is saved with gensim's native save(), it can be reloaded with the native load() method:
model = Doc2Vec.load(filename)
Note that large internal arrays may have been saved alongside the main filename, in other filenames with extra extensions – and all those files must be kept together to re-load a fully-functional model. (You still need to specify only the main save file, and the auxiliary files will be discovered at expected names alongside it in the same directory.)
You may have other issues trying to use those pre-trained models. In particular:
as noted in the linked page, the author used a custom variant of gensim that forked off about 2 years ago; the files might not load in standard gensim, or later gensims
it's not completely clear what parameters were used to train those models (though I suppose if you succeed in loading them you could see them as properties in the model), and how much meta-optimization was used for which purposes, and whether those purposes will match your own project
if the parameters are as shown in one of the repo files, [train_model.py][1], some are inconsistent with best practices (a min_count=1 is usually bad for Doc2Vec) or apparent model-size (a mere 1.4GB model couldn't hold 300-dimensional vectors for all of the millions of documents or word-tokens in 2015 Wikipedia)
I would highly recommend training your own model, on a corpus you understand, with recent code, and using metaparameters optimized for your own purposes.
Try this:
import gensim.models as g
model="model_folder/doc2vec.bin" #point to downloaded pre-trained doc2vec model
#load model
m = g.Doc2Vec.load(model)

Tensorflow: How can I extract image features from a specific layer of a pre-trained CNN?

I have a pre-trained CNN model as a .pb file. I can load the model and extract the final vector from the last layer for all images. Now I would like to extract the vector coming from a specific layer and not the final for my images. I am using an import_graph_def function to load the model and I don't know the names of the layers because .pb file is large and I can't open it.
How can I run one part of the model and not the whole in order to get vectors until the layer I want?
One approach other than what was mentioned by Peter Hawkins, to use tf.Graph.get_operations() in the comments is to use tensorboard to find the name of the layer you would like to extract from.
From there you can just use
graph.get_tensor_by_name("import/layer_name")
to extract out whichever features you want.

Categories