计算权重文件参数量和大小

权重文件参数量大小

import torch
from ultralytics import YOLO
model = YOLO()  
checkpoint = torch.load("/share1/luli/yolov8/runs/detect/train20/weights/best.pt")

# 获取模型参数
model_state_dict = checkpoint['model'].state_dict()

# 统计模型参数数量
total_params = sum(p.numel() for p in model_state_dict.values())
print(f"Total parameters: {total_params}")

# Total parameters: 25890096

权重文件大小

import os

# 定义文件路径
file_path = "/share1/luli/yolov8/runs/detect/train20/weights/best.pt"

# 获取文件大小(以字节为单位)
file_size_bytes = os.path.getsize(file_path)
print(file_size_bytes)

# 转换为更方便阅读的单位(例如,KB、MB)
file_size_kb = file_size_bytes / 1024
file_size_mb = file_size_kb / 1024

print(f"File size: {file_size_bytes} bytes ({file_size_kb:.2f} KB, {file_size_mb:.2f} MB)")




你可能感兴趣的:(实习记录,python)