I applied an inception model and my model has been savde but how do I avoid training the dataset again and agian? - machine-learning

I have same my inception model in Pycharm using TensorFlow library. Every time I run the project, it starts training the Data set. I want to skip the training every time I run model because once the model has been save ,there is no need to train the data again and again. How I get to know my model has been save successfully? How can I apply the save model in same file?

You can save/restore/load your model using TensorFlow:
Save:
builder = tf.saved_model.builder.SavedModelBuilder(export_dir) with tf.Session(graph=tf.Graph()) as sess: ... builder.add_meta_graph_and_variables(sess,
[tag_constants.TRAINING],
signature_def_map=foo_signatures,
assets_collection=foo_assets,
strip_default_attrs=True)
...
builder.save()
Load:
with tf.Session(graph=tf.Graph()) as sess:
tf.saved_model.loader.load(sess, [tag_constants.TRAINING], export_dir)
...
For further reference: TensorFlow Guide on Saving a Model

Actually, once you have saved your model, some files will be saved to your directory with the extension .YAML, .h5 or .meta(for graph), you can check the accuracy of model by restoring from saved file, just for sanity check.
There is nice tutorial on this:
https://www.tensorflow.org/guide/saved_model
http://cv-tricks.com/tensorflow-tutorial/save-restore-tensorflow-models-quick-complete-tutorial/
If you are use keras-api to build model, then this link will be useful for saving and restoring https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model

Related

How to export/save/load the actual AutoKeras "super" model, not the underlying tensorflow model

Is there a way to export/save/load a previously trained autokeras model? I understand I can use the following code to save/load the underlying tensorflow best model:
model = reg.export_model()
model.save(MODEL_FILEPATH, save_format="tf")
best_model = load_model(MODEL_FILEPATH, custom_objects=ak.CUSTOM_OBJECTS)
However, in practice that wouldn't work, since my data has been fitted by autokeras, which takes care of data preparation and scaling. I don't think I have access to what autokeras is doing to the input data (X) before actually fitting, so I can't actually use the exported tensorflow best model to predict labels for new samples with un-prepared and unscaled features.
Am I missing something major here?
Also I noticed that there are some binaries in the autokeras temporary dir. That dir seems to be generated automatically. Is there a way to use that dir to load the previously-fit autokeras "super" model?
Just using import pickle will do the job - https://github.com/keras-team/autokeras/issues/1081#issuecomment-645508111 :

Keras Loaded model always train instead of predict

I am doing a project of deep learning. After training I save the model as h5. In another file, I load the saved model and use the model to predict. However when I run the code in Pycharm, the model starts training again. I restart my laptop and run again but the same thing still appears. Is pycharm running on wrong file?
model.save('model_10000.h5')
Then in another file
model = load_model('model_10000.h5')
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
# predict for test set
pred = model.predict(testX)
this is what i got
You don't need to compile your saved model, maybe that has something to do with it.
model.save('model_10000.h5')
model = load_model('model_10000.h5')
pred = model.predict(testX)
Check this for more detail: https://www.tensorflow.org/tutorials/keras/save_and_load
You need not re-compile the model. your saved model is always compiled and when you load it back, it always return the compiled model (refer Keras FAQ).
So, just remove the model.compile step and you will be good to go.

how to get Tensorflow session from only keras .h5 file without session

