Upload README_ONNX.md
Browse files- README_ONNX.md +93 -0
README_ONNX.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hướng dẫn sử dụng DeepPrint ONNX Model
|
| 2 |
+
|
| 3 |
+
File ONNX model này chỉ chứa **DeepPrint model** (không có LoFTR). LoFTR là bước tiền xử lý riêng biệt.
|
| 4 |
+
|
| 5 |
+
## File ONNX Model
|
| 6 |
+
|
| 7 |
+
- **Đường dẫn**: `onnx_models/deepprint_fvc_db1_loftr_final.onnx`
|
| 8 |
+
- **Input**: `input_image` - shape `[batch_size, 1, 299, 299]` (grayscale image)
|
| 9 |
+
- **Outputs**:
|
| 10 |
+
- `texture_embeddings`: shape `[batch_size, 256]`
|
| 11 |
+
- `minutia_embeddings`: shape `[batch_size, 256]`
|
| 12 |
+
|
| 13 |
+
## Cài đặt
|
| 14 |
+
|
| 15 |
+
```bash
|
| 16 |
+
pip install onnxruntime
|
| 17 |
+
```
|
| 18 |
+
|
| 19 |
+
## Cách sử dụng
|
| 20 |
+
|
| 21 |
+
### 1. Sử dụng script có sẵn
|
| 22 |
+
|
| 23 |
+
```bash
|
| 24 |
+
# Extract embedding từ 1 ảnh
|
| 25 |
+
python use_onnx_model.py --image1 path/to/fingerprint.tif
|
| 26 |
+
|
| 27 |
+
# So sánh 2 ảnh fingerprint
|
| 28 |
+
python use_onnx_model.py --image1 path/to/image1.tif --image2 path/to/image2.tif --threshold 0.5
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
### 2. Sử dụng trong code Python
|
| 32 |
+
|
| 33 |
+
```python
|
| 34 |
+
import numpy as np
|
| 35 |
+
import onnxruntime as ort
|
| 36 |
+
from PIL import Image
|
| 37 |
+
import torchvision.transforms.functional as VTF
|
| 38 |
+
import sys
|
| 39 |
+
|
| 40 |
+
# Add DeepPrint path
|
| 41 |
+
sys.path.append("deepprint/fixed-length-fingerprint-extractors")
|
| 42 |
+
from flx.image_processing.binarization import LazilyAllocatedBinarizer
|
| 43 |
+
from flx.data.image_helpers import pad_and_resize_to_deepprint_input_size
|
| 44 |
+
|
| 45 |
+
# Load ONNX model
|
| 46 |
+
onnx_model_path = "onnx_models/deepprint_fvc_db1_loftr_final.onnx"
|
| 47 |
+
session = ort.InferenceSession(onnx_model_path)
|
| 48 |
+
|
| 49 |
+
# Preprocess image
|
| 50 |
+
def preprocess_image(image_path):
|
| 51 |
+
img = Image.open(image_path).convert('L')
|
| 52 |
+
img_tensor = VTF.to_tensor(img)
|
| 53 |
+
|
| 54 |
+
# Binarize fingerprint
|
| 55 |
+
binarizer = LazilyAllocatedBinarizer(5.0)
|
| 56 |
+
img_tensor = binarizer(img_tensor)
|
| 57 |
+
|
| 58 |
+
# Pad to square and resize to 299x299
|
| 59 |
+
img_tensor = pad_and_resize_to_deepprint_input_size(img_tensor)
|
| 60 |
+
img_tensor = img_tensor.unsqueeze(0) # Add batch dimension
|
| 61 |
+
|
| 62 |
+
return img_tensor.numpy().astype(np.float32)
|
| 63 |
+
|
| 64 |
+
# Extract embeddings
|
| 65 |
+
input_tensor = preprocess_image("path/to/fingerprint.tif")
|
| 66 |
+
outputs = session.run(['texture_embeddings', 'minutia_embeddings'],
|
| 67 |
+
{'input_image': input_tensor})
|
| 68 |
+
|
| 69 |
+
texture_emb = outputs[0][0] # [256]
|
| 70 |
+
minutia_emb = outputs[1][0] # [256]
|
| 71 |
+
combined_emb = np.concatenate([texture_emb, minutia_emb]) # [512]
|
| 72 |
+
|
| 73 |
+
# Tính similarity giữa 2 embeddings
|
| 74 |
+
def cosine_similarity(emb1, emb2):
|
| 75 |
+
emb1_norm = emb1 / (np.linalg.norm(emb1) + 1e-8)
|
| 76 |
+
emb2_norm = emb2 / (np.linalg.norm(emb2) + 1e-8)
|
| 77 |
+
return np.dot(emb1_norm, emb2_norm)
|
| 78 |
+
|
| 79 |
+
similarity = cosine_similarity(combined_emb1, combined_emb2)
|
| 80 |
+
print(f"Similarity: {similarity:.6f}")
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
## Preprocessing Pipeline
|
| 84 |
+
|
| 85 |
+
Model yêu cầu preprocessing theo thứ tự sau:
|
| 86 |
+
|
| 87 |
+
1. **Load image**: PIL Image (grayscale)
|
| 88 |
+
2. **Convert to tensor**: `VTF.to_tensor()` → shape `[1, H, W]`
|
| 89 |
+
3. **Binarize**: `LazilyAllocatedBinarizer(5.0)` → enhance fingerprint ridges
|
| 90 |
+
4. **Pad & Resize**: `pad_and_resize_to_deepprint_input_size()` → `[1, 299, 299]`
|
| 91 |
+
5. **Add batch dimension**: `unsqueeze(0)` → `[1, 1, 299, 299]`
|
| 92 |
+
|
| 93 |
+
|