inference()

Do inference prediction.

newsclassifier\inference.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def inference() -> None:
    """Do inference prediction."""
    logger.info("Loading inference data.")
    try:
        test_dataset = NewsDataset(os.path.join(Cfg.preprocessed_data_path, "test.csv"))
        test_loader = DataLoader(test_dataset, batch_size=Cfg.batch_size, shuffle=False, num_workers=4, pin_memory=True, drop_last=False)
    except Exception as e:
        logger.error(e)

    logger.info("loading model.")
    try:
        model = CustomModel(num_classes=Cfg.num_classes)
        model.load_state_dict(torch.load(Cfg.model_path, map_location=torch.device("cpu")))
        model.to(device)
    except Exception as e:
        logger.error(e)

    y_true, y_pred = test_step(test_loader, model)

    print(
        f'Precision: {precision_score(y_true, y_pred, average="weighted")} \n Recall: {recall_score(y_true, y_pred, average="weighted")} \n F1: {f1_score(y_true, y_pred, average="weighted")} \n Accuracy: {accuracy_score(y_true, y_pred)}'
    )

test_step(test_loader, model)

Test Step.

Parameters:
  • test_loader (DataLoader) –

    Test dataset loader.

  • model (_type_) –

    model to be used for inference

Returns:
  • Tuple[ndarray, ndarray]

    Tuple[np.ndarray, np.ndarray]: ground truth labels with predictions.

newsclassifier\inference.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def test_step(test_loader: DataLoader, model) -> Tuple[np.ndarray, np.ndarray]:
    """Test Step.

    Args:
        test_loader (DataLoader): Test dataset loader.
        model (_type_): model to be used for inference

    Returns:
        Tuple[np.ndarray, np.ndarray]: ground truth labels with predictions.
    """
    model.eval()
    y_trues, y_preds = [], []
    with torch.inference_mode():
        for step, (inputs, labels) in tqdm(enumerate(test_loader)):
            inputs = collate(inputs)
            for k, v in inputs.items():
                inputs[k] = v.to(device)
            labels = labels.to(device)
            y_pred = model(inputs)
            y_trues.extend(labels.cpu().numpy())
            y_preds.extend(torch.argmax(y_pred, dim=1).cpu().numpy())
    return np.vstack(y_trues), np.vstack(y_preds)