将图片制作成内存对象数据集
import tensorflow as tf
import os
from matplotlib import pyplot as plt
import numpy as np
from sklearn.utils import shuffle
def load_sample(sample_dir):
'''递归读取问件。只支持一级。返回文件名、数值标签、数值对应的标签名'''
print('loading sample dataset..')
lfilenames=[]
labelsnames=[]
for (dirpath,dirnames,filenames) in os.walk(sample_dir):
for filename in filenames:
filename_path=os.sep.join([dirpath,filename])
lfilenames.append(filename_path)
labelsnames.append(int(dirpath.split('\\')[-1]))
lab=list(sorted(set(labelsnames)))
labdict=dict(zip(lab,list(range(len(lab)))))
labels=[labdict[i] for i in labelsnames]
return shuffle(np.asarray(lfilenames), np.asarray(labels), np.asarray(lab))
def get_batchews(image,label,resize_w,resize_h,channels,batch_size):
print(image)
queue=tf.train.slice_input_producer([image,label])
label=queue[1]
image_c=tf.read_file(queue[0])
print(image_c)
image=tf.image.decode_bmp(image_c,channels)
image=tf.image.resize_image_with_crop_or_pad(image,resize_w,resize_h)
image=tf.image.per_image_standardization(image)
image_batch,label_batch=tf.train.batch([image,label],batch_size=batch_size,num_threads=64)
image_batch=tf.cast(image_batch,tf.float32)
labels_batch=tf.reshape(label_batch,[batch_size])
return image_batch,label_batch
def showresult(subplot,title,thisimg):
p=plt.subplot(subplot)
plt.axis('off')
plt.imshow(np.reshape(thisimg),(28,28))
plt.title(title)
def showing(index,label,img,ntop):
plt.figure(figsize=(8,8))
plt.axis('off')
ntop=min(ntop,9)
print(index)
for i in range(ntop):
showresult(100+10*ntop+1+i,label[i],img[i])
plt.show()
if __name__=='__main__':
sample_dir=os.getcwd()+'\\'+'images'
image,labelsnames,label=load_sample(sample_dir)
print(len(image),image[:2],len(label),label[:2])
print(label[:2])
print(labelsnames[:2],label)
batch_size=16
image_batches,label_batches=get_batchews(image,label,28,28,1,batch_size)
with tf.Session() as sess:
init=tf.global_variables_initializer()
sess.run(init)
coord=tf.train.Coordinator()
threads=tf.train.start_queue_runners(sess=sess,coord=coord)
try:
for step in np.arange(10):
if coord.should_stop():
break
images,label=sess.run([image_batches,label_batches])
showing(step,label,images,batch_size)
print(label)
except tf.errors.OutOfRangeError:
print("Done!!!")
finally:
coord.request_stop()
coord.join(threads)