|
|
|
|
|
""" |
|
|
PyTorch ์ฒดํฌํฌ์ธํธ์์ ๊ฐ์ค์น๋ฅผ ์ถ์ถํ์ฌ binary ํ์์ผ๋ก ์ ์ฅ |
|
|
""" |
|
|
import torch |
|
|
import numpy as np |
|
|
import struct |
|
|
import sys |
|
|
from pathlib import Path |
|
|
|
|
|
def extract_weights(checkpoint_path, output_path): |
|
|
"""์ฒดํฌํฌ์ธํธ์์ ๊ฐ์ค์น ์ถ์ถ""" |
|
|
print(f"Loading checkpoint: {checkpoint_path}") |
|
|
checkpoint = torch.load(checkpoint_path, map_location='cpu') |
|
|
|
|
|
|
|
|
if 'model_state_dict' in checkpoint: |
|
|
state_dict = checkpoint['model_state_dict'] |
|
|
elif 'state_dict' in checkpoint: |
|
|
state_dict = checkpoint['state_dict'] |
|
|
else: |
|
|
state_dict = checkpoint |
|
|
|
|
|
print(f"Found {len(state_dict)} parameters") |
|
|
|
|
|
|
|
|
with open(output_path, 'wb') as f: |
|
|
|
|
|
f.write(b'LCNN') |
|
|
f.write(struct.pack('I', 1)) |
|
|
|
|
|
|
|
|
f.write(struct.pack('I', len(state_dict))) |
|
|
|
|
|
for name, param in state_dict.items(): |
|
|
print(f" {name}: {param.shape}") |
|
|
|
|
|
|
|
|
name_bytes = name.encode('utf-8')[:256] |
|
|
f.write(struct.pack('I', len(name_bytes))) |
|
|
f.write(name_bytes) |
|
|
|
|
|
|
|
|
data = param.cpu().numpy().astype(np.float32) |
|
|
|
|
|
|
|
|
ndim = len(data.shape) |
|
|
f.write(struct.pack('I', ndim)) |
|
|
for dim in data.shape: |
|
|
f.write(struct.pack('I', dim)) |
|
|
|
|
|
|
|
|
data_flat = data.flatten('C') |
|
|
f.write(struct.pack(f'{len(data_flat)}f', *data_flat)) |
|
|
|
|
|
print(f"\nWeights saved to: {output_path}") |
|
|
print(f"File size: {Path(output_path).stat().st_size / 1024 / 1024:.2f} MB") |
|
|
|
|
|
if __name__ == '__main__': |
|
|
checkpoint_path = sys.argv[1] if len(sys.argv) > 1 else '~/mycnn/checkpoints/LiteCNNPro_best.pth' |
|
|
output_path = sys.argv[2] if len(sys.argv) > 2 else './model_weights.bin' |
|
|
|
|
|
checkpoint_path = Path(checkpoint_path).expanduser() |
|
|
extract_weights(checkpoint_path, output_path) |
|
|
|