-
Notifications
You must be signed in to change notification settings - Fork 2
/
inference.py
72 lines (54 loc) · 2.56 KB
/
inference.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
66
67
68
69
70
71
72
from argparse import ArgumentParser
import random
from pathlib import Path
import numpy as np
import pandas as pd
import pytorch_lightning as pl
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
from pytorch_lightning.loggers import TestTubeLogger
from pytorch_lightning import Trainer
import torch as t
from skimage import io, transform
from pvpower.model import PVModel
from pvpower.data import PVPowerCustomDataset
from pvpower.visualization import visualize_fmap
def create_parser():
parser = ArgumentParser()
parser.add_argument('--checkpoint_path', type=Path, default=Path(__file__).parent / 'lightning_logs',
help='Path to checkpoint file')
parser.add_argument('--data_path', type=Path, help='Data folder')
parser.add_argument('--target_path', type=Path, help='Where to write results')
parser.add_argument('--num_imgs', type=int, default=-1, help='How many images to process? Default: -1 (=all)')
return parser
def run_fn(params):
# load model from checkpoint
model = PVModel.load_from_checkpoint(str(params.checkpoint_path.absolute()), strict=False)
# load data
data = PVPowerCustomDataset(params.data_path.absolute(), model.test_time_transforms(), params.num_imgs)
# prepare model
model.eval()
model = model.cuda()
# accumulate results here
results = list()
# no gradient needed
with t.no_grad():
for i in range(len(data)):
x, _, _, p = data[i]
x = x.unsqueeze(0).cuda() # convert to NCHW cuda tensor
if 'model_variant' in model.hparams and model.hparams.model_variant == 'physical':
# pass through network and perform visualization
fmap_calibration = np.load(params.checkpoint_path.parent / 'fmap_calibration.npy')
y, fmap = model(x, return_features=True)
img = transform.resize(io.imread(p, as_gray=True), (1500, 900))
calibrated_fmap = fmap.cpu().squeeze().numpy()-fmap_calibration # subtract constant bias
visualize_fmap(img.T, calibrated_fmap, params.target_path, p.stem)
else:
# pass through network without visualization
y = model(x)
results.append(dict(path=str(p), relative_power=y.cpu().squeeze().numpy()))
# save results
pd.DataFrame(results).to_csv(params.target_path / 'results.csv')
if __name__ == '__main__':
parser = create_parser()
params = parser.parse_args()
run_fn(params)