""" Export TinyCNN PyTorch weights and a test image to HLS headers. Default command: python hls_cnn/export_weights.py --model data/models/tiny_cnn.pth --test_image data/processed/val/class_0/10.jpg """ import argparse import os import sys import numpy as np import torch from PIL import Image def configure_stdio(): for stream in (sys.stdout, sys.stderr): if hasattr(stream, "reconfigure"): stream.reconfigure(encoding="utf-8", errors="replace") configure_stdio() SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) PROJECT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, "..")) PC_TRAINING = os.path.join(PROJECT_ROOT, "pc_training") if PC_TRAINING not in sys.path: sys.path.insert(0, PC_TRAINING) from tiny_cnn import create_tiny_cnn # noqa: E402 WEIGHT_SCALE = 1024 LOGIT_SCALE = 1024 def quantize_array(array, scale=WEIGHT_SCALE): quantized = np.round(array * scale) quantized = np.clip(quantized, -32768, 32767) return quantized.astype(np.int16) def nested_initializer(array, indent=0, line_width=12): space = " " * indent if array.ndim == 1: values = [str(int(value)) for value in array] chunks = [] for idx in range(0, len(values), line_width): chunks.append(", ".join(values[idx:idx + line_width])) if len(chunks) == 1: return "{ " + chunks[0] + " }" inner = (",\n" + " " * (indent + 4)).join(chunks) return "{\n" + " " * (indent + 4) + inner + "\n" + space + "}" parts = [ nested_initializer(array[idx], indent=indent + 4, line_width=line_width) for idx in range(array.shape[0]) ] return "{\n" + " " * (indent + 4) + (",\n" + " " * (indent + 4)).join(parts) + "\n" + space + "}" def c_array(name, array, c_type="const short", line_width=12): dims = "".join(f"[{dim}]" for dim in array.shape) return f"{c_type} {name}{dims} = {nested_initializer(array, indent=0, line_width=line_width)};" def load_model(model_path, num_classes=10): checkpoint = torch.load(model_path, map_location="cpu") model = create_tiny_cnn(num_classes=num_classes) state_dict = checkpoint.get("model_state_dict", checkpoint) model.load_state_dict(state_dict) model.eval() return model, checkpoint def preprocess_image(image_path, input_size=64): image = Image.open(image_path).convert("L") image = image.resize((input_size, input_size)) return np.asarray(image, dtype=np.uint8) def export_weights(model, output_header): state = model.state_dict() arrays = { "conv1_w": quantize_array(state["features.0.weight"].numpy()), "conv1_b": quantize_array(state["features.0.bias"].numpy()), "conv2_w": quantize_array(state["features.3.weight"].numpy()), "conv2_b": quantize_array(state["features.3.bias"].numpy()), "conv3_w": quantize_array(state["features.6.weight"].numpy()), "conv3_b": quantize_array(state["features.6.bias"].numpy()), "fc_w": quantize_array(state["classifier.weight"].numpy()), "fc_b": quantize_array(state["classifier.bias"].numpy()), } lines = [ "#ifndef TINY_CNN_WEIGHTS_H", "#define TINY_CNN_WEIGHTS_H", "", "// Auto-generated from TinyCNN PyTorch checkpoint.", f"#define TINY_CNN_WEIGHT_SCALE {WEIGHT_SCALE}", f"#define TINY_CNN_LOGIT_SCALE {LOGIT_SCALE}", "", ] for name, array in arrays.items(): lines.append(c_array(name, array)) lines.append("") lines.append("#endif") os.makedirs(os.path.dirname(os.path.abspath(output_header)), exist_ok=True) with open(output_header, "w", encoding="utf-8", newline="\n") as file: file.write("\n".join(lines)) print(f"Wrote weights: {output_header}") def export_test_image(image_path, output_header, input_size=64): pixels = preprocess_image(image_path, input_size=input_size) lines = [ "#ifndef TINY_CNN_TEST_IMAGE_H", "#define TINY_CNN_TEST_IMAGE_H", "", f"#define TINY_CNN_TEST_IMAGE_PATH \"{image_path.replace(os.sep, '/')}\"", c_array("test_image_u8", pixels, c_type="const unsigned char", line_width=16), "", "#endif", ] os.makedirs(os.path.dirname(os.path.abspath(output_header)), exist_ok=True) with open(output_header, "w", encoding="utf-8", newline="\n") as file: file.write("\n".join(lines)) print(f"Wrote test image: {output_header}") def main(): parser = argparse.ArgumentParser(description="Export TinyCNN weights for HLS") parser.add_argument("--model", default="data/models/tiny_cnn.pth") parser.add_argument("--test_image", default="data/processed/val/class_0/10.jpg") parser.add_argument("--weights_header", default="hls_cnn/include/tiny_cnn_weights.h") parser.add_argument("--test_header", default="hls_cnn/include/tiny_cnn_test_image.h") parser.add_argument("--input_size", type=int, default=64) parser.add_argument("--num_classes", type=int, default=10) args = parser.parse_args() model_path = args.model if not os.path.isabs(model_path): model_path = os.path.join(PROJECT_ROOT, model_path) test_image = args.test_image if not os.path.isabs(test_image): test_image = os.path.join(PROJECT_ROOT, test_image) weights_header = args.weights_header if not os.path.isabs(weights_header): weights_header = os.path.join(PROJECT_ROOT, weights_header) test_header = args.test_header if not os.path.isabs(test_header): test_header = os.path.join(PROJECT_ROOT, test_header) model, checkpoint = load_model(model_path, num_classes=args.num_classes) print(f"Loaded {model_path}") print(f"Checkpoint best_acc: {checkpoint.get('best_acc', 'unknown')}") export_weights(model, weights_header) export_test_image(test_image, test_header, input_size=args.input_size) if __name__ == "__main__": main()