def from_model_folder(model_folder, load_stored_params=True, model_param_file=None, iteration=None): """ Loads a DenseCorrespondenceNetwork from a model folder :param model_folder: the path to the folder where the model is stored. This direction contains files like - 003500.pth - training.yaml :type model_folder: :return: a DenseCorrespondenceNetwork objecc t :rtype: """ model_folder = utils.convert_to_absolute_path(model_folder) if model_param_file is None: model_param_file, _, _ = utils.get_model_param_file_from_directory(model_folder, iteration=iteration) model_param_file = utils.convert_to_absolute_path(model_param_file) training_config_filename = os.path.join(model_folder, "training.yaml") training_config = utils.getDictFromYamlFilename(training_config_filename) config = training_config["dense_correspondence_network"] config["path_to_network_params_folder"] = model_folder fcn = resnet_dilated.Resnet34_8s(num_classes=config['descriptor_dimension']) dcn = DenseCorrespondenceNetwork(fcn, config['descriptor_dimension'], image_width=config['image_width'], image_height=config['image_height']) # load the stored params if load_stored_params: # old syntax try: dcn.load_state_dict(torch.load(model_param_file)) except: logging.info("loading params with the new style failed, falling back to dcn.fcn.load_state_dict") dcn.fcn.load_state_dict(torch.load(model_param_file)) # this is the new format # dcn.cuda() dcn.train() dcn.config = config return dcn
def from_model_folder(model_folder, load_stored_params=True, model_param_file=None, iteration=None): """ Loads a DenseCorrespondenceNetwork from a model folder :param model_folder: the path to the folder where the model is stored. This direction contains files like - 003500.pth - training.yaml :type model_folder: :return: a DenseCorrespondenceNetwork objecc t :rtype: """ from_model_folder = False model_folder = utils.convert_to_absolute_path(model_folder) if model_param_file is None: model_param_file, _, _ = utils.get_model_param_file_from_directory( model_folder, iteration=iteration) from_model_folder = True model_param_file = utils.convert_to_absolute_path(model_param_file) training_config_filename = os.path.join(model_folder, "training.yaml") training_config = utils.getDictFromYamlFilename( training_config_filename) config = training_config["dense_correspondence_network"] config["path_to_network_params_folder"] = model_folder config["model_param_filename_tail"] = os.path.split( model_param_file)[1] dcn = DenseCorrespondenceNetwork.from_config( config, load_stored_params=load_stored_params, model_param_file=model_param_file) # whether or not network was constructed from model folder dcn.constructed_from_model_folder = from_model_folder dcn.model_folder = model_folder return dcn