Add : model conversion script
Browse files
wechat_qr/model_conversion.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import sys
|
| 3 |
+
import os
|
| 4 |
+
import argparse
|
| 5 |
+
import hashlib
|
| 6 |
+
|
| 7 |
+
# Only use sys.path if caffe2onnx isn't installed in your pip/conda env
|
| 8 |
+
sys.path.insert(0, "/path/to/cloned/caffe2onnx")
|
| 9 |
+
|
| 10 |
+
def compute_sha256(filepath):
|
| 11 |
+
"""Compute SHA-256 hash of a file for integrity verification."""
|
| 12 |
+
sha256 = hashlib.sha256()
|
| 13 |
+
with open(filepath, "rb") as f:
|
| 14 |
+
for chunk in iter(lambda: f.read(8192), b""):
|
| 15 |
+
sha256.update(chunk)
|
| 16 |
+
return sha256.hexdigest()
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def convert_caffe_to_onnx(prototxt, caffemodel, onnx_path):
|
| 20 |
+
"""
|
| 21 |
+
Convert a single Caffe model (.prototxt + .caffemodel) to ONNX.
|
| 22 |
+
|
| 23 |
+
Parameters
|
| 24 |
+
----------
|
| 25 |
+
prototxt : str — path to Caffe .prototxt (network architecture)
|
| 26 |
+
caffemodel : str — path to Caffe .caffemodel (weights)
|
| 27 |
+
onnx_path : str — output .onnx file path
|
| 28 |
+
"""
|
| 29 |
+
from caffe2onnx.src.load_save_model import loadcaffemodel, saveonnxmodel
|
| 30 |
+
from caffe2onnx.src.caffe2onnx import Caffe2Onnx
|
| 31 |
+
|
| 32 |
+
assert os.path.isfile(prototxt), "Prototxt not found: {}".format(prototxt)
|
| 33 |
+
assert os.path.isfile(caffemodel), "Caffemodel not found: {}".format(caffemodel)
|
| 34 |
+
|
| 35 |
+
print("Converting: {} -> {}".format(prototxt, onnx_path))
|
| 36 |
+
|
| 37 |
+
graph, params = loadcaffemodel(prototxt, caffemodel)
|
| 38 |
+
converter = Caffe2Onnx(graph, params, onnx_path)
|
| 39 |
+
onnx_model = converter.createOnnxModel()
|
| 40 |
+
|
| 41 |
+
saveonnxmodel(onnx_model, onnx_path)
|
| 42 |
+
|
| 43 |
+
sha = compute_sha256(onnx_path)
|
| 44 |
+
print(" Saved: {} (SHA-256: {})".format(onnx_path, sha))
|
| 45 |
+
return sha
|
| 46 |
+
|
| 47 |
+
MODELS = {
|
| 48 |
+
"detect": ("dnn-models/dnn/wechat_2021-01/detect.prototxt", "dnn-models/dnn/wechat_2021-01/detect.caffemodel"),
|
| 49 |
+
"sr": ("dnn-models/dnn/wechat_2021-01/sr.prototxt", "dnn-models/dnn/wechat_2021-01/sr.caffemodel"),
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
parser = argparse.ArgumentParser(
|
| 55 |
+
description="Convert WeChatQR Caffe models to ONNX.")
|
| 56 |
+
parser.add_argument("--input_dir", default=".",
|
| 57 |
+
help="Directory with Caffe model files (default: cwd)")
|
| 58 |
+
parser.add_argument("--output_dir", default=".",
|
| 59 |
+
help="Directory for output ONNX files (default: cwd)")
|
| 60 |
+
args = parser.parse_args()
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
from caffe2onnx.src.load_save_model import loadcaffemodel, saveonnxmodel
|
| 64 |
+
from caffe2onnx.src.caffe2onnx import Caffe2Onnx
|
| 65 |
+
except ImportError:
|
| 66 |
+
print("Error: caffe2onnx is not installed.")
|
| 67 |
+
print(" pip install caffe2onnx")
|
| 68 |
+
sys.exit(1)
|
| 69 |
+
|
| 70 |
+
os.makedirs(args.output_dir, exist_ok=True)
|
| 71 |
+
|
| 72 |
+
print("=" * 60)
|
| 73 |
+
print("WeChatQR Caffe -> ONNX Conversion")
|
| 74 |
+
print(" Input : {}".format(args.input_dir))
|
| 75 |
+
print(" Output : {}".format(args.output_dir))
|
| 76 |
+
print("=" * 60)
|
| 77 |
+
|
| 78 |
+
for name, (proto_file, caffe_file) in MODELS.items():
|
| 79 |
+
proto_path = os.path.join(args.input_dir, proto_file)
|
| 80 |
+
caffe_path = os.path.join(args.input_dir, caffe_file)
|
| 81 |
+
onnx_path = os.path.join(args.output_dir, name + "_2026april.onnx.onnx")
|
| 82 |
+
|
| 83 |
+
convert_caffe_to_onnx(proto_path, caffe_path, onnx_path)
|
| 84 |
+
|
| 85 |
+
print("=" * 60)
|
| 86 |
+
print("Done.")
|