-
Notifications
You must be signed in to change notification settings - Fork 56
/
utils.py
147 lines (134 loc) · 5.11 KB
/
utils.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from PIL import Image
import numpy as np
import scipy.misc as misc
import scipy.io as sio
import os
import pickle
def read_cifar(data, labels, batch_size):
rand_select = np.random.randint(0, 50000, [batch_size])
batch = data[rand_select]
batch_labels = labels[rand_select]
return batch, batch_labels
def read_face(data, batch_size):
rand_select = np.random.randint(0, 13233, [batch_size])
batch = data[rand_select]
return batch, 0
# os.listdir("./dataset")
def random_batch_(path, batch_size, shape, c_nums):
folder_names = os.listdir(path)
rand_select = np.random.randint(0, folder_names.__len__())
if not c_nums == folder_names.__len__():
print("Error: c_nums must match the number of the folders")
return
y = np.zeros([1, c_nums])
y[0, rand_select] = 1
path = path + folder_names[rand_select] + "//"
data = sio.loadmat(path + "dataset.mat")["data"]
rand_select = np.random.randint(0, np.size(data, 0), [batch_size])
batch = data[rand_select]
return batch, y
def random_batch(path, batch_size, shape, c_nums):
folder_names = os.listdir(path)
rand_select = np.random.randint(0, folder_names.__len__())
if not c_nums == folder_names.__len__():
print("Error: c_nums must match the number of the folders")
return
y = np.zeros([1, 1])
y[0, 0] = rand_select
path = path + folder_names[rand_select] + "//"
file_names = os.listdir(path)
rand_select = np.random.randint(0, file_names.__len__(), [batch_size])
batch = np.zeros([batch_size, shape[0], shape[1], shape[2]])
for i in range(batch_size):
img = np.array(Image.open(path + file_names[rand_select[i]]).resize([shape[0], shape[1]]))[:, :, :3]
if img.shape.__len__() == 2:
img = np.dstack((img, img, img))
batch[i, :, :, :] = img
return batch, y
def random_face_batch(path, batch_size):
filenames_young = os.listdir(path+"0//")
filenames_cats = os.listdir(path+"1//")
rand_gender = np.random.randint(0, 2)
batch = np.zeros([batch_size, 64, 64, 3])
Y = np.zeros([1, 2])
if rand_gender == 0:#young
rand_samples = np.random.randint(0, filenames_young.__len__(), [batch_size])
c = 0
for i in rand_samples:
img = np.array(Image.open(path+"0//"+filenames_young[i]))
center_h = img.shape[0] // 2
center_w = img.shape[1] // 2
# batch[c, :, :, :] = misc.imresize(img[center_h - 70:center_h + 70, center_w - 70:center_w + 70, :], [64, 64])
batch[c, :, :, :] = misc.imresize(img, [64, 64])
c += 1
Y[0, 0] = 1
else:
rand_samples = np.random.randint(0, filenames_cats.__len__(), [batch_size])
c = 0
for i in rand_samples:
img = np.array(Image.open(path + "1//" + filenames_cats[i]))
batch[c, :, :, :] = misc.imresize(img, [64, 64])
c += 1
Y[0, 1] = 1
return batch, Y
def random_batch_(path, batch_size, shape):
filenames = os.listdir(path)
rand_samples = np.random.randint(0, filenames.__len__(), [batch_size])
batch = np.zeros([batch_size, shape[0], shape[1], shape[2]])
c = 0
y = np.zeros([batch_size, 2])
for idx in rand_samples:
if (filenames[idx])[:3] == "cat":
y[c, 0] = 1
else:
y[c, 1] = 1
try:
batch[c, :, :, :] = misc.imresize(crop(np.array(Image.open(path + filenames[idx]))), [shape[0], shape[1]])
except:
img = crop(np.array(Image.open(path + filenames[0])))
batch[c, :, :, :] = misc.imresize(img, [shape[0], shape[1]])
c += 1
return batch, y
def crop(img):
h = img.shape[0]
w = img.shape[1]
if h < w:
x = 0
y = np.random.randint(0, w - h + 1)
length = h
elif h > w:
x = np.random.randint(0, h - w + 1)
y = 0
length = w
else:
x = 0
y = 0
length = h
return img[x:x+length, y:y+length, :]
def unpickle(file):
with open(file, 'rb') as fo:
dict = pickle.load(fo)
return dict
def to_img(src_path, dst_path):
filenames = os.listdir(src_path)
for filename in filenames:
data = unpickle(src_path + filename)
imgs = data["data"]
labels = data["labels"]
for i in range(np.size(imgs, 0)):
img = np.transpose(np.reshape(imgs[i, :], [3, 64, 64]), [1, 2, 0])
if not os.path.exists(dst_path+str(labels[i])):
os.mkdir(dst_path+str(labels[i]))
Image.fromarray(np.uint8(img)).save(dst_path + str(labels[i]) + "//" + filename + "_" + str(labels[i]) + "_" + str(i) + ".jpg")
print(filename)
# if __name__ == "__main__":
# import os
#
# filenames = os.listdir("./generate/")
# img = np.zeros([11*32, 11*32, 3])
# c = 0
# for i in range(11):
# for j in range(11):
# img[i*32:i*32+32, j*32:j*32+32] = np.array(Image.open("./generate/" + filenames[c]))
# c += 1
# aaa = 0