-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
28 lines (24 loc) · 749 Bytes
/
Copy pathinference.py
File metadata and controls
28 lines (24 loc) · 749 Bytes
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
import json
import torch
import torch.nn.functional as F
from commons import get_model, transform_image
model = get_model()
class_dict = {1:'Malignent',0:'Benign'}
def get_prediction(image_bytes):
transform_image(image_bytes=image_bytes)
#try:
tensor = transform_image(image_bytes=image_bytes)
model.eval()
with torch.no_grad():
output = model.forward(tensor)
#_, predicted = torch.max(output.data, 1)
probability = F.softmax(output.data,dim=1)
prob,clas = probability.topk(1)
#outputs = model.forward(tensor)
#print(prob,clas)
#except Exception:
# return 0, 'error'
#_, y_hat = outputs.max(1)
#predicted_idx = str(y_hat.item())
#print(clas)
return class_dict[clas[0][0].tolist()],round(prob[0][0].tolist(),3)