-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
146 lines (118 loc) · 4.46 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import sys
from pathlib import Path
project_dir = Path(__file__).parent.resolve()
sys.path.insert(0, str(project_dir / "src"))
import argparse
import os
import gdown
import torch
import tqdm
import yaml
from einops import rearrange
import inv3d_illuminator.models.model_factory as model_factory
from inv3d_util.load import load_image, save_image
from inv3d_util.path import list_dirs
model_sources = yaml.safe_load((project_dir / "models.yaml").read_text())
data_source = "https://drive.google.com/drive/folders/1Zoj9ydSp5TSztha1VULNFl_b9-GT6rmk?usp=sharing"
def inference(model_name: str, dataset: str, gpu: int):
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu)
# prepare model
model_url = model_sources[model_name]
model_dir = project_dir / "models" / model_name
if not model_dir.is_dir():
gdown.download_folder(model_url, output=model_dir.as_posix())
# prepare data
inv3d_real_unwarp_dir = Path("input/inv3d_real_unwarp")
if dataset == "inv3d_real_unwarp" and not inv3d_real_unwarp_dir.is_dir():
gdown.download_folder(data_source, output=inv3d_real_unwarp_dir.as_posix())
# search checkpoint
checkpoints = list(model_dir.rglob("checkpoint-epoch=*.ckpt"))
if len(checkpoints) > 0:
checkpoint = max(
checkpoints, key=lambda x: int(x.stem.replace("=", "-").split("-")[2])
)
else:
checkpoint = model_dir / "checkpoints" / "last.ckpt"
if not checkpoint.is_file():
print(f"ERROR! Could not find a checkpoint in directory '{model_dir}'")
# load model
model = model_factory.load_from_checkpoint(model_name.split("@")[0], checkpoint)
model.to("cuda")
model.eval()
input_dir = project_dir / "input" / dataset
output_dir = project_dir / "output" / f"{dataset} - {model_name}"
output_dir.mkdir(exist_ok=True)
image_paths = list(input_dir.rglob("norm_image.png"))
for image_path in tqdm.tqdm(image_paths, "Remove illumination"):
# prepare image
image = load_image(image_path)
image = rearrange(image, "h w c -> () c h w")
image = image.astype("float32") / 255
image = torch.from_numpy(image)
image = image.to("cuda")
model_kwargs = {"data": {"input": {"image": image}}}
# prepare template
if "template" in model_name:
template_path = image_path.parent / "template.png"
template = load_image(template_path)
template = rearrange(template, "h w c -> () c h w")
template = template.astype("float32") / 255
template = torch.from_numpy(template)
template = template.to("cuda")
model_kwargs["data"]["input"]["template"] = template
if "pad" in model_name:
model_kwargs["template_patch_padding"] = int(model_name.split("=")[-1])
if "full" in model_name:
model_kwargs["template_patch_padding"] = None
# inference model
out_image = model(**model_kwargs)
# post process image
out_image = out_image.detach().cpu()
out_image = out_image.numpy()
out_image = (out_image * 255).astype("uint8")
out_image = rearrange(out_image, "() c h w -> h w c")
# export results
sample_name = "_".join(image_path.relative_to(input_dir).parent.parts)
save_image(
output_dir / f"corrected_{sample_name}.png",
out_image,
override=True,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--model",
type=str,
choices=list(model_sources.keys()),
required=True,
help="Select the model and the dataset used for training.",
)
parser.add_argument(
"--dataset",
type=str,
choices=list(
sorted(
list(
set(
list(map(lambda x: x.name, list_dirs(project_dir / "input")))
+ ["inv3d_real_unwarp"]
)
)
)
),
required=True,
help="Selects the inference dataset. All folders in the input directory can be selected.",
)
parser.add_argument(
"--gpu",
type=int,
required=True,
help="The index of the GPU to use for inference.",
)
args = parser.parse_args()
inference(
model_name=args.model,
dataset=args.dataset,
gpu=args.gpu,
)