Exemple #1
0
def load_model(weights_fpath: Path, device=None):
    """
    Loads the model in memory. If this function is not explicitely called, it will be run on the 
    first call to embed_frames() with the default weights file.
    
    :param weights_fpath: the path to saved model weights.
    :param device: either a torch device or the name of a torch device (e.g. "cpu", "cuda"). The 
    model will be loaded and will run on this device. Outputs will however always be on the cpu. 
    If None, will default to your GPU if it"s available, otherwise your CPU.
    """
    # TODO: I think the slow loading of the encoder might have something to do with the device it
    #   was saved on. Worth investigating.
    global _model, _device
    if device is None:
        _device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    elif isinstance(device, str):
        _device = torch.device(device)
    _model = SpeakerEncoder(_device, torch.device("cpu"))
    weights_fpath = Path(weights_fpath)
    weights_fpath = "/home/project/zhrtvc/models-gmw/models/encoder/saved_models/ge2e_pretrained.pt"
    checkpoint = torch.load(weights_fpath, map_location=_device.type)
    _model.load_state_dict(checkpoint["model_state"])
    _model.eval()
    print("Loaded encoder \"%s\" trained to step %d" %
          (Path(weights_fpath).name, checkpoint["step"]))  # 有错误屏蔽掉
Exemple #2
0
    def load(self, weights_fpath: Path, device=None, model_name='default'):
        """
        Loads the model in memory. If this function is not explicitely called, it will be run on the
        first call to embed_frames() with the default weights file.

        :param weights_fpath: the path to saved model weights.
        :param device: either a torch device or the name of a torch device (e.g. "cpu", "cuda"). The
        model will be loaded and will run on this device. Outputs will however always be on the cpu.
        If None, will default to your GPU if it"s available, otherwise your CPU.
        :param model_name: an identifier to uniquely identify a model in order to avoid loading
        the same model more than once
        """
        if model_name in self.models:
            return

        # TODO: I think the slow loading of the encoder might have something to do with the device it
        #   was saved on. Worth investigating.
        if device is None:
            device = torch.device(
                "cuda" if torch.cuda.is_available() else "cpu")
        elif isinstance(device, str):
            device = torch.device(device)
        model = SpeakerEncoder(device, torch.device("cpu"))
        checkpoint = torch.load(
            weights_fpath,
            map_location=None if torch.cuda.is_available() else 'cpu')
        model.load_state_dict(checkpoint["model_state"])
        model.eval()
        logging.info('Loaded encoder {} trained to step {}'.format(
            weights_fpath.name, checkpoint["step"]))

        self.models[model_name] = model
        self.devices[model_name] = device
def load_model(weights_fpath: Path, device=None):
    """
    Loads the model in memory. If this function is not explicitely called, it will be run on the 
    first call to embed_frames() with the default weights file.
    
    :param weights_fpath: the path to saved model weights.
    :param device: either a torch device or the name of a torch device (e.g. "cpu", "cuda"). The 
    model will be loaded and will run on this device. Outputs will however always be on the cpu. 
    If None, will default to your GPU if it"s available, otherwise your CPU.
    """
    # TODO: I think the slow loading of the encoder might have something to do with the device it
    #   was saved on. Worth investigating.
    global _model, _device
    if device is None:
        _device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    elif isinstance(device, str):
        _device = torch.device(device)
    checkpoint = torch.load(weights_fpath)
    if 'quantized' in checkpoint and checkpoint['quantized'] == True:
        _quantized = checkpoint['quantized']
        _device = torch.device("cpu")
        _model = SpeakerEncoder(_device, torch.device("cpu"))
        _model = torch.quantization.quantize_dynamic(_model,
                                                     {nn.LSTM, nn.Linear},
                                                     dtype=torch.qint8)
        _model.load_state_dict(checkpoint["model_state"])
    else:
        _model = SpeakerEncoder(_device, torch.device("cpu"))
        _model.load_state_dict(checkpoint["model_state"])
    _model.eval()
    print("Loaded encoder \"%s\" trained to step %d" %
          (weights_fpath.name, checkpoint["step"]))