The motivation behind this question is I had saved a Keras model using Matterport's MaskRCNN and in the tf.keras.callbacks.ModelCheckpoint() had very explicitly set the save_weights_only argument to False, so that the entire model would be saved (not just the weights).
Turns out there's a bug in the ModelCheckpoint() callback where it sometimes does not save the full model.
This is obviously a problem when you go to load the model after closing your TF session, as the Graph, architecture, and optimizer state are gone, making it hard (if not impossible) to reload that saved model.
Therefore, I am asking whether it is possible to somehow extract the TF session retroactively, from just the .h5 weights file, after the session has closed (resulting from, for example, your Notebook kernel crashing).
Not much code to go on, but there it is:
Given a .h5 file that was saved after each epoch of training a model in Keras, is it possible to extract the Graph session from that .h5 file, and if so, how?
I have several models saved in .h5 format but never called tf.get_session() during the saving of the model weights in h5 format.
with tf.session() as sess:
how to load this model using Tensorflow
TF 2.0 makes this a cinch, but how to solve this on Tensorflow version 1.14?
The end goal of this is to take a model saved with Keras as a .h5 file and do inference with it on Tensorflow Serving, which needs, to my knowledge, a protobuf file in .pb format.
https://medium.com/#pipidog/how-to-convert-your-keras-models-to-tensorflow-e471400b886a
I've tried keras_to_tensorflow:
https://github.com/amir-abdi/keras_to_tensorflow
The code to convert ModelCheckPoint saved in .h5 format to .pb format is shown below:
import tensorflow as tf
# The export path contains the name and the version of the model
tf.keras.backend.set_learning_phase(0) # Ignore dropout at inference
model = tf.keras.models.load_model('./model.h5')
export_path = './PlanetModel/1'
# Fetch the Keras session and save the model
# The signature definition is defined by the input and output tensors
# And stored with the default serving key
with tf.keras.backend.get_session() as sess:
tf.saved_model.simple_save(
sess,
export_path,
inputs={'input_image': model.input},
outputs={t.name:t for t in model.outputs})
For more information, please refer this article.
For other ways to do it, please refer this Stack Overflow Answer.

Difference between Keras model.save() and model.save_weights()?

To save a model in Keras, what are the differences between the output files of:
model.save()
model.save_weights()
ModelCheckpoint() in the callback
The saved file from model.save() is larger than the model from model.save_weights(), but significantly larger than a JSON or Yaml model architecture file. Why is this?
Restating this: Why is size(model.save()) + size(something) = size(model.save_weights()) + size(model.to_json()), what is that "something"?
Would it be more efficient to just model.save_weights() and model.to_json(), and load from these than to just do model.save() and load_model()?
What are the differences?
save() saves the weights and the model structure to a single HDF5 file. I believe it also includes things like the optimizer state. Then you can use that HDF5 file with load() to reconstruct the whole model, including weights.
save_weights() only saves the weights to HDF5 and nothing else. You need extra code to reconstruct the model from a JSON file.
model.save_weights(): Will only save the weights so if you need, you are able to apply them on a different architecture
mode.save(): Will save the architecture of the model + the the weights + the training configuration + the state of the optimizer
Just to add what ModelCheckPoint's output is, if it's relevant for anyone else: used as a callback during model training, it can either save the whole model or just the weights depending on what state the save_weights_only argument is set to. TRUE and weights only are saved, akin to calling model.save_weights(). FALSE (default) and the whole model is saved, as in calling model.save().
Adding to the answers above, as of tf.keras version '2.7.0', the model can be saved in 2 formats using model.save() i.e., the TensorFlow SavedModel format, and the older Keras H5 format. The recommended format is SavedModel and it is the default when model.save() is called. To save to .h5(HDF5) format, use model.save('my_model', save_format='h5') More

Does training tensorflow model automatically save parameters?

I ran the demo tensorflow MNIST model(in models/image/mnist) by
python -m tensorflow.models.image.mnist.convolutional
Does it mean that after the model completes training, the parameters/weights are automatically stored on secondary storage? Or do we have to edit the code to include "saver" functions for parameters to be stored?
No they are not automatically saved. Everything is in memory. You have to explicitly add a saver function to store your model to a secondary storage.
First you create a saver operation
saver = tf.train.Saver(tf.all_variables())
Then you want to save your model as it progresses in the train process, usually after N steps. This intermediate steps are commonly named "checkpoints".
# Save the model checkpoint periodically.
if step % 1000 == 0:
checkpoint_path = os.path.join('.train_dir', 'model.ckpt')
saver.save(sess, checkpoint_path)
Then you can restore the model from the checkpoint:
saver.restore(sess, model_checkpoint_path)
Take a look at tensorflow.models.image.cifar10 for a concrete example

Resources