-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
341 lines (274 loc) · 10.3 KB
/
Copy pathutils.py
File metadata and controls
341 lines (274 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision import datasets
from tqdm import tqdm
import numpy as np
from sklearn.metrics import confusion_matrix
# from color_transforms import apply_color_transform, apply_random_color
from pathlib import Path
import datetime
import logging
import argparse
import matplotlib.pyplot as plt
import seaborn as sns
from typing import Tuple
import os
import glob
from collections import defaultdict
from typing import Dict, Tuple
import models
# from opts import OPT as opt
import random
def set_random_seed(seed: int) -> None:
"""Set random seed for reproducibility across all libraries"""
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # for multi-GPU
torch.backends.cudnn.enabled = True
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(seed)
# seed the python.
os.environ["PYTHONSEED"] = str(seed)
# seed everything about the program.
random.seed(seed)
def create_run_dir(args, method_names) -> Path:
"""Create a uniquely named directory for this run"""
# Get timestamp
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
# Create run name
# method_names = {0: "vanilla", 1: "noise", 2: "kl", 3: "kl1"}
method_name = method_names[args.method]
if args.mode == "CR":
# Format: output/YYYYMMDD_HHMMSS_method_forgetN
run_name = f"{timestamp}_{method_name}_{args.dataset}_forget{args.forget_class}"
elif args.mode == "HR":
run_name = f"HR/{timestamp}_{method_name}_{args.dataset}"
if args.eval_only:
run_name = f"{timestamp}_eval_only"
# Create directory structure
run_dir = Path(args.output_dir) / run_name
run_dir.mkdir(parents=True, exist_ok=True)
# Create subdirectories
(run_dir / "models").mkdir(exist_ok=True)
(run_dir / "logs").mkdir(exist_ok=True)
return run_dir
def create_run_color_dir(args, mode) -> Path:
"""Create a uniquely named directory for this run"""
# Get timestamp
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
# Create run name
method_names = {0: "vanilla", 1: "noise", 2: "kl"}
method_name = method_names[args.method]
# Format: output/YYYYMMDD_HHMMSS_method_forgetN
run_name = f"{timestamp}_{method_name}_{mode}"
if args.eval_only:
run_name = f"{timestamp}_eval_only"
# Create directory structure
run_dir = Path(args.output_dir) / run_name
run_dir.mkdir(parents=True, exist_ok=True)
# Create subdirectories
(run_dir / "models").mkdir(exist_ok=True)
(run_dir / "logs").mkdir(exist_ok=True)
return run_dir
def plot_confusion_matrix(cm, title, save_path):
"""Plot and save confusion matrix"""
plt.figure(figsize=(10, 8))
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")
plt.title(title)
plt.ylabel("True Label")
plt.xlabel("Predicted Label")
plt.savefig(save_path, bbox_inches="tight", pad_inches=0.1)
plt.close()
def plot_accuracy_comparison(orig_acc, unl_acc, save_path, forget_class):
"""Plot and save accuracy comparison bar chart"""
plt.figure(figsize=(12, 6))
x = np.arange(10)
width = 0.35
plt.bar(x - width / 2, orig_acc, width, label="Original Model")
plt.bar(x + width / 2, unl_acc, width, label="Unlearned Model")
plt.xlabel("Digit")
plt.ylabel("Accuracy (%)")
plt.title(f"Per-class Accuracy Comparison (forget_class={forget_class})")
plt.xticks(x)
plt.legend()
plt.grid(True, alpha=0.3)
plt.savefig(save_path, bbox_inches="tight", pad_inches=0.1)
plt.close()
def setup_logging(run_dir: Path) -> None:
"""Configure logging to file and console for each run"""
log_file = run_dir / "logs/run.log"
# Get root logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# Remove existing handlers
for handler in logger.handlers[:]:
logger.removeHandler(handler)
# Create formatter
formatter = logging.Formatter("%(asctime)s - %(message)s")
# File handler
file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(formatter)
# Console handler
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
# Add handlers
logger.addHandler(file_handler)
logger.addHandler(console_handler)
def plot_accuracy_comparison_experiments(orig_acc, unl_acc, save_path):
"""绘制不同实验之间原模型和unlearn模型准确率的对比柱状图"""
plt.figure(figsize=(12, 6))
modes = list(orig_acc.keys())
x = np.arange(len(modes))
width = 0.35
orig_values = [orig_acc[mode] for mode in modes]
unl_values = [unl_acc[mode] for mode in modes]
orig_bars = plt.bar(x - width / 2, orig_values, width, label="Original Model")
unl_bars = plt.bar(x + width / 2, unl_values, width, label="Unlearned Model")
plt.xlabel("color_mode")
plt.ylabel("Accuracy (%)")
plt.title(f"Comparations between original and unlearned model")
plt.xticks(x, modes)
plt.legend()
plt.grid(True, alpha=0.3)
# 在每个柱子上添加数据标签
for bar in orig_bars:
height = bar.get_height()
plt.text(
bar.get_x() + bar.get_width() / 2.0,
height,
f"{height:.2f}%",
ha="center",
va="bottom",
fontsize=10,
)
for bar in unl_bars:
height = bar.get_height()
plt.text(
bar.get_x() + bar.get_width() / 2.0,
height,
f"{height:.2f}%",
ha="center",
va="bottom",
fontsize=10,
)
plt.savefig(save_path, bbox_inches="tight", pad_inches=0.1)
plt.close()
def load_model(model_path, num_classes=10):
model = models.ResNet18()
model.conv1 = nn.Conv2d(
in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=1, bias=False
)
model.fc = torch.nn.Linear(512, num_classes)
model.load_state_dict(torch.load(model_path, weights_only=True))
return model
def find_model_path(seed: int, mode, method: str, class_id: int) -> str:
"""通过特征匹配查找模型路径"""
if mode == "CR":
base_dir = f"../outputs/seed{seed}/cifar10/{method}"
if not os.path.exists(base_dir):
return None
target_suffix = f"{method}_forget{class_id}"
candidate_dirs = []
# 遍历所有子目录
for dir_name in os.listdir(base_dir):
dir_path = os.path.join(base_dir, dir_name)
# 验证是否为有效目录
if not os.path.isdir(dir_path):
continue
# 分割目录名特征
parts = dir_name.split("_")
if len(parts) < 2:
continue
# 匹配方法名和遗忘类
if "_".join(parts[-2:]) == target_suffix:
candidate_dirs.append(dir_path)
# 按目录名排序(假设目录名含时间戳)
candidate_dirs.sort(reverse=True)
# 检查模型文件
for dir_path in candidate_dirs:
model_path = os.path.join(
dir_path, "models", f"cifar10_model_best_{method}_forget{class_id}.pth"
)
if os.path.exists(model_path):
return model_path
elif mode == "HR":
base_dir = f"../outputs_seed{seed}/HR"
if not os.path.exists(base_dir):
return None
target_suffix = f"{method}"
candidate_dirs = []
# 遍历所有子目录
for dir_name in os.listdir(base_dir):
dir_path = os.path.join(base_dir, dir_name)
# 验证是否为有效目录
if not os.path.isdir(dir_path):
continue
# 分割目录名特征
parts = dir_name.split("_")
if len(parts) < 2:
continue
# 匹配方法名和遗忘类
if "_".join(parts[-1:]) == target_suffix:
candidate_dirs.append(dir_path)
# 按目录名排序(假设目录名含时间戳)
# candidate_dirs.sort(reverse=True)
# 检查模型文件
for dir_path in candidate_dirs:
model_path = os.path.join(
dir_path, "models", f"cifar10_model_best_{method}.pth"
)
if os.path.exists(model_path):
return model_path
return None
def calculate_differences(avg_results: Dict) -> Tuple[Dict, np.ndarray]:
"""计算方法间的差异"""
# 方法间两两差异(按类别)
class_diffs = defaultdict(dict)
# 方法间整体差异矩阵
methods = list(avg_results.keys())
n_methods = len(methods)
diff_matrix = np.zeros((n_methods, n_methods))
for i, m1 in enumerate(methods):
for j, m2 in enumerate(methods):
if i >= j:
continue
# 按类别计算差异
class_diffs[(m1, m2)] = {
c: avg_results[m1][c] - avg_results[m2][c] for c in avg_results[m1]
}
# 计算整体平均差异
diff_matrix[i, j] = np.mean(list(class_diffs[(m1, m2)].values()))
diff_matrix[j, i] = -diff_matrix[i, j]
return class_diffs, diff_matrix
def accuracy(net, loader, single_class=False):
"""Return accuracy on a dataset given by the data loader."""
correct = 0
total = 0
total_sc = torch.zeros((opt.num_classes))
correct_sc = torch.zeros((opt.num_classes))
pred_all = []
target_all = []
for inputs, targets in loader:
inputs, targets = inputs.to(opt.device), targets.to(opt.device)
outputs = net(inputs)
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
if single_class:
pred_all.append(predicted.detach().cpu())
target_all.append(targets.detach().cpu())
if single_class:
pred_all = torch.cat(pred_all)
target_all = torch.cat(target_all)
for i in range(opt.num_classes):
buff_tar = target_all[target_all == i]
buff_pred = pred_all[target_all == i]
total_sc[i] = buff_tar.shape[0]
correct_sc[i] = (buff_pred == i).sum().item()
return correct / total, correct_sc / total_sc
else:
return correct / total