Using Custom ResNet32 Model Trained by Fastai on Pytorch

Hey, I am currently trying to use a model I trained with FastAI ( Colab: Google Colaboratory) on Pytorch. Here are the steps I use on pytorch side:

//Create the instance of ResNET34 model, if I don’t use the num_classes = 2 overload, I end up with incompatible size error on output layer of the neural network (By default ResNET model has 1000 on Fc layer even though my trained model is trained on fc layer with size 2?).

model = torchvision.models.resnet34(pretrained=False, num_classes = 2)
model_dict = model.state_dict()
loc = torch.load(“models/stage-2.pth”, map_location=“cpu”)[‘model’]

    loc, strict=False

transform = torchvision.transforms.ToTensor()
image = transform(image).unsqueeze(0)
result = model(image)

However, my results make no sense. Sometimes it shows an output like:

tensor([[ 3.6697, -6.6085]], grad_fn=)

And when I rerun I end up with something like;

dim=tensor([[ 2.4605, -5.6492]], grad_fn=)

Can anyone lead me on which step I’m missing? Thanks!

I trained testing the model with the images I had on training folder. It still gives this input:

Fake Result: tensor([[ 4.7905, 13.1582]], grad_fn=)
Real Result: tensor([[ 3.6796, 10.8140]], grad_fn=)

Something is definetly off here :slight_smile:

I’m unsure what the concern is and why the outputs wouldn’t make sense, so please add some more details in case I misunderstand it.
For multi-class classification/segmentation use cases usually nn.CrossEntropyLoss is used as the criterion, which expects raw logits as the model output. These logits are not bound to a specific range and can take arbitrary values. If you want to see the probabilities, you could apply a softmax on the output (but don’t pass the softmax output to nn.CrossEntropyLoss).