-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassification.py
54 lines (44 loc) · 1.82 KB
/
classification.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import torch
import torch.nn as nn
from torchvision import models, transforms
from PIL import Image
# Load the saved model
model = models.resnet18(pretrained=True)
model.fc = nn.Linear(model.fc.in_features, 1000) # Adjust to match the original model's output units
model.load_state_dict(torch.load('animal_classification_model.pth'))
model.eval()
# Create a new model with the correct final layer
new_model = models.resnet18(pretrained=True)
new_model.fc = nn.Linear(new_model.fc.in_features, 2) # Adjust to match the desired output units
# Copy the weights and biases from the loaded model to the new model
new_model.fc.weight.data = model.fc.weight.data[0:2] # Copy only the first 2 output units
new_model.fc.bias.data = model.fc.bias.data[0:2]
# Load and preprocess the unseen image
image_path = 'test3.jpg' # Replace with the path to your image
image = Image.open(image_path)
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
input_tensor = preprocess(image)
input_batch = input_tensor.unsqueeze(0) # Add a batch dimension
# Perform inference
with torch.no_grad():
output = model(input_batch)
# Get the predicted class
_, predicted_class = output.max(1)
# Map the predicted class to the class name
class_names = ['cats', 'dogs'] # Make sure these class names match your training data
predicted_class_name = class_names[predicted_class.item()]
print(f'The predicted class is: {predicted_class_name}')
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
# Display the image with the predicted class name
image = np.array(image)
plt.imshow(image)
plt.axis('off')
plt.text(10, 10, f'Predicted: {predicted_class_name}', fontsize=12, color='white', backgroundcolor='red')
plt.show()