Loading a model, independent of structure

I have a set of data that I’m trying to analyze in different ways. Because of this, I have 2 models I’m working with, one that classifies it into 2 classes, and one that uses 3 classes.

It would be helpful to me to be able to load in a model given just a path to it. Currently, I’m using:

learn = cnn_learner(dls, resnet50, metrics=error_rate)
learn = learn.load(modelPath)

However, this throws a runtime error when I try to load the 3 class model. Is there any way I could create a learner or model object independent of the structure of the model it’s directed to? All I need to be able to do once the model is loaded is get prediction values from individual images.

Could you pass in n_out=3 in cnn_learner so it could load the 3-class model?

Any reason you’re not using the learn.export/load_learner paradigm? Generally this is what it’s designed to do. IE:

learn_1.export('2class')
learn_2.export('3class')

### load it in
two_class_learn = load_learner('2class')
three_class_learn = load_learner('3class')

They’re fully loaded Learner's, so you can run inference/test_dl, etc

1 Like

Agree with the recommendations above. However I’m curious what the runtime error is? I was having trouble with this the other day so curious if its a related issue?