-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpatch.py
68 lines (50 loc) · 1.74 KB
/
patch.py
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
"""
Sample command:
python src/patch.py \
--train-dataset=MNIST \
--epochs=5 \
--lr=0.00001 \
--batch-size=128 \
--model=ViT-L/14 \
--eval-datasets=ImageNet,MNIST \
--results-db=results.jsonl \
--save=models/patch/vit_l_14 \
--data-location=~/data \
--alpha 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
"""
import os
from src.eval import evaluate
from src.finetune import finetune
from src.modeling import ImageEncoder
from src.args import parse_arguments
def patch(args):
assert args.save is not None, 'Please provide a path to store models'
# First, fine-tune
zeroshot_checkpoint, finetuned_checkpoint = finetune(args)
# Load models
zeroshot = ImageEncoder.load(zeroshot_checkpoint)
finetuned = ImageEncoder.load(finetuned_checkpoint)
theta_0 = {k: v.clone() for k, v in zeroshot.state_dict().items()}
theta_1 = {k: v.clone() for k, v in finetuned.state_dict().items()}
del zeroshot
# make sure checkpoints are compatible
assert set(theta_0.keys()) == set(theta_1.keys())
alphas = args.alpha
for alpha in alphas:
print('='*100)
print(f'Evaluating with alpha={alpha:.2f}')
args.alpha = alpha
# interpolate between all weights in the checkpoints
theta = {
key: (1-alpha) * theta_0[key] + alpha * theta_1[key]
for key in theta_0.keys()
}
# update the model (in-place) acccording to the new weights
finetuned.load_state_dict(theta)
# save model
finetuned.save(os.path.join(args.save, args.train_dataset, f'patched_alpha={alpha:.3f}.pt'))
# evaluate
evaluate(finetuned, args)
if __name__ == '__main__':
args = parse_arguments()
patch(args)