File size: 985 Bytes
6a922b2
 
 
 
 
 
 
c5e43e4
 
6a922b2
 
c5e43e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import gradio as gr
import torch
import torchvision.transforms as transforms
from PIL import Image

# Load the trained model
model_path = "cifar_net.pth"

model = torch.load(model_path)
model.eval()

# Prepare the image for prediction
image_path = 'download.jpg'
image = Image.open(image_path)

# Transform the image to match CIFAR-10 format
transform = transforms.Compose([
    transforms.Resize((32, 32)),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))  # Normalize with CIFAR-10 mean and std
])
input_image = transform(image).unsqueeze(0)

# Make predictions
with torch.no_grad():
    outputs = model(input_image)

# Retrieve the predicted class label
_, predicted = torch.max(outputs, 1)
class_index = predicted.item()

# Load the CIFAR-10 class labels
classes = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

# Print the predicted class label
print('Predicted class label:', classes[class_index])