-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
78 lines (52 loc) · 2.15 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
import cv2
import os
dataset_path = "Sketchy/"
photo_path = os.path.join(dataset_path, 'photo/')
sketch_path = os.path.join(dataset_path, 'sketch/')
def load_img(path):
img = cv2.imread(path)/255
return cv2.resize(img, (100,100))
def get_dict():
photo_dictionary = {}
for category in os.listdir(photo_path):
category_path = os.path.join(photo_path, category)
photo_dictionary[category] = os.listdir(category_path)
sketch_dictionary = {}
for category in os.listdir(sketch_path):
category_path = os.path.join(sketch_path, category)
sketch_dictionary[category] = os.listdir(category_path)
return photo_dictionary, sketch_dictionary
def get_batch(photo_dictionary, sketch_dictionary):
l = []
p_ = []
s_ = []
for _ in range(128):
if np.random.uniform() >= 0.5:
photo_class = np.random.choice(list(photo_dictionary))
photo = np.random.choice(photo_dictionary[photo_class])
photo_dictionary[photo_class].remove(photo)
p = photo_class + '/' + photo
sketch_class = photo_class
sketch = np.random.choice(sketch_dictionary[sketch_class])
sketch_dictionary[sketch_class].remove(sketch)
s = sketch_class + '/' + sketch
label = 1
else:
x = list(photo_dictionary)
photo_class = np.random.choice(x)
photo = np.random.choice(photo_dictionary[photo_class])
photo_dictionary[photo_class].remove(photo)
p = photo_class + '/' + photo
x.remove(photo_class)
sketch_class = np.random.choice(x)
sketch = np.random.choice(sketch_dictionary[sketch_class])
sketch_dictionary[sketch_class].remove(sketch)
s = sketch_class + '/' + sketch
label = 0
p_.append(os.path.join(dataset_path, 'photo/', p))
s_.append(os.path.join(dataset_path, 'sketch/', s))
l.append(label)
images = np.array([load_img(i) for i in p_])
sketches = np.array([load_img(i) for i in s_])
labels = np.array(l)
return images, sketches, labels