|
|
import torch |
|
|
import torch.nn as nn |
|
|
from torchvision import models, transforms |
|
|
from PIL import Image |
|
|
import io |
|
|
|
|
|
class EndpointHandler(): |
|
|
def __init__(self, path=""): |
|
|
|
|
|
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
|
|
|
self.class_names = ['Gray Leaf Spot', 'Healthy'] |
|
|
|
|
|
|
|
|
|
|
|
self.model = models.resnet50(weights=None) |
|
|
self.model.fc = nn.Linear(self.model.fc.in_features, len(self.class_names)) |
|
|
|
|
|
|
|
|
|
|
|
state_dict = torch.load(f"{path}/model.pth", map_location=self.device) |
|
|
self.model.load_state_dict(state_dict) |
|
|
self.model.to(self.device) |
|
|
self.model.eval() |
|
|
|
|
|
|
|
|
self.transform = transforms.Compose([ |
|
|
transforms.Resize((256, 256)), |
|
|
transforms.ToTensor(), |
|
|
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) |
|
|
]) |
|
|
|
|
|
def __call__(self, data): |
|
|
|
|
|
inputs = data.pop("inputs", data) |
|
|
|
|
|
|
|
|
image = Image.open(io.BytesIO(inputs)).convert("RGB") |
|
|
|
|
|
|
|
|
tensor = self.transform(image).unsqueeze(0).to(self.device) |
|
|
|
|
|
|
|
|
with torch.no_grad(): |
|
|
outputs = self.model(tensor) |
|
|
probs = torch.nn.functional.softmax(outputs, dim=1) |
|
|
conf, pred_idx = torch.max(probs, 1) |
|
|
|
|
|
|
|
|
return [ |
|
|
{"label": self.class_names[pred_idx.item()], "score": conf.item()} |
|
|
] |