'''第七章:图像数据处理'''
# 将 MNIST 输入数据转化为 TFRecord 的格式
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
#生成整数型的属性
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
# 生成字符串型的属性
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
mnist = input_data.read_data_sets('D:/BaiduNetdiskDownload/deeplearning/TensorFlow/Data_sets/MNIST_data',dtype=tf.uint8,one_hot=True)
images = mnist.train.images
labels = mnist.train.labels
pixels = images.shape[1]
num_examples = mnist.train.num_examples
# 输出TFRecord 文件的地址
filename = 'D:/BaiduNetdiskDownload/deeplearning/TensorFlow/output.tfrecords'
# 创造一个 writer 来写 TFRecord 文件
writer = tf.python_io.TFRecordWriter(filename)
for index in range(num_examples):
# 将图片矩阵转换成一个字符串
image_raw = images[index].tostring()
# 将一个样例转化为 Example Protocol Buffer,并将所有的信息写入这个数据结构
example = tf.train.Example(features=tf.train.Features(feature={
'pixels':_int64_feature(pixels),
'label': _int64_feature(np.argmax(labels[index])),
'image_raw': _bytes_feature(image_raw)}))
# 将一个Example 写入TFRecord 文件
writer.write(example.SerializeToString())
writer.close()
In [14]:
'''读取 TFRecord 文件中的数据'''
import tensorflow as tf
reader = tf.TFRecordReader()
# 创建一个队列来维护输入文件列表
filename_queue = tf.train.string_input_producer(
['D:/BaiduNetdiskDownload/deeplearning/TensorFlow/output.tfrecords'])
# 从文件中读取一个样例,也可以使用 read_up_to 函数一次性读取多个样例
_,serialized_example = reader.read(filename_queue)
# 解析读入的样例,如果解析多个,可以使用parse_example 函数
features = tf.parse_single_example(serialized_example,features={
'image_raw': tf.FixedLenFeature([],tf.string),
'pixels': tf.FixedLenFeature([],tf.int64),
'label': tf.FixedLenFeature([],tf.int64),
})
# tf.decode_raw 可以将字符串解析成图像对应的像素数组
image = tf.decode_raw(features['image_raw'],tf.uint8)
label = tf.cast(features['label'],tf.int32)
pixels = tf.cast(features['pixels'],tf.int32)
sess = tf.Session()
# 启动多线程处理输入数据
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess,coord=coord)
for i in range(1):
print(sess.run([image,label,pixels]))
In [36]:
import matplotlib.pyplot as plt
# 读取文件的原始数据
image_row_data = tf.gfile.FastGFile('D:/BaiduNetdiskDownload/deeplearning/TensorFlow/tensorflow-tutorial-master/Deep_Learning_with_TensorFlow/datasets/cat.jpg', 'rb').read()
with tf.Session() as sess:
# 对png格式的图像进行解码,从而得到图像对应的三维矩阵
img_data = tf.image.decode_jpeg(image_row_data)
# 把对应的三维数组改为实数数组
img_data = tf.image.convert_image_dtype(img_data,dtype=tf.float32)
resized = tf.image.resize_images(img_data,[180,267],method=1)
# 组成四维矩阵
batched = tf.expand_dims(resized,0)
boxes = tf.constant([[[0.05,0.05,0.9,0.7],[0.35,0.47,0.5,0.56]]])
result = tf.image.draw_bounding_boxes(batched,boxes)
# print(img_data.eval())
#显示图片
plt.imshow(result[0].eval())
plt.show()
#保存图片
# encoded_image = tf.image.encode_jpeg(img_data)
# with tf.gfile.FastGFile('D:/BaiduNetdiskDownload/deeplearning/TensorFlow/output','wb') as f:
# f.write(encoded_image.eval())
In [46]:
'''队列与多线程'''
#初始化先进先出队列,
q = tf.FIFOQueue(2,'int32')
init = q.enqueue_many(([0,10],))
x = q.dequeue()
y = x + 1
q_inc = q.enqueue([y])
with tf.Session() as sess:
sess.run(init)
for _ in range(5):
v,_ = sess.run([x,q_inc])
print(v)
# 多线程
import threading
import time
def MyLoop(coord,worker_id):
while not coord.should_stop():
if np.random.rand() < 0.1:
print('Stoping from id: %d\n' % worker_id)
# 通知其他线程停止
coord.request_stop()
else:
print('Working on id: %d\n' % worker_id)
time.sleep(1)
coord = tf.train.Coordinator()
# 创建5个线程
threads = [threading.Thread(target=MyLoop,args=(coord,i)) for i in range(5)]
#启动所有线程
for t in threads:
t.start()
coord.join(threads)
In [49]:
'''LSTM 结构的简单实现'''
lstm_hidden_size = 2
batch_size = 100
# 定义一个LSTM 结构,LSTM 中使用的变量也会在该函数中自动生成
lstm = tf.nn.rnn_cell.BasicLSTMCell(lstm_hidden_size)
state = lstm.zero_state(batch_size,tf.float32)
# 定义损失函数
loss = 0
# for i in range(num_steps):
# if i > 0 :
# tf.get_variable_scope().reuse_variables()
# lstm_output,state = lstm(current_input,state)
# final_output = fully_connected(lstm_output)
# loss += calc_loss(final_output,expected_output)
In [1]:
'''利用循环神经网络实现对Sin x 的取值的预测'''
import tensorflow as tf
import numpy as np
# import matplotlib as mpl
# mpl.use('Agg')
import matplotlib.pyplot as plt
hidden_size = 30 # LSTM 中隐藏层
num_layers = 2 # LSTM 的层数
timesteps = 10 #循环神经网络的训练序列长度
training_steps = 10000 # 训练轮数
batch_size = 32 # batch 的大小
training_examples = 10000 #训练数据个数
testing_examples = 1000 #测试数据个数
sample_gap = 0.01 # 采样间隔
def generate_data(seq):
X = []
Y = []
for i in range(len(seq) - timesteps):
X.append([seq[i: i + timesteps]])
Y.append([seq[i + timesteps]])
return np.array(X,dtype=np.float32),np.array(Y,dtype=np.float32)
def lstm_model(X,Y,is_training):
# 使用多层的 LSTM 结构
cell = tf.nn.rnn_cell.MultiRNNCell([tf.nn.rnn_cell.BasicLSTMCell(hidden_size) for _ in range(num_layers)])
# 使用tensorflow 借口将多层的LSTM结构连接成RNN网络,并计算其前向传播结果
outputs,_ = tf.nn.dynamic_rnn(cell,X,dtype=tf.float32)
# outputs 是顶层LSTM在每一步的输出结果,它的维度是[batch_size,time,hidden_size]
output = outputs[:,-1,:]
# 对LSTM网络的输出再加一层全连接层并计算损失
predictions = tf.contrib.layers.fully_connected(output,1,activation_fn=None)
# 只在训练时计算损失函数和优化步骤。测试时直接返回预测结果
if not is_training:
return predictions,None,None
# 计算损失函数
loss = tf.losses.mean_squared_error(labels=Y,predictions=predictions)
# 创建模型优化器并得到优化步骤
train_op = tf.contrib.layers.optimize_loss(loss,tf.train.get_global_step(),
optimizer='Adagrad',learning_rate=0.1)
return predictions,loss,train_op
def train(sess,train_X,train_Y):
# 将训练数据以数据集的方式提供给计算图
ds = tf.data.Dataset.from_tensor_slices((train_X,train_Y))
ds = ds.repeat().shuffle(1000).batch(batch_size)
X,Y = ds.make_one_shot_iterator().get_next()
# 调用模型,得到预测结果、损失函数、和训练操作
with tf.variable_scope('model'):
predictions,loss,train_op = lstm_model(X,Y,True)
# 初始化变量
sess.run(tf.global_variables_initializer())
for i in range(training_steps):
_,l = sess.run([train_op,loss])
if i % 100 == 0:
print('train step: ' + str(i) + ", loss: "+ str(l))
def run_eval(sess,test_X,test_Y):
# 将测试数据以数据集的方式提供给计算图
ds = tf.data.Dataset.from_tensor_slices((test_X,test_Y))
ds = ds.batch(1)
X,Y = ds.make_one_shot_iterator().get_next()
# 调用模型得到计算结果。这里不需要输入真正的y值
with tf.variable_scope('model',reuse=True):
prediction,_,_ = lstm_model(X,[0.0],False)
# 将预测数据存入一个数组
predictions = []
labels = []
for i in range(testing_examples):
p, l = sess.run([prediction,Y])
predictions.append(p)
labels.append(l)
#计算rmse 作为评价指标
predictions = np.array(predictions).squeeze()
labels = np.array(labels).squeeze()
rmse = np.sqrt(((predictions - labels) ** 2).mean(axis=0))
print('Mean Square Error is : %f' % rmse)
# 对预测的sin 函数曲线进行绘图,
plt.figure()
plt.plot(predictions,label='predictions')
plt.plot(labels,label='real_sin')
plt.legend()
plt.show()
test_start = (training_examples + timesteps)* sample_gap
test_end = test_start + (testing_examples + timesteps) * sample_gap
train_X,train_Y = generate_data(np.sin(np.linspace(0,test_start,training_examples + timesteps,dtype=np.float32)))
test_X,test_Y = generate_data(np.sin(np.linspace(test_start,test_end,testing_examples + timesteps,dtype=np.float32)))
with tf.Session() as sess:
# 训练数据
train(sess,train_X,train_Y)
# 使用训练好的模型对测试数据进行预测
run_eval(sess,test_X,test_Y)