forked from TachibanaYoshino/AnimeGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_loader.py
81 lines (55 loc) · 2.47 KB
/
data_loader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import tensorflow as tf
import cv2,random
import numpy as np
class ImageGenerator(object):
def __init__(self, image_dir,size, batch_size, num_cpus = 16):
self.paths = self.get_image_paths_train(image_dir)
self.num_images = len(self.paths)
self.num_cpus = num_cpus
self.size = size
self.batch_size = batch_size
def get_image_paths_train(self, image_dir):
image_dir = os.path.join(image_dir)
paths = []
for path in os.listdir(image_dir):
# Check extensions of filename
if path.split('.')[-1] not in ['jpg', 'jpeg', 'png', 'gif']:
continue
# Construct complete path to anime image
path_full = os.path.join(image_dir, path)
# Validate if colorized image exists
if not os.path.isfile(path_full):
continue
paths.append(path_full)
return paths
def read_image(self, img_path1):
if 'style' in img_path1.decode() or 'smooth' in img_path1.decode():
image1 = cv2.imread(img_path1.decode()).astype(np.float32)
image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
image2 = cv2.imread(img_path1.decode(),cv2.IMREAD_GRAYSCALE).astype(np.float32)
image2 = np.asarray([image2,image2,image2])
image2= np.transpose(image2,(1,2,0))
else:
image1 = cv2.imread(img_path1.decode()).astype(np.float32)
image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
image2 = np.zeros(image1.shape).astype(np.float32)
return image1, image2
def load_image(self, img1 ):
image1, image2 = self.read_image(img1)
processing_image1 = image1/ 127.5 - 1.0
processing_image2 = image2/ 127.5 - 1.0
return (processing_image1,processing_image2)
def load_images(self):
dataset = tf.data.Dataset.from_tensor_slices(self.paths)
# Repeat indefinitely
dataset = dataset.repeat()
# Unform shuffle
dataset = dataset.shuffle(buffer_size=len(self.paths))
# Map path to image
dataset = dataset.map(lambda img: tf.py_func(
self.load_image, [img], [tf.float32,tf.float32]),
self.num_cpus)
dataset = dataset.batch(self.batch_size)
img1,img2 = dataset.make_one_shot_iterator().get_next()
return img1,img2