-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
54 lines (44 loc) · 1.38 KB
/
inference.py
File metadata and controls
54 lines (44 loc) · 1.38 KB
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
51
52
53
54
# python imports
import os
import numpy as np
import torch
from torch.utils.data import DataLoader
# local imports
from gsformer import GSFormer, CustomImageDataset
# constants
ROOT = os.path.join("home", "niche", "gsformer")
DIR_DATA = os.path.join(ROOT, "data")
DIR_OUT = os.path.join(ROOT, "out")
DIR_TESTDATA = os.path.join(DIR_DATA, "images", "test")
PATH_OUT = os.path.join(DIR_OUT, "preds.csv")
PATH_MODEL = os.path.join(DIR_OUT, "gsformer.pt")
DEVICE = torch.device("cuda")
BATCH = 128
N_WORKERS = 4
def main():
# 1. load dataset
dataset = CustomImageDataset(DIR_TESTDATA)
loader = DataLoader(dataset, batch_size=BATCH, shuffle=False, num_workers=N_WORKERS)
# 2. configure model
model = GSFormer()
model.load_state_dict(torch.load(PATH_MODEL, map_location=DEVICE))
model.float().to(DEVICE)
model.eval()
# 3. model inference
preds = []
with torch.no_grad():
i = 1
for inputs, labels in loader:
print(f"Batch {i} of {len(loader)}")
inputs = inputs.float().to(DEVICE)
labels = labels.float().to(DEVICE)
pred = model(inputs)
preds.append(pred.cpu().numpy())
i += 1
# 4. save predictions
preds = np.concatenate(preds)
dt_out = loader.dataset.img_labels
dt_out["pred"] = preds
dt_out.to_csv(PATH_OUT, index=False)
if __name__ == "__main__":
main()