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}")
|