def model_fn(model_dir):
    """Load the PyTorch model from the `model_dir` directory."""
    print("Loading model.")

    # First, load the parameters used to create the model.
    model_info = {}
    model_info_path = os.path.join(model_dir, 'model_info.pth')
    with open(model_info_path, 'rb') as f:
        model_info = torch.load(f)

    print("model_info: {}".format(model_info))

    # Determine the device and construct the model.
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = LSTMClassifier(model_info['embedding_dim'],
                           model_info['hidden_dim'], model_info['vocab_size'])

    # Load the stored model parameters.
    model_path = os.path.join(model_dir, 'model.pth')
    with open(model_path, 'rb') as f:
        model.load_state_dict(torch.load(f))

    # Load the saved word_dict.
    word_dict_path = os.path.join(model_dir, 'word_dict.pkl')
    with open(word_dict_path, 'rb') as f:
        model.word_dict = pickle.load(f)

    model.to(device).eval()

    print("Done loading model.")
    return model
def get_prediction(tweet):
    vocab2index = json.load(open(vocab2index_path))
    words = json.load(open(words_path))
    input_dim = len(words)
    model = LSTMClassifier(
        input_dim,
        embed_dim,
        hidden_dim,
        output_dim,
        n_layers,
        bidirectional,
        dropout,
        padding_idx,
        batch_first,
    )
    model.load_state_dict(
        torch.load(model_path, map_location=torch.device("cpu")))
    return predict(tweet, model, vocab2index)
def model_fn(model_dir):
    """
    Load the PyTorch model from the `model_dir` directory
    """

    # Begin loading model:
    print("Loading model: Beginning...\n")

    # First, load the parameters used to create the model:
    model_info = {}
    model_info_path = os.path.join(model_dir, 'model_info.pth')
    with open(model_info_path, 'rb') as f:
        model_info = torch.load(f)
    print("*** Model info: {}".format(model_info))

    # Determine the device:
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print("*** Device: {}".format(device))

    # Construct the model:
    model = LSTMClassifier(model_info['embedding_dim'],
                           model_info['hidden_dim'], model_info['vocab_size'])

    # Load the store model parameters:
    model_path = os.path.join(model_dir, 'model.pth')
    with open(model_path, 'rb') as f:
        model.load_state_dict(torch.load(f))

    # Load the saved word_dict:
    word_dict_path = os.path.join(model_dir, 'word_dict.pkl')
    with open(word_dict_path, 'rb') as f:
        model.word_dict = pickle.load(f)

    # Move to evaluation mode:
    model.to(device).eval()

    # Print built model:
    print("*** Model:\n{}".format(model))

    # End loading model:
    print("\nLoading model: Done...")

    # Return model:
    return model
Exemple #4
0
def test(args):
    dataset_test = FirmaData_select_subjects(args.data_dir, 30, args.subset_par[0], args.subset_par[1], args.subset_par[2],args.subjects_list,
                                          subset='test', pre_process=False)
    dat_loader_test = DataLoader(dataset_test, batch_size=args.batch_size, shuffle=True)
    if args.test_all:
        for loadid in range(args.num_epochs):
            saved_model = os.path.join(args.save_path, 'model_' + str(loadid) + '.tar')
            checkpoint = torch.load(saved_model)

            model = LSTMClassifier(dataset_test[0][0].shape[1], args.hidden_dim, output_size=3)
            model.cuda()
            model.load_state_dict(checkpoint['model_state_dict'])
            acc,f1,_= evaluate_test_set(model, dat_loader_test)
            print('model {} test_accuracy:{:5.4f}, f1_score:{:5.4f}'.format(loadid,acc,f1))
    else:
        loadid=args.test_id
        saved_model = os.path.join(args.save_path, 'model_' + str(loadid) + '.tar')
        checkpoint = torch.load(saved_model)
        model = LSTMClassifier(dataset_test[0][0].shape[1], args.hidden_dim, output_size=3)
        model.cuda()
        model.load_state_dict(checkpoint['model_state_dict'])
        acc,f1, _ = evaluate_test_set(model, dat_loader_test)
        print('model {} test_accuracy:{:5.4f}, f1_score:{:5.4f}'.format(loadid,acc,f1))
