def separate_lum_chr(gen_img_cvar):
    gen_img = gen_img_cvar.data.copy()
    if gpu_flag:
        gen_img = xp.asnumpy(gen_img)

    # roll back to standard arrangement and flip to RGB
    gen_img = np.rollaxis(np.squeeze(gen_img, 0), 0, 3)[..., ::-1]

    # separate channel
    gen_img_lum, gen_img_chr = rgb_to_yiq(gen_img)
    gen_img_lum = yiq_to_rgb(gen_img_lum)
    gen_img_chr = yiq_to_rgb(gen_img_chr)

    # flip to BGR
    gen_img_lum = gen_img_lum[..., ::-1]
    gen_img_chr = gen_img_chr[..., ::-1]

    # convert to Chainer Variables
    gen_img_lum = Variable(gen_img_lum)
    gen_img_chr = Variable(gen_img_chr)

    # transform images into bc01 arrangement
    gen_img_lum = F.rollaxis(gen_img_lum, 2, 0)[np.newaxis, ...]
    gen_img_chr = F.rollaxis(gen_img_chr, 2, 0)[np.newaxis, ...]

    return gen_img_lum, gen_img_chr
Exemple #2
0
    def __call__(self, x, s1, s2):
        h = F.relu(self.conv1(x))
        self.r = self.conv2(h)

        q = self.conv3(self.r)
        self.v = F.max(q, axis=1, keepdims=True)

        for i in xrange(self.k - 1):
            q = self.conv3(self.r) + self.conv3b(self.v)
            self.v = F.max(q, axis=1, keepdims=True)

        q = self.conv3(self.r) + self.conv3b(self.v)

        t = s2 * q.data.shape[3] + s1
        q = F.reshape(q, (q.data.shape[0], q.data.shape[1], -1))
        q = F.rollaxis(q, 2, 1)

        t_data_cpu = chainer.cuda.to_cpu(t.data)
        w = np.zeros(q.data.shape, dtype=np.float32)
        w[six.moves.range(t_data_cpu.size), t_data_cpu] = 1.0

        if isinstance(q.data, chainer.cuda.ndarray):
            w = chainer.cuda.to_gpu(w)

        w = chainer.Variable(w, volatile=not self.train)
        q_out = F.sum(w * q, axis=1)
        self.ret = self.l3(q_out)
        return self.ret
def load_images(content_name, style_name):
    # load images as arrays
    content_img = sp.misc.imread(content_name, mode='RGB').astype(np.float32)
    style_img = sp.misc.imread(style_name, mode='RGB').astype(np.float32)
    style_img = sp.misc.imresize(style_img,
                                 size=content_img.shape[0:2],
                                 interp='lanczos').astype(np.float32)

    # flip to BGR
    content_img = content_img[..., ::-1]
    style_img = style_img[..., ::-1]

    # convert to Chainer Variables
    content_img = Variable(content_img)
    style_img = Variable(style_img)

    # transform loaded images into bc01 arrangement
    content_img = F.rollaxis(content_img, 2, 0)[np.newaxis, ...]
    style_img = F.rollaxis(style_img, 2, 0)[np.newaxis, ...]

    return content_img, style_img
def mean_subtraction(img_cvar):
    mean_pixel = np.array([103.939, 116.779, 123.680]).astype(np.float32)

    # roll back to standard arrangement
    temp_img = np.rollaxis(np.squeeze(img_cvar.data.copy(), 0), 0, 3)

    if gpu_flag:
        temp_img = xp.asnumpy(temp_img)

    temp_img -= mean_pixel

    temp_cvar = Variable(temp_img)
    temp_cvar = F.rollaxis(temp_cvar, 2, 0)[np.newaxis, ...]

    return temp_cvar
Exemple #5
0
    def __call__(self, x):
        if self.embed_input:
            x = self.embed(x)
            x = F.rollaxis(x, 3, 1)
        h = self.l0(x)

        if self.depth in ['one', 'two', 'three']:
            h = F.relu(self.l1(h))

        if self.depth in ['two', 'three']:
            h = F.max_pooling_2d(h, (1, 2), (1, 2))
            h = F.relu(self.l2(h))
            h = F.relu(self.l3(h))

        if self.depth in ['three']:
            h = F.max_pooling_2d(h, (1, 2), (1, 2))
            h = F.relu(self.l4(h))
            h = F.relu(self.l5(h))

        return F.max_pooling_2d(h, (1, self.width))
