Exemplo n.º 1
0
    def get_example(self, i):
        filename = self.filenames[i]
        with Image.open(self.source_dir / filename) as f:
            source = img_to_chw_array(f)
        with Image.open(self.target_dir / filename) as f:
            target = img_to_chw_array(f)
        C, H, W = source.shape
        py, px = random.choice([(0, 0), (1, 0), (0, 1)])
        source[:, 1::2, 1::2] = source[:, py::2, px::2]

        c_source = source.shape[0]
        t = np.concatenate([source, target], axis=0)
        t = self.argument_image(t, c_source, self.char_size, self.fine_size)

        return t[:c_source], t[c_source:]
Exemplo n.º 2
0
    def __call__(self, imgs):
        xp = self.gen.xp
        self.gen.fix_broken_batchnorm()

        x = xp.asarray([img_to_chw_array(img) for img in imgs])
        x_in = chainer.Variable(x)
        with chainer.using_config('train', False), chainer.using_config(
                'enable_back_prop', False):
            x_out = self.gen(x_in)

        return [chw_array_to_img(x) for x in chainer.cuda.to_cpu(x_out.data)]
Exemplo n.º 3
0
    def get_example(self, i):
        with Image.open(str(self.filepaths[i])) as f:
            target = img_to_chw_array(f)

        target = random_crop(target, (self.fine_size, self.fine_size))
        target = random_flip(target, x_random=True)
        # randomize and alignment
        source = resize(
            downscale_random_nearest_neighbor(target.copy()),
            (self.fine_size, self.fine_size),
            Image.NEAREST,
        )
        # alignment
        target = resize(
            resize(
                target,
                (self.fine_size // 2, self.fine_size // 2),
                Image.NEAREST,
            ),
            (self.fine_size, self.fine_size),
            Image.NEAREST,
        )
        return source, target
Exemplo n.º 4
0
    def get_example(self, i):
        r = random.random()
        if r < 0.5:
            with Image.open(
                    str(self.chartips[np.random.randint(len(
                        self.chartips))])) as img:
                front = img_to_chw_array(img)
        elif r < 0.75:
            with Image.open(str(self.objs[np.random.randint(len(
                    self.objs))])) as img:
                front = img_to_chw_array(img)
        else:
            S = 1
            w, h = self.fine_size * 2 * S, self.fine_size * 2 * S
            front = Image.new('RGBA', (w, h))
            draw = ImageDraw.Draw(front)
            fontsize = np.random.randint(32, 64) * S
            font = ImageFont.truetype(
                str(self.fonts[np.random.randint(len(self.fonts))]),
                fontsize * 3 // 4)
            txt = ''
            for i in range(math.ceil(fontsize / h) * 4):
                txt += ''.join(
                    np.random.choice(self.charset,
                                     math.ceil(fontsize / w) * 4))
                txt += '\n'
            r, g, b = [np.random.randint(256) for i in range(3)]
            draw.text((0, 0), txt, font=font, fill=(r, g, b, 255))
            front = img_to_chw_array(front)
        front = random_crop(front, (self.fine_size, self.fine_size))
        front = random_flip(front, x_random=True)

        r = random.random()
        if r < 0.9:
            with Image.open(str(self.tiles[np.random.randint(len(
                    self.tiles))])) as img:
                back = img_to_chw_array(img)
        elif r < 0.95:
            r, g, b = [np.random.randint(256) for i in range(3)]
            back = Image.new('RGBA', (self.fine_size, self.fine_size),
                             (r, g, b))
            back = img_to_chw_array(back)
        else:
            back = Image.new('RGBA', (self.fine_size, self.fine_size),
                             (0, 0, 0, 0))
            back = img_to_chw_array(back)

        back = random_crop(back, (self.fine_size, self.fine_size))
        back = random_flip(back, x_random=True)
        m = np.tile((front[3, :, :] == 1).reshape(
            (1, self.fine_size, self.fine_size)),
                    (4, 1, 1)).reshape(4 * self.fine_size**2)
        back = back.reshape(4 * self.fine_size**2)
        back[m] = front.reshape(4 * self.fine_size**2)[m]
        target = back.reshape((4, self.fine_size, self.fine_size))
        source = resize(
            resize(
                target,
                (self.fine_size // 2, self.fine_size // 2),
                Image.NEAREST,
            ),
            (self.fine_size, self.fine_size),
            Image.NEAREST,
        )
        return source, target