-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
50 lines (40 loc) · 1.48 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import logging
import time
import os
import json
import torch
import numpy as np
import pandas as pd
from collections import OrderedDict
from utils.util import plot_confusion_matrix, toConfusionMatrix
from train import AverageMeter, cmMetter, outputToPred
from torch.utils.data import DataLoader
from torchvision import transforms
import multiprocessing
from tqdm import tqdm
_logger = logging.getLogger('test')
def test(model, testloader, accelerator, savedir, args) -> dict:
source_csv_path = os.path.join(args.datadir, args.test_file)
target_csv_path = os.path.join(args.datadir, f'test_{args.exp_name}_{args.exp_num}.csv')
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print(f'The device_age is ready\t>>\t{device}')
print('Loading the model ...')
model = model
print('Loading checkpoint ...')
state_dict = torch.load(os.path.join(savedir, f'best_model.pt'))
model.load_state_dict(state_dict)
print("Starting testing ...")
model.eval()
result = []
pbar_test = tqdm(testloader)
for _, (test_img, _) in enumerate(pbar_test):
pbar_test.set_description(f"Test. iter:")
with torch.no_grad():
test_img = test_img.to(device)
test_pred = torch.max(model(test_img), 1)[1]
result.append(test_pred.item())
pbar_test.close()
df = pd.read_csv(source_csv_path)
df['ans'] = result
df.to_csv(target_csv_path, index=False)
print('Save CSV file', target_csv_path)