def init(config_filename, log_verbosity): """ :param str config_filename: filename to config-file :param int log_verbosity: """ rnn.init_better_exchook() rnn.init_thread_join_hack() print("Using config file %r." % config_filename) assert os.path.exists(config_filename) rnn.init_config( config_filename=config_filename, extra_updates={ "use_tensorflow": True, "log": None, "log_verbosity": log_verbosity, "task": __file__, # just extra info for the config }) global config config = rnn.config rnn.init_log() print("Returnn compile-tf-graph starting up.", file=log.v1) rnn.returnn_greeting() rnn.init_backend_engine() assert util.BackendEngine.is_tensorflow_selected( ), "this is only for TensorFlow" rnn.init_faulthandler() rnn.init_config_json_network()
def init(config_filename, log_verbosity): """ :param str config_filename: filename to config-file :param int log_verbosity: """ rnn.init_better_exchook() rnn.init_thread_join_hack() if config_filename: print("Using config file %r." % config_filename) assert os.path.exists(config_filename) rnn.init_config(config_filename=config_filename, command_line_options=[]) global config config = rnn.config config.set("log", None) config.set("log_verbosity", log_verbosity) config.set("use_tensorflow", True) rnn.init_log() print("Returnn compile-native-op starting up.", file=log.v1) rnn.returnn_greeting() rnn.init_backend_engine() assert util.BackendEngine.is_tensorflow_selected(), "this is only for TensorFlow" rnn.init_faulthandler() rnn.init_config_json_network() if 'network' in config.typed_dict: print("Loading network") from returnn.tf.network import TFNetwork network = TFNetwork( name="root", config=config, rnd_seed=1, train_flag=False, eval_flag=True, search_flag=False) network.construct_from_dict(config.typed_dict["network"])
def init(config_filename, cmd_line_opts, dataset_config_str): """ :param str config_filename: global config for CRNN :param list[str] cmd_line_opts: options for init_config method :param str dataset_config_str: dataset via init_dataset_via_str() """ rnn.init_better_exchook() rnn.init_thread_join_hack() if config_filename: rnn.init_config(config_filename, cmd_line_opts) rnn.init_log() else: log.initialize(verbosity=[5]) print("Returnn hdf_dump starting up.", file=log.v3) rnn.init_faulthandler() if config_filename: rnn.init_data() rnn.print_task_properties() assert isinstance(rnn.train_data, Dataset) dataset = rnn.train_data else: assert dataset_config_str dataset = init_dataset(dataset_config_str) print("Source dataset:", dataset.len_info(), file=log.v3) return dataset
def init(config_filename, log_verbosity): """ :param str config_filename: filename to config-file :param int log_verbosity: """ rnn.init_better_exchook() rnn.init_thread_join_hack() if config_filename: print("Using config file %r." % config_filename) assert os.path.exists(config_filename) rnn.init_config(config_filename=config_filename, command_line_options=[]) global config config = rnn.config config.set("task", "calculate_wer") config.set("log", None) config.set("log_verbosity", log_verbosity) config.set("use_tensorflow", True) rnn.init_log() print("Returnn calculate-word-error-rate starting up.", file=log.v1) rnn.returnn_greeting() rnn.init_backend_engine() assert util.BackendEngine.is_tensorflow_selected( ), "this is only for TensorFlow" rnn.init_faulthandler() rnn.init_config_json_network() rnn.print_task_properties()
def init(config_filename, log_verbosity, remaining_args=()): """ :param str config_filename: filename to config-file :param int log_verbosity: :param list[str] remaining_args: """ rnn.init_better_exchook() rnn.init_thread_join_hack() print("Using config file %r." % config_filename) assert os.path.exists(config_filename) rnn.init_config(config_filename=config_filename, command_line_options=remaining_args, extra_updates={ "use_tensorflow": True, "log": None, "log_verbosity": log_verbosity, "task": "search", }, default_config={ "debug_print_layer_output_template": True, }) global config config = rnn.config rnn.init_log() print("Returnn %s starting up." % os.path.basename(__file__), file=log.v1) rnn.returnn_greeting() rnn.init_backend_engine() assert util.BackendEngine.is_tensorflow_selected( ), "this is only for TensorFlow" rnn.init_faulthandler() better_exchook.replace_traceback_format_tb() # makes some debugging easier rnn.init_config_json_network()
def init(config_filename=None): rnn.init_better_exchook() rnn.init_thread_join_hack() if config_filename: rnn.init_config(config_filename, command_line_options=[]) rnn.init_log() else: log.initialize() print("RETURNN collect-orth-symbols starting up.", file=log.v3) rnn.init_faulthandler() if config_filename: rnn.init_config_json_network() rnn.init_data() rnn.print_task_properties()
def init(config_str, config_dataset, verbosity): """ :param str config_str: either filename to config-file, or dict for dataset :param str|None config_dataset: :param int verbosity: """ rnn.init_better_exchook() rnn.init_thread_join_hack() dataset_dict = None config_filename = None if config_str.strip().startswith("{"): print("Using dataset %s." % config_str) dataset_dict = eval(config_str.strip()) elif config_str.endswith(".hdf"): dataset_dict = {"class": "HDFDataset", "files": [config_str]} print("Using dataset %r." % dataset_dict) assert os.path.exists(config_str) else: config_filename = config_str print("Using config file %r." % config_filename) assert os.path.exists(config_filename) rnn.init_config(config_filename=config_filename, default_config={"cache_size": "0"}) global config config = rnn.config config.set("log", None) config.set("log_verbosity", verbosity) if dataset_dict: assert not config_dataset config.set("train", dataset_dict) elif config_dataset and config_dataset != "train": print("Use dataset %r from config." % config_dataset) config.set("train", "config:%s" % config_dataset) else: print("Use train dataset from config.") assert config.value("train", None) rnn.init_log() print("Returnn dump-dataset starting up.", file=log.v2) rnn.returnn_greeting() rnn.init_faulthandler() rnn.init_config_json_network() # We use 'train' from the config. if config.has("dev"): config.set("dev", None) if config.has("eval"): config.set("eval", None) rnn.init_data() rnn.print_task_properties()
def init(config_filename, command_line_options): """ :param str config_filename: :param list[str] command_line_options: """ rnn.init_better_exchook() rnn.init_thread_join_hack() rnn.init_config(config_filename, command_line_options) global config config = rnn.config rnn.init_log() print("RETURNN demo-dataset starting up", file=log.v3) rnn.init_faulthandler() rnn.init_config_json_network() rnn.init_data() rnn.print_task_properties()
def init(config_str, config_dataset, verbosity): """ :param str config_str: either filename to config-file, or dict for dataset :param str|None config_dataset: :param int verbosity: """ global dataset rnn.init_better_exchook() rnn.init_thread_join_hack() dataset_dict = None config_filename = None if config_str.strip().startswith("{"): print("Using dataset %s." % config_str) dataset_dict = eval(config_str.strip()) elif config_str.endswith(".hdf"): dataset_dict = {"class": "HDFDataset", "files": [config_str]} print("Using dataset %r." % dataset_dict) assert os.path.exists(config_str) else: config_filename = config_str print("Using config file %r." % config_filename) assert os.path.exists(config_filename) rnn.init_config(config_filename=config_filename, default_config={"cache_size": "0"}) global config config = rnn.config config.set("log", None) config.set("log_verbosity", verbosity) if dataset_dict: assert not config_dataset dataset = init_dataset(dataset_dict) elif config_dataset and config_dataset != "train": print("Use dataset %r from config." % config_dataset) dataset = init_dataset("config:%s" % config_dataset) else: print("Use train dataset from config.") assert config.value("train", None) dataset = init_dataset("config:train") rnn.init_log() print("Returnn dump-dataset starting up.", file=log.v2) rnn.returnn_greeting() rnn.init_faulthandler() rnn.init_config_json_network() print("Dataset:", file=log.v2) print(" input:", dataset.num_inputs, "x", dataset.window, file=log.v2) print(" output:", dataset.num_outputs, file=log.v2) print(" ", dataset.len_info() or "no info", file=log.v2)
def init(config_filename, log_verbosity): """ :param str config_filename: filename to config-file :param int log_verbosity: """ rnn.init_better_exchook() rnn.init_thread_join_hack() if config_filename: print("Using config file %r." % config_filename) assert os.path.exists(config_filename) rnn.init_config(config_filename=config_filename, command_line_options=[]) global config config = rnn.config config.set("task", "dump") config.set("log", None) config.set("log_verbosity", log_verbosity) rnn.init_log() print("Returnn dump-dataset-raw-strings starting up.", file=log.v1) rnn.returnn_greeting() rnn.init_faulthandler()
def _init_base(configfile=None, target_mode=None, epoch=None, sprint_opts=None): """ :param str|None configfile: filename, via init(), this is set :param str|None target_mode: "forward" or so. via init(), this is set :param int epoch: via init(), this is set :param dict[str,str]|None sprint_opts: optional parameters to override values in configfile """ global isInitialized isInitialized = True # Run through in any case. Maybe just to set targetMode. if not getattr(sys, "argv", None): # Set some dummy. Some code might want this (e.g. TensorFlow). sys.argv = [__file__] global Engine global config if not config: # Some subset of what we do in rnn.init(). rnn.init_better_exchook() rnn.init_thread_join_hack() if configfile is None: configfile = DefaultSprintCrnnConfig assert os.path.exists(configfile) rnn.init_config(config_filename=configfile, extra_updates={"task": target_mode}) assert rnn.config config = rnn.config if sprint_opts is not None: config.update(sprint_opts) rnn.init_log() rnn.returnn_greeting(config_filename=configfile) rnn.init_backend_engine() rnn.init_faulthandler(sigusr1_chain=True) rnn.init_config_json_network() if BackendEngine.is_tensorflow_selected(): # Use TFEngine.Engine class instead of Engine.Engine. from returnn.tf.engine import Engine elif BackendEngine.is_theano_selected(): from returnn.theano.engine import Engine import atexit atexit.register(_at_exit_handler) if target_mode: set_target_mode(target_mode) _init_dataset() if target_mode and target_mode == "forward" and epoch: model_filename = config.value('model', '') fns = [ EngineBase.epoch_model_filename(model_filename, epoch, is_pretrain) for is_pretrain in [False, True] ] fn_postfix = "" if BackendEngine.is_tensorflow_selected(): fn_postfix += ".meta" fns_existing = [fn for fn in fns if os.path.exists(fn + fn_postfix)] assert len(fns_existing) == 1, "%s not found" % fns model_epoch_filename = fns_existing[0] config.set('load', model_epoch_filename) assert EngineBase.get_epoch_model(config)[1] == model_epoch_filename, ( "%r != %r" % (EngineBase.get_epoch_model(config), model_epoch_filename)) global engine if not engine: devices = rnn.init_theano_devices() rnn.print_task_properties(devices) rnn.init_engine(devices) engine = rnn.engine assert isinstance(engine, Engine)
def init(config_str, config_dataset, use_pretrain, epoch, verbosity): """ :param str config_str: either filename to config-file, or dict for dataset :param str|None config_dataset: :param bool use_pretrain: might overwrite config options, or even the dataset :param int epoch: :param int verbosity: """ rnn.init_better_exchook() rnn.init_thread_join_hack() dataset_opts = None config_filename = None if config_str.strip().startswith("{"): print("Using dataset %s." % config_str) dataset_opts = eval(config_str.strip()) elif config_str.endswith(".hdf"): dataset_opts = {"class": "HDFDataset", "files": [config_str]} print("Using dataset %r." % dataset_opts) assert os.path.exists(config_str) else: config_filename = config_str print("Using config file %r." % config_filename) assert os.path.exists(config_filename) rnn.init_config(config_filename=config_filename, default_config={"cache_size": "0"}) global config config = rnn.config config.set("log", None) config.set("log_verbosity", verbosity) rnn.init_log() print("Returnn %s starting up." % __file__, file=log.v2) rnn.returnn_greeting() rnn.init_faulthandler() rnn.init_config_json_network() util.BackendEngine.select_engine(config=config) if not dataset_opts: if config_dataset: dataset_opts = "config:%s" % config_dataset else: dataset_opts = "config:train" if use_pretrain: from returnn.pretrain import pretrain_from_config pretrain = pretrain_from_config(config) if pretrain: print("Using pretrain %s, epoch %i" % (pretrain, epoch), file=log.v2) net_dict = pretrain.get_network_json_for_epoch(epoch=epoch) if "#config" in net_dict: config_overwrites = net_dict["#config"] print("Pretrain overwrites these config options:", file=log.v2) assert isinstance(config_overwrites, dict) for key, value in sorted(config_overwrites.items()): assert isinstance(key, str) orig_value = config.typed_dict.get(key, None) if isinstance(orig_value, dict) and isinstance( value, dict): diff_str = "\n" + util.dict_diff_str(orig_value, value) elif isinstance(value, dict): diff_str = "\n%r ->\n%s" % (orig_value, pformat(value)) else: diff_str = " %r -> %r" % (orig_value, value) print("Config key %r for epoch %i:%s" % (key, epoch, diff_str), file=log.v2) config.set(key, value) else: print("No config overwrites for this epoch.", file=log.v2) else: print("No pretraining used.", file=log.v2) elif config.typed_dict.get("pretrain", None): print("Not using pretrain.", file=log.v2) dataset_default_opts = {} Dataset.kwargs_update_from_config(config, dataset_default_opts) print("Using dataset:", dataset_opts, file=log.v2) global dataset dataset = init_dataset(dataset_opts, default_kwargs=dataset_default_opts) assert isinstance(dataset, Dataset) dataset.init_seq_order(epoch=epoch)