|
"""ttv_utils包提供了用于特征预测 和 模型训练过程收集 的工具函数 |
|
|
|
主要组件: |
|
1. FeaturePredictor: 用于从模型中间层特征向量预测预测结果的类 |
|
使用示例: |
|
```python |
|
predictor = FeaturePredictor( |
|
model_class=model, 模型类 |
|
model_weights_path=weight_path,模型权重文件路径 |
|
layer_info_path=layer_info_path,层信息文件路径 |
|
device=device 运行设备 |
|
) |
|
``` |
|
|
|
2. predict_feature: 从模型中间层特征向量预测预测结果便捷函数 |
|
使用示例: |
|
```python |
|
output = predict_feature( |
|
model=model, # 模型类 |
|
weight_path=weight_path, # 模型权重文件路径 |
|
layer_info_path=layer_info_path, # 层信息文件路径 |
|
feature=feature, # 特征向量 |
|
device=device # 运行设备 |
|
) |
|
``` |
|
|
|
3. time_travel_saver: 用于在训练过程中保存模型权重、特征和预测结果的类 |
|
使用示例: |
|
```python |
|
# 创建一个保存器实例 |
|
saver = time_travel_saver( |
|
model=model, # 模型实例 |
|
dataloader=ordered_loader, # 顺序数据加载器 |
|
device='cuda:0', # 计算设备 |
|
save_dir='./checkpoints', # 保存根目录 |
|
model_name='alexnet', # 模型名称 |
|
interval=1 # 每隔多少个epoch保存一次 |
|
) |
|
|
|
# 在训练循环中调用save方法 |
|
for epoch in range(epochs): |
|
# 训练代码... |
|
if epoch % interval == 0: |
|
saver.save(model) # 保存当前epoch的模型状态 |
|
``` |
|
|
|
保存的文件结构: |
|
- model/{epoch}.pth: 模型权重 |
|
- dataset/representation/{epoch}.npy: 特征向量 |
|
- dataset/prediction/{epoch}.npy: 预测结果 |
|
- dataset/label/labels.npy: 标签 |
|
""" |
|
|
|
from .feature_predictor import FeaturePredictor, predict_feature |
|
from .save_embeddings import time_travel_saver |
|
|
|
__all__ = ['FeaturePredictor', 'predict_feature', 'time_travel_saver'] |
|
|