certified
This commit is contained in:
131
Tune_new.py
131
Tune_new.py
@@ -10,6 +10,9 @@ from sets.Data import *
|
||||
from sets.IdentitySubset import IdentitySubset
|
||||
from architectures.Model import Model, Architecture
|
||||
from unlearning.CertifiedRemoval import CertifiedRemoval
|
||||
from unlearning.CertifiedUnlearning import CertifiedUnlearning
|
||||
from unlearning.LinearFiltration import LinearFiltration
|
||||
from unlearning.WeightFiltration import WeightFiltration
|
||||
|
||||
# Global Hyperparameters
|
||||
CLASS_SIZE = 20
|
||||
@@ -17,7 +20,7 @@ BATCH_SIZE = 16
|
||||
SAMPLE_SIZE = 30
|
||||
TRAINING_SAMPLE = 27
|
||||
RESOLUTION = 224
|
||||
ARCH = Architecture.RESNET50
|
||||
ARCH = Architecture.RESNET18
|
||||
|
||||
|
||||
# Data preparation and model setup
|
||||
@@ -27,14 +30,17 @@ def prepare_data_and_model_environment():
|
||||
train-test class splits, and configures the architecture base.
|
||||
"""
|
||||
device = SetUp.get_device()
|
||||
dataset_name = Set_Name.CELEBA
|
||||
dataset_name = Set_Name.CASIAFACES
|
||||
if dataset_name == Set_Name.CASIAFACES:
|
||||
SAMPLE_SIZE = 400
|
||||
TRAINING_SAMPLE = 320
|
||||
|
||||
dataset = get_set(set_name=dataset_name)
|
||||
print(f"> {dataset.__class__.__name__} dataset loaded")
|
||||
|
||||
# Select target identities (deterministic top sample identities)
|
||||
selected_identities = select_top_ids(dataset=dataset, class_size=CLASS_SIZE)
|
||||
print(f'> Selected {CLASS_SIZE} random identity classes from CelebA dataset.')
|
||||
print(f'> Selected {CLASS_SIZE} random identity classes from {dataset_name.name} dataset.')
|
||||
print(f'> A class has {TRAINING_SAMPLE} train and {SAMPLE_SIZE - TRAINING_SAMPLE} test samples')
|
||||
|
||||
# Isolate sample index partitions
|
||||
@@ -81,6 +87,8 @@ def prepare_data_and_model_environment():
|
||||
|
||||
# Fine tunning and evaluation
|
||||
def run_finetuning_or_baseline_eval(env_dict, run_training=False, lr_rate=0.0001, epochs=10):
|
||||
|
||||
|
||||
"""
|
||||
Handles model training (if flag is true) and logs the baseline fine-tuned
|
||||
performance to file metrics.
|
||||
@@ -91,13 +99,15 @@ def run_finetuning_or_baseline_eval(env_dict, run_training=False, lr_rate=0.0001
|
||||
|
||||
test_loader = DataLoader(test_data, batch_size=BATCH_SIZE, shuffle=False)
|
||||
|
||||
# Optional training configuration switch
|
||||
if run_training:
|
||||
train_loader = DataLoader(train_data, batch_size=BATCH_SIZE, shuffle=True)
|
||||
print(f"Starting training on {env_dict['device']}...")
|
||||
model.train(epochs=epochs, loader=train_loader, rate=lr_rate)
|
||||
model.save(filename=ARCH.name.lower())
|
||||
print(f"Model saved to trained_models/{ARCH.name.lower()}.pth")
|
||||
|
||||
train_loader = DataLoader(train_data, batch_size=BATCH_SIZE, shuffle=True)
|
||||
|
||||
if not run_training:
|
||||
return
|
||||
#print(f"Starting training on {env_dict['device']}...")
|
||||
model.train(epochs=epochs, loader=train_loader, rate=lr_rate)
|
||||
model.save(filename=ARCH.name.lower())
|
||||
print(f"Model saved to trained_models/{ARCH.name.lower()}.pth")
|
||||
|
||||
print(f"Total test images for these {CLASS_SIZE} classes: {len(test_data)}")
|
||||
|
||||
@@ -119,7 +129,7 @@ def run_finetuning_or_baseline_eval(env_dict, run_training=False, lr_rate=0.0001
|
||||
|
||||
|
||||
# Unlearning and strategy eval
|
||||
def run_unlearning_and_strategy_eval(env_dict, forget_class_idx):
|
||||
def run_unlearning_and_strategy_eval(env_dict, forget_class_idx, strategy, evaluate = False):
|
||||
"""
|
||||
Reloads a clean model state, applies the isolated unlearning framework,
|
||||
and runs specific target evaluation domain checks.
|
||||
@@ -128,13 +138,34 @@ def run_unlearning_and_strategy_eval(env_dict, forget_class_idx):
|
||||
train_data = env_dict["train_data"]
|
||||
test_data = env_dict["test_data"]
|
||||
|
||||
# testing valuse * *
|
||||
#---------------------------------------------------------------------------
|
||||
# S1 50 5 5 5 5 5
|
||||
# S2 1000 200 1000 500 200 300
|
||||
# BS 5 5 5 5 5 5
|
||||
# scale 2000 500 8000 5000 10000 8000
|
||||
# std 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001
|
||||
|
||||
# Initialize the strategy hyperparameters matching standard settings
|
||||
certified_removal = CertifiedRemoval(
|
||||
target_class_index=forget_class_idx,
|
||||
removal_bound=0.05,
|
||||
epsilon=0.5,
|
||||
l2_reg=15
|
||||
)
|
||||
# increase s2, decrease scale ---sweet spot
|
||||
'''certified_removal = CertifiedRemoval(
|
||||
target_class_index=forget_class_idx,
|
||||
s1=4,
|
||||
s2=350, # 350 best
|
||||
unlearn_bs=5,
|
||||
scale=6000.0, # 6000 was good
|
||||
std=0.00001
|
||||
)'''
|
||||
'''certified_removal = CertifiedUnlearning(
|
||||
target_class_index=0,
|
||||
l2_reg=0.0005,
|
||||
gamma=0.1,
|
||||
scale=7000.0,
|
||||
s1=2,
|
||||
s2=350,
|
||||
std=1e-5,
|
||||
unlearn_bs=2
|
||||
)'''
|
||||
|
||||
# Segment specific unlearning loaders using class index boundaries
|
||||
forget_train_loader, retain_train_loader = get_unlearning_loaders(
|
||||
@@ -147,11 +178,17 @@ def run_unlearning_and_strategy_eval(env_dict, forget_class_idx):
|
||||
# Instantiate a clean copy of the model to keep weights isolated
|
||||
reloaded = Model.create(arch=ARCH, device=device, size=CLASS_SIZE)
|
||||
reloaded.load(arch=ARCH)
|
||||
|
||||
if evaluate:
|
||||
reloaded.evaluate(
|
||||
loader=retain_test_loader, mode="finetuned"
|
||||
)
|
||||
|
||||
print("fine tunned model loaded into evaluation sandbox")
|
||||
|
||||
# Execute strategic parameter unlearning step
|
||||
certified_removal.apply(reloaded.model, forget_train_loader, retain_train_loader)
|
||||
strategy_in_use = certified_removal.__class__.__name__
|
||||
strategy.apply(reloaded.model, forget_train_loader, retain_train_loader)
|
||||
strategy_in_use = strategy.__class__.__name__
|
||||
|
||||
# Define validation tracking steps dynamically
|
||||
evaluation_domains = [
|
||||
@@ -180,10 +217,62 @@ if __name__ == "__main__":
|
||||
runtime_environment = prepare_data_and_model_environment()
|
||||
|
||||
# Baseline Evaluation
|
||||
finetuning = False
|
||||
# switch finetuning for tests on strategies only
|
||||
run_finetuning_or_baseline_eval(runtime_environment, run_training=True)
|
||||
run_finetuning_or_baseline_eval(runtime_environment, run_training=finetuning)
|
||||
|
||||
finetuning = True
|
||||
# Unlearning Iterations
|
||||
for i in range(0, 1):
|
||||
|
||||
# strategies
|
||||
#
|
||||
#certified_removal = CertifiedRemoval(
|
||||
# target_class_index=i,
|
||||
# s1=4,
|
||||
# s2=350, # 350 best
|
||||
# unlearn_bs=5,
|
||||
# scale=6000.0, # 6000 was good
|
||||
# std=0.00009
|
||||
# )
|
||||
|
||||
|
||||
|
||||
certified_unlearning = CertifiedUnlearning(
|
||||
target_class_index=i,
|
||||
l2_reg=0.000002,
|
||||
gamma=0.1,
|
||||
scale= 20000,# 16400.0, # took ages to reach this sweet spot
|
||||
s1=2,
|
||||
s2=300,
|
||||
std=0.00001,
|
||||
unlearn_bs=16
|
||||
)
|
||||
|
||||
# works perfectly
|
||||
linear_filtration = LinearFiltration(
|
||||
|
||||
target_class_index=i
|
||||
)
|
||||
|
||||
weight_filtration = WeightFiltration(
|
||||
target_class_index=i,
|
||||
epochs=3,
|
||||
lr=0.5,
|
||||
gamma=150
|
||||
)
|
||||
|
||||
strategies = [
|
||||
certified_unlearning,
|
||||
# weight_filtration,
|
||||
# linear_filtration
|
||||
]
|
||||
|
||||
print(f"\n>>> Executing Unlearning Framework for Target Identity Index: {i} <<<")
|
||||
run_unlearning_and_strategy_eval(runtime_environment, forget_class_idx=i)
|
||||
for strategy in strategies:
|
||||
run_unlearning_and_strategy_eval(
|
||||
runtime_environment,
|
||||
forget_class_idx=i,
|
||||
strategy=strategy,
|
||||
evaluate= not finetuning
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user