data_reader.py 1.9 KB
Newer Older
W
whs 已提交
1 2 3
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
W
whs 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import os
from PIL import Image
import numpy as np

A_LIST_FILE = "./data/horse2zebra/trainA.txt"
B_LIST_FILE = "./data/horse2zebra/trainB.txt"
A_TEST_LIST_FILE = "./data/horse2zebra/testA.txt"
B_TEST_LIST_FILE = "./data/horse2zebra/testB.txt"
IMAGES_ROOT = "./data/horse2zebra/"


def image_shape():
    return [3, 256, 256]


def max_images_num():
W
whs 已提交
20
    return 1335
W
whs 已提交
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


def reader_creater(list_file, cycle=True, shuffle=True, return_name=False):
    images = [IMAGES_ROOT + line for line in open(list_file, 'r').readlines()]

    def reader():
        while True:
            if shuffle:
                np.random.shuffle(images)
            for file in images:
                file = file.strip("\n\r\t ")
                image = Image.open(file)
                image = image.resize((256, 256))
                image = np.array(image) / 127.5 - 1
                if len(image.shape) != 3:
                    continue
                image = image[:, :, 0:3].astype("float32")
                image = image.transpose([2, 0, 1])
                if return_name:
                    yield image[np.newaxis, :], os.path.basename(file)
                else:
                    yield image
            if not cycle:
                break

    return reader


W
whs 已提交
49
def a_reader(shuffle=True):
W
whs 已提交
50 51 52
    """
    Reader of images with A style for training.
    """
W
whs 已提交
53
    return reader_creater(A_LIST_FILE, shuffle=shuffle)
W
whs 已提交
54 55


W
whs 已提交
56
def b_reader(shuffle=True):
W
whs 已提交
57 58 59
    """
    Reader of images with B style for training.
    """
W
whs 已提交
60
    return reader_creater(B_LIST_FILE, shuffle=shuffle)
W
whs 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74


def a_test_reader():
    """
    Reader of images with A style for test.
    """
    return reader_creater(A_TEST_LIST_FILE, cycle=False, return_name=True)


def b_test_reader():
    """
    Reader of images with B style for test.
    """
    return reader_creater(B_TEST_LIST_FILE, cycle=False, return_name=True)