How could I get predictions for my PyTorch Image Classifier?
I have implemented this code for My Keras model that worked accurately but now I wanna implement this for my PyTorch Model, but I am unable to configure it for making predictions, Below is my full code, I need help with the classify function:
# importing libraries

import tkinter as tk
from tkinter import *
# import PIL
from tkinter import filedialog

import numpy
from PIL import Image, ImageTk
import torch

# importing model

model=torch.load('corelK_model_0.pt')

classes = {
    0:'africa',
    1: 'beach',
    2: 'tallbuilding', 
    3: 'buses',
    4: 'dinosaurs',
    5: 'elephants',
    6: 'Roses', 
    7: 'horses',
    8: 'mountains',
    9: 'food'
}


def upload_image():
    file_path = filedialog.askopenfilename()
    uploaded = Image.open(file_path)
    uploaded.thumbnail(((top.winfo_width() / 2.25, (top.winfo_height() / 2.25))))
    im = ImageTk.PhotoImage(uploaded)
    sign_image.configure(image=im)
    sign_image.image = im
    label.configure(text=' ')
    show_classify_button(file_path)


def show_classify_button(file_path):
    classify_btn = Button(top, text=&quot;Classify Image&quot;, command=lambda: classify(file_path), padx=10, pady=5)
    classify_btn.configure(background=&quot;#364156&quot;, foreground=&quot;white&quot;, font=('arial', 10, 'bold'))
    classify_btn.place(relx=0.79, rely=0.46)


def classify(file_path):
    image = Image.open(file_path)
    image = image.resize((32, 32))
    image = numpy.expand_dims(image, axis=0)
    image = numpy.array(image)
    pred = model.predict_classes([image])[0]
    sign = classes[pred]
    
    print(sign)
    label.configure(foreground='#011638', text=sign)


# initialize GUI
top = tk.Tk()  # calling the constructor or creating the object of tk class
top.geometry('800x600')  # set height and width
top.title(&quot;Image Classification CIFAR10&quot;)
top.configure(background=&quot;#CDCDCD&quot;)

# set Heading

heading = Label(top, text=&quot;Image Classifier&quot;, pady=20, font=('arial', 20, 'bold'))
heading.configure(background=&quot;#CDCDCD&quot;, foreground='#364156')
heading.pack()

upload = Button(top, text=&quot;Upload an image&quot;, command=upload_image, padx=10, pady=5)
heading.configure(background=&quot;#364156&quot;, foreground='white', font=('arial', 10, 'bold'))
upload.pack(side=BOTTOM, pady=50)

# upload image
sign_image = Label(top)
sign_image.pack(side=BOTTOM, expand=True)

# predicted class

label = Label(top, background=&quot;#CDCDCD&quot;, font=('arial', 15, 'bold'))
label.pack(side=BOTTOM, expand=True)

top.mainloop()

The model I have used is VGG16 after transfer learning, I have saved it using the torch.save .

Can you try the following?

After loading the model, set it to evaluation mode using this statement:

model=torch.load('corelK_model_0.pt')
model.eval()


You're not applying the same image transformations (on the test image) that were used while model training. Your classify function should look like this:

def classify(file_path):
    image = Image.open(file_path)
    image = image.resize((32, 32))
    image = numpy.expand_dims(image, axis=0)
    image = numpy.array(image)

    # Start of transformations
    # .......................
    # End of transformations

    pred = model.predict_classes([image])[0]
    sign = classes[pred]

    print(sign)
    label.configure(foreground='#011638', text=sign)

