certified

This commit is contained in:
2026-06-24 21:05:06 +02:00
parent 207fcae699
commit 3c6ee9e12d
10 changed files with 610 additions and 281 deletions

68
Tune.py
View File

@@ -41,6 +41,7 @@ EPOCHS = 10
# Inception = 299
RESOLUTION = 224
FINETUNE = False # whether to fintune or just load finetuned model from dir
# model architecture options are
# - RESNET18
# - RESNET50
@@ -112,19 +113,24 @@ device = SetUp.get_device()
for i in range(0,1):#CLASS_SIZE):
FORGET_CLASS_IDX = i
# Create model using Factory
model = Model.create(
arch = arch,
device = device,
size = CLASS_SIZE)
# we may need to load existing model or finetune
#model.train(
# epochs = EPOCHS,
# loader = train_loader,
# rate = LR_RATE)
model = None
# save.
#model.save(filename=arch.name.lower())
if FINETUNE:
model = Model.create(
arch = arch,
device = device,
size = CLASS_SIZE)
# we may need to load existing model or finetune
model.train(
epochs = EPOCHS,
loader = train_loader,
rate = LR_RATE)
# save.
file_name = f"{arch.name.lower}_{dataset_name.name.lower()}"
model.save(filename=arch.name.lower())
# done tuning
@@ -147,18 +153,21 @@ for i in range(0,1):#CLASS_SIZE):
# Evaluate
current_mode = "Finetuned"
#accuracy, report_dict = model.evaluate(
# loader = test_loader,
# mode=current_mode
#)
if FINETUNE:
Util._log_to_csv(
arch=model.__class__.__name__,
mode = current_mode,
accuracy=accuracy,
report_dict=report_dict,
strategy="base"
)
#current_mode = "Finetuned"
accuracy, report_dict = model.evaluate(
loader = test_loader,
mode=current_mode
)
Util._log_to_csv(
arch=model.__class__.__name__,
mode = current_mode,
accuracy=accuracy,
report_dict=report_dict,
strategy="base"
)
# unlearning algorithms
#linear_filtration = LinearFiltration(target_class_index=FORGET_CLASS_IDX)
@@ -167,7 +176,14 @@ for i in range(0,1):#CLASS_SIZE):
#weight_filtration = WeightFiltration(num_classes = CLASS_SIZE,target_class_idx=FORGET_CLASS_IDX)
#weight_filtration.apply(reloaded.model)
certified_removal = CertifiedRemoval(target_class_index=FORGET_CLASS_IDX,removal_bound=0.05, epsilon=0.5, l2_reg=15)
certified_removal = CertifiedRemoval(
target_class_index=FORGET_CLASS_IDX,
s1=2,
s2=500,
unlearn_bs=2,
scale=100.0, # Drop scale to match lower s2 depth
std=0.00001)
#,removal_bound=0.05, epsilon=0.5, l2_reg=15)
#certified_removal.apply(reloaded.model)
# to be unlearned
@@ -200,6 +216,12 @@ for i in range(0,1):#CLASS_SIZE):
# loader = test_loader
#)
if not FINETUNE:
reloaded.evaluate(
loader = test_loader,
mode=current_mode
)
# Unlearning
# train loaders passed here
strategy.apply(reloaded.model, forget_train_loader, retain_train_loader)