Python: How to load a picklefile containing a multilabel classification model

Viewed 60

I trained a multilabel classification model following this jupyter notebook found on github: https://github.com/tsennikova/fashion-ai/blob/main/fashion-attribute-recognition.ipynb

Then I loaded my model using the following code copied from this other jupyter notebook of the same Github account that was intended to be used for another model for cloth-recognition that was not a multilabel classification model but a "single output" classification model: https://github.com/tsennikova/fashion-ai/blob/main/inference.py

Now my problem is that when I load that multilabel classification model and I use it to do predictions, it outputs just one label and I don't know how to change the code to load the model to make it predict multiple labels. Anyone have an idea? Thank you very much! I have spent hours trying to fix this but I feel completely lost.

This is the code I used to load and use the model to predict the clothing attributes(the multilabels) of a picture, and as a result I retrieve a single output that is:"print", one of the possible labels, but the model is supposed to predict multiple labels.


    import torch
    import torchvision.models as models
    from torchvision import transforms
    from torch.autograd import Variable
    from PIL import Image
    import torch.nn as nn

    MODEL_PATH = r"C:\Users\damia\Documents\UNIVERSITA\Master in Big Data Solutions in B 
    arcelona\CORSI\Final Project\jupyter notebooks - python files\attributes recognition\atr-recognition- 
    stage-2-resnet34.pkl"
    DATA_PATH = r"C:\Users\damia\Documents\UNIVERSITA\Master in Big Data Solutions in 
    Barcelona\CORSI\Final Project\DeepFashionDatabase\dataset\Category and Attribution Prediction 
    Benchmark\img\Cutout_Abstract_Print_Dress\img_00000008.jpg"
    CLASSES_PATH = r"C:\Users\damia\Documents\UNIVERSITA\Master in Big Data Solutions in 
    Barcelona\CORSI\Final Project\jupyter notebooks - python files\attributes recognition\attribute- 
    classes.txt"

    class ClassificationModel():
    
        def __init__(self):
            return
        
        def load(self, model_path, labels_path,  eval=False):
            self.model = torch.load(model_path)
            self.model = nn.Sequential(self.model)
        
            self.labels = open(labels_path, 'r').read().splitlines()
        
            if eval:
                print(model.eval())
            return
    
        def predict(self, image_path):
        
            device = torch.device("cpu")
            img = Image.open(image_path)
        
            test_transforms = transforms.Compose([transforms.Resize(224),
                                      transforms.ToTensor(),
                                      transforms.Normalize([0.485, 0.456, 0.406],
                                                           [0.229, 0.224, 0.225])
                                     ])
        
            image_tensor = test_transforms(img).float()
            image_tensor = image_tensor.unsqueeze_(0)
            inp = Variable(image_tensor)
            inp = inp.to(device)
            output = self.model(inp)
            index = output.data.cpu().numpy().argmax()
            return self.labels[index]

    learner = ClassificationModel()
    learner.load(MODEL_PATH, CLASSES_PATH)
    learner.predict(DATA_PATH)

0 Answers
Related