﻿# =========================================================
# Sensor CNN Train + Direct TFLite UINT8 Convert
# - .h5 저장/로드 없이 학습된 Keras model 객체에서 바로 TFLite 생성
# - BIN 파일 내부의 SET 단위 데이터를 각각 학습 샘플로 사용
# =========================================================

import os
from pathlib import Path

import numpy as np
import tensorflow as tf

from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.utils import to_categorical


# =========================================================
# 1. 기본 설정
# =========================================================

target_path = "C:/work/0000_연도별_업무/2026년/7_AIoT_센서_데이터_학습/Dataset/Dataset/data"

folders = {
    "normal": Path(target_path) / "new_normal_raw_data",
    "touch": Path(target_path) / "new_touch_raw_data",
}

class_names = ["normal", "touch"]
label_map = {name: idx for idx, name in enumerate(class_names)}

MODEL_DIR = Path("model")
MODEL_DIR.mkdir(parents=True, exist_ok=True)

TFLITE_PATH = MODEL_DIR / "converted_model_uint8.tflite"

GYRO_1AXIS_SIZE_OF_BYTE = 2
SAMPLE_SIZE = GYRO_1AXIS_SIZE_OF_BYTE * 3      # 6 bytes: x, y, z
SAMPLES_PER_SET = 52 * 8                       # 416 samples
SET_SIZE = SAMPLE_SIZE * SAMPLES_PER_SET       # 2496 bytes


# =========================================================
# 2. RAW BIN 로드
# =========================================================

def load_raw_bin_file(bin_file_path):
    """
    BIN 파일을 SET 단위로 분리해서 반환한다.
    return:
        list[np.ndarray]
        [
            shape=(416, 3),
            shape=(416, 3),
            ...
        ]
    """
    with open(bin_file_path, "rb") as f:
        data = f.read()

    sets = []

    for offset in range(0, len(data), SET_SIZE):
        chunk = data[offset:offset + SET_SIZE]

        if len(chunk) < SET_SIZE:
            # 마지막에 깨진/부족한 데이터가 있으면 버림
            continue

        samples = []

        for i in range(0, SET_SIZE, SAMPLE_SIZE):
            x = int.from_bytes(chunk[i:i+2], byteorder="little", signed=True)
            y = int.from_bytes(chunk[i+2:i+4], byteorder="little", signed=True)
            z = int.from_bytes(chunk[i+4:i+6], byteorder="little", signed=True)
            samples.append([x, y, z])

        sets.append(np.array(samples, dtype=np.float32))

    return sets


# =========================================================
# 3. 전처리 함수
# =========================================================

def complementary_filter(X_data, alpha=0.98, dt=0.01):
    """
    X_data: shape=(seq_len, 3)
    return: shape=(seq_len, 3)
    """
    filtered = np.zeros_like(X_data, dtype=np.float32)
    filtered[0] = X_data[0]

    for i in range(1, len(X_data)):
        gyro_component = filtered[i - 1] + X_data[i] * dt
        accel_component = X_data[i]
        filtered[i] = alpha * gyro_component + (1 - alpha) * accel_component

    return filtered


def process_sensor_array(X_data):
    """
    X_data: shape=(416, 3)

    CNN 입력 형태:
        shape=(3, 416, 1)
    """
    if X_data.ndim != 2 or X_data.shape[1] != 3:
        raise ValueError(f"Invalid sensor shape: {X_data.shape}, expected=(seq_len, 3)")

    # 1. 영점 보정
    X_offset = X_data - X_data[0]

    # 2. 절대값
    X_abs = np.abs(X_offset)

    # 3. 상보 필터
    X_comp = complementary_filter(X_abs)

    # 4. CNN 입력 형태 변환
    # 기존 코드의 X_comp.T[np.newaxis, :, :, np.newaxis]에서 batch 차원만 제외
    # 개별 샘플 shape = (3, seq_len, 1)
    X_cnn = X_comp.T[:, :, np.newaxis].astype(np.float32)

    return X_cnn


# =========================================================
# 4. 전체 데이터셋 생성
# =========================================================

