Esempio n. 1
0
def load_model(load_path: dict, device: torch.device) -> FullModel:
    """
    Instantiate the model from saved dictionary of pretrained model.
    Args:
        num_graph_layers: Number of layers to use in the model.
        num_classes: Number of classes in the dataset.
        pretrain_load_path: Use pretrained weights.
    Returns:
        The instantiated model with the requested parameters.
    """

    ckpt = torch.load(f=load_path, map_location=device)
    model_params_dict = ckpt["model_params_dict"]
    model_params_dict['graph_layers_sizes'] = model_params_dict[
        'graph_layers_sizes'][1:]
    model = FullModel(
        model_type=model_params_dict['model_type'],
        num_classes=model_params_dict['num_classes'],
        num_node_features=model_params_dict['num_node_features'],
        graph_layers_sizes=model_params_dict['graph_layers_sizes'],
        text_embed_dim=model_params_dict['text_embed_dim'],
        text_output_dim=model_params_dict['text_output_dim'],
        linear_layers_sizes=model_params_dict['linear_layers_sizes'],
        dropout_rate=model_params_dict['dropout_rate'],
        vocab_size=model_params_dict['vocab_size'])

    model.load_state_dict(state_dict=ckpt["model_state_dict"])

    # transfer model to cpu or gpu
    model = model.to(device=device)

    return model, model_params_dict
Esempio n. 2
0
    ### Create model
    CRNN_model = CRNN(IN_SIZE, HIDDEN_SIZE, KERNEL_SIZE, STRIDE,
                      GRU_NUM_LAYERS)
    attn_layer = AttnMech(HIDDEN_SIZE * NUM_DIRS)
    apply_attn = ApplyAttn(HIDDEN_SIZE * 2, NUM_CLASSES)

    ### Download ready models
    # checkpoint = torch.load('crnn_final', map_location=device)
    # CRNN_model.load_state_dict(checkpoint['model_state_dict'])
    # checkpoint = torch.load('attn_final', map_location=device)
    # attn_layer.load_state_dict(checkpoint['model_state_dict'])
    # checkpoint = torch.load('apply_attn_final', map_location=device)
    # apply_attn.load_state_dict(checkpoint['model_state_dict'])

    full_model = FullModel(CRNN_model, attn_layer, apply_attn)
    print(full_model.to(device))
    print(count_parameters(full_model))
    #wandb.init()
    #wandb.watch(full_model)

    ### Create optimizer
    opt = torch.optim.Adam(full_model.parameters(), weight_decay=1e-5)

    ### Train_val loop
    for n in range(NUM_EPOCHS):
        train_epoch(full_model,
                    opt,
                    train_loader,
                    melspec_train,
                    GRU_NUM_LAYERS,
                    HIDDEN_SIZE,