-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlabeltools.py
111 lines (102 loc) · 3.19 KB
/
labeltools.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
import os
import tensorflow as tf
def diff(re2,key):
keys = tf.fill([tf.size(re2)],key[0])
numpoi= tf.cast(tf.equal(re2, keys),tf.int32)
numpoi=tf.argmax(numpoi)
return numpoi
def splitfilenames(inputs,allstringlen):
a = tf.string_split(inputs, "/\\")
bigin = tf.cast(tf.size(a.values) / allstringlen -2, tf.int32)
slitsinglelen = tf.cast(tf.size(a.values) / allstringlen, tf.int32)
val = tf.reshape(a.values, [allstringlen, slitsinglelen])
re2 = tf.cast(tf.slice(val, [0, bigin], [allstringlen, 1]),tf.string)
re2 =tf.reshape(re2,[allstringlen])
re2 =tf.unique(re2).y
return re2
def getfilelist(data_dir):
filenames=[]
categorylist = sorted(os.listdir(data_dir))
i = 1
for item in categorylist:
print("load file type:" + str(i))
fpath = ''.join([data_dir,'\\',item])
tmp = os.listdir(fpath)
for seitem in tmp:
fulseitem = [fpath,'\\', seitem]
filenames.append(''.join(fulseitem).replace("\\",'/'))
i += 1
return filenames,categorylist
def PackageMultImageIntoBin(files,allFilesDir,targetpath):
if tf.gfile.Exists(targetpath):
tf.gfile.DeleteRecursively(targetpath)
tf.gfile.MakeDirs(targetpath)
from PIL import Image
import numpy as np
maxLabelCount=255
categorylist = sorted(os.listdir(allFilesDir))
i=1
c=0
resetW =32
resetH =32
batch=list([])
categoryDic=GetDic(categorylist)
if len(categoryDic) > maxLabelCount:
print("label只占1位unit8 最大不能超过255")
return
print("shuffle files start")
files=shuffle(files)
for imgsrc in files:
try:
im = Image.open(imgsrc)
im = im.resize((resetW, resetH))
im = np.array(im,np.uint8)
r = im[:, :, 0].flatten()
g = im[:, :, 1].flatten()
b = im[:, :, 2].flatten()
fileByloneLabel=str(imgsrc).split("/")[-2]
find=categoryDic.get(fileByloneLabel,-1)
if find ==-1:
continue
label=[find]
temp=list(label) + list(r) + list(g) + list(b)
if len(temp)!=3*32*32+1:
print("打包数组总数不能超过原总数")
return
batch+=temp
if(i%2000==0):
out = np.array(batch, np.uint8)
c+=1
out.tofile(targetpath + "/eval_batch_"+str(c)+".bin")
print("out:%s" % (len(out)))
batch = []
i+=1
except Exception as e:
print(e)
print('error imgsrc:%s'%imgsrc)
def shuffle(lis):
import random
result = lis[:]
for i in range(1, len(lis)):
j = random.randrange(0, i)
result[i] = result[j]
result[j] = lis[i]
return result
def loadOne():
from PIL import Image
import numpy as np
imgsrc="/imagenet/srsdone/evalimg1/n01440764/n01440764_15560.jpg"
im = Image.open(imgsrc)
print(im.size)
im= im.resize((32,32))
im = (np.array(im))
temp=im[:, :, 0]
r = temp.flatten()
return r
def GetDic(categorylist):
dic={}
i=0
for item in categorylist:
i=i+1
dic[item] = i
return dic