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

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

在这里插入图片描述

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)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值