Skip to content

Commit 6151078

Browse files
committed
simplify visualizer
1 parent aa0b8a9 commit 6151078

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

test.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from options.test_options import TestOptions
33
from data import CreateDataLoader
44
from models import create_model
5-
from util.visualizer import Visualizer
5+
from util.visualizer import save_images
66
from util import html
77

88

@@ -16,7 +16,6 @@
1616
data_loader = CreateDataLoader(opt)
1717
dataset = data_loader.load_data()
1818
model = create_model(opt)
19-
visualizer = Visualizer(opt)
2019
# create website
2120
web_dir = os.path.join(opt.results_dir, opt.name, '%s_%s' % (opt.phase, opt.which_epoch))
2221
webpage = html.HTML(web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.which_epoch))
@@ -28,7 +27,8 @@
2827
model.test()
2928
visuals = model.get_current_visuals()
3029
img_path = model.get_image_paths()
31-
print('%04d: process image... %s' % (i, img_path))
32-
visualizer.save_images(webpage, visuals, img_path, aspect_ratio=opt.aspect_ratio)
30+
if i % 5 == 0:
31+
print('processing (%04d)-th image... %s' % (i, img_path))
32+
save_images(webpage, visuals, img_path, aspect_ratio=opt.aspect_ratio, width=opt.display_winsize)
3333

3434
webpage.save()

util/visualizer.py

+26-25
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@
77
from scipy.misc import imresize
88

99

10+
# save image to the disk
11+
def save_images(webpage, visuals, image_path, aspect_ratio=1.0, width=256):
12+
image_dir = webpage.get_image_dir()
13+
short_path = ntpath.basename(image_path[0])
14+
name = os.path.splitext(short_path)[0]
15+
16+
webpage.add_header(name)
17+
ims, txts, links = [], [], []
18+
19+
for label, im_data in visuals.items():
20+
im = util.tensor2im(im_data)
21+
image_name = '%s_%s.png' % (name, label)
22+
save_path = os.path.join(image_dir, image_name)
23+
h, w, _ = im.shape
24+
if aspect_ratio > 1.0:
25+
im = imresize(im, (h, int(w * aspect_ratio)), interp='bicubic')
26+
if aspect_ratio < 1.0:
27+
im = imresize(im, (int(h / aspect_ratio), w), interp='bicubic')
28+
util.save_image(im, save_path)
29+
30+
ims.append(image_name)
31+
txts.append(label)
32+
links.append(image_name)
33+
webpage.add_images(ims, txts, links, width=width)
34+
35+
1036
class Visualizer():
1137
def __init__(self, opt):
1238
self.display_id = opt.display_id
@@ -124,28 +150,3 @@ def print_current_losses(self, epoch, i, losses, t, t_data):
124150
print(message)
125151
with open(self.log_name, "a") as log_file:
126152
log_file.write('%s\n' % message)
127-
128-
# save image to the disk
129-
def save_images(self, webpage, visuals, image_path, aspect_ratio=1.0):
130-
image_dir = webpage.get_image_dir()
131-
short_path = ntpath.basename(image_path[0])
132-
name = os.path.splitext(short_path)[0]
133-
134-
webpage.add_header(name)
135-
ims, txts, links = [], [], []
136-
137-
for label, im_data in visuals.items():
138-
im = util.tensor2im(im_data)
139-
image_name = '%s_%s.png' % (name, label)
140-
save_path = os.path.join(image_dir, image_name)
141-
h, w, _ = im.shape
142-
if aspect_ratio > 1.0:
143-
im = imresize(im, (h, int(w * aspect_ratio)), interp='bicubic')
144-
if aspect_ratio < 1.0:
145-
im = imresize(im, (int(h / aspect_ratio), w), interp='bicubic')
146-
util.save_image(im, save_path)
147-
148-
ims.append(image_name)
149-
txts.append(label)
150-
links.append(image_name)
151-
webpage.add_images(ims, txts, links, width=self.win_size)

0 commit comments

Comments
 (0)