forked from Project-MONAI/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
113 lines (97 loc) · 3.87 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
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import json
import logging
import os
import sys
from datetime import datetime
from pathlib import Path
import numpy as np
import torch
from generative.inferers import LatentDiffusionInferer
from generative.networks.schedulers import DDPMScheduler
from monai.config import print_config
from monai.utils import set_determinism
from PIL import Image
from utils import define_instance
from visualize_image import visualize_2d_image
def main():
parser = argparse.ArgumentParser(description="PyTorch Object Detection Training")
parser.add_argument(
"-e",
"--environment-file",
default="./config/environment.json",
help="environment json file that stores environment path",
)
parser.add_argument(
"-c",
"--config-file",
default="./config/config_train_32g.json",
help="config json file that stores hyper-parameters",
)
parser.add_argument(
"-n",
"--num",
type=int,
default=1,
help="number of generated images",
)
args = parser.parse_args()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print_config()
torch.backends.cudnn.benchmark = True
torch.set_num_threads(4)
env_dict = json.load(open(args.environment_file, "r"))
config_dict = json.load(open(args.config_file, "r"))
for k, v in env_dict.items():
setattr(args, k, v)
for k, v in config_dict.items():
setattr(args, k, v)
set_determinism(42)
# load trained networks
autoencoder = define_instance(args, "autoencoder_def").to(device)
trained_g_path = os.path.join(args.model_dir, "autoencoder.pt")
autoencoder.load_state_dict(torch.load(trained_g_path))
diffusion_model = define_instance(args, "diffusion_def").to(device)
trained_diffusion_path = os.path.join(args.model_dir, "diffusion_unet_last.pt")
diffusion_model.load_state_dict(torch.load(trained_diffusion_path))
scheduler = DDPMScheduler(
num_train_timesteps=args.NoiseScheduler["num_train_timesteps"],
schedule="scaled_linear_beta",
beta_start=args.NoiseScheduler["beta_start"],
beta_end=args.NoiseScheduler["beta_end"],
)
inferer = LatentDiffusionInferer(scheduler, scale_factor=1.0)
Path(args.output_dir).mkdir(parents=True, exist_ok=True)
latent_shape = [p // 4 for p in args.diffusion_train["patch_size"]]
noise_shape = [1, args.latent_channels] + latent_shape
for _ in range(args.num):
noise = torch.randn(noise_shape, dtype=torch.float32).to(device)
with torch.no_grad():
synthetic_images = inferer.sample(
input_noise=noise,
autoencoder_model=autoencoder,
diffusion_model=diffusion_model,
scheduler=scheduler,
)
filename = os.path.join(args.output_dir, datetime.now().strftime("synimg_%Y%m%d_%H%M%S.jpeg"))
final_img = synthetic_images[0, 0, ...].cpu().numpy().transpose(1, 0)[::-1, ::-1]
img = Image.fromarray(visualize_2d_image(final_img), "RGB")
img.save(filename)
if __name__ == "__main__":
logging.basicConfig(
stream=sys.stdout,
level=logging.INFO,
format="[%(asctime)s.%(msecs)03d][%(levelname)5s](%(name)s) - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
main()