def histogram_match(cont_img_cvar, sty_img_cvar):
    cont_img = cont_img_cvar.data.copy()
    sty_img = sty_img_cvar.data.copy()

    # roll back to standard arrangement
    cont_img = np.rollaxis(np.squeeze(cont_img, 0), 0, 3)
    sty_img = np.rollaxis(np.squeeze(sty_img, 0), 0, 3)

    # compute row means
    cont_mu = np.mean(cont_img, axis=(0, 1))
    sty_mu = np.mean(sty_img, axis=(0, 1))

    # compute covariance matrix
    cont_sigma = np.cov(np.concatenate(cont_img), rowvar=False, bias=True)
    sty_sigma = np.cov(np.concatenate(sty_img), rowvar=False, bias=True)

    # eigendecomposition for square roots
    sty_q, sty_l = sp.linalg.eig(sty_sigma)
    sty_q = np.diag(np.sqrt(sty_q))
    sty_sigma_sqrtm = sty_l.dot(sty_q).dot(sty_l.T)
    sty_sigma_sqrtm_inv = np.linalg.inv(sty_sigma_sqrtm)

    cont_sty_cov = sty_sigma_sqrtm.dot(cont_sigma).dot(sty_sigma_sqrtm)
    cs_q, cs_l = sp.linalg.eig(cont_sty_cov)
    cs_q = np.diag(np.sqrt(cs_q))
    cs_sqrtm = cs_l.dot(cs_q).dot(cs_l.T)

    # color matching transformation
    a = sty_sigma_sqrtm_inv.dot(cs_sqrtm).dot(sty_sigma_sqrtm_inv)
    sty_img_col = np.add(np.dot(sty_img - sty_mu, a.T), cont_mu).real

    # normalize
    sty_img_col = np.ceil(255.0 * normalize(sty_img_col))

    # convert to a Chainer Variables
    sty_img_cm_cvar = Variable(sty_img_col)

    # transform image back to bc01
    sty_img_cm_cvar = F.rollaxis(sty_img_cm_cvar, 2, 0)[np.newaxis, ...]

    return sty_img_cm_cvar
Exemple #7
0
 def forward_cnn(self, h):
     # Check and prepare for 2d convolutions
     h = F.expand_dims(h, 2)
     h = F.swapaxes(h, 1, 2)
     # Apply each CNN layer
     for i, cnn_layer in enumerate(self.cnns):
         # cnn pass
         h = self[cnn_layer](h)
         # Apply batch normalization
         if self.cnn_bn:
             bn_lname = '{0:s}_bn'.format(cnn_layer)
             h = self[bn_lname](h)
         # Apply non-linearity
         h = F.relu(h)
     """
     Prepare return
     batch size * num time frames after pooling * cnn out dim
     """
     h = F.swapaxes(h, 1, 2)
     h = F.reshape(h, h.shape[:2] + tuple([-1]))
     h = F.rollaxis(h, 1)
     return h
Exemple #8
0
 def f(x):
     return functions.rollaxis(x, self.axis, self.start)
Exemple #9
0
    def check_forward(self, x_data):
        x = chainer.Variable(x_data)
        y = functions.rollaxis(x, self.axis, self.start)

        expect = numpy.rollaxis(self.x, self.axis, self.start)
        testing.assert_allclose(y.data, expect)
Exemple #10
0
 def check_type_error(self, x):
     with self.assertRaises(type_check.InvalidType):
         functions.rollaxis(x, self.axis, self.start)
Exemple #11
0
 def f(x):
     return functions.rollaxis(x, self.axis, self.start)
Exemple #12
0
 def test_invalid_start(self):
     with self.assertRaises(TypeError):
         functions.rollaxis(self.x, 0, start='a')
Exemple #13
0
def get_element(*input, index=0, axis=1):
    return [F.rollaxis(i, axis)[index] for i in input]
Exemple #14
0
 def f(x):
     y = functions.rollaxis(x, self.axis, self.start)
     return y * y
Exemple #15
0
 def forward(self, inputs, device):
     x, = inputs
     y = functions.rollaxis(x, self.axis, self.start)
     return y,
Exemple #16
0
 def forward(self, inputs, device):
     x, = inputs
     y = functions.rollaxis(x, self.axis, self.start)
     return y,
    def check_forward(self, x_data):
        x = chainer.Variable(x_data)
        y = functions.rollaxis(x, self.axis, self.start)

        expect = numpy.rollaxis(self.x, self.axis, self.start)
        testing.assert_allclose(y.data, expect)
 def check_type_error(self, x):
     with self.assertRaises(type_check.InvalidType):
         functions.rollaxis(x, self.axis, self.start)
Exemple #19
0
	def __call__(self, x):
		return functions.rollaxis(x, self.axis, self.start)
Exemple #20
0
	def __call__(self, x):
		return functions.rollaxis(x, self.axis, self.start)