Repository navigation
Expand file tree
/
Copy pathpredict.py
More file actions
57 lines (37 loc) · 1.59 KB
/
Copy pathpredict.py
File metadata and controls
57 lines (37 loc) · 1.59 KB
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
55
56
57
import argparse
import numpy as np
import onnxruntime as rt
from PIL import Image
from config import SPLIT_TEST_DIR, MODELS_DIR
from src.modeling.transforms import get_transforms
def main():
print("Starting inference...")
parser = argparse.ArgumentParser(description="Run inference on a test image using an ONNX model.")
parser.add_argument("--test_image", type=str, help="Path to the test image.")
parser.add_argument("--model_path", type=str, help="Path to the ONNX model.")
args = parser.parse_args()
class_names = ['her2-enriched', 'luminal-a', 'luminal-b', 'triple-negative']
if args.test_image:
test_image = args.test_image
else:
test_image = SPLIT_TEST_DIR / 'triple-negative' / 'D2-0218_CC-L.png'
if args.model_path:
model_path = args.model_path
else:
model_path = MODELS_DIR / 'artifacts' / 'best.onnx'
session = rt.InferenceSession(model_path)
input_name = session.get_inputs()[0].name
image = Image.open(test_image).convert('RGB')
image = np.array(image)
# Pick the right transform
test_transform = get_transforms(augment=False)
transformed = test_transform(image=image)
transformed_image = transformed['image']
input_tensor = np.expand_dims(transformed_image, axis=0)
result = session.run(None, {input_name: input_tensor})
predicted_class = np.argmax(result[0], axis=1)
print(f"Predicted class: {predicted_class[0]}")
predicted_class_name = class_names[predicted_class[0]]
print(f"Predicted class name: {predicted_class_name}")
if __name__ == "__main__":
main()