tensorflow / models

Models and examples built with TensorFlow
Other
77.2k stars 45.75k forks source link

Append a save/restore example to colab tutorial for TF2 Object Detection API #8884

Open IvanBrasilico opened 4 years ago

IvanBrasilico commented 4 years ago

Prerequisites

Please answer the following question for yourself before submitting an issue.

1. The entire URL of the file you are using

https://github.com/tensorflow/models/tree/master/research/object_detection/colab_tutorials/eager_few_shot_od_training_tf2_colab.ipynb

2. Describe the feature you request

The notebook go through the steps necessary to modify number of classes and fine tune an existing model to detect new classes, but do not show how to save the model for using in other contexts, like tensorflow serving or restoring the model in other scripts/notebooks.I have successfully trained a model, but I tried to restore the checkpoints, tried many common tensorflow save model commands, but they do not worked. Can't figure out how to save the model.

3. Additional context

In the modified notebook:

https://github.com/IvanBrasilico/ajna_bbox/blob/master/notebooks/eager_few_shot_od_training_tf2.ipynb

I do a very simple test training to detect containers bounds on xray inspection images. The training works well, even with a few training. I do some predictions on new images with the model in memory and save the images to see the results. Then I save the model weights to a checkpoint and recreate the model, restoring the checkpoints and something goes wrong, the model do not do predictions anymore. Can figure out how to do it.

4. Are you willing to contribute it? (Yes or No)

Yes

pn12 commented 4 years ago

"Then I save the model weights to a checkpoint" - How did you saved the model weights?

anjapadu commented 4 years ago

I also failed using the normal ways to save so end up getting the weights weights = detection_model.get_weights(), saving them in a serialized format (im just using numpy.save), and after that you can just load the weights with detection_model.set_weights(weights).

pn12 commented 4 years ago

Thanks, I will try this.

IvanBrasilico commented 4 years ago

Hi pn12.

The complete process is described in the notebook ( https://github.com/IvanBrasilico/ajna_bbox/blob/master/notebooks/eager_few_shot_od_training_tf2.ipynb )

I am doing the fine tunning with my own images, as stated in TF2 example. Then I make some predictions on a test set with the fine tuned model and it works OK.

Since the initial weights are restored via checkpoint:

fake_model = tf.compat.v2.train.Checkpoint( _feature_extractor=detection_model._feature_extractor, _box_predictor=fake_box_predictor) ckpt = tf.compat.v2.train.Checkpoint(model=fake_model) ckpt.restore(checkpoint_path).expect_partial()

I added to TF2 example the following line to save weights after training:

ckpt.save('ckpt/ssd_resnet50_v1_fpn_640x640_eager_few_shot')

And after that, I recreate the model exactly as before in the example, but instead of restoring the zoo pre-trained checkpoint, I restore the checkpoint from my saved checkpoint:

ckpt_trained.restore('ckpt/ssd_resnet50_v1_fpn_640x640_eager_few_shot-30')

@anjapadu do you have the example using numpy? How do you would create the model ub other context, by instance? Could you pass me please?

Thank you very much!!

pn12 commented 4 years ago

@IvanBrasilico - Let me test this. thanks for sharing.