-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
269 lines (195 loc) · 7.06 KB
/
util.py
File metadata and controls
269 lines (195 loc) · 7.06 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import tensorflow as tf
import os
import codecs
import json
import numpy as np
import random
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from collections import defaultdict
pathData_ = "D:/src/quickDraw/data/"
path_ = "D:/src/quickDraw/test/"
pathImage_ = "D:/src/quickDraw/animalImg"
pathImageText_ = "D:/src/quickDraw/aimgText"
trainingSet = []
testSet = []
allClassNum = 6
allFilePath = []
image_size = 64 #4x4
def prepare_dirs(path):
filenames = tf.gfile.ListDirectory(path)
filenames = sorted(filenames)
filenames = [os.path.join(path, f) for f in filenames]
return filenames
def makeTrainingSet(files, sess, coord):
dataDict = defaultdict(lambda : 0)
for e in files:
dataDict[e[1]] = []
for e in files:
dataDict[e[1]] += [e[0]]
tr = []
for a, b in dataDict.items():
tr += makeNumpyArray(a , b, sess, coord)
print ("loaded class " + str(a))
random.shuffle(tr)
return np.array(tr).T.tolist()
def makeNumpyArray(classNum, fileNames, sess, coord):
_, images = loadImage(sess,fileNames, len(fileNames))
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
input = sess.run(images)
output = np.zeros([allClassNum])
output[classNum] = 1
tmp = []
for e in input:
tmp += [[e, output]]
return tmp
def loadImage(sess, filenames, imageNum):
filename_queue = tf.train.string_input_producer(filenames)
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
image = tf.image.decode_png(value, channels=1)
image = tf.cast(image, tf.float32) / 255.0
image.set_shape((image_size, image_size, 1))
# Generate batch
batch_size = imageNum
num_preprocess_threads = 1
min_queue_examples = imageNum
image_batch = tf.train.batch(
[image],
batch_size=batch_size,
num_threads=num_preprocess_threads,
capacity=min_queue_examples + 3 * batch_size)
image_batch = tf.reshape(image_batch, [batch_size, image_size * image_size])
return key, image_batch
def testing(bi):
x_ = tf.placeholder(tf.float32, shape=[None, image_size * image_size])
x_image = tf.reshape(x_, [-1, image_size, image_size, 1], name='image')
print (tf.Session().run(tf.shape(x_image), {x_: bi}))
def prepareLabel():
pivot = 10112
train = []
test = []
for i in range(allClassNum):
pp = prepare_dirs(path_ + str(i) + "/")
tmp = []
for e in pp:
tmp += [[e, i]]
train += tmp[0:pivot]
test += tmp[pivot:pivot + 1024]
print ("prepare " + str(i))
random.shuffle(train)
random.shuffle(test)
nbt1 = len(train)//128
nbt2 = len(test)//1024
return [train[i::nbt1] for i in range(nbt1)], [test[i::nbt2] for i in range(nbt2)]
def initAllClass():
classesDict = defaultdict(lambda: 0)
firstClass = 0
for file in os.listdir(pathImage_):
if firstClass >= 9999:
break
if file not in classesDict:
classesDict[file] = firstClass
firstClass += 1
return classesDict
def getDataForTesting(index, cd):
imgList = []
dictSize = len(cd)
pIT = pathImageText_ + "/" + str(index) + ".txt"
print (pIT)
with open(pIT, "r") as f:
for line in f:
data = line.split(" | ")
outLabel = np.zeros(dictSize)
outLabel[cd[data[0]]] = 1
arr = np.fromstring(data[1], sep=",")
arr = np.reshape(arr, [64, 64]).tolist()
if random.choice([True, False]):
arr = np.flip(arr, 1)
arr = np.reshape(arr , [4096])
imgList += [[arr, outLabel]]
random.shuffle(imgList)
return imgList
def imageToArray():
p = "D:/src/quickDraw/test/"
l = []
for i in range(1,10):
pixes = mpimg.imread(p + str(i) + ".png")
pixes = pixes[:, :, 0]
print (np.shape(pixes))
l.append(np.reshape(pixes, [4096]))
return l
def getDataFromTxt(index, cd):
imgList = []
dictSize = len(cd)
pIT = pathImageText_ + "/" + str(index) + ".txt"
print (pIT)
with open(pIT, "r") as f:
for line in f:
data = line.split(" | ")
outLabel = np.zeros(dictSize)
outLabel[cd[data[0]]] = 1
arr = np.fromstring(data[1], sep=",")
arr = np.reshape(arr, [64, 64]).tolist()
if random.choice([True, False]):
arr = np.flip(arr, 1)
arr = np.reshape(arr , [4096])
imgList += [[arr, outLabel]]
#random.shuffle(imgList)
trS = imgList[0:31104]
teS = imgList[31104:]
nbt1 = len(trS) // 64
nbt2 = len(teS) // 64
return [trS[i::nbt1] for i in range(nbt1)] , [teS[i::nbt2] for i in range(nbt2)]
def parse_line(ndjson_line):
"""Parse an ndjson line and return ink (as np array) and classname."""
sample = json.loads(ndjson_line)
class_name = sample["word"]
inkarray = sample["drawing"]
maxY = np.max(np.max([y[1] for y in inkarray]))
for e in inkarray:
e[1] = maxY - e[1]
shouldFlipX = random.choice([True, False])
if shouldFlipX:
maxX = np.max(np.max([x[0] for x in inkarray]))
for e in inkarray:
e[0] = maxX - e[0]
return inkarray, class_name
def testDraw(data, ext):
plt.figure(figsize=(1, 1))
plt.axis('off')
plt.tick_params(axis='both', left='off', top='off', right='off', bottom='off', labelleft='off', labeltop='off', labelright='off', labelbottom='off')
for e in data:
plt.plot(e[0], e[1], color='black',linewidth=np.random.uniform(1,2))
plt.savefig(pathImage_ + ext + '.png', bbox_inches='tight', dpi=59, origin='upper',cmap=plt.cm.gray)
plt.close()
def fromNdjson2Img():
classesDict = defaultdict(lambda: 0)
for file in os.listdir(pathImage_):
classesDict[file] = 1
isStart = False
startLine = 22000
endLine = 44000
processing = True
for file in os.listdir(pathData_):
if file.endswith(".ndjson"):
fp = pathData_ + file
fClass = file.replace(".ndjson","")
if "flamingo" == fClass:
isStart = True
if "mermaid" == fClass:
processing = False
if processing:
if isStart and fClass in classesDict:
with codecs.open(fp, 'r', 'utf-8') as f:
i = 0
for line in f:
img , cName = parse_line(line)
i += 1
if i > startLine:
testDraw(img, '/' + cName + '/' + str(i))
if i > endLine:
break
if i%200 == 0:
print (cName + ' ' + str(i))
#fromNdjson2Img()