Upload 02-model-soup.py
Browse files- 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}")
|