将图片制作成内存对象数据集
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 datatest...")
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(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)
data_dir='mnist_digits_images\\'
(image,label),labelsnames=load_sample(data_dir)
print(len(image),image[:2],len(label),label[:2])
print(labelsnames[label[:2]],labelsnames)
def get_batches(image,label,resize_w,resize_h,channels,batch_size):
queue=tf.train.slice_input_producer([image,label])#实现一个输入队列
label=queue[1]#从输入队列里读取标签
image_c=tf.read_file(queue[0])
print(label)
print(image)
print(image,resize_h,resize_w)
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)
images_batch=tf.cast(image_batch,tf.float32)#将数据类型转换为float32
#修改标签的形状
labels_batch=tf.reshape(label_batch,[batch_size])
return images_batch,labels_batch
batch_size=16
image_batches,label_batches=get_batches(image,label,28,28,1,batch_size)
def showresult(subplot,title,thisimg):
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=(20,10))
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__':
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)