-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
106 lines (85 loc) · 2.96 KB
/
data.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
import glob
import h5py
import numpy as np
#import scipy.misc
import random
import cv2
from args import Args
def normalize4gan(im):
'''
Convert colorspace and
cale the input in [-1, 1] range, as described in ganhacks
'''
#im = cv2.cvtColor(im, cv2.COLOR_RGB2YCR_CB).astype(np.float32)
# HSV... not helpful.
im = im.astype(np.float32)
im /= 128.0
im -= 1.0 # now in [-1, 1]
return im
def denormalize4gan(im):
'''
Does opposite of normalize4gan:
[-1, 1] to [0, 255].
Warning: input im is modified in-place!
'''
im += 1.0 # in [0, 2]
im *= 127.0 # in [0, 255]
return im.astype(np.uint8)
def make_hdf5(ofname, wildcard):
'''
Preprocess files given by wildcard and save them in hdf5 file, as ofname.
'''
pool = list(glob.glob(wildcard))
if Args.dataset_sz <= 0:
fnames = pool
else:
fnames = []
for i in range(Args.dataset_sz):
# possible duplicate but don't care
fnames.append(random.choice(pool))
"""
with h5py.File(ofname, "w") as f:
faces = f.create_dataset("faces", (len(fnames), Args.h, Args.w, 3), dtype='f')
for i, fname in enumerate(fnames):
print(fname)
im = scipy.misc.imread(fname, mode='RGB') # some have alpha channel
im = scipy.misc.imresize(im, (Args.h, Args.w))
faces[i] = normalize4gan(im)
"""
with h5py.File(ofname, "w") as f:
faces = f.create_dataset("faces", (2*len(fnames), Args.h, Args.w, 3), dtype='f')
for i, fname in enumerate(fnames):
print(fname)
#im = scipy.misc.imread(fname, mode='RGB') # some have alpha channel
#im = scipy.misc.imresize(im, (Args.h, Args.w))
im = cv2.imread(fname)
im = cv2.resize(im,(Args.w, Args.h))
im2 = np.fliplr(im)
faces[2*i] = normalize4gan(im)
faces[2*i + 1] = normalize4gan(im2)
def test(hdff):
'''
Reads in hdf file and check if pixels are scaled in [-1, 1] range.
'''
with h5py.File(hdff, "r") as f:
X = f.get("faces")
print(np.min(X[:,:,:,0]))
print(np.max(X[:,:,:,0]))
print(np.min(X[:,:,:,1]))
print(np.max(X[:,:,:,1]))
print(np.min(X[:,:,:,2]))
print(np.max(X[:,:,:,2]))
print("Dataset size:", len(X))
assert np.max(X) <= 1.0
assert np.min(X) >= -1.0
if __name__ == "__main__" :
# Thankfully the dataset is in PNG, not JPEG.
# Anime style suffers from significant quality degradation in JPEG.
#make_hdf5("data.hdf5", "animeface-character-dataset/thumb/*/*.png")
#make_hdf5("data.hdf5", "animeface-character-dataset/thumb/*/*.jpg")
#make_hdf5("data.hdf5", "animeface-character-dataset/thumb/025*/*.png")
make_hdf5("data.hdf5", Args.data_dir + "*/*.jpg")
#make_hdf5("data.hdf5", Args.data_dir + "*.jpg")
# Uncomment and run test, if you want.
test("data.hdf5")