将图片制作成内存对象数据集

将图片制作成内存对象数据集

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))
    # return lfilenames,labels,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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值