started unlearning setup
This commit is contained in:
@@ -7,6 +7,7 @@ import time
|
||||
import numpy as np
|
||||
from sklearn.metrics import classification_report
|
||||
from pathlib import Path
|
||||
from unlearning.Strategy import Strategy
|
||||
|
||||
class Model(ABC):
|
||||
def __init__(self, device, size):
|
||||
@@ -92,6 +93,21 @@ class Model(ABC):
|
||||
self.model.to(self.device)
|
||||
print(f'Model loaded from {file_path}')
|
||||
|
||||
|
||||
def unlearn(self, strategy: Strategy, forget_loader, retain_loader):
|
||||
""" Executes a targeted unlearning strategy and profiles efficiency """
|
||||
print(f"Executing: {strategy.__class__.__name__}...")
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
# Delegate the actual algorithmic weight/logit manipulation to the strategy
|
||||
strategy.apply(self.network, forget_loader, retain_loader)
|
||||
|
||||
elapsed_time = time.time() - start_time
|
||||
print(f"{strategy.__class__.__name__} completed in {elapsed_time:.4f} seconds.")
|
||||
|
||||
return elapsed_time
|
||||
|
||||
|
||||
|
||||
# Using the factory patern here
|
||||
|
||||
@@ -23,14 +23,14 @@ class ResNet50(Model):
|
||||
m = models.resnet50(weights=models.ResNet50_Weights.DEFAULT)
|
||||
|
||||
# freez all layers
|
||||
for param in m.parameters():
|
||||
param.requires_grad = False
|
||||
#for param in m.parameters():
|
||||
#param.requires_grad = False
|
||||
|
||||
# unfreez the last two
|
||||
# NOTE: Freezing everything and unfrizing the last 3 yeilded the best performance
|
||||
for param in m.layer2.parameters(): param.requires_grad = True
|
||||
for param in m.layer3.parameters(): param.requires_grad = True
|
||||
for param in m.layer4.parameters(): param.requires_grad = True
|
||||
#for param in m.layer2.parameters(): param.requires_grad = True
|
||||
#for param in m.layer3.parameters(): param.requires_grad = True
|
||||
#for param in m.layer4.parameters(): param.requires_grad = True
|
||||
|
||||
m.fc = nn.Linear(m.fc.in_features, self.size)
|
||||
return m
|
||||
Reference in New Issue
Block a user