def build_dataset():
    X_list = []
    y_list = []

    for cls_name, folder_path in folders.items():
        print(f"==== 데이터 로드: {cls_name} ====")

        bin_files = sorted(folder_path.glob("*.bin"))

        if not bin_files:
            print(f"[WARN] 폴더 내 .bin 파일 없음: {folder_path}")
            continue

        for bin_file in bin_files:
            raw_sets = load_raw_bin_file(bin_file)

            if not raw_sets:
                print(f"[WARN] 유효 SET 없음: {bin_file.name}")
                continue

            for raw_data in raw_sets:
                X_list.append(process_sensor_array(raw_data))
                y_list.append(label_map[cls_name])

    if not X_list:
        raise RuntimeError("학습 가능한 데이터가 없습니다. target_path/folders 경로를 확인하세요.")

    X_all = np.stack(X_list, axis=0).astype(np.float32)   # (N, 3, 416, 1)
    y_all_idx = np.array(y_list, dtype=np.int32)          # (N,)
    y_all = to_categorical(y_all_idx, num_classes=len(class_names))

    print("\n=== Dataset Info ===")
    print("X_all shape:", X_all.shape)
    print("y_all shape:", y_all.shape)
    print("class count:", {name: int(np.sum(y_all_idx == idx)) for name, idx in label_map.items()})

    return X_all, y_all, y_all_idx


X_all, y_all, y_all_idx = build_dataset()


# =========================================================
# 5. Train / Validation Split
# =========================================================

X_train, X_val, Y_train, Y_val = train_test_split(
    X_all,
    y_all,
    test_size=0.2,
    shuffle=True,
    random_state=42,
    stratify=y_all_idx
)

print("\n=== Split Info ===")
print("X_train:", X_train.shape)
print("X_val  :", X_val.shape)
print("Y_train:", Y_train.shape)
print("Y_val  :", Y_val.shape)


# =========================================================
# 6. 모델 생성
# =========================================================

model = Sequential([
    Conv2D(4, (2, 5), activation="relu", input_shape=(3, X_all.shape[2], 1)),
    MaxPooling2D(pool_size=(1, 2)),

    Conv2D(4, (2, 5), activation="relu"),
    MaxPooling2D(pool_size=(1, 2)),

    Flatten(),
    Dense(6, activation="relu"),
    Dense(len(class_names), activation="softmax")
])

model.compile(
    optimizer="adam",
    loss="categorical_crossentropy",
    metrics=["accuracy"]
)

model.summary()


# =========================================================
# 7. 학습
# =========================================================

history = model.fit(
    X_train,
    Y_train,
    batch_size=1,
    epochs=100,
    validation_data=(X_val, Y_val),
    verbose=1
)


# =========================================================
# 8. Keras 모델 평가
# =========================================================

print("\n=== Keras Model Validation Evaluation ===")

keras_prob = model.predict(X_val)
keras_pred = np.argmax(keras_prob, axis=1)
keras_true = np.argmax(Y_val, axis=1)

print("Keras accuracy:", accuracy_score(keras_true, keras_pred))
print(classification_report(keras_true, keras_pred, target_names=class_names))


# =========================================================
# 9. Representative Dataset
# =========================================================

def representative_dataset():
    """
    INT8/UINT8 양자화용 대표 데이터셋.
    학습 입력과 동일하게 float32, shape=(1, 3, 416, 1)로 yield 한다.
    과적합이 보이면 적게 
    """
    max_samples = min(100, len(X_train))

    for i in range(max_samples):
        x = X_train[i:i+1].astype(np.float32)
        yield [x]


# =========================================================
# 10. .h5 저장 없이 직접 TFLite UINT8 변환
# =========================================================

converter = tf.lite.TFLiteConverter.from_keras_model(model)

converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset

converter.target_spec.supported_ops = [
    tf.lite.OpsSet.TFLITE_BUILTINS_INT8
]

# 입력/출력 UINT8
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8

tflite_quant_model = converter.convert()

with open(TFLITE_PATH, "wb") as f:
    f.write(tflite_quant_model)

print(f"\nTFLite model saved: {TFLITE_PATH}")


# =========================================================
# 11. TFLite 모델 정보 확인
# =========================================================

interpreter = tf.lite.Interpreter(model_content=tflite_quant_model)
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

inp = input_details[0]
out = output_details[0]

print("\n=== TFLite Tensor Details ===")
print("input details :", inp)
print("output details:", out)

print("input quant :", inp["quantization"])
print("output quant:", out["quantization"])


