-
Notifications
You must be signed in to change notification settings - Fork 130
/
average_models.py
45 lines (36 loc) · 1.87 KB
/
average_models.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
import argparse
import logging
import sys
import os
from keras_wrapper.utils import average_models
sys.path.insert(1, os.path.abspath("."))
sys.path.insert(0, os.path.abspath("../"))
logging.basicConfig(level=logging.INFO, format='[%(asctime)s] %(message)s', datefmt='%d/%m/%Y %H:%M:%S')
logger = logging.getLogger(__name__)
def parse_args():
parser = argparse.ArgumentParser("Averages models")
parser.add_argument("-d", "--dest",
default='./model',
required=False,
help="Path to the averaged model. If not specified, the model is saved in './model'.")
parser.add_argument("-v", "--verbose", required=False, default=0, type=int, help="Verbosity level")
parser.add_argument("-w", "--weights", nargs="*", help="Weight given to each model in the averaging. You should provide the same number of weights than models."
"By default, it applies the same weight to each model (1/N).", default=[])
parser.add_argument("-m", "--models", nargs="+", required=True, help="Path to the models")
return parser.parse_args()
def weighted_average(args):
"""
Apply a weighted average to the models.
:param args: Options for the averaging function:
* models: Path to the models.
* dest: Path to the averaged model. If unspecified, the model is saved in './model'
* weights: Weight given to each model in the averaging. Should be the same number of weights than models.
If unspecified, it applies the same weight to each model (1/N).
:return:
"""
logger.info("Averaging %d models" % len(args.models))
average_models(args.models, args.dest, weights=args.weights)
logger.info('Averaging finished.')
if __name__ == "__main__":
args = parse_args()
weighted_average(args)