tf.Variable: 用于W与b等参数tf.constant: 用于超参数tf.placeholder: 用于数据tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost): 进行反向传播, 在优化的时候, 一定要sess.run(optimizer, feed_dict={}), run的是optimizer, 但是在测试查看损失或者准确率的时候, 要使用sess.run(cost, feed_dict={}), 不要进行优化tf.nn: TensorFlow的核心模块tf.train: 训练模块tf.image: 图像增强模块tf.image.decode_jpegtf.image.decode_pngtf.image.encode_jpegtf.image.encode_pngtf.image.convert_image_dtype: 在大部分的图像API中即使我们输入的0-255的图像, 它在内部还是会转换为0-1之间的实数, 接着在转为0-255返回给用户, 为了避免精度的损失, 推荐调用该函数提前转换类型tf.image.resize_images
tf.image.resize_image_with_crop_or_pad: 对图像进行裁剪或者填充tf.image.central_crop: 等比例缩放, 比例在(0,1]tf.image.flip_up_down: 上下翻转tf.image.flip_left_right: 水平翻转tf.image.transpose_image: 沿着对角线翻转tf.image.adjust_brightnesstf.image.adjust_constrasttf.image.adjust_huetf.image.adjust_saturationtf.image.per_image_standardizationtf.image.draw_bounding_boxes: 画出bounding_boxtf.expand_dims(image, 0): 将第0维度之前使用1添加一个维度, 该函数在添加维度的时候非常好用tf.clip_by_value(v, 1e-10, 1): 将v限定在min与maxtf.addtf.reduce_meantf.reduce_sumtf.argmaxtf.matmultf.multiplytf.subtracttf.random_normal: 正态分布, 参数stddev(standard deviation)为标准差tf.truncated_normal: 与tf.random_normal类似, 正态分布, 如果生成的随机数偏移平均值超过2个标准差, 重新生成tf.random_uniform: 均匀分布tf.random_gamma: Gamma分布tf.zerostf.onestf.filltf.constanttf.nn.sigmoidtf.nn.tanhtf.nn.relutf.GraphKeys中tf.GraphKeys.TRAINABLE_VARIABLES: 返回字符串trainable_variables, 这是一个内置集合的名字; 默认通过tf.Variable定义时会添加到该集合中, 如果将trainable=False则不会了tf.GraphKeys.LOSSES: 默认的损失集合名, 之后使用tf.losses模块才会使用到该内置集合tf.GraphKeys.BIASES: 默认的偏移集合名tf.GraphKeys.WEIGHTS: 默认的权重集合名BIASES和WEIGHTS不常用TRAINABLE_VARIABLES包含在MODEL_VARIABLES中,MODEL_VARIABLES包含在GLOBAL_VARIABLES中tf.Optimizer只优化tf.GraphKeys.TRAINABLE_VARIABLES中的变量tf.add_to_collection(name, value): 将value添加到名为name的集合中tf.get_collection(name): 返回名为name的集合的所有元素tf.get_collection(name, namespace_prefix): 返回名为name的集合namespace_prefix名称空间下的Tensor, 注意末尾不能有/tf.nn.conv2d(input, filter, strides, padding)tf.nn.max_pool(input, ksize, strides, padding)tf.nn.bias_add(input, bias): 注意这里的bias应该为(n, )之类的奇怪的矩阵, 也就是我们在创建bias的时候, 要tf.get_variable(‘biases‘, [SIZE], init...), 而不能为tf.get_variable(‘biases‘, [1, SIZE]或者[1, 1, 1, SIZE], init...)import tensorflow.contrib.slim as slimimport tensorflow.contrib.slim.python.slim.nets.inception_v3 as inception_v3: 定义了InceptionV3的网络结构, 但是在Google中已经完成了训练的权重不在这里, 需要从官网上下载过来进行迁移学习
mnist = input_data.read_data_sets('mnist/', dtype=tf.uint8, one_hot=True)
writer = tf.python_io.TFRecordWriter('demo.tfrecord')
for i in range(mnist.train.num_examples):
image_raw = mnist.train.images[i].tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'pixels': tf.train.Feature(int64_list=tf.train.Int64List(value=[len(mnist.train.images[i])])),
'label': tf.train.Feature(int64_list=tf.train.Int64List( value=[np.argmax(len(mnist.train.labels[i]))])),
'image_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_raw]))
}))
writer.write(example.SerializeToString())
reader = tf.TFRecordReader()
# 加载文件名队列
filename_queue = tf.train.string_input_producer(['./demo.tfrecord'])
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example, features={
'pixels': tf.FixedLenFeature([], tf.int64),
'label': tf.FixedLenFeature([], tf.int64),
'image_raw': tf.FixedLenFeature([], tf.string)
})
image = tf.decode_raw(features['image_raw'], tf.uint8)
pixels = tf.cast(features['pixels'], tf.int32)
label = tf.cast(features['label'], tf.int32)
with tf.Session() as sess:
coord = tf.train.Coordinator()
# 这个必须有, 否则程序一直阻塞
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
for i in range(10):
print(sess.run([image, pixels, label]))
tf.train.exponential_decay(staircase=True):
decay_learning_rate=learning_rate*decay_rate^(global_step/decay_step)
learning_rate为初始学习率(如0.1), decay_rate为衰减系数(0-1, 如0.1), decay_step为衰减速率(如50)tf.nn.softmax_cross_entropy_with_logits(labels=,logits=)tf.nn.sigmoid_cross_entropy_with_logits(labels=,logits=)tf.nn.sparse_softmax_cross_entropy_with_logits(labels=,logits=)tf.greater(v1, v2): v1 > v2返回True, 否则返回Falsetf.where(tf.greater(v1, v2), v1, v2): 分段函数, 当v1>v2是为v1, 否则为v2保存模型
```
* 生成三个文件:.meta,.data,checkpoint, 注意没有model.ckpt`文件只导入变量数据, 而不导入图的结构, 这样我们使用模型的时候, 需要先创建好图的结构, 在使用saver导入变量, 这样我们需要时初始化全局变量, 因为变量的是从外部导入过来的
```py
py saver = tf.train.import_meta_graph(‘/path/to/model.ckpt.meta‘) with tf.Session() as sess: sess.restore(sess, ‘/path/to/model.ckpt‘) # 通过张量的名称来获取张量, 因为在导入图时, 我们一般处于一个新的项目中, 没有图, 而导入了图我们有没有引用, 这个时候时候就使用如下方法即可 print(sess.run(tf.get_default_graph().get_tensor_\ by_name(‘add:0‘))保存为.ckpt格式, 计算图与变量是分开的, 但是有时候, 尤其是在保存预训练模型的模型的时候, 我们希望freeze一些层, 也就是将一些W和b的变量固定住, 这个时候就需要将Variable转为constant, 见下面
保存模型
py from tensorflow.python.framework import graph_util with tf.Session() as sess: sess.run(init_op) with tf.gile.GFile(‘/path/to/model.pb‘, ‘wb‘) as fd: graph_def = tf.get_default_graph().as_graph_def() ouput_graph_def = graph_util.convert_variables_to_constants(sess, graph_def, [‘add‘]) fd.write(output_graph_def.SerializeToString())
导入模型
py with tf.Session() as sess: with tf.gile.GFile(‘/path/to/model.pb‘, ‘rb‘) as fd: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) result = tf.import_graph_def(graph_def, return_elements=[‘add:0‘]) # 注意这里填写的是张量名, add为节点名, 因为我们在上面将变量转为了常量, 所以可以直接输出结果, 现在result保存了结果 print(sess.run(result))
tf.get_checkpoint_state(dirpath): 返回ckpt对象ckpt.model_checkpoint_path: ckpt文件路径tf.global_variables(): 返回所有的Variable, 他们存储在tf.GraphKeys.VARIABLEStf.Variable: 创建一个变量tf.get_variable: 创建一个变量, 与tf.Variable不同的是一定要指定变量名在命名空间中创建变量, reuse一定要记得到使用上
```py
with tf.variable_scope(‘‘, result=True): # 可以拿到在上面foo创建的v v = tf.get_variable(‘foo/v‘, shape=[1, 1]) print(v)tf.group([train, cost]): 在sess.run([...])是我们传入一个列表是为了可以同时计算多个op, 我们也可以通过op = tf.group([...])的方法返回一个op, 这样在sess.run(op)就可以写少一点了tf.one_hot(labels, C, axis=0): 将labels转为维度为C, 在axis=0方向的填充的one_hottf.reduce_mean(mat, axis=0): 计算均值tf.train.string_input_producer(filename_list, shuffle=False, num_epochs=5): 将filename_list中的文件预先记录加来, 将来要放到文件名队列中, 返回文件名队列tf.WholeFileReader(): 返回reader对象, 用于读取文件名队列加载文件tf.FixLengthRecordReader(record_bytes=bytes): 返回reader对象, 每次读取定长record_bytestf.train.start_queue_runner(sess=sess): 与上面的string_input_producer, WholeFileReader, FixLengthRecordReader关系密切, 调用此方法才真正的将文件名队列加载到队列中, tf读取数据才起步tf.examples.tutorials.mnist.input_data.read_data_sets(‘MNIST_data‘, one_hot=True): input_data模块调用read_data_sets函数读取注明的MNIST数据到MNIST_data中, 如果不存在则从网上下载, 返回一个Datasets对象
train.images, train.labels, validation.images, validation.labels, test.images, test.labels, 他们都是np.ndarray; next_batch(50): 下一个batch(采用mini-batch)tf.gfile: 类似于os模块, 可以等价替换掉os模块tf.truncated_normal(shape, stddev=0.1): 一般用于W的初始化tf.zeros(shape_tuple): 类似于np.zerostf.matmul, tf.reshape, tf.add, tf.equaltf.FIFOQueuetf.RandomShuffleQueue: 每次出队会随机从队列中取出一个元素tf.train.Coordinator: 多线程协同tf.train.QueueRunner: 启动多线程操作同一个队列
tf.train.Cooridator和tf.train.QueueRunnertf.random_crop(img, [h, w, c]): 裁剪图片tf.image.random_brightness(img, max_delta=60): 亮度tf.image.random_contrast(img, lower=0.2, upper=1.8): 对比度tf.image.random_flip_left_right(img): 随机翻转, 50%水平左右翻转, 50%不会
with tf.Session() as sess:
image = tf.image.convert_image_dtype(tf.image.decode_jpeg(tf.gfile.FastGFile('/path/to/pic.jpeg', 'rb').read()), tf.float32)
# 要为4维度的, 第一个维度为样本的个数
batched = tf.expand_dims(image, 0)
# 一定要为3维度的
boxes = tf.constant([[[0.5, 0.5, 0.75, 0.75]]])
image_with_box = tf.image.draw_bounding_boxes(batched, boxes)
result = sess.run(image_with_box)
plt.imshow(result)
plt.show()
with tf.Session() as sess:
image = tf.image.convert_image_dtype(tf.image.decode_jpeg(tf.gfile.FastGFile('/path/to/pic.jpeg', 'rb').read()), tf.float32)
# 要为4维度的, 第一个维度为样本的个数
batched = tf.expand_dims(image, 0)
# 一定要为3维度的
boxes = tf.constant([[[0.5, 0.5, 0.75, 0.75]]])
# 注意注意注意:!!!, 此处一定要给出min_object_coverted=0.1参数
bbox_begin, bbox_size, bbox_for_draw = tf.image.sample_distorted_bounding_boxes(tf.shape(image), bounding_boxes=bboxes, min_object_coverted=0.1)
image_with_box = tf.image.draw_bounding_boxes(image, bbox_for_draw)
result = sess.run(image_with_box)
plt.imshow(result)
plt.show()
tf.image.sample_distorted_bounding_box(tf.shape(image), bounding_boxes=bbox, min_object_covered=0.1)tf.image.convert_image_dtypetf.image.resize_imagestf.image.random_flip_left_righttf.slice: 从原始图片中裁剪标注框大小
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# 对输入的图像进行饱和度, 对比度, 亮度, 色相的随机变化
def distort_color(image, color_ordering=0):
if color_ordering == 0:
image = tf.image.random_brightness(image, max_delta=32./255.)
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
image = tf.image.random_hue(image, max_delta=0.2)
image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
elif color_ordering == 1:
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
image = tf.image.random_brightness(image, max_delta=32./255.)
image = tf.image.random_hue(image, max_delta=0.2)
image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
return tf.clip_by_value(image, 0.0, 1.0)
# 从原始图像中裁剪出一个bounding box, 对bounding box的图像进行缩放到可以输入到NN中的大小
def preprocess_for_train(image, height, width, bbox):
if bbox is None:
bbox = tf.contrast([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
if image.dtype != tf.float32:
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(tf.shape(image), bounding_boxes=bbox, min_object_covered=0.1)
distorted_image = tf.slice(image, bbox_begin, bbox_size)
distorted_image = tf.image.resize_images(distorted_image, [height, width], method=np.random.randint(4))
distorted_image = tf.image.random_flip_left_right(distorted_image)
distorted_image = distort_color(distorted_image, np.random.randint(1))
return distorted_image
def main():
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
image_raw_data = tf.gfile.FastGFile('./15.jpg', 'rb').read()
img_data = tf.image.decode_jpeg(image_raw_data)
boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])
for i in range(6):
result = preprocess_for_train(img_data, 299, 299, boxes)
plt.imshow(sess.run(result))
plt.show()
if __name__ == '__main__':
main()
tensorboard --logdir dir/ --port 6006: 启动tensorboard
import os
import sys
import time
import tensorflow as tf
from tensorflow.python.framework import graph_util
from tensorflow.examples.tutorials.mnist import input_data
INPUT_NODES = 784
OUTPUT_NODES = 10
LAYER1_NODES = 50
BATCH_SIZE = 100
LEARNING_RATE = 0.001
REGULARIZATION_RATE = 0.0001
TRAINING_STEP = 30000
def get_weight_variable(shape, regularize):
weights = tf.get_variable('weights', shape=shape, initializer=tf.truncated_normal_initializer(stddev=0.1, seed=1))
if regularize:
tf.add_to_collection('losses', regularize(weights))
return weights
def inference(input_tensor, regularize):
with tf.variable_scope('layer1', reuse=False):
weights = get_weight_variable([INPUT_NODES, LAYER1_NODES], regularize)
biases = tf.Variable(tf.constant(0.1, shape=[LAYER1_NODES]), name='biases')
layer1 = tf.nn.relu(tf.matmul(input_tensor, weights) + biases)
with tf.variable_scope('layer2', reuse=False):
weights = get_weight_variable([LAYER1_NODES, OUTPUT_NODES], regularize)
biases = tf.Variable(tf.constant(0.1, shape=[OUTPUT_NODES]), name='biases')
layer2 = tf.matmul(layer1, weights) + biases
return layer2
import os
import sys
import time
import tensorflow as tf
from tensorflow.python.framework import graph_util
from tensorflow.examples.tutorials.mnist import input_data
import mnist_inference
def train(mnist):
X = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODES], name='X')
Y = tf.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODES], name='Y')
global_step = tf.Variable(0, trainable=False)
regularize = tf.contrib.layers.l2_regularizer(0.001)
Z2 = mnist_inference.inference(X, regularize)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=Z2)) + tf.add_n(tf.get_collection('losses'))
optimizer = tf.train.AdamOptimizer(mnist_inference.LEARNING_RATE).minimize(cost, global_step=global_step)
prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Z2, 1))
accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32))
init_op = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init_op)
for i in range(30000):
batch = mnist.train.next_batch(mnist_inference.BATCH_SIZE)
_, accuracy_score, step = sess.run([optimizer, accuracy, global_step], feed_dict={X:batch[0], Y:batch[1]})
if i % 1000 == 0:
print('After %s step(s), accuracy is %g' % (step, accuracy_score))
saver.save(sess, 'models/model.ckpt', global_step=global_step) # 使用global_step=global_step, 则持久化的ckpt文件名会加上-global_step的名字
def main(argv=None):
mnist = input_data.read_data_sets('mnist/', one_hot=True)
train(mnist)
if __name__ == '__main__':
tf.app.run()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import time
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import graph_util
from tensorflow.examples.tutorials.mnist import input_data
import mnist_inference
import mnist_train
def eval(mnist):
X = tf.placeholder(tf.float32, shape=[None, mnist_inference.INPUT_NODES], name='X')
Y = tf.placeholder(tf.float32, shape=[None, mnist_inference.OUTPUT_NODES], name='Y')
Z2 = mnist_inference.inference(X, None)
prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Z2, 1))
accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32))
validate_feed = {X:mnist.validation.images, Y:mnist.validation.labels}
saver = tf.train.Saver()
while True:
with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state('models/')
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
accuracy_score = sess.run(accuracy, feed_dict=validate_feed)
print('After %s training step(s), validation accuracy = %g' % (global_step, accuracy_score))
else:
print('Checkpoint File Is Not Found')
time.sleep(10)
def main(argv=None):
mnist = input_data.read_data_sets('mnist/', one_hot=True)
eval(mnist)
if __name__ == '__main__':
tf.app.run()
[batch_size, h, w, c], 但是我们的filter为[h, w, c, num_of_fitlers][size, h, w, c], 因此如果接下来为FC, 则我们需要他变为[node, h * w * c]/cpu:0with tf.device(‘/gpu:0‘):的语法, 在上下面管理器中定义OpNode和变量, 但是在Tensorflow不同版本中GPU处理的数据类型不同, 有些版本无法处理float64类型的, 如果人为强制的指定则会报错, 不能把代码写死, 所以这里在sess.run时要指定一个关键字参数, sess.run(optimizer, allow_soft_placement=True), 这样当GPU无法处理时自动使用CPU处理原文:https://www.cnblogs.com/megachen/p/10661052.html