-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhydra_predict.py
65 lines (47 loc) · 1.74 KB
/
hydra_predict.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import hydra
import numpy as np
import pytorch_lightning as pl
import torch
from omegaconf import DictConfig
from pytorch_lightning.loggers import CometLogger, TensorBoardLogger
from src.lightning_classes.lightning_wheat import LitWheat
from src.utils.get_dataset import get_test_dataset
from src.utils.utils import set_seed, format_prediction_string, collate_fn
def predict(cfg: DictConfig) -> None:
"""
Run pytorch-lightning model
Args:
cfg: hydra config
"""
set_seed(cfg.training.seed)
test_dataset = get_test_dataset(cfg)
path = r'wheat\outputs\2020_05_06_09_32_36\saved_models\_ckpt_epoch_0.ckpt'
model = LitWheat.load_from_checkpoint(checkpoint_path=path)
model.eval()
test_loader = torch.utils.data.DataLoader(
test_dataset,
batch_size=cfg.data.batch_size,
num_workers=cfg.data.num_workers,
shuffle=False,
collate_fn=collate_fn,
)
detection_threshold = 0.5
results = []
for images, _, image_ids in test_loader:
images = (image.to(cfg.general) for image in images)
outputs = model(images)
for i, _ in enumerate(images):
boxes = outputs[i]['boxes'].data.cpu().numpy()
scores = outputs[i]['scores'].data.cpu().numpy()
boxes = boxes[scores >= detection_threshold].astype(np.int32)
scores = scores[scores >= detection_threshold]
image_id = image_ids[i]
result = {'image_id': image_id, 'PredictionString': format_prediction_string(boxes, scores)}
results.append(result)
@hydra.main(config_path='conf/config.yaml')
def run_model(cfg: DictConfig) -> None:
print(cfg.pretty())
predict(cfg)
if __name__ == '__main__':
run_model()