DeepPrint_preprocessed / README_ONNX.md
LamTNguyen's picture
Upload README_ONNX.md
a379098 verified
# Hướng dẫn sử dụng DeepPrint ONNX Model
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.
## File ONNX Model
- **Đường dẫn**: `onnx_models/deepprint_fvc_db1_loftr_final.onnx`
- **Input**: `input_image` - shape `[batch_size, 1, 299, 299]` (grayscale image)
- **Outputs**:
- `texture_embeddings`: shape `[batch_size, 256]`
- `minutia_embeddings`: shape `[batch_size, 256]`
## Cài đặt
```bash
pip install onnxruntime
```
## Cách sử dụng
### 1. Sử dụng script có sẵn
```bash
# Extract embedding từ 1 ảnh
python use_onnx_model.py --image1 path/to/fingerprint.tif
# So sánh 2 ảnh fingerprint
python use_onnx_model.py --image1 path/to/image1.tif --image2 path/to/image2.tif --threshold 0.5
```
### 2. Sử dụng trong code Python
```python
import numpy as np
import onnxruntime as ort
from PIL import Image
import torchvision.transforms.functional as VTF
import sys
# Add DeepPrint path
sys.path.append("deepprint/fixed-length-fingerprint-extractors")
from flx.image_processing.binarization import LazilyAllocatedBinarizer
from flx.data.image_helpers import pad_and_resize_to_deepprint_input_size
# Load ONNX model
onnx_model_path = "onnx_models/deepprint_fvc_db1_loftr_final.onnx"
session = ort.InferenceSession(onnx_model_path)
# Preprocess image
def preprocess_image(image_path):
img = Image.open(image_path).convert('L')
img_tensor = VTF.to_tensor(img)
# Binarize fingerprint
binarizer = LazilyAllocatedBinarizer(5.0)
img_tensor = binarizer(img_tensor)
# Pad to square and resize to 299x299
img_tensor = pad_and_resize_to_deepprint_input_size(img_tensor)
img_tensor = img_tensor.unsqueeze(0) # Add batch dimension
return img_tensor.numpy().astype(np.float32)
# Extract embeddings
input_tensor = preprocess_image("path/to/fingerprint.tif")
outputs = session.run(['texture_embeddings', 'minutia_embeddings'],
{'input_image': input_tensor})
texture_emb = outputs[0][0] # [256]
minutia_emb = outputs[1][0] # [256]
combined_emb = np.concatenate([texture_emb, minutia_emb]) # [512]
# Tính similarity giữa 2 embeddings
def cosine_similarity(emb1, emb2):
emb1_norm = emb1 / (np.linalg.norm(emb1) + 1e-8)
emb2_norm = emb2 / (np.linalg.norm(emb2) + 1e-8)
return np.dot(emb1_norm, emb2_norm)
similarity = cosine_similarity(combined_emb1, combined_emb2)
print(f"Similarity: {similarity:.6f}")
```
## Preprocessing Pipeline
Model yêu cầu preprocessing theo thứ tự sau:
1. **Load image**: PIL Image (grayscale)
2. **Convert to tensor**: `VTF.to_tensor()` → shape `[1, H, W]`
3. **Binarize**: `LazilyAllocatedBinarizer(5.0)` → enhance fingerprint ridges
4. **Pad & Resize**: `pad_and_resize_to_deepprint_input_size()``[1, 299, 299]`
5. **Add batch dimension**: `unsqueeze(0)``[1, 1, 299, 299]`