chirmy commited on
Commit
39fc4a0
Β·
verified Β·
1 Parent(s): ade1877

Upload 02-model-soup.py

Browse files
Files changed (1) hide show
  1. 02-model-soup.py +49 -0
02-model-soup.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from timm import create_model
3
+
4
+ # εŠ θ½½ζ¨‘εž‹
5
+ def load_model(model_path):
6
+ model = create_model('tf_efficientnet_b3_ns', num_classes=1605, pretrained=False)
7
+ model.load_state_dict(torch.load(model_path))
8
+ return model
9
+
10
+ # ζ¨‘εž‹θžεˆ
11
+ def model_soup(models, weights):
12
+ if len(models) != len(weights):
13
+ raise ValueError("Number of models and weights must match")
14
+
15
+ # Normalize weights
16
+ weights = [w / sum(weights) for w in weights]
17
+
18
+ # Initialize the fused model with the structure of the first model
19
+ fused_model = create_model('tf_efficientnet_b3_ns', num_classes=1605, pretrained=False)
20
+ fused_model_dict = fused_model.state_dict()
21
+
22
+ for key in fused_model_dict.keys():
23
+ fused_model_dict[key] = sum(weight * models[i].state_dict()[key] for i, weight in enumerate(weights))
24
+
25
+ fused_model.load_state_dict(fused_model_dict)
26
+ return fused_model
27
+
28
+ # ζ¨‘εž‹ζƒι‡θ·―εΎ„
29
+ model_paths = [
30
+ '/data/cjm/FungiCLEF2024/EfficientNet/output/trick_1.4.3/efficientnet_b3_epoch_28.pth', # 权重 4
31
+ '/data/cjm/FungiCLEF2024/EfficientNet/output/trick_1.4.3.2/efficientnet_b3_epoch_28.pth', # 权重 2.6
32
+ '/data/cjm/FungiCLEF2024/EfficientNet/output/trick_1.4.1/efficientnet_b3_epoch_23.pth', # 权重 2.4
33
+ '/data/cjm/FungiCLEF2024/EfficientNet/output/trick_1.5.2/efficientnet_b3_epoch_21.pth', # 权重 1
34
+ ]
35
+
36
+ # εŠ θ½½ζ¨‘εž‹
37
+ models = [load_model(path) for path in model_paths]
38
+
39
+ # ζ¨‘εž‹ζƒι‡
40
+ weights = [4, 2.6, 2.4, 1]
41
+
42
+ # θΏ›θ‘Œζ¨‘εž‹θžεˆ
43
+ fused_model = model_soup(models, weights)
44
+
45
+ # δΏε­˜θžεˆεŽηš„ζ¨‘εž‹ζƒι‡
46
+ fused_model_path = '/data/cjm/FungiCLEF2024/EfficientNet/output/fused_model_soup.pth'
47
+ torch.save(fused_model.state_dict(), fused_model_path)
48
+
49
+ print(f"Fused model saved to {fused_model_path}")