Loading weights from a saved model to use with creation of a new learner

Hi all, I am new to deep learning. Kindly help me on this.

I have trained and saved a model

learn.save('trn_val=0.2_tst_res50-1')

and now, I need to create a new learner with the weights from the saved model. The new learner is something like the following
learn = cnn_learner(data_1, models.resnet50, metrics = accuracy)

Thanks for your help.

learn.load should work just fine on the new learner, passing in the old saved model:

learn.load('trn_val=0.2_tst_res50-1')

Hi @nbharatula ,
I should first load using learn.load( 'trn_val=0.2_tst_res50-1') and then training a new model like
learn_new = cnn_learner(data_1, models.resnet50, metrics = accuracy) so that my new learner learn_new will start learning with the weights developed by the learner ‘learn’ right?

Create the new learner first then load the weights to the new learner. Does this new learner have a significant difference from the old? Same architecture? Same number of classes?

1 Like

Hi @muellerzr, no significant difference. It is with the same architecture and classes.
Thans

Then yes. You’d generate a new learner and then do a learn.load() for your old weights

Thanks.
Just for clarification,
Should I first generate a new learner like
learn_new = cnn_learner(data_1, models.resnet50, metrics = accuracy)
then
learn.load('learn_old')
and then training further with
learn_new.fit(data_1, models.resnet50, metrics = accuracy) ?

Thanks a lot for helping me continuously. :blush:

Not quite. Close! Learn_new.load since it’s your new learn object :slight_smile:
Then learn.fit(x) where x is our epochs. Learn_new.load takes care of the date etc

1 Like

Yes, sorry. That (learn.fit) was a mistake in typing. So,
learn_new = cnn_learner(data_1, models.resnet50, metrics = accuracy)
learn_new.load('learn_old')
learn_new.fit(x)

am I right now?

Yep! Perfect! :slight_smile:

1 Like

@muellerzr thanks a lot. You have helped me in understanding another bit of deep learning with fastai.

1 Like

Yes! Sorry I had to step away for a bit.

1 Like

It’s OK @nbharatula .
Thanks for helping. :blush: