Tensorflow, change checkpoint model(.meta .index .data) to frozen model(.pd) - python

I am not familiar with tensorflow.
I want to transform this network, https://github.com/jiangsutx/SRN-Deblur, from tensorflow to nvidia tensorRT. It need a '.pb' model file, but the project only giving three model files as follow:
deblur.model-52300.data-00000-of-00001
deblur.model-52300.index
deblur.model-52300.meta
So I want to transform these files to a '.pb' file.
I have tested the ideas given by:
https://blog.metaflow.fr/tensorflow-how-to-freeze-a-model-and-serve-it-with-a-python-api-d4f3596b3adc
Tensorflow: How to convert .meta, .data and .index model files into one graph.pb file
The problem is that both gave ideas are failed because of the get_checkpoint_state() and latest_checkpoint() giving None value.
Is this caused by the missing of checkpoint file?
Are there other ways to implement this?
Any idea is appreciated.
Thanks.

As you can see from their own repo: they use get_checkpoint_state to test a pre-trained model.
https://github.com/jiangsutx/SRN-Deblur/blob/master/models/model.py#L245
So I'd say yes it is because of the missing .ckpt file, not provided by the author.
From experience, usually the first method from metaflow works quite well.

Related

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?

MultiLayerNetwork saved in DL4J is not loading in Python

I have been working with neural networks in Deeplearning4j and needed to switch it to Python. To use the same model (MultiLayerNetwork in DL4J) I saved it as a .h5 file. Like this:
File newFile = new File("newModel.h5");
ModelSerializer.writeModel(network, newFile, true);
Now, when i try to load it in Python I get the following error:
OSError: SavedModel file does not exist at: newModel.h5/{saved_model.pbtxt|saved_model.pb}
I have tried to use different extensions like .pb and used relative and absolute paths in python. Nothing helped. Can anyone explain to me why this happens? There seems to be not enough information on this issue on the internet and it seems to be the only way to implement the same code in python is to train a new model, etc.
a dl4j model is a zip file. Could you clarify what you're trying to do? If you imported it from keras and need to resave it, the best you can do is export the weights as a numpy array and recreate the architecutre. You can do that with model.params() which gives you the weights.

How to convert matterport mask_rcnn keras(.h5) model to coreml model(.mlmodel) using coremltools

I trained a model using matterport maskrcnn. I already had .h5 model file but i am not able to convert it to .mlmodel. As there are many custom layers involved. I already tried whatever I am able to find on google regarding the same. I also tried https://github.com/edouardlp/Mask-RCNN-CoreML for conversion. So far no success.
Does anybody able to did the conversion so far successfully, if yes can you share the codebase or tutorial for the same.
I am able to convert using the same github repo mentioned in the question. But you can't debug the code in Xcode as maskrcnn is to memory heavy. Its better to use another architecture like deeplab.
Here's a github project https://github.com/edouardlp/Mask-RCNN-CoreML/releases/tag/0.2 with a MaskRCNN.ml model.
Note: You have to copy the models into the project to get it to compile.

Load tensorflow SavedModel in Rstudio trained in Google Cloud ML

I trained a model in Google Cloud ML and saved it as a saved model format. I've attached the directory for the saved model below.
https://drive.google.com/drive/folders/18ivhz3dqdkvSQY-dZ32TRWGGW5JIjJJ1?usp=sharing
I am trying to load the model into R using the following code but it is returning <tensorflow.python.training.tracking.tracking.AutoTrackable> with an object size of 552 bytes, definetly not correct. If anyone can properly load the model, I would love to know how you did it. It should also be able to be loaded into python I assume, that could work too. The model was trained on GPU, not sure which tensorflow version. Thank you very much!
library(keras)
list.files("/path/to/inceptdual400OG")
og400<-load_model_tf("/path/to/inceptdual400OG")
Since the shared model is not available anymore (it says that is in the trash folder)and it is not specified in the question I can't tell which framework you used to save the model on first place. I will suggest trying the Keras load function or the Tensorflow load function depending on which type of saved file model you have.
Bear in mind modify this argument as "compile = FALSE" if you have the model already compiled.
Remember to import the latest libraries if you trained your model with tf>=2.0 because of dependencies incompatibilities {Tensorflow, Keras} and rsconnect::appDependencies() output would be worth checking.

How to share tensorflow weights with other users

is it possible to share tensorflow checkpoint files with other users (plattform & CPU/GPU independet)? I had shared a tensorflow implementation of the DeconvNet and now I want to provide the trained weights. Can I simply upload the saved model or is there another tf way? I'm asking because I read a tutorial were the weights were stored using numpy.savetxt and then restored during the weight initalization. But this method was used for the MNIST example which uses a very small net..
Thanks!
You could save metagraph + provide code to restore and run your model --
http://tensorflow.org/how_tos/meta_graph
One downside of this is that it doesn't provide annotations of which tensors to feed/fetch, so you need to provide some code showing how to use it.
SavedModel is the next iteration of TensorFlow checkpoint format that takes care of that, but it doesn't have much documentation yet.
I use pickle, in binary mode, to dump and load big numpy matrix and it works quite well.

Categories