Files
product-detection/inspect_weights.py
T

53 lines
2.0 KiB
Python

"""Inspecte un fichier de poids .pt sans rien modifier : type, clés, formes, métadonnées.
Usage : python inspect_weights.py runs\\classify\\best.pt"""
import sys
import torch
path = sys.argv[1]
try:
ck = torch.load(path, map_location="cpu", weights_only=True)
print("Chargement sécurisé (weights_only=True) : OK")
except Exception as e:
print(f"weights_only=True a échoué ({type(e).__name__}) -> rechargement classique (fichier à toi)")
ck = torch.load(path, map_location="cpu", weights_only=False)
print("Type :", type(ck).__name__)
def is_state(d):
return isinstance(d, dict) and d and all(isinstance(v, torch.Tensor) for v in d.values())
def describe_state(d, indent=" "):
n_params = sum(v.numel() for v in d.values())
keys = list(d.keys())
print(f"{indent}{len(keys)} tenseurs, {n_params / 1e6:.2f} M paramètres")
for k in keys[:4]:
print(f"{indent} début : {k} {tuple(d[k].shape)}")
for k in keys[-4:]:
print(f"{indent} fin : {k} {tuple(d[k].shape)}")
heads = [k for k in keys if "classifier" in k or k.startswith("fc") or ".head" in k]
print(f"{indent}clés de tête (classifier/fc/head) : {heads[:6]}")
if is_state(ck):
print("C'est directement un state_dict :")
describe_state(ck)
elif isinstance(ck, dict):
print("Clés de premier niveau :", list(ck.keys()))
for k, v in ck.items():
if is_state(v):
print(f" [{k}] = state_dict :")
describe_state(v)
elif isinstance(v, (int, float, str, bool)) or v is None:
print(f" [{k}] = {v!r}")
elif isinstance(v, (list, tuple)):
print(f" [{k}] = liste de {len(v)} éléments : {list(v)[:8]}")
elif isinstance(v, dict):
print(f" [{k}] = dict de {len(v)} entrées : {list(v.items())[:6]}")
else:
print(f" [{k}] = {type(v).__name__}")
else:
print("Objet inattendu :", ck if isinstance(ck, (str, int, float)) else type(ck))