Views
No views yet
1import torch
2from torchvision import transforms, models
3from PIL import Image
4
5# Load model
6model = models.resnet18(weights=None)
7model.fc = torch.nn.Linear(model.fc.in_features, 8)
8state_dict = torch.load("pytorch_model.pth", map_location="cpu")
9model.load_state_dict(state_dict)
10model.eval()
11
12# Preprocess image
13transform = transforms.Compose([
14 transforms.Resize((512, 512)),
15 transforms.ToTensor(),
16 transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
17])
18
19image = Image.open("dental_photo.jpg").convert("RGB")
20input_tensor = transform(image).unsqueeze(0)
21
22# Predict
23with torch.no_grad():
24 outputs = model(input_tensor)
25 probabilities = torch.nn.functional.softmax(outputs, dim=1)
26 predicted_class = torch.argmax(probabilities, dim=1).item()