converting pretrained tensorflow models for tensorflow serving - python

I'm trying to use tensorflow serving. However, any of the pretrained models that are available for download (like from here: the TF detection zoo) don't have any files in the saved_models/variables directory that is required by the serving model.
How do you create the files required in the saved_models/variables directory using the pretrained models available from the detection model zoo?
There is some information from the official documentation, but it doesn't cover my use case of converting a pretrained model to be served.
Other things I've tried is to use the tensorflow serving examples. However, most of the existing documentation uses the Resent implementation as an example, and the pretrained model for resnet has been removed by Tensorflow. This is the linked that tutorials use, note that there's no direct link to download the models. As an aside, but an additional funsy, the python examples in the Tensorflow Serving repo don't work with Tensorflow 2.0.
It appears that this link may be useful in the conversion: https://github.com/tensorflow/models/issues/1988

Ok, as of the time of writing the object detection tutorials only support tensorflow 1.12.0.
It's a little difficult to do this because it's so multitiered, but you need to:
clone the tensorflow open model zoo
patch the models/research/object_detection/exporter.py according to these instructions. Alternatively, you can use this patch which are the aforementioned instructions.
Follow the object detection installation instructions as found here in your cloned repo. It's important to both follow the protobuf compilation steps AND update your python path for the slim libraries.
Follow the instructions for exporting a trained model for inference. Note that the important part of the instruction that is important is that the downloaded model will come will three model.ckpt filenames. The filename that needs to be passed into the exporting script is the base filename of these three filenames. So if the three files are /path/to/model.ckpt.data-00000-of-00001, /path/to/model.ckpt.meta, and /path/to/model.ckpt.index, the parameter to pass into to the script is: /path/to/model.ckpt
Enjoy your new model!

Related

How do I use YOLOv5 without the internet for kaggle competition

I am attempting to use a software called YOLOv5 in my python code for a Kaggle competition. The only thing is it requires you not to use the internet. I have the yolov5 file already loaded into the kaggle code. but am unsure how to call in in my code.
I've tried loading it in the following way but keep getting errors.
import torch
torch.save('yolov5x6.pt', 'yolov5')
yolov5x6_model = torch.load('yolov5')
Yolov5 is a follow up version of yolo which is a neural network library in c language, also known as Darknet created by pjreddie.
It is an object detector model which can be trained to recognise objects in images or videos.
If you just want to detect some daily life object then you can just run inference on images/videos using python and trained weights and config file. You will find these files under the pretrained checkpoints section at the following link.
https://github.com/ultralytics/yolov5

BERT tokenizer and model without transformers library

I'm trying to use both BertTokenizer and BertForTokenClassification offline. The goal of the project is to use these pretrained models and apply them to my own dataset for an NLP task. Due to network and security limitations, I am not able to install the transformers library from HuggingFace. I am only able to use PyTorch.
At this point, I have downloaded and saved the following bert-base-uncased files from the HuggingFace website to a local directory:
config.json
python_model.bin
vocab.txt
I've used the transformers library before, so I'm familiar with initializing the models from local files using something like BertTokenizer.from_pretrained('/path/to/local'). However, since I'm not able to install the package and call the model classes, I don't know how to use these downloaded local files in a similar manner. How do I use these local files to use BertTokenizer and BertForTokenClassification?
I've been instructed to use the following link to implement this: https://pytorch.org/tutorials/beginner/saving_loading_models.html

Tensorflow-Keras: Loading weights from checkpoint increases graph size significantly

I am working with a rather large network (98 million parameters), I am using the Keras ModelCheckPoint callback to save my weights as follows, when I reload my saved weights using keras, I can see that the loading operation adds approximately 10 operations per layer in my graph. This results in a huge memory increase of my total network. Is this expected behavior? And if so, are there any known work arounds?
Details:
I am using: tf.keras.callbacks.ModelCheckpoint with "save_weights_only=True" as argument to save the weights
The code for loading it is:
model.load_weights(path_to_existing_weights)
where model is a custom keras model.
I am using Tensorflow 1.14 and Keras 2.3.0
Anyone that has any ideas?
This seems to me to be unexpected behavior but I can't see anything obvious that you are doing wrong. Are you sure there were no changes to your model between the time you saved the weights and the time you reloaded the weights? All I can suggest is try to do the same thing except this time in the callback change it to save the entire model. Then reload the model then check the graph. I also ran across this, doubt it is the problem but I would check it out
In order to save your Keras models as HDF5 files, e.g. via keras.callbacks.ModelCheckpoint,
Keras uses the h5py Python package. It is a dependency of Keras and should be installed by default.
If you are unsure if h5py is installed you can open a Python shell and load the module via
import h5py If it imports without error it is installed, otherwise you can find detailed installation
instructions here: http://docs.h5py.org/en/latest/build.html
Perhaps you might try reinstalling it.

Can't choose framework for segmentation_models library in python

I am trying to build a ResNet34 model using segmentation_models(sm) library in python. The sm library uses keras framework by default when importing, but I am working with tf.keras to build my datasets used for training and testing.
The documentation says that in order to change the default framework I should either use an environmental variable SM_FRAMEWORK=tf.keras before importing (which I tried but it didn't work) or set it using the method set_framework (which doesn't show up in the suggestions/it says it doesn't exist when I try to execute it).
Is there any other way to overcome this problem?
I trained model on colab and I used :"%env SM_FRAMEWORK=tf.keras" to set the environment to tf.keras and it worked perfectly.

Loading Torch7 trained models (.t7) in PyTorch

I am using Torch7 library for implementing neural networks. Mostly, I rely on pre-trained models. In Lua I use torch.load function to load a model saved as torch .t7 file. I am curious about switching to PyTorch( http://pytorch.org) and I read the documents. I couldn't find any information regarding the mechanisms to load a pre-trained model. The only relevant information I was able to find is this page:http://pytorch.org/docs/torch.html
But the function torch.load described in the page seems to load a file saved with pickle. If someone has additional information on loading .t7 models in PyTorch, please share it here.
The correct function is load_lua:
from torch.utils.serialization import load_lua
x = load_lua('x.t7')
As of PyTorch 1.0 torch.utils.serialization is completely removed. Hence no one can import models from Lua Torch into PyTorch anymore. Instead, I would suggest installing PyTorch 0.4.1 through pip in a conda environment (so that you can remove it after this) and use this repo to convert your Lua Torch model to PyTorch model, not just the torch.nn.legacy model that you cannot use for training. Then use PyTorch 1.xx to do whatever with it. You can also train your converted Lua Torch models in PyTorch this way :)

Categories