Update app.py
Browse files
app.py
CHANGED
@@ -5,47 +5,32 @@ from PIL import Image
|
|
5 |
|
6 |
# Load the trained model
|
7 |
model_path = "cifar_net.pth"
|
8 |
-
|
9 |
-
model =
|
10 |
-
model.load_state_dict(state_dict)
|
11 |
model.eval()
|
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 |
-
|
37 |
-
|
38 |
-
fn=classify_images,
|
39 |
-
inputs=inputs_image,
|
40 |
-
outputs=outputs_image,
|
41 |
-
title="CIFAR-10 Image Classifier",
|
42 |
-
description="Classify images into one of the CIFAR-10 classes.",
|
43 |
-
examples=[
|
44 |
-
['image_0.jpg'],
|
45 |
-
['image_1.jpg']
|
46 |
-
],
|
47 |
-
allow_flagging=False
|
48 |
-
)
|
49 |
-
|
50 |
-
if __name__ == "__main__":
|
51 |
-
interface_image.launch()
|
|
|
5 |
|
6 |
# Load the trained model
|
7 |
model_path = "cifar_net.pth"
|
8 |
+
|
9 |
+
model = torch.load(model_path)
|
|
|
10 |
model.eval()
|
11 |
|
12 |
+
# Prepare the image for prediction
|
13 |
+
image_path = 'download.jpg'
|
14 |
+
image = Image.open(image_path)
|
15 |
+
|
16 |
+
# Transform the image to match CIFAR-10 format
|
17 |
+
transform = transforms.Compose([
|
18 |
+
transforms.Resize((32, 32)),
|
19 |
+
transforms.ToTensor(),
|
20 |
+
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # Normalize with CIFAR-10 mean and std
|
21 |
+
])
|
22 |
+
input_image = transform(image).unsqueeze(0)
|
23 |
+
|
24 |
+
# Make predictions
|
25 |
+
with torch.no_grad():
|
26 |
+
outputs = model(input_image)
|
27 |
+
|
28 |
+
# Retrieve the predicted class label
|
29 |
+
_, predicted = torch.max(outputs, 1)
|
30 |
+
class_index = predicted.item()
|
31 |
+
|
32 |
+
# Load the CIFAR-10 class labels
|
33 |
+
classes = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
|
34 |
+
|
35 |
+
# Print the predicted class label
|
36 |
+
print('Predicted class label:', classes[class_index])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|