import argparse import sys from modelList import readYAMLModelList, Model as AlphaDotsModel, saveModelToYAML # parse command line args parser = argparse.ArgumentParser(description='Create a new AlphaZero model') parser.add_argument('--name', required=True) parser.add_argument('--newname', required=True) parser.add_argument('--mcts', action='store_true') args = parser.parse_args() model_list = readYAMLModelList() model_copy = None for model in model_list: if model['name'] == args.name: if args.mcts: ai = 'MCTS-AlphaZero' else: ai = 'ConvNet' model_copy = AlphaDotsModel(args.newname, model['desc'], model['path'], model['type'], ai) if model_copy is not None: print('adding model copy') saveModelToYAML(model_copy) else: print('failed to find model named ' + args.name)
imgWidth = 11 imgHeight = 11 kernelSize = (args.kernelsize, args.kernelsize) filterCnt = args.filters l2reg = 1e-4 resBlockCnt = args.resblocks print("createModel configuration:") print("|-> model name: " + args.name) print("|-> kernel size: " + str(args.kernelsize)) print("|-> filters: " + str(args.filters)) print("|-> res blocks: " + str(args.resblocks)) print("|-> with mcts ai: " + str(args.withmctsai)) for model in readYAMLModelList(): if model['name'] == args.name: print("ERROR: name is already in use!") exit(1) ### build the model def build_residual_block(x, index): in_x = x res_name = "res" + str(index) x = Conv2D(filters=filterCnt, kernel_size=kernelSize, padding="same", data_format="channels_last", kernel_regularizer=l2(l2reg),
def load_alphadots_model(self, model, width, height): if self.is_master: self.logger.write( "ERROR: master mode model server may not load models!") return None # only import keras based modules when we really need them from keras.models import load_model import modelList import modelHandlers.DirectInference import modelHandlers.Sequence import modelHandlers.DirectInferenceCategorical import modelHandlers.SequenceCategorical import modelHandlers.PolicyValue from models.LineFilterLayer import LineFilterLayer from models.SequenceLineFilterLayer import SequenceLineFilterLayer from models.ValueLayer import ValueLayer # load model self.logger.write("[LOAD MODEL] preparing to load model " + model) sys.stdout.flush() # LineFilterLayer has to be set before loading the model LineFilterLayer.imgWidth = width LineFilterLayer.imgHeight = height # SequenceLineFilterLayer has to be set before loading the model SequenceLineFilterLayer.imgWidth = width SequenceLineFilterLayer.imgHeight = height # ValueLayer has to be set before loading the model ValueLayer.imgWidth = width ValueLayer.imgHeight = height self.logger.write("[LOAD MODEL] reading model list") sys.stdout.flush() models = modelList.readYAMLModelList() assert (len(models) > 0) selectedModel = models[0] foundModel = False for m in models: if m['name'] == model: selectedModel = m foundModel = True if not foundModel: self.logger.write("ERROR: Can't find model!") self.logger.write("models:") self.logger.write(models) exit(1) self.logger.write("[LOAD MODEL] loading model: %s" % selectedModel) sys.stdout.flush() scriptDir = os.path.dirname(os.path.realpath(__file__)) model = load_model(scriptDir + "/models/" + selectedModel['path'], custom_objects={ 'LineFilterLayer': LineFilterLayer, 'SequenceLineFilterLayer': SequenceLineFilterLayer, 'ValueLayer': ValueLayer }) self.logger.write("[LOAD MODEL] preparing model handler") sys.stdout.flush() # load model handler modelHandler = None if selectedModel['type'] == 'DirectInference': modelHandler = modelHandlers.DirectInference.DirectInference( model, self.debug) elif selectedModel['type'] == 'Sequence': modelHandler = modelHandlers.Sequence.Sequence(model) elif selectedModel['type'] == 'DirectInferenceCategorical': modelHandler = modelHandlers.DirectInferenceCategorical.DirectInferenceCategorical( model) elif selectedModel['type'] == 'SequenceCategorical': modelHandler = modelHandlers.SequenceCategorical.SequenceCategorical( model) elif selectedModel['type'] == 'PolicyValue': invertValue = False if 'options' in selectedModel and 'InvertValue' in selectedModel[ 'options']: invertValue = True modelHandler = modelHandlers.PolicyValue.PolicyValue( model, invertValue, self.debug, self.logdest, self.logger) else: self.logger.write("unknown model handler type!") exit(1) self.logger.write("[LOAD MODEL] starting model handler %s" % selectedModel['type']) return modelHandler
import yaml import sys from modelList import readYAMLModelList, Model models = readYAMLModelList() print(models) yaml.dump({'models': models}, sys.stdout, default_flow_style=False)