How to make multiclass image classification using this hybrid model

Hello everyone, I would like to know what changes shall I make in this model to make this model eligible for multiclass image classification (for eg 5 image classes).

It appears you didn’t specify a particular model. I’m assuming you’re working on a hybrid model? Typically, if you’re using a classical model that’s already been trained on ImageNet (which comprises 1000 classes), you would modify the final fully-connected layer to correspond to your specific number of classes – in this case, 5. Here’s an illustration:

self.classifier = torch.nn.Linear(n_qubits, 5)

Hope this helps.

Hey @ThunderBolt-OS! Welcome to the forum :rocket:

Usually with machine learning models that have multinomial classification (i.e., the model is asked to be able to classify more than just two different types of objects), the final layer in the network is a softmax layer. A softmax layer is also commonly used for binary classification, but it’s more general than that.

With this layer at the end of the network, it allows for you to be able to interpret your network’s output as a probability distribution over the possible types of objects it could classify an input as.

It would look like this, for example:

[0.1, 0.3, 0.4, 0.2]

where entry i in the array corresponds to the probability of your network classifying the input as the i^{\text{th}} type (e.g., entry 1 through 4 would correspond to the probability of a cat :cat: , dog :dog: , panda :panda_face: , and racoon :raccoon:, respectively).

Hope that helps!

Yes, so if I am using ResNet152 as the pretrained network, then I need to change the parameters below from 2 to 5, right?

        self.post_net = nn.Linear(n_qubits, 2)

which would be

        self.post_net = nn.Linear(n_qubits, 5)

I have tested your suggestion, and it worked well with an accuracy of 87%. However, when I convert it into a .pkl file and use that file in Django to work with APIs, it throws a signature error.

Thank you so much @isaacdevlugt for such an amazing explanation, I have clearly understood the concept.

I’m not sure exactly what you need to change, as I’m not super familiar with ResNet152 and the dataset you want to apply it to. The output dimension just needs to match the cardinality of the classification set (i.e., the total number of classification types — 4 in the animal example I gave above).

For saving/loading your torch models, you can check out this PR that adds better instructions for doing as much in PennyLane: Add documentation and tests for `TorchLayer` saving by eddddddy · Pull Request #4149 · PennyLaneAI/pennylane · GitHub (specifically see pennylane/qnn/ This will be part of v0.31, which comes out soon!