Example #1
0
def main():
    args = parse_args()

    # dataset
    X_train, y_train, X_val, y_val, X_test, y_test = load_dataset(flatten=True)

    # model layer dimensions
    input_dim = X_train.shape[1]
    num_classes = 10

    # create model
    model = Model()
    model.add(Dense(input_dim, 100), activation='relu')
    model.add(Dense(100, 200), activation='relu')
    model.add(Dense(200, 200), activation='relu')
    model.add(Dense(200, num_classes))

    # train model
    model.fit(X_train,
              y_train,
              val_data=(X_val, y_val),
              verbose=True,
              epochs=args.epochs,
              batch_size=args.batch_size,
              lr=args.lr)

    # evaluate model
    model.eval(X_test, y_test, verbose=True)
Example #2
0
def main():
    seed = 0
    fix_seed(seed)

    data = cr.Dataset(
        data_paths=cfg.data_paths,
        exp_id=cfg.exp_id,
        img_shape=cfg.img_shape,
        img_crop_size=cfg.img_crop_size,
        max_trace=cfg.max_trace_len,
    )

    x_train, x_test, y_train, y_test = data.split_training_test_data(
        test_split=.20, seed=10, for_deep=True)

    trainsets = NNDataset(x_train, y_train, DEVICE)
    testsets = NNDataset(x_test, y_test, DEVICE)
    train_loader = torch.utils.data.DataLoader(trainsets, batch_size=32)
    test_loader = torch.utils.data.DataLoader(testsets, batch_size=32)

    for model_name in MODEL_LIST:
        print(f'\n======== {model_name} ========\n')
        if model_name == 'LSTM':
            model = Model(t_stage='LSTM',
                          device=DEVICE,
                          t_hidden_dim=500,
                          t_output_dim=500,
                          use_cnn_for_trace=False)
        elif model_name == 'CNN_LSTM':
            model = Model(t_stage='LSTM',
                          device=DEVICE,
                          t_hidden_dim=500,
                          t_output_dim=500)
        elif model_name == 'OnlyCNN':
            model = Model(s_stage='CNN', device=DEVICE, block_num=3)
        else:
            model = Model(s_stage=model_name,
                          t_stage='LSTM',
                          device=DEVICE,
                          pretrained=PRETRAINED,
                          block_num=3,
                          t_hidden_dim=500,
                          t_output_dim=500)
        if MODE == 'train':
            score, model = train(model,
                                 model_name,
                                 train_loader,
                                 test_loader,
                                 DEVICE,
                                 log_path=f'{ROOT}/out/{model_name}.txt')
            model = model.to('cpu')
            torch.save(model.state_dict(),
                       f'{ROOT}/best_models/{model_name}.pth')
        elif MODE == 'fps':
            model.eval()
            inputs = (torch.rand(1, 1,
                                 500).to(DEVICE), torch.rand(1, 1, 80,
                                                             80).to(DEVICE))
            t0 = time.time()
            for i in range(100):
                model(inputs)
            with open(f'{ROOT}/out/speed.txt', 'a') as f:
                f.write(f'{model_name}: {100 / (time.time() - t0):.04f} fps\n')
        else:
            raise ValueError