Views
No views yet
convnext_tiny (kiến trúc Convolutional tiên tiến thế hệ mới của Meta, đầu vào 224x224).0: No DR (Bình thường)1: Mild DR (Nhẹ)2: Moderate DR (Trung bình)3: Severe DR (Nặng)4: Proliferative DR (Tăng sinh nguy hiểm)best_model.pt: Checkpoint weights của PyTorch chứa state_dict.convnext_inference.pt: Phiên bản mô hình đóng gói dưới dạng TorchScript (đã tích hợp Logit Adjustment và Softmax). Nhận đầu vào là Tensor ảnh đã chuẩn hóa và trả về xác suất 5 lớp trực tiếp.convnext_inference.onnx & convnext_inference.onnx.data: Phiên bản mô hình định dạng ONNX dùng cho deploy đa nền tảng không phụ thuộc PyTorch.preprocessing.py: Script Python duy nhất chứa toàn bộ luồng tiền xử lý (letterbox resize, Ben Graham transform) và lớp DRPredictor chạy suy luận.preprocessing.py và convnext_inference.pt về chung thư mục dự án của bạn và chạy suy luận trực tiếp:1from preprocessing import DRPredictor
2
3# Khởi tạo predictor (chỉ cần chạy 1 lần khi startup hệ thống để nạp model vào RAM/GPU)
4# Chỉ định convnext_path trỏ tới file .pt vừa tải
5predictor = DRPredictor(convnext_path="convnext_inference.pt")
6
7# Dự đoán từ đường dẫn ảnh hoặc dữ liệu bytes nhận được từ client upload
8result = predictor.predict("test_retina.jpg", use_ben_graham=True)
9
10print("Kết quả chẩn đoán:", result)1{
2 "class_id": 0,
3 "class_name": "No DR",
4 "confidence": 0.7325,
5 "probabilities": {
6 "No DR": 0.7325,
7 "Mild": 0.2048,
8 "Moderate": 0.0626,
9 "Severe": 0.0001,
10 "Proliferative DR": 0.0000
11 }
12}input_image. Định dạng float32, Shape: [1, 3, 224, 224] (NCHW format). Ảnh được resize letterbox về (224, 224) và chuẩn hóa ImageNet (mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]).probabilities. Định dạng float32, Shape: [1, 5]. Chứa phân phối xác suất Softmax của 5 lớp.1const ort = require('onnxruntime-node');
2const sharp = require('sharp'); // Thư viện xử lý ảnh cho Node.js
3
4async function predict(imagePath) {
5 const session = await ort.InferenceSession.create('./convnext_inference.onnx');
6
7 // 1. Thực hiện Resize & Normalize ảnh (tương đương preprocessing.py)
8 // - Resize letterbox về 224x224
9 // - Chuyển sang Float32 và chuẩn hóa ImageNet: (pixel / 255.0 - mean) / std
10 // - Sắp xếp lại chiều thành NCHW [1, 3, 224, 224]
11 const inputTensor = new ort.Tensor('float32', float32Data, [1, 3, 224, 224]);
12
13 // 2. Chạy suy luận
14 const outputs = await session.run({ input_image: inputTensor });
15 const probabilities = outputs.probabilities.data;
16
17 console.log("Xác suất dự đoán:", probabilities);
18}onnxruntime-web chạy trên WebAssembly (WASM).onnxruntime-react-native hoặc onnxruntime_flutter.1import * as ort from 'onnxruntime-web';
2
3async function runOnClient(imageElement) {
4 // Tải mô hình trực tiếp từ thư mục public hoặc CDN
5 const session = await ort.InferenceSession.create('/models/convnext_inference.onnx');
6
7 // Thực hiện trích xuất dữ liệu pixel của ảnh từ thẻ <canvas> hoặc <img>
8 // Chuẩn hóa và đóng gói thành Float32Array [1, 3, 224, 224]
9 const tensor = new ort.Tensor('float32', Float32ArrayPixels, [1, 3, 224, 224]);
10
11 const results = await session.run({ input_image: tensor });
12 console.log("Kết quả chẩn đoán client-side:", results.probabilities.data);
13}