Esempio n. 1
0
                                map_location=lambda storage, loc: storage)
        model.load_state_dict(state_dict)
        print("model loaded from {}".format(os.path.abspath(save_model_path)))

    ## generate predictions on test data set
    if run_test:
        ## first optize sigmoid thresholds
        print("optimizing sigmoid cutoffs for each class")
        sig_scores, y_array = get_scores(model, train_loader, dtype)
        sigmoid_threshold = optimize_F2(sig_scores, y_array)
        print("optimal thresholds: ", sigmoid_threshold)

        print("running model on test set")
        test_dataset = ResnetTestDataset(test_csv_path,
                                         test_img_path,
                                         dtype,
                                         img_ext=img_ext,
                                         channel_means=NPY_7CHANNEL_MEAN,
                                         channel_stds=NPY_7CHANNEL_STD)
        test_loader = DataLoader(test_dataset,
                                 batch_size=batch_size,
                                 num_workers=num_workers)
        test_preds = test_model(model,
                                test_loader,
                                train_loader.dataset.mlb,
                                dtype,
                                sigmoid_threshold=sigmoid_threshold,
                                out_file_name=test_results_csv_path)
        print("test set results saved as {}".format(
            os.path.abspath(test_results_csv_path)))
Esempio n. 2
0
            val_acc_history.append(f2_acc)
            loss_history += epoch_losses
            print("END epoch {}/{}: validation F2 score = {:.02f}".format(
                epoch + 1, num_epochs, f2_acc))
        ## serialize model data and save as .pkl file
        torch.save(model.state_dict(), save_model_path)
        print("model saved as {}".format(os.path.abspath(save_model_path)))
        ## save loss and accuracy as .mat file
        save_accuracy_and_loss_mat(save_mat_path, train_acc_history,
                                   val_acc_history, loss_history, num_epochs)
    ## load model params from file
    else:
        state_dict = torch.load(save_model_path,
                                map_location=lambda storage, loc: storage)
        model.load_state_dict(state_dict)
        print("model loaded from {}".format(os.path.abspath(save_model_path)))
    ## generate predictions on test data set
    if run_test:
        test_dataset = AmazonTestDataset(test_csv_path, test_img_path, img_ext,
                                         dtype)
        test_loader = DataLoader(test_dataset,
                                 batch_size=batch_size,
                                 num_workers=num_workers)
        test_preds = test_model(model,
                                test_loader,
                                train_loader.dataset.mlb,
                                dtype,
                                out_file_name=test_results_csv_path)
        print("test set results saved as {}".format(
            os.path.abspath(test_results_csv_path)))