add gitigore
Browse files- .gitignore +1 -0
- test_save.py +0 -114
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
test_save.py
|
test_save.py
DELETED
@@ -1,114 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import numpy as np
|
3 |
-
import torch
|
4 |
-
from pathlib import Path
|
5 |
-
|
6 |
-
def test_saved_files(content_path, num_epochs=3):
|
7 |
-
"""测试保存的文件是否符合要求
|
8 |
-
|
9 |
-
Args:
|
10 |
-
content_path: 训练过程的根目录
|
11 |
-
num_epochs: 要测试的epoch数量
|
12 |
-
"""
|
13 |
-
# 检查目录结构
|
14 |
-
required_dirs = [
|
15 |
-
'model',
|
16 |
-
'dataset/representation',
|
17 |
-
'dataset/prediction',
|
18 |
-
'dataset/label'
|
19 |
-
]
|
20 |
-
|
21 |
-
for dir_path in required_dirs:
|
22 |
-
full_path = os.path.join(content_path, dir_path)
|
23 |
-
if not os.path.exists(full_path):
|
24 |
-
print(f"错误: 目录不存在: {full_path}")
|
25 |
-
return False
|
26 |
-
|
27 |
-
# 检查模型文件
|
28 |
-
print("\n检查模型文件...")
|
29 |
-
for epoch in range(1, num_epochs + 1):
|
30 |
-
model_path = os.path.join(content_path, 'model', f'{epoch}.pth')
|
31 |
-
if not os.path.exists(model_path):
|
32 |
-
print(f"错误: 模型文件不存在: {model_path}")
|
33 |
-
return False
|
34 |
-
try:
|
35 |
-
# 尝试加载模型文件以验证其有效性
|
36 |
-
state_dict = torch.load(model_path, map_location='cpu')
|
37 |
-
print(f"✓ {epoch}.pth 格式正确")
|
38 |
-
except Exception as e:
|
39 |
-
print(f"错误: 无法加载模型文件 {model_path}: {str(e)}")
|
40 |
-
return False
|
41 |
-
|
42 |
-
# 检查特征向量文件
|
43 |
-
print("\n检查特征向量文件...")
|
44 |
-
prev_samples = None
|
45 |
-
for epoch in range(1, num_epochs + 1):
|
46 |
-
repr_path = os.path.join(content_path, 'dataset', 'representation', f'{epoch}.npy')
|
47 |
-
if not os.path.exists(repr_path):
|
48 |
-
print(f"错误: 特征向量文件不存在: {repr_path}")
|
49 |
-
return False
|
50 |
-
try:
|
51 |
-
features = np.load(repr_path)
|
52 |
-
samples, dim = features.shape
|
53 |
-
if not (512 <= dim <= 1024):
|
54 |
-
print(f"警告: 特征维度 {dim} 不在预期范围[512, 1024]内")
|
55 |
-
if prev_samples is not None and samples != prev_samples:
|
56 |
-
print(f"错误: epoch {epoch} 的样本数量与之前不一致")
|
57 |
-
return False
|
58 |
-
prev_samples = samples
|
59 |
-
print(f"✓ {epoch}.npy 格式正确 [样本数: {samples}, 特征维度: {dim}]")
|
60 |
-
except Exception as e:
|
61 |
-
print(f"错误: 无法加载特征向量文件 {repr_path}: {str(e)}")
|
62 |
-
return False
|
63 |
-
|
64 |
-
# 检查预测结果文件
|
65 |
-
print("\n检查预测结果文件...")
|
66 |
-
for epoch in range(1, num_epochs + 1):
|
67 |
-
pred_path = os.path.join(content_path, 'dataset', 'prediction', f'{epoch}.npy')
|
68 |
-
if not os.path.exists(pred_path):
|
69 |
-
print(f"错误: 预测结果文件不存在: {pred_path}")
|
70 |
-
return False
|
71 |
-
try:
|
72 |
-
predictions = np.load(pred_path)
|
73 |
-
samples, classes = predictions.shape
|
74 |
-
if samples != prev_samples:
|
75 |
-
print(f"错误: 预测结果的样本数量与特征向量不一致")
|
76 |
-
return False
|
77 |
-
if classes != 10: # CIFAR-10 有10个类别
|
78 |
-
print(f"警告: 类别数量 {classes} 不等于10")
|
79 |
-
print(f"✓ {epoch}.npy 格式正确 [样本数: {samples}, 类别数: {classes}]")
|
80 |
-
except Exception as e:
|
81 |
-
print(f"错误: 无法加载预测结果文件 {pred_path}: {str(e)}")
|
82 |
-
return False
|
83 |
-
|
84 |
-
# 检查标签文件
|
85 |
-
print("\n检查标签文件...")
|
86 |
-
label_path = os.path.join(content_path, 'dataset', 'label', 'labels.npy')
|
87 |
-
if not os.path.exists(label_path):
|
88 |
-
print(f"错误: 标签文件不存在: {label_path}")
|
89 |
-
return False
|
90 |
-
try:
|
91 |
-
labels = np.load(label_path)
|
92 |
-
if len(labels.shape) != 1:
|
93 |
-
print(f"错误: 标签文件维度不正确,应为1维数组,实际为{len(labels.shape)}维")
|
94 |
-
return False
|
95 |
-
if labels.shape[0] != prev_samples:
|
96 |
-
print(f"错误: 标签数量与样本数量不一致")
|
97 |
-
return False
|
98 |
-
if not np.all((labels >= 0) & (labels < 10)): # CIFAR-10 的标签范围是0-9
|
99 |
-
print("错误: 存在超出范围的标签值")
|
100 |
-
return False
|
101 |
-
print(f"✓ labels.npy 格式正确 [样本数: {labels.shape[0]}]")
|
102 |
-
except Exception as e:
|
103 |
-
print(f"错误: 无法加载标签文件 {label_path}: {str(e)}")
|
104 |
-
return False
|
105 |
-
|
106 |
-
print("\n✓ 所有文件格式检查通过!")
|
107 |
-
return True
|
108 |
-
|
109 |
-
if __name__ == "__main__":
|
110 |
-
# 设置要测试的目录路径
|
111 |
-
content_path = "/home/ruofei/RRF/ttvnet/Image/AlexNet/model/0"
|
112 |
-
|
113 |
-
# 运行测试
|
114 |
-
test_saved_files(content_path, num_epochs=44)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|