File size: 1,783 Bytes
39fc4a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from timm import create_model

# εŠ θ½½ζ¨‘εž‹
def load_model(model_path):
    model = create_model('tf_efficientnet_b3_ns', num_classes=1605, pretrained=False)
    model.load_state_dict(torch.load(model_path))
    return model

# ζ¨‘εž‹θžεˆ
def model_soup(models, weights):
    if len(models) != len(weights):
        raise ValueError("Number of models and weights must match")
    
    # Normalize weights
    weights = [w / sum(weights) for w in weights]
    
    # Initialize the fused model with the structure of the first model
    fused_model = create_model('tf_efficientnet_b3_ns', num_classes=1605, pretrained=False)
    fused_model_dict = fused_model.state_dict()
    
    for key in fused_model_dict.keys():
        fused_model_dict[key] = sum(weight * models[i].state_dict()[key] for i, weight in enumerate(weights))
    
    fused_model.load_state_dict(fused_model_dict)
    return fused_model

# ζ¨‘εž‹ζƒι‡θ·―εΎ„
model_paths = [
    '/data/cjm/FungiCLEF2024/EfficientNet/output/trick_1.4.3/efficientnet_b3_epoch_28.pth',  # 权重 4
    '/data/cjm/FungiCLEF2024/EfficientNet/output/trick_1.4.3.2/efficientnet_b3_epoch_28.pth',  # 权重 2.6
    '/data/cjm/FungiCLEF2024/EfficientNet/output/trick_1.4.1/efficientnet_b3_epoch_23.pth',  # 权重 2.4
    '/data/cjm/FungiCLEF2024/EfficientNet/output/trick_1.5.2/efficientnet_b3_epoch_21.pth', # 权重 1
]

# εŠ θ½½ζ¨‘εž‹
models = [load_model(path) for path in model_paths]

# ζ¨‘εž‹ζƒι‡
weights = [4, 2.6, 2.4, 1]

# θΏ›θ‘Œζ¨‘εž‹θžεˆ
fused_model = model_soup(models, weights)

# δΏε­˜θžεˆεŽηš„ζ¨‘εž‹ζƒι‡
fused_model_path = '/data/cjm/FungiCLEF2024/EfficientNet/output/fused_model_soup.pth'
torch.save(fused_model.state_dict(), fused_model_path)

print(f"Fused model saved to {fused_model_path}")