How to get weights into text file from saved model in Keras/Tensorflow - tensorflow

I have a trained model in 'h5' format. It has some layers and their names with it. I want to read the weights and put them in a single array text file.
I am trying to use h5py but it needs the name of layer in details manually and then weights can be extracted and saved.
Is there other technique to write the weights to text file automatically?

Related

Can't save model in saved_model format when finetune bert model

When training the bert model, the weights are saved well, but the entire model is not saved.
After model.fit,
save model as model.save_weights('bert_xxx.h5') and load_weights works fine,
but since only weights are saved, the model frame must be loaded separately.
So I want to save the entire model at once.
However, the following error occurs.
The tensorflow version was 2.4, and the bert code used https://qiita.com/namakemono/items/4c779c9898028fc36ff3
Why is only the weights saved and not the entire model?
And how can I save the whole model??

What are these 2 files in the CenterNet MobileNetV2 from the Tensorflow OD model zoo?, Do we need them?

Do we need these files?, The Tensorflow Doc don't say anything about them
The model.tflite file is the pretrained model in .tflite format. So if you want to use the model out of the box, you can use this file.
The label_map.txt is used to map the output of your network to actual comprehensible results. I.e. both of the files are needed if you want to use the model out of the box. It is not needed for re-training.

How to feed input into one layer in a tensorflow pre-trained model?

The pretrained model has many layers, I want to feed my input directly into one intermediate layer (and discard the result of the previous layers).
I only got the .pb file and the ckpt files of that model, so how to modify the computation flow without the source code?
This is the only code file that I got, but I dont know how to use it. Is the graph generate by this file?(much different from the normal tensorflow files)https://github.com/tensorflow/models/blob/master/research/object_detection/models/ssd_mobilenet_v2_feature_extractor.py
Here is what you need to do :
Load the model
Find the name of the layer or retrieve the tensor of the layer you want to feed values to (let's name it 'Z' for the sake of the explanation)
Find the name of the layer or retrieve the tensor of the layer you want to get results from ('Y')
Run this code snippet :results = sess.run('Y:0', {'Z:0': your_value})

Keras how to save specified variable?

I want to save my trained model in keras with 10 fold cv, and I use the word embedding in my model, so keras saved model file is too big, about 550M. and 10 fold trained model is about 5GB.
If I could delete the embedding variable in the saved model, and just save another variable, I think I could save most file size, because the word embedding array file is about 500M. And then total file size will be reduced to 1GB.
But I can not make it with keras.
Also when I try using tf.train.saver in keras, something strange happens.
Does anyone have any idea?
Have you tried only to save the weights instead of the whole model as mentioned here: https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model
model.save_weights('my_model_weights.h5')

Can I save a graph with its values without saving the inputs?

I have a network with weights filled by manual tf.assign, and now I want to save the network with the weight values but without the placeholder inputs. It seems tf.train.Saver works only when I have the feed_dict available, and tf.train.export_meta_graph only saves the network structure. I tried pickle and dill but they both have errors. Are there any better solutions for this kind of saving?
Placeholders convert the input data into Tensors so I guess they are an important part of the Graph and I don't understand why you don't want to include them.
Even if you use tf.assign, you can freeze the graph, which means combining the structure with the weights. What freezing does is to convert Tensorflow variables into constants.
You have to save the structure of your graph:
gdef = g.as_graph_def()
tf.train.write_graph(gdef,".","graph.pb",False)
Then save the weights (after training)
saver.save(sess, 'tmp/my-weights')
And freeze the graph according to the tutorial in https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/lite
After that, you can use the Graph.