# =========================================================
# 12. TFLite 추론용 Quantize 함수
# =========================================================

def quantize_input_float_to_uint8(x_float, input_detail):
    """
    x_float: float32, shape=(1, 3, 416, 1)
    """
    scale, zero_point = input_detail["quantization"]

    if scale == 0:
        raise ValueError("Input quantization scale is 0")

    x_q = np.round(x_float / scale + zero_point)
    x_q = np.clip(x_q, 0, 255).astype(np.uint8)

    return x_q


def dequantize_output_uint8_to_float(y_uint8, output_detail):
    scale, zero_point = output_detail["quantization"]

    if scale == 0:
        return y_uint8

    return (y_uint8.astype(np.float32) - zero_point) * scale


# =========================================================
# 13. TFLite 모델 평가
# =========================================================

print("\n=== TFLite Model Validation Evaluation ===")

all_true = []
all_pred = []

for i in range(len(X_val)):
    x_float = X_val[i:i+1].astype(np.float32)

    if input_details[0]["dtype"] == np.uint8:
        x_input = quantize_input_float_to_uint8(x_float, input_details[0])
    else:
        x_input = x_float.astype(input_details[0]["dtype"])

    interpreter.set_tensor(input_details[0]["index"], x_input)
    interpreter.invoke()

    output_data = interpreter.get_tensor(output_details[0]["index"])

    # argmax만 볼 거면 dequantize 안 해도 되지만, 확인용으로 남김
    if output_details[0]["dtype"] == np.uint8:
        output_float = dequantize_output_uint8_to_float(output_data, output_details[0])
    else:
        output_float = output_data

    pred_label_idx = int(np.argmax(output_float, axis=1)[0])
    true_label_idx = int(np.argmax(Y_val[i]))

    all_true.append(true_label_idx)
    all_pred.append(pred_label_idx)

print("TFLite accuracy:", accuracy_score(all_true, all_pred))
print(classification_report(all_true, all_pred, target_names=class_names))
print("confusion matrix:")
print(confusion_matrix(all_true, all_pred))


# =========================================================
# 14. 전체 BIN 파일 기준 TFLite 추론 결과 출력
# =========================================================

print("\n=== Full BIN File TFLite Inference ===")

file_true = []
file_pred = []

for cls_name, folder_path in folders.items():
    print(f"\n==== 클래스 '{cls_name}' 처리 시작 ====")

    bin_files = sorted(folder_path.glob("*.bin"))

    if not bin_files:
        print(f"폴더 내 .bin 파일이 없습니다: {folder_path}")
        continue

    for bin_file in bin_files:
        raw_sets = load_raw_bin_file(bin_file)

        if not raw_sets:
            print(f"[WARN] 유효 SET 없음: {bin_file.name}")
            continue

        set_preds = []

        for raw_data in raw_sets:
            x_float = process_sensor_array(raw_data)[np.newaxis, ...].astype(np.float32)

            if input_details[0]["dtype"] == np.uint8:
                x_input = quantize_input_float_to_uint8(x_float, input_details[0])
            else:
                x_input = x_float.astype(input_details[0]["dtype"])

            interpreter.set_tensor(input_details[0]["index"], x_input)
            interpreter.invoke()

            output_data = interpreter.get_tensor(output_details[0]["index"])

            if output_details[0]["dtype"] == np.uint8:
                output_float = dequantize_output_uint8_to_float(output_data, output_details[0])
            else:
                output_float = output_data

            pred_idx = int(np.argmax(output_float, axis=1)[0])
            set_preds.append(pred_idx)

        # BIN 파일에 SET이 여러 개 있는 경우 다수결
        final_pred_idx = max(set(set_preds), key=set_preds.count)
        final_pred_label = class_names[final_pred_idx]

        file_true.append(label_map[cls_name])
        file_pred.append(final_pred_idx)

        print(f"파일: {bin_file.name} -> 예측: {final_pred_label}, SET별 예측: {[class_names[i] for i in set_preds]}")

print("\n=== 전체 BIN 파일 기준 결과 평가 ===")
print("정확도:", accuracy_score(file_true, file_pred))
print(classification_report(file_true, file_pred, target_names=class_names))
print("confusion matrix:")
print(confusion_matrix(file_true, file_pred))