Exemple #4
0
def main():
    saved_model_dir = './saved_model/'
    float_model_file = 'pretrained.pt'

    # create directory
    if not os.path.exists(saved_model_dir):
        try:
            os.makedirs(saved_model_dir)
        except OSError as e:
            raise Exception("Could not create directory {0:}. Please check file system permissions.".format(saved_model_dir))
    if not os.path.exists(saved_model_dir + float_model_file):
        raise Exception("Cannot perform static quantization without trained model. Please provide weights file.")

    num_calibration_batches = 10

    # set default device to cpu since pytorch only supports quantization on CPU
    _device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    _device = torch.device("cpu")

    myModel = SpeakerEncoder(_device, torch.device("cpu"))
    checkpoint = torch.load(saved_model_dir + float_model_file)
    myModel.load_state_dict(checkpoint["model_state"])
    myModel.to(_device)
    myModel.eval()

    # Fuse Conv, bn and relu
    # myModel.fuse_model()

    # Specify quantization configuration
    # Start with simple min/max range estimation and per-tensor quantization of weights
    # myModel.qconfig = torch.quantization.default_qconfig
    # print(myModel.qconfig)
    # torch.quantization.prepare(myModel, inplace=True)

    # Calibrate first
    print('Post Training Quantization Prepare: Inserting Observers')
    #print('\n Inverted Residual Block:After observer insertion \n\n', myModel.features[1].conv)

    # Calibrate with the training set
    print('Post Training Quantization: Calibration done')

    # Convert to quantized model
    myModel = torch.quantization.quantize_dynamic(myModel, {nn.LSTM, nn.Linear}, dtype=torch.qint8)
    # torch.quantization.convert(myModel, inplace=True)
    print('Post Training Quantization: Convert done')
    #print('\n Inverted Residual Block: After fusion and quantization, note fused modules: \n\n',myModel.features[1].conv)

    print("Size of model before quantization: ", end="")
    print('{0:} (MB):'.format(os.path.getsize(saved_model_dir + float_model_file)/1e6))
    print("Size of model after quantization: ", end="")
    step = checkpoint['step']
    store_file = {'step': step, 'model_state': myModel.state_dict(), 'quantized': True}
    torch.save(store_file, saved_model_dir + "quantized.pt")
    print('{0:} (MB):'.format(os.path.getsize(saved_model_dir + "quantized.pt")/1e6))
Exemple #5
0
def load_model(weights_fpath: Path):
    """
    Loads the model in memory. If this function is not explicitely called, it will be run on the 
    first call to embed_frames() with the default weights file.
    
    :param weights_fpath: the path to saved model weights.
    """
    # TODO: I think the slow loading of the encoder might have something to do with the device it
    #   was saved on. Worth investigating.
    global _model, _device
    _device = torch.device("cpu")
    _model = SpeakerEncoder(_device, torch.device("cpu"))
    checkpoint = torch.load(weights_fpath, map_location=torch.device('cpu'))
    _model.load_state_dict(checkpoint["model_state"])
    _model.eval()
Exemple #6
0
def load_model(weights_fpath: Path, device=None):
    '''
    This Method loads the model 
    PARAMS:
    weights_fpath: The path to the weights of the pretrained model(Must be a Path object)   
    '''
    global _model, _device
    if device is None:
        _device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    elif isinstance(device, str):
        _device = torch.device(device)
    _model = SpeakerEncoder(_device, torch.device("cpu"))
    checkpoint = torch.load(weights_fpath)
    _model.load_state_dict(checkpoint["model_state"])
    _model.eval()
def get_model(weights_fpath: Path, device=None):
    """
    Loads the model in memory. If this function is not explicitely called, it will be run on the 
    first call to embed_frames() with the default weights file.
    
    :param weights_fpath: the path to saved model weights.
    :param device: either a torch device or the name of a torch device (e.g. "cpu", "cuda"). The 
    model will be loaded and will run on this device. Outputs will however always be on the cpu. 
    If None, will default to your GPU if it"s available, otherwise your CPU.
    """
    # TODO: I think the slow loading of the encoder might have something to do with the device it
    #   was saved on. Worth investigating.
    model = SpeakerEncoder(_device, torch.device("cpu"))
    checkpoint = torch.load(weights_fpath)
    model.load_state_dict(checkpoint["model_state"])
    model.eval()
    print("Loaded encoder \"%s\" trained to step %d" %
          (weights_fpath.name, checkpoint["step"]))
    return model
def load_model(weights_fpath: Path, device=None):
    """
    Loads the model into memory. If this funciton is not explicitly called, it will be run on the
    first call to embed_frames() with the default weights file.

    :param weights_fpath: The path to the saved model weights
    :param device: either a torch device or the name of a torch device (e.g. "cpu", "cuda"). The
    model loaded will be loaded and will run on this device. Outputs however will always be on the
    cpu. If None, will default to your GPU if available, otherwise your CPU.
    """

    global _model, _device
    if device is None:
        _device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    elif isinstance(device, str):
        _device = torch.device(device)
    _model = SpeakerEncoder(_device, torch.device("cpu"))
    checkpoint = torch.load(weights_fpath)
    _model.load_state_dict(checkpoint["model_state"])
    _model.eval()
    print("Loaded encoder \"%s\" trained to step %d" %
          (weights_fpath.name, checkpoint["step"]))