forked from Yacalis/celeba-classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataLoader.py
82 lines (74 loc) · 2.62 KB
/
dataLoader.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 20 15:21:00 2018
@author: Yacalis
"""
import numpy as np
import os
import matplotlib.image as mpimg
from random import shuffle
def retrieve_data(data_dict, image_dir) -> ():
# instantiate arrays
x_data = []
y_data = []
keys = data_dict.keys()
print('image dir: ', image_dir)
print('\tsub dirs:')
try:
for sub_dir in os.listdir(image_dir):
filepath = os.path.join(image_dir, sub_dir)
# make sure only directories
if os.path.isdir(filepath):
print('\t\t', sub_dir)
for file in os.listdir(filepath):
# get y_data
key = os.path.join(sub_dir, file)
# make sure key exists in dict
if key in keys:
value = data_dict[key]
# make sure y_data is a correct number
if value == 1.0 or value == 0.0:
# get x_data
filename = os.path.join(filepath, file)
im_arr = mpimg.imread(filename)
# make sure x_data is correct shape
if im_arr.shape == (228, 228, 3):
# now that we know y_data and x_data are OK
x_data.append(im_arr)
#y_data.append(value)
if value == 1.0:
y_data.append(np.array([0, 1]))
else:
y_data.append(np.array([1, 0]))
except Exception as e:
print(str(e))
x_data = np.array(x_data)
y_data = np.array(y_data)
print('shape of x_data: ', x_data.shape)
return x_data, y_data
def retrieve_celeba_data(data_dict, image_dir) -> ():
# instantiate arrays
x_data = []
y_data = []
keys = data_dict.keys()
i = 1
images = os.listdir(image_dir)
shuffle(images)
try:
for file in images:
if i > 1000:
break
if file in keys:
filepath = os.path.join(image_dir, file)
im_arr = mpimg.imread(filepath)
if im_arr.shape == (218, 178, 3):
x_data.append(im_arr)
y_data.append(np.array(data_dict[file]))
i += 1
except Exception as e:
print(str(e))
x_data = np.array(x_data)
y_data = np.array(y_data)
print('shape of x_data: ', x_data.shape)
return x_data, y_data