-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload_cyclegan_test.py
80 lines (66 loc) · 2.56 KB
/
load_cyclegan_test.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
import os
import time
import numpy as np
from config import BEAMNG_SIM_NAME
from cyclegan.data import create_dataset
from cyclegan.data.base_dataset import get_transform
from cyclegan.models import create_model
from cyclegan.util.util import get_base_and_test_default_options, tensor2im
from PIL import Image
from utils.dataset_utils import image_size_after_crop
if __name__ == "__main__":
cyclegan_experiment_name = "beamng"
gpu_ids = "0"
cyclegan_checkpoints_dir = f"cyclegan{os.sep}checkpoints"
cyclegan_epoch = "35"
opt = get_base_and_test_default_options(
name=cyclegan_experiment_name,
gpu_ids=gpu_ids,
checkpoints_dir=cyclegan_checkpoints_dir,
epoch=cyclegan_epoch,
dataroot="cyclegan",
dataset_mode="archive",
archive_filepath=f"logs{os.sep}beamng-2022_05_31_18_50_03-archive-agent-autopilot-seed-0-episodes-1.npz",
max_dataset_size=float("inf"),
)
dataset = create_dataset(
opt
) # create a dataset given opt.dataset_mode and other options
cyclegan_model = create_model(
opt
) # create a model given opt.model and other options
cyclegan_model.setup(
opt
) # regular setup: load and print networks; create schedulers
cyclegan_model.eval()
cyclegan_options = opt
print("Loaded cyclegan model")
print(cyclegan_model.device)
input_nc = (
cyclegan_options.output_nc
if cyclegan_options.direction == "BtoA"
else cyclegan_options.input_nc
)
transform = get_transform(opt=cyclegan_options, grayscale=(input_nc == 1))
image_size = image_size_after_crop(
env_name=BEAMNG_SIM_NAME,
original_image_size=cyclegan_options.original_image_size,
)
for i, data in enumerate(dataset):
cyclegan_model.set_input(data) # unpack data from data loader
start_time = time.perf_counter()
cyclegan_model.test() # run inference
print("Inference time: {:.2f}s".format(time.perf_counter() - start_time))
start_time = time.perf_counter()
im_data = cyclegan_model.get_current_visuals().get("fake")
# print(im_data.size())
im = tensor2im(im_data)
# print(im.shape)
pil_image = Image.fromarray(im)
# for some reason PIL wants the size parameter reversed
pil_image = pil_image.resize(size=reversed(image_size))
im = np.asarray(pil_image)
# print(im.shape)
# assert False
# im = tensor2im(im_data)
# print("Back and forth time: {:.2f}s".format(time.perf_counter() - start_time))