File size: 1,270 Bytes
75534d6 | 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 | import torch
from torchvision import models, transforms
from PIL import Image
# ==== Configuration ====
MODEL_PATH = 'models/cat_dog_classifier.pth'
CLASS_NAMES = ['cat', 'dog'] # Make sure this order matches your training dataset
# ==== Load Model ====
model = models.mobilenet_v2(pretrained=False)
model.classifier[1] = torch.nn.Linear(model.last_channel, 2)
model.load_state_dict(torch.load(MODEL_PATH, map_location='cpu'))
model.eval() # Set to evaluation mode
# ==== Image Preprocessing ====
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225]),
])
# ==== Inference Function ====
def predict(image_path):
image = Image.open(image_path).convert('RGB')
input_tensor = transform(image).unsqueeze(0) # Add batch dimension
with torch.no_grad():
outputs = model(input_tensor)
predicted_class = outputs.argmax(1).item()
confidence = torch.softmax(outputs, dim=1)[0][predicted_class].item()
return {
'class': CLASS_NAMES[predicted_class],
'confidence': confidence
}
if __name__ == "__main__":
print(predict('raw_data/train/dog.0.jpg'))
|