Setting up model and learner for multilabel classification model deployment


I hope that someone can clarify this for me. I have successfully deployed the model that does single class classification. Now I want to try and deploy the model that will do multilabel classification. I deploy on GCP and as far as I understand, file will needs to be modified.

For the single class I have the following:

model_file_url = ‘www’
model_file_name = ‘model’
classes = [‘Class1’, ‘Class2’, ‘Class3’]
path = Path(file).parent


async def setup_learner():
await download_file(model_file_url, path/‘models’/f’{model_file_name}.pth’)
data_bunch = ImageDataBunch.single_from_classes(path, classes, size=(90,90)).normalize(imagenet_stats)
learn = cnn_learner(data_bunch, models.resnet50, pretrained=False)
return learn


return JSONResponse({
‘result’: str(learn.predict(img)[0]),

I feel like needs to be changed to reflect multilabel classification, but could not figure out what needs to be changed and how exactly. Could someone please point me to some possible solutions? Or maybe there is a file already present somewhere for multilabel classification model deployment?

Thank you in advance!

1 Like

Assuming your model works correctly, then it’s up to you to parse and process what the model returns and how you want to serve it. Personally I usually go for a dictionary of classes and their probabilities for example, or you could just take the top class and return only that one. It depends on your preferences and use-case, it’s not something specific to the library.

1 Like

But it is multilabel, that is why I am confused. The return should be something like that: class1+class 2, or class1+class2+class4, etc. Like in that planet example dataset the prediction is: agriculture+clear+habitation+primary+road

Hey Natalija,

Yeah, my model is multilabel as well :slight_smile: do you mean that all the labels are binary? That’s fine, too - what I said before applies. You can return the probability for the positive class for each label (sometimes known as the “logit”), so you would get something like {'agriculture': 0.82, 'clear': 0.43, 'habitation': 0.98} etc, or you can only return a list of labels with probabilities higher than some threshold, or whatever else you want. Again, it depends on what YOU want to serve through the API and not on the library.

1 Like

You can do this with the API as well. Make a little noop function and pass it to get preds. It won’t apply the softmax:

def my_func(x): return x

1 Like

Sorry, I am still a bit confused. I found your reply to a similar thread:

Also what about classes= in here:

model_file_url = ‘www’
model_file_name = ‘model’
classes = [‘Class1’, ‘Class2’, ‘Class3’]
path = Path( file ).parent

Do we still need to list all multilabels categories here?

Just to confirm, I am talking about multilabel , like in this example, only in my case there could be 4 to 6 multilabel tags in a single image.

You likely should, though again it depends on what you return in your prediction function. You could also just return completely decoded classes from learn.dls.vocab, so that wouldn’t be needed. I don’t believe I did when I was deploying a few models. Again as @orendar it is entirely up to you. For just how different and yet similar it can get, I did a few examples back in v1 (multilabel is not one of them):

1 Like

no, I need probabilities like happy.marge = XXX,angry.homer= XXX, like in this example, not for each class label separately. In my case, one image can have 4 to 6 tags in it.

Then just return the probabilities from get_preds and don’t decode them?

1 Like

Sorry, I am not being clear. I need users to see tags, not probabilities. They upload a file to my webapp, the model makes a prediction based on my set threshold and they get the end result: happy. marge. dances. alone

Is there an example of a file anywhere set for multilabel problem like this marge/homer stuff or planets stuff?

EDIT: on a second thought, is it possible to get both? Show the user the labels , but also have numerical probabilities for data viz purposes later?

I am not sure you are looking for other options for deploying your model, but I would recommend taking a look at this repo ->

Straightforward serving of several models, including PyTorch. Should work with, but I have not tried myself.
Hope it helps.

1 Like

Thank you! I will definitely check it out!

Is there an example of a file anywhere set for multilabel problem like this marge/homer stuff or planets stuff?

Guys still waiting for someone to help me with the issue - .