Upload easy_inference.py
Browse files- inference/easy_inference.py +106 -0
inference/easy_inference.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import numpy as np
|
| 3 |
+
import onnxruntime as ort
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from transformers import AutoTokenizer
|
| 6 |
+
|
| 7 |
+
def predict_thumbnail_onnx(onnx_path: str, image_path: str, image_size: int = 299, threshold: float = 0.5):
|
| 8 |
+
session = ort.InferenceSession(onnx_path)
|
| 9 |
+
input_name = session.get_inputs()[0].name
|
| 10 |
+
|
| 11 |
+
img = Image.open(image_path).convert('RGB').resize((image_size, image_size))
|
| 12 |
+
img_np = np.array(img, dtype=np.float32) / 255.0
|
| 13 |
+
img_np = np.transpose(img_np, (2, 0, 1))
|
| 14 |
+
|
| 15 |
+
mean = np.array([0.485, 0.456, 0.406]).reshape(3, 1, 1)
|
| 16 |
+
std = np.array([0.229, 0.224, 0.225]).reshape(3, 1, 1)
|
| 17 |
+
img_np = (img_np - mean) / std
|
| 18 |
+
|
| 19 |
+
input_tensor = np.expand_dims(img_np, axis=0).astype(np.float32)
|
| 20 |
+
|
| 21 |
+
outputs = session.run(None, {input_name: input_tensor})
|
| 22 |
+
logit = outputs[0].item()
|
| 23 |
+
|
| 24 |
+
probability = 1 / (1 + np.exp(-logit))
|
| 25 |
+
prediction = 1 if probability >= threshold else 0
|
| 26 |
+
|
| 27 |
+
return probability, prediction
|
| 28 |
+
|
| 29 |
+
def predict_title_onnx(onnx_path: str, title: str, tokenizer_name: str = "sentence-transformers/all-MiniLM-L6-v2", threshold: float = 0.5):
|
| 30 |
+
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
|
| 31 |
+
|
| 32 |
+
inputs = tokenizer(
|
| 33 |
+
title,
|
| 34 |
+
return_tensors="np",
|
| 35 |
+
padding="max_length",
|
| 36 |
+
truncation=True,
|
| 37 |
+
max_length=100
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
input_ids = inputs["input_ids"].astype(np.int64)
|
| 41 |
+
attention_mask = inputs["attention_mask"].astype(np.int64)
|
| 42 |
+
|
| 43 |
+
session = ort.InferenceSession(onnx_path)
|
| 44 |
+
|
| 45 |
+
ort_inputs = {
|
| 46 |
+
"input_ids": input_ids,
|
| 47 |
+
"attention_mask": attention_mask
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
expected_inputs = [inp.name for inp in session.get_inputs()]
|
| 51 |
+
if "token_type_ids" in expected_inputs and "token_type_ids" in inputs:
|
| 52 |
+
ort_inputs["token_type_ids"] = inputs["token_type_ids"].astype(np.int64)
|
| 53 |
+
|
| 54 |
+
outputs = session.run(None, ort_inputs)
|
| 55 |
+
logit = outputs[0].item()
|
| 56 |
+
|
| 57 |
+
probability = 1 / (1 + np.exp(-logit))
|
| 58 |
+
prediction = 1 if probability >= threshold else 0
|
| 59 |
+
|
| 60 |
+
return probability, prediction
|
| 61 |
+
|
| 62 |
+
def main():
|
| 63 |
+
parser = argparse.ArgumentParser(description="ONNX Inference CLI for Thumbnail and Title Predictors")
|
| 64 |
+
|
| 65 |
+
parser.add_argument("--image_path", type=str, help="Path to the thumbnail image to classify.")
|
| 66 |
+
parser.add_argument("--title", type=str, help="Title text to classify.")
|
| 67 |
+
|
| 68 |
+
# Models default to ./onnx/ directory
|
| 69 |
+
parser.add_argument("--thumbnail_model", type=str, default="./onnx/thumbnail_predictor.onnx", help="Path to ONNX thumbnail model.")
|
| 70 |
+
parser.add_argument("--title_model", type=str, default="./onnx/title_predictor.onnx", help="Path to ONNX title model.")
|
| 71 |
+
|
| 72 |
+
# Follows the standard 299 resolution from the original training config[span_0](start_span)[span_0](end_span)
|
| 73 |
+
parser.add_argument("--image_size", type=int, default=299, help="Input resolution for the image model.")
|
| 74 |
+
parser.add_argument("--threshold", type=float, default=0.5, help="Classification threshold.")
|
| 75 |
+
|
| 76 |
+
args = parser.parse_args()
|
| 77 |
+
|
| 78 |
+
if not args.image_path and not args.title:
|
| 79 |
+
print("Error: You must provide either --image_path or --title (or both).")
|
| 80 |
+
return
|
| 81 |
+
|
| 82 |
+
if args.image_path:
|
| 83 |
+
prob, pred = predict_thumbnail_onnx(
|
| 84 |
+
onnx_path=args.thumbnail_model,
|
| 85 |
+
image_path=args.image_path,
|
| 86 |
+
image_size=args.image_size,
|
| 87 |
+
threshold=args.threshold
|
| 88 |
+
)
|
| 89 |
+
print("--- Thumbnail Inference ---")
|
| 90 |
+
print(f"File: {args.image_path}")
|
| 91 |
+
print(f"Probability: {prob:.4f}")
|
| 92 |
+
print(f"Prediction: {pred}\n")
|
| 93 |
+
|
| 94 |
+
if args.title:
|
| 95 |
+
prob, pred = predict_title_onnx(
|
| 96 |
+
onnx_path=args.title_model,
|
| 97 |
+
title=args.title,
|
| 98 |
+
threshold=args.threshold
|
| 99 |
+
)
|
| 100 |
+
print("--- Title Inference ---")
|
| 101 |
+
print(f"Text: '{args.title}'")
|
| 102 |
+
print(f"Probability: {prob:.4f}")
|
| 103 |
+
print(f"Prediction: {pred}\n")
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
main()
|