retrain model using python api
I want to retain the fast rcnn model which I have input training data A before. Now, I would like to add data B to optimize this model. So I modify A2_RunWithPyModel file,
https://github.com/Microsoft/CNTK/tree/master/Examples/Image/Detection/FastRCNN
First,I comment the following codes out,
if os.path.exists(model_path):
trained_model = load_model(model_path)
else:
trained_model = train_fast_rcnn()
trained_model.save(model_path)
Then,I add the following codes,
trained_model = load_model(model_path)
trained_model = train_fast_rcnn()
trained_model.save(model_path)
In my opinion, the above codes just only train B and overwrite the model, because the variables W, b will be initialized when the function train_fast_rcnn() runs. But the results of these codes is different from the one I got just training B. So I am confused if the above code have effect on retraining?
Any help is appreciated. Thank you.
Correct, the above code only trains on B, because train_fast_rcnn() creates a new model from the base classification model (AlexNet) from scratch. The different results might be related to non-determinism. Did you try running either version a few times to see whether each one has always the same output? The code as you posted it definitely trains a new network on B only.
Thank you for your help. I do not understand the word "non-determinism". Could you please give more detail about it? I ran the two versions several times, and the results are always different. But the result stays the same when i just only train on B.
Also, here is another question.
In this document:
https://docs.microsoft.com/en-us/cognitive-toolkit/How-do-I-Train-models-in-Python#load-pre-trained-checkpointed-model-and-continue-retraining
I think the function of the following code:
trainer.save_checkpoint(checkpoint_file)
is loading the parameters W,b of the pretrained model into the new model.
So I add it into function train_fast_rcnn() of file A2_RunWithPyModel, like this:
trainer = Trainer(frcn_output, (ce, pe), learner, progress_printer)
trainer.save_checkpoint("C:\\Users\\...\\frcn_py.model")
the main function is modified as:
trained_model = train_fast_rcnn()
trained_model.save(model_path)
Then, I ran the new version code. The result is also different from what I got when I only train on B. But, I am not sure the new model retained successfully. (The number of images in training data A and B are both 90. Maybe the training data number is not enough.)
So does "trainer.save_checkpoint" really work and did i add it at the right place? Do you have any good ideas of proving the retraining is really done, or give me some advice of how to retrain. Thank you!
Here is what Wikipedia has on nondeterministic algorithms. Nondeterminism in CNTK training can be caused by randomizing the order of the training data or selecting a different convolution engine (which is done by CNTK under the hood). If you want to continue training your model with additional data then one option is to load the trained model (using load_model()), connect new Input variables to it and train. You shoul dmake sure that you only train the layers you want to; in Fast R-CNN we do not train the conv layers currently (see here for details).
Is the reason that everytime you train the network with the deferent init weights?
@ShuyuZ @pkranen The model is trained on dataset B using this code trained_model = load_model(model_path) trained_model = train_fast_rcnn() trained_model.save(model_path) .
-
It works on dataset B, right ?
-
Will it work on dataset A too ?
-
According to my understanding how this happens is the model for dataset B is trained over a model which was previously trained on dataset A only. So The model will trained for only dataset B and will not touch the training done for dataset A, like adding a new layer for dataset B over the previous model of dataset A.