解析函数
def pres_tf(example_proto)
dics={}
dics['lable']=tf.FixedLenFeature((),dtype=tf.int64,default_value=0)
dics['image']=tf.FixedLenFeature((),dtype=tf.string,default_value='')
parsed=tf.decode_raw(parsed_single_example——example(serialized=example_proto,features=dics)
image= tf.decode-raw(parsed_example['image'],out_type=tf.unit8)
image=pre_process(image) image=tf.cast(image,tf.float32)/255 image=tf.reshpe(image,(28,28,1)) lable=parsed_example['lable'] lable=tf.cast(lable,tf.int32) lable=tf.one_hot(lable,depth=10,on_value=1.0,off_value=0.0) return image,lable
dataset
def dataset(filenames,batch_size,epoch):
dataset=tf.data.TFRecordDataset(filenames=filename)
shuffle_dataset=dataset.map(parres_tf)
batch_dataset=shuffle_dataset.batch(batch_size).repeat(epoc函数)
batch_dataset.prefetch(10
iterator=batch_dataset.make_one_shot_iterator()
next_element=iterator.qet_next()
retur next_element
图像预处理
def pre_process(images,random_flip_up_down=False,random_flip_right=False,random_brightness
if random_flip_up_down:
image=tf.image.random_flip_up_down(images)
if random_flip_left_right:
image=tf.image.random_flip_left_right(images)
if random_brightness
image=tf.image.random_brightness(images,max_delta=0.2)
if random_contrast:
image=tf.image.random_contrast(images,0.9,1.1)
if random_saturation:
image=tf.image.random_saturation(images,0.3,0.5)
if random_hue:
image=tf.image.random_hue(images,0.2)
new_size=tf.constant([28,28],dtype=tf.int32)
image=tf.image.resize_images(images,new_size)
return image
tf.reset_defult_graph() filename=['train.tfrecords'] next_element=dataset(filenames,batch_size=5,epochs=1) init=tf.global_variables_initializer()
with tf.session() as sess:
sess.run(init)
batch_images,batch_lables=sess.run[[next_element[0],next_element[1]])
for i in range(batch_images.shape[0]]):
print(batch_image.shape)
img=np.array(batch_image[i])
import tensorflow as tf
tf.reset——default_graph()
epochs=15
batch_size=100
total_sum=0
filenames=['train_{}.tfrecords'.format(i) for i in range(10)
next_element=dataset(filenames,batch_size=batch_size,epochs=epochs)
mnist=input_data.read_data_sets('MNIST_data',onr_hot=Ture)
train_num=mnist.train.num_examples
input_data=tf.placeholder(tf.float32,shape=(None,784)
input_lable=tf.placeholder(tf.float32,shape=(None,10)
w1=tf.get_variable(shape=(784,64),name='hidden_1_w')
b1=tf.get_variable(shape=(64),initializer=tf.zeros_initializer(),name='hidden_1_b')
w2=tf.get_variable(shape=(64,32),name='hidden_2_w')
b1=tf.get_variable(shape=(32),initializer=tf.zeros_initializer(),name='hidden_2_b')
w3=tf.get_variable(shape=(32,10),name='layer_output')
logit层
output=tf.matmu1(tf.nn.relu(tf.matmul(tf.nn.relu(tf.matmul(input_data,w1)+b1),w2)+b2,w3)
loss=tf.