I am using matterport repository to train MASK RCNN on a custom dataset. I have been successful in training. Now I want to save the trained model and use it in a web application to detect objects. How do I save the mask rcnn model after training? Please guide me.
The link of the repository:
https://github.com/matterport/Mask_RCNN
Based on this discussion on GitHub, it appears that trained model or weights of matterport/Mask RCNN can be saved as a JSON file in a manner similar to those trained via standard Keras:
import keras
import json
def save_model(trained_model, out_fname="model.json"):
jsonObj = trained_model.keras_model.to_json()
with open(out_fname, "w") as fh:
fj.write(jsonObj)
save_model(model, "mymodel.json")
Update: If you run into the error related to thread-like object, you might find this post helpful...
In the Inspect_model.ipynb notebook under the "Load Model" topic you can save it after it loads the model in inference mode.
in the folder Mask_RCNN/logs generates a folder inside it
I am not sure if we really need to save the whole model again since normally when we used the matterport git we just train new weights on the existing architecture and doesnt make changes to the architecture. When we used this for a pet project , post training - we defined a new model as the MASK RCNN object (from mrcnn.model import MaskRCNN) with the parameter mode as inference and then loaded the newly trained weights model.load_weights('<logpath/trainedweights.h5>', by_name=True)
Related
I have defined a deep learning model my_unet() in tensorflow. During training I set save_weigths=False since I wanted to save the entire model (not only the wieghts bu the whole configuration). The generated file is path_to_model.hdf5.
However, when loading back the model I used the earlier version (I forgot to update it) in which I first called the model and then load the model using:
model = my_unet()
model.load_weights(path_to_model.hdf5)
Instead of simply using: model = tf.keras.models.load_model(path_to_model.hdf5) to load the entire model.
Both ways of loading the model and the weights provided the same predictions when run in some dummy data and there were no errors.
My question is: Why loading the entire model using model.load_weights() does not generate any problem? What is the structure of the hdf5 file and how theese two ways of loading exactly work? Where can I find this information?
You can please see the documentation here for any future reference: http://davis.lbl.gov/Manuals/HDF5-1.8.7/UG/03_DataModel.html
I have been searching for a method to do this for so long, and I can not find an answer. Most threads I found are of people wanting to do the opposite.
Backstory:
I am experimenting with some pre-trained models provided by the tensorflow/models repository. The models are saved as .pb frozen graphs. I want to fine-tune some of these models by changing the final layers to suit my application.
Hence, I want to load the models inside a jupyter notebook as a normal keras h5 model.
How can I do that?
do you have a better way to do so?
Thanks.
seems like all you would have to do is download the model files and store them in a directory. Call the directory for example c:\models. Then load the model.
model = tf.keras.models.load_model(r'c:\models')
model.summary() # prints out the model layers
# generate code to modify the model as you typically do for transfer learning
# compile the changed model
# train the model
# save the trained model as a .h5 file
dir=r'path to the directory you want to save the model to'
model_identifier= 'abcd.h5' # for abcd use whatever identification you want
save_path=os.path.join(dir, model_identifier)
model.save(save_path)
I am a novice in using tensorflow and I built a CNN which is trained and tested both with 80-85% accuracy. I tried to save my trained model using model.save('example.h5'), and download the file using files.download('example.h5')
Afterwards I tried to load it to my flask back-end using model = tf.keras.models.load_model('example.h5').
When I tried using it with random images, it's like the model has never been trained before. Any solutions? thank you
A common reason to this might be that you are normalizing your data on training but not before predicting. Make sure you normalize the input data before you run predictions, if you have trained on normalized data.
I built a CNN model for image classification using the Keras library. However training takes many hours. Once I trained my model, how can I use it without training once more? I mean after I trained my model, I want to use it many times.
Because I will use my model in android studio.
Any help is appreciated
Thank YOU...
EDIT
When I wrote this question, I did not know the save model and load.model, in the answers you see the appropriate usage of them.
You can easily save your model after the training process by using:
model.save('my_model.h5')
you can later load that model by using:
model = load_model('my_model.h5')
for more details have a look at the documentation: https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model
Tensorflow recently seems to have released pretrained model for instance segmentation using MaskRCNN as per below tweet.
https://twitter.com/TensorFlow/status/963472849511092225
I downloaded mask_rcnn_resnet101_atrous_coco_2018_01_28.tar.gz from this and was trying to figure out how to use it.I found frozen model (pb) file and loaded the graph in Tensorboard.
But I cant figure out what should be the input for the model.I couldn't find a node where simply I can input an image , though I was able to locate nodes where we get classes,masks,bounding boxes etc.
Also there seems to be no details online on how to use it (May be because it is new)
If you follow this tensorflow tutorial it will show you how to run the frozen model on a single/group of images. To apply this to the model you downloaded, the simplest way would be to first replace the line:
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
with a path to your downloaded model i.e.
PATH_TO_CKPT = '/absolute/path/to/frozen_inference_graph.pb'
Then there is no need to run the code under the section Download Model. The rest should work the same.