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()
tf读取图像并预处理例程
最新推荐文章于 2021-09-15 15:48:00 发布