File size: 2,267 Bytes
17124c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Export an inference-only float16 TFLite model and verify parity."""

import argparse
import json
from pathlib import Path

import numpy as np

from src.common import configure


def main():
    p = argparse.ArgumentParser()
    p.add_argument("--model-dir", default="artifacts/release")
    args = p.parse_args()
    configure()
    import tensorflow as tf

    out = Path(args.model_dir)
    model = tf.keras.models.load_model(out / "model.keras", compile=False)
    # Rebuild graph with training=False so augmentation/dropout never enter inference graph.
    inputs = tf.keras.Input((64, 64, 3), batch_size=1)
    inference = tf.keras.Model(inputs, model(inputs, training=False))
    converter = tf.lite.TFLiteConverter.from_keras_model(inference)
    converter.optimizations = [tf.lite.Optimize.DEFAULT]
    converter.target_spec.supported_types = [tf.float16]
    data = converter.convert()
    (out / "model.float16.tflite").write_bytes(data)
    interpreter = tf.lite.Interpreter(model_content=data)
    interpreter.allocate_tensors()
    from src.dataset import load_data

    cfg = json.loads((out / "training_config.json").read_text())
    splits, _ = load_data(cfg["data_dir"], cfg["seed"])
    samples = splits["test"][0][:16].astype("float32")
    errors = []
    for image in samples:
        x = image[None]
        interpreter.set_tensor(interpreter.get_input_details()[0]["index"], x)
        interpreter.invoke()
        actual = interpreter.get_tensor(interpreter.get_output_details()[0]["index"])
        expected = np.asarray(inference(x, training=False))
        errors.append(float(np.max(np.abs(actual - expected))))
        if not np.allclose(actual, expected, atol=0.005):
            raise RuntimeError(f"TFLite parity failed: {errors[-1]}")
    delta = max(errors)
    (out / "export.json").write_text(
        json.dumps(
            {
                "format": "TFLite float16",
                "bytes": len(data),
                "max_absolute_error": delta,
                "parity_input": "first 16 held-out test patches",
                "samples": len(samples),
            },
            indent=2,
        )
    )
    print("TFLite export verified:", len(data), "bytes; max error", delta)


if __name__ == "__main__":
    main()