45 lines
1.6 KiB
Python

import numpy as np
import json
json_file_path = './knn-model.json'
bin_file_path = './knn-model.bin'
with open(json_file_path, 'r') as f:
model_data = json.load(f)
with open(bin_file_path, 'rb') as f:
binary_full_data = f.read()
feature_dim = model_data['featureDim']
print(f"Feature Dimension: {feature_dim}")
for label, meta in model_data['dataset'].items():
start_byte = meta['start']
length_byte = meta['length']
print(f"\n--- Class: {label} ---")
print(f"Start Byte: {start_byte}")
print(f"Length Byte: {length_byte}")
# 提取当前类别的数据片段
class_binary_data = binary_full_data[start_byte : start_byte + length_byte]
# 转换为 Float32 数组
try:
class_features_elements = np.frombuffer(class_binary_data, dtype=np.float32)
num_elements = len(class_features_elements)
print(f"Float32 elements extracted: {num_elements}")
if num_elements % feature_dim == 0:
num_samples = num_elements // feature_dim
print(f"SUCCESS: Aligned. Number of samples: {num_samples}")
else:
print(f"ERROR: Not aligned. {num_elements} elements / {feature_dim} dim = {num_elements / feature_dim} (not an integer).")
print(f"Expected length in bytes to be multiple of {feature_dim * 4} = {feature_dim * 4} bytes.")
print(f"Actual length in bytes: {length_byte}. Remainder when dividing by {feature_dim * 4}: {length_byte % (feature_dim * 4)}")
except Exception as e:
print(f"Error processing binary data for class {label}: {e}")