Skip to content

Commit

Permalink
Fix image pre/post processing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Aixile committed Apr 11, 2017
1 parent a0b9c11 commit abb9f61
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
4 changes: 2 additions & 2 deletions evaluation.py
Expand Up @@ -32,7 +32,7 @@ def _eval(trainer, it):

img_c = img.reshape((side, side, 3, image_size, image_size))
img_c = img_c.transpose(0,1,3,4,2)
img_c = img_c*255.0 + 127.5
img_c = (img + 1) *127.5
img_c = np.clip(img_c, 0, 255)
img_c = img_c.astype(np.uint8)
img_c = img_c.reshape((side, side, image_size, image_size, 3)).transpose(0,2,1,3,4).reshape((side*image_size, side*image_size, 3))[:,:,::-1]
Expand All @@ -43,7 +43,7 @@ def _eval(trainer, it):
img_t = result.data.get()
img_t = img_t.reshape( (side, side, 3, image_size, image_size))
img_t = img_t.transpose(0,1,3,4,2)
img_t = img_t*255.0 + 127.5
img_t = (img + 1) *127.5
img_t = np.clip(img_t, 0, 255)
img_t = img_t.astype(np.uint8)
img_t = img_t.reshape((side, side, image_size, image_size, 3)).transpose(0,2,1,3,4).reshape((side*image_size, side*image_size, 3))[:,:,::-1]
Expand Down
4 changes: 2 additions & 2 deletions horse2zebra.py
Expand Up @@ -40,12 +40,12 @@ def preprocess_image(img):
#img -= np.array([103.939, 116.779, 123.68], dtype=np.float32)
"""
img = img.astype("f")
img = (img-127.5)/255.0
img = img/127.5 - 1
img = img.transpose((2, 0, 1))
return img

def postprocess_image(img):
img = (img*255.0)+127.5
img = (img + 1) *127.5
img = np.clip(img, 0, 255)
img = img.astype(np.uint8)
img.transpose((1, 2, 0))
Expand Down
11 changes: 6 additions & 5 deletions train.py
Expand Up @@ -137,7 +137,8 @@ def make_optimizer(model, alpha=0.0002, beta1=0.5):
'lambda1': args.lambda1,
'lambda2': args.lambda2,
# 'lambda3': args.lambda3,
'image_size' : args.crop_to
'image_size' : args.crop_to,
'eval_folder' : args.eval_folder
#'lambda4': args.lambda4,
})

Expand All @@ -157,10 +158,10 @@ def make_optimizer(model, alpha=0.0002, beta1=0.5):
trainer.extend(extensions.PrintReport(log_keys), trigger=(20, 'iteration'))
trainer.extend(extensions.ProgressBar(update_interval=50))

trainer.extend(
evaluation(gen_g, gen_f, args.eval_folder, image_size=args.crop_to
), trigger=(args.eval_interval ,'iteration')
)
#trainer.extend(
# evaluation(gen_g, gen_f, args.eval_folder, image_size=args.crop_to
# ), trigger=(args.eval_interval ,'iteration')
#)

# Run the training
trainer.run()
Expand Down
23 changes: 23 additions & 0 deletions updater.py
Expand Up @@ -9,6 +9,8 @@
from chainer import cuda, optimizers, serializers, Variable
from chainer import training

from PIL import Image

def cal_l2_sum(h, t):
return F.sum((h-t)**2)/ np.prod(h.data.shape)

Expand Down Expand Up @@ -50,6 +52,7 @@ def __init__(self, *args, **kwargs):
self._lambda2 = params['lambda2']
#self._lambda3 = params['lambda3']
self._image_size = params['image_size']
self._eval_foler = params['eval_folder']
#self._lambda4 = params['lambda4']
self._iter = 0
self._max_buffer_size = 50
Expand Down Expand Up @@ -88,6 +91,16 @@ def getAndUpdateBufferY(self, data):
id = np.random.randint(0, self._max_buffer_size)
return self._buffer_y[id, :].reshape((1, 3, self._image_size, self._image_size))

def save_images(self,img, w=2, h=3):
img = cuda.to_cpu(img)
img = img.reshape((w, h, 3, self._image_size, self._image_size))
img = img.transpose(0,1,3,4,2)
img = (img + 1) *127.5
img = np.clip(img, 0, 255)
img = img.astype(np.uint8)
img = img.reshape((w, h, self._image_size, self._image_size, 3)).transpose(0,2,1,3,4).reshape((w*self._image_size, h*self._image_size, 3))[:,:,::-1]
Image.fromarray(img).save(self._eval_foler+"/iter_"+str(self._iter)+".jpg")


def update_core(self):
xp = self.gen_g.xp
Expand Down Expand Up @@ -176,3 +189,13 @@ def update_core(self):
chainer.report({'loss_rec': loss_cycle_x}, self.gen_f)
chainer.report({'loss_adv': loss_gen_g_adv}, self.gen_g)
chainer.report({'loss_adv': loss_gen_f_adv}, self.gen_f)

if self._iter%50 ==0:
img = xp.zeros((6, 3, w_in, w_in)).astype("f")
img[0, : ] = x.data
img[1, : ] = x_y.data
img[2, : ] = x_y_x.data
img[3, : ] = y.data
img[4, : ] = y_x.data
img[5, : ] = y_x_y.data
self.save_images(img)

0 comments on commit abb9f61

Please sign in to comment.