tf读取图像并预处理例程

import tensorflow as tf
import numpy as np
import cv2 
import random
import matplotlib.pyplot as plt

batchsize = 50
ImgSize = 96

#读取数据
#------------------------------------------------------------------------------
import pickle
def load_obj(name ):
    with open(name, 'rb') as f:
        return pickle.load(f)

dict_read = load_obj('CASIA-WebFace_S.dat')

vfilenames = dict_read['ValidName'].copy()
vlabels = dict_read['ValidLab'].copy()
vfilenames = vfilenames.reshape(vfilenames.shape[0],1)
vlabels = vlabels.reshape(vlabels.shape[0],1)

InputFilenames = tf.placeholder(tf.string, shape=[None,1])
InputLables = tf.placeholder(tf.int32, shape=[None,1])

dataset = tf.data.Dataset.from_tensor_slices( (InputFilenames,InputLables) )

def ReadTrainImg(filename, label):
    print(filename)
    print(label)
    image_string = tf.read_file(filename[0])
    image_decoded = tf.image.decode_png(image_string,channels=3)

    image_decoded = tf.image.random_flip_left_right(image_decoded)

    #image_resized = tf.image.resize_images(image_decoded, [ImgSize, ImgSize])
    #image_resized = tf.random_crop(image_decoded,[ImgSize,ImgSize,3])
    image_resized = tf.image.crop_to_bounding_box(image_decoded,8,8,ImgSize,ImgSize)
    #image_resized = (image_resized/127.5)-1
    return image_resized, label

dataset = dataset.map(ReadTrainImg)
#dataset = dataset.shuffle(buffer_size=10000)
dataset = dataset.batch(batchsize)
dataset = dataset.repeat()
iterator = dataset.make_initializable_iterator()

next_element = iterator.get_next()

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(iterator.initializer, feed_dict={InputFilenames: vfilenames,InputLables: vlabels})
    for i in range(10):
        value = sess.run(next_element)
        img = value[0][0]
        img = img.astype(np.uint8)
        cv2.imshow('Test',img)
        cv2.waitKey()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值