Exemple #5
0
def train(args):
    #subject_lists=[[5,7,9],[12,9,16],[2,11,5],[17,9,6],[1,13,6]]
    #subject_lists = [[1,14,15,2,6,16,7],[3,12,4,15,9,10,2],[10,14,7,11,15,8,17],[16,10,6,5,13,8,12],[17,2,13,4,7,8,16]]
    #subject_lists=[[1,7,2,8,6,11,5,15,9,3],[4,3,10,11,15,7,16,6,14,17],[5,4,12,6,10,8,15,13,2,11],[13,4,6,3,7,12,2,10,16,5],[12,15,17,13,3,9,5,14,8,2]]
    subject_lists=[[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]]
    window_sizes=[1,5,15,30,60]
    scns=['shared_data_1','shared_data_2']
    n_weeks=['1_weeks','2_weeks','3_weeks']
    random.seed(args.seed)
    logfile=open('log.txt','w+')
    for scn in scns:
        for n_week in n_weeks:
            data_dir = os.path.join(args.data_dir, scn, n_week)
            for window_size in window_sizes:
                for subjects_list in subject_lists:
                    dataset_train = FirmaData_select_subjects(data_dir, window_size, args.subset_par[0], args.subset_par[1],
                                                           args.subset_par[2], subjects_list,subset='train', pre_process=False)
                    dataset_val = FirmaData_select_subjects(data_dir, window_size, args.subset_par[0], args.subset_par[1], args.subset_par[2],subjects_list,
                                                         subset='val', pre_process=False)
                    dataset_test=FirmaData_select_subjects(data_dir, window_size, args.subset_par[0], args.subset_par[1], args.subset_par[2],subjects_list,
                                                  subset='test', pre_process=False)
                    dat_loader_train = DataLoader(dataset_train, batch_size=args.batch_size, shuffle=True)
                    dat_loader_val = DataLoader(dataset_val, batch_size=args.batch_size, shuffle=True)
                    dat_loader_test = DataLoader(dataset_test,batch_size=args.batch_size, shuffle=True)
                    model = LSTMClassifier(dataset_train[0][0].shape[1], args.hidden_dim, output_size=len(subjects_list))
                    model.cuda()
                    optimizer = optim.Adam(model.parameters(), lr=args.learning_rate)
                    save_pa= os.path.join(args.save_path, scn, n_week, str(subjects_list),str(window_size))
                    _, test_id= train_model(model, optimizer, dat_loader_train, dat_loader_val, args.num_epochs, save_pa)
                    saved_model = os.path.join(save_pa, 'model_' + str(test_id) + '.tar')
                    checkpoint = torch.load(saved_model)
                    model.load_state_dict(checkpoint['model_state_dict'])
                    acc, f1, _ = evaluate_test_set(model, dat_loader_test)
                    logfile.write(scn+' '+n_week+' ' + 'subjects:  '+ str(subjects_list)+ 'window_size {} model {} test_accuracy:{:5.4f}, f1_score:{:5.4f}'.format(window_size,test_id,acc,f1) +"\n")
                    logfile.flush()
    logfile.close()
Exemple #6
0
    print('Done !')

    ### Load data
    print('Loading Data ... ', end='')

    d_test = TextDataset(word2idx, fp_test, train=False)
    test_loader = DataLoader(d_test, batch_size=batch_size, shuffle=False)

    print('Done !')

    ### Load model
    print('Loading Model ... ', end='')

    model = LSTMClassifier(embedding_dim, hidden_dim, num_layers, batch_size)
    model.cuda()
    model.load_state_dict(torch.load(fp_model))

    print('Done !')

    ### Predict
    print('Predict ... ', end='')

    pred = predict(model, test_loader)

    print('Done !')

    ### Write
    print('Write ... ', end='')

    df_pred = pd.DataFrame()
    df_pred['id'] = np.arange(len(pred) - 1)
Exemple #7
0
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

model.to(device)
optim = torch.optim.Adam(model.parameters(), lr=lr)
loss = torch.nn.CrossEntropyLoss()

train_loss, train_acc, val_loss, val_acc = train_model(model=model,
                                                       train_iter=train_iter,
                                                       val_iter=val_iter,
                                                       optim=optim,
                                                       loss=loss,
                                                       num_epochs=num_epochs,
                                                       batch_size=batch_size)

model.load_state_dict(torch.load('state_dict.pth'))
model.eval()
results_target = list()

with torch.no_grad():
    for batch in tqdm(test_iter):
        for text in zip(batch.text[0]):  #, batch.id):
            text = text[0].unsqueeze(0)
            res, _ = model(text, hidden=None)

            target = np.round(res.cpu().numpy())

            results_target.append(target[0][1])

print("Making submit")
make_submit(results_target)
Exemple #8
0
def main():
    parser = argparse.ArgumentParser("Script to train model on a GPU")
    parser.add_argument(
        "--checkpoint",
        type=str,
        default=None,
        help=
        "Optional path to saved model, if none provided, the model is trained from scratch."
    )
    parser.add_argument("--n_epochs",
                        type=int,
                        default=5,
                        help="Number of training epochs.")
    args = parser.parse_args()

    sampling_rate = 125
    n_velocity_bins = 32
    seq_length = 1024
    n_tokens = 256 + sampling_rate + n_velocity_bins
    #early_stopping = 100000   # very high value to basically turn it off
    early_stopping = 200  # regular value
    # transformer = MusicTransformer(n_tokens, seq_length,
    #         d_model = 64, n_heads = 8, d_feedforward=256,
    #         depth = 4, positional_encoding=True, relative_pos=True, xavier_init=True)
    # set xavier_init = True to run xavier_init optimization
    # transformer = LongMusicTransformer(n_tokens, seq_length,
    #  d_model=64, n_heads=8, d_feedforward=256,
    #  depth=4, positional_encoding=True, relative_pos=False,
    #  xavier_init=True)
    transformer = LSTMClassifier(input_dim=1,
                                 hidden_dim=413,
                                 label_size=413,
                                 n_tokens=n_tokens,
                                 xavier_init=True)
    if args.checkpoint is not None:
        state = torch.load(args.checkpoint)
        transformer.load_state_dict(state)
        print(f"Successfully loaded checkpoint at {args.checkpoint}")
    #rule of thumb: 1 minute is roughly 2k tokens

    pipeline = PreprocessingPipeline(input_dir="data",
                                     stretch_factors=[0.975, 1, 1.025],
                                     split_size=30,
                                     sampling_rate=sampling_rate,
                                     n_velocity_bins=n_velocity_bins,
                                     transpositions=range(-2, 3),
                                     training_val_split=0.9,
                                     max_encoded_length=seq_length + 1,
                                     min_encoded_length=257)
    pipeline_start = time.time()
    pipeline.run()
    runtime = time.time() - pipeline_start
    print(f"MIDI pipeline runtime: {runtime / 60 : .1f}m")

    today = datetime.date.today().strftime('%m%d%Y')
    t = str(time.time())
    # checkpoint = f"saved_models/tf_{today}_{t}"
    checkpoint = f"saved_models/tf_lstm_both"
    training_sequences = pipeline.encoded_sequences['training']
    validation_sequences = pipeline.encoded_sequences['validation']

    batch_size = 16

    train(transformer,
          training_sequences,
          validation_sequences,
          epochs=args.n_epochs,
          evaluate_per=1,
          batch_size=batch_size,
          batches_per_print=100,
          padding_index=0,
          checkpoint_path=checkpoint,
          early_stopping_value=early_stopping)
Exemple #9
0
    # First, load the parameters used to create the model.
    model_info = {}
    model_info_path = os.path.join(model_dir, 'model_info.pth')
    with open(model_info_path, 'rb') as f:
        model_info = torch.load(f)
​
    print("model_info: {}".format(model_info))
​
    # Determine the device and construct the model.
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = LSTMClassifier(model_info['embedding_dim'], model_info['hidden_dim'], model_info['vocab_size'])
​
    # Load the stored model parameters.
    model_path = os.path.join(model_dir, 'model.pth')
    with open(model_path, 'rb') as f:
        model.load_state_dict(torch.load(f))
​
    # Load the saved word_dict.
    word_dict_path = os.path.join(model_dir, 'word_dict.pkl')
    with open(word_dict_path, 'rb') as f:
        model.word_dict = pickle.load(f)
​
    model.to(device).eval()
​
    print("Done loading model.")
    return model
​
def _get_train_data_loader(batch_size, training_dir):
    print("Get train data loader.")
​
    train_data = pd.read_csv(os.path.join(training_dir, "train.csv"), header=None, names=None)