def load_model(dirname, device, weights=None, half=False): """ Load a model from disk """ if not os.path.isdir(dirname) and os.path.isdir( os.path.join(__dir__, "models", dirname)): dirname = os.path.join(__dir__, "models", dirname) if not weights: # take the latest checkpoint weight_files = glob(os.path.join(dirname, "weights_*.tar")) if not weight_files: raise FileNotFoundError("no model weights found in '%s'" % dirname) weights = max( [int(re.sub(".*_([0-9]+).tar", "\\1", w)) for w in weight_files]) device = torch.device(device) config = os.path.join(dirname, 'config.toml') weights = os.path.join(dirname, 'weights_%s.tar' % weights) model = Model(toml.load(config)) model.to(device) state_dict = torch.load(weights, map_location=device) new_state_dict = OrderedDict() for k, v in state_dict.items(): name = k.replace('module.', '') new_state_dict[name] = v model.load_state_dict(new_state_dict) if half: model = model.half() model.eval() return model
def load_model(dirname, device, weights=None): """ Load a model from disk """ if not weights: # take the latest checkpoint weight_files = glob(os.path.join(dirname, "weights_*.tar")) weights = max( [int(re.sub(".*_([0-9]+).tar", "\\1", w)) for w in weight_files]) device = torch.device(device) config = os.path.join(dirname, 'config.toml') weights = os.path.join(dirname, 'weights_%s.tar' % weights) model = Model(toml.load(config)) model.to(device) model.load_state_dict(torch.load(weights, map_location=device)) model.eval() return model