"""Inspect rec.onnx graph structure and model.safetensors weights. Answers: - exact input/output shapes and names of the recognizer graph - op sequence: conv attributes (strides, padding), activations, pooling - whether BN is folded into convs - parameter count from the safetensors and cross-check with ONNX initializers """ import json from pathlib import Path import numpy as np import onnx import safetensors.numpy MODEL_DIR = Path("/mnt/c/Users/Dell/Desktop/OCR_INDRA/models/indra-ocr-v3.1.1") def graph_summary(model): g = model.graph print("== INPUTS ==") for i in g.input: dims = [(d.dim_param or d.dim_value) for d in i.type.tensor_type.shape.dim] print(f" {i.name}: dims={dims} dtype={i.type.tensor_type.elem_type}") print("== OUTPUTS ==") for o in g.output: dims = [(d.dim_param or d.dim_value) for d in o.type.tensor_type.shape.dim] print(f" {o.name}: dims={dims} dtype={o.type.tensor_type.elem_type}") print(f"== {len(g.node)} nodes, {len(g.initializer)} initializers ==") ops = {} for n in g.node: ops[n.op_type] = ops.get(n.op_type, 0) + 1 print(" op counts:", dict(sorted(ops.items(), key=lambda x: -x[1]))) imp = model.opset_import for i in imp: print(f" opset domain={i.domain!r} version={i.version}") def shape_of(t): dims = getattr(t, "dims", None) or [] return list(dims) def initializer_map(model): # large models may be external; these are small return {t.name: onnx.numpy_helper.to_array(t) for t in model.graph.initializer} def conv_attrs(model): return { n.name: {a.name: onnx.helper.get_attribute_value(a) for a in n.attribute} for n in model.graph.node if n.op_type in ("Conv",) } def tensors_of(model, prefix=""): # ValueInfo for nursing intermediate shapes return model.graph def main(): print("### rec.onnx graph ###") m = onnx.load(str(MODEL_DIR / "rec.onnx")) graph_summary(m) # print the full node sequence with attrs for the main ops print("\n== node sequence (abbrev) ==") for k, n in enumerate(m.graph.node): attrs = {a.name: onnx.helper.get_attribute_value(a) for a in n.attribute} if n.op_type == "Constant": continue ins = ", ".join(n.input[:2]) outs = ", ".join(n.output[:1]) if k < 200 and n.op_type in ("Conv", "ConvTranspose", "BatchNormalization", "Relu", "LeakyRelu", "Sigmoid", "Multiply", "Add", "ReduceMean", "Div", "GlobalAveragePool", "AveragePool", "MaxPool", "Resize", "Squeeze", "Unsqueeze", "Concat", "Transpose", "Reshape", "Cast", "ArgMax", "ReduceMax", "ReduceArgMax", "Gather", "Shape", "Less", "Greater", "Where"): print(f" {k:3d} {n.op_type:22s} {outs} <- {ins} {dict(list(attrs.items())[:6])}") elif k < 3 or n.op_type not in ("Cast", "Shape", "Gather", "Concat", "Unsqueeze", "Squeeze", "Reshape", "Sub", "Mul", "Div", "Slice", "Constant"): pass # count up to 60 first 20 lines then finalize print("\n(total nodes:", len(m.graph.node), ")") def main2(): print("\n### conv attributes (kernel/dilation/pad only) ###") m = onnx.load(str(MODEL_DIR / "rec.onnx")) for n in m.graph.node: if n.op_type != "Conv": continue attrs = {a.name: onnx.helper.get_attribute_value(a) for a in n.attribute} w = attrs.get("kernel_shape") print(f" {n.name:20s} kernel={w} stride={attrs.get('strides')} " f"dil={attrs.get('dilations')} pad_mode={attrs.get('auto_pad')} " f"group={attrs.get('group', 1)} pads={attrs.get('pads')}") def main3(): print("\n### safetensors ###") with open(MODEL_DIR / "model.safetensors", "rb") as f: header_size = int.from_bytes(f.read(8), "little") header = json.loads(f.read(header_size)) tensors = {k: v for k, v in header.items() if k != "__metadata__"} print(f"{len(tensors)} tensors") total = 0 for name, info in list(tensors.items()): n = int(np.prod(info["shape"])) if info["shape"] else 1 total += n print(f" {name:44s} {str(info['shape']):24s} {info['dtype']:6s} {n}") print(f"{total} total parameters") def main4(): print("\n### crosscheck: rec.onnx initializer count/params ###") m = onnx.load(str(MODEL_DIR / "rec.onnx")) init = {t.name: onnx.numpy_helper.to_array(t) for t in m.graph.initializer} tot = sum(int(np.prod(a.shape)) if a.ndim else 1 for a in init.values()) print(f"{len(init)} initializers, {tot} parameters") # show the head and first block names: sizes = sorted( [(n, a.shape) for n, a in init.items() if a.ndim >= 1], key=lambda x: -int(np.prod(x[1])) if len(x[1]) else 0, )[:10] for n, s in sizes: print(f" big: {n:36s} {s}") def main5(): print("\n### other models: graph I/O ###") for f in ["det.onnx", "orient.onnx", "order.onnx", "script.onnx", "rec_cjk.onnx"]: m = onnx.load(str(MODEL_DIR / f)) print(f"\n{f}:") for i in m.graph.input: dims = [(d.dim_param or d.dim_value) for d in i.type.tensor_type.shape.dim] print(f" in {i.name}: dims={dims}") for o in m.graph.output: dims = [(d.dim_param or d.dim_value) for d in o.type.tensor_type.shape.dim] print(f" out {o.name}: dims={dims}") ops = {} for n in m.graph.node: ops[n.op_type] = ops.get(n.op_type, 0) + 1 print(" ops:", ops) if __name__ == "__main__": main() main2() main3() main4() main5()