1. ## 判断CUDA是否可用
    2. tf.test.is_built_with_cuda()
    3. ##判断GPU是否可用
    4. tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None)
    5. ## Tensorboard可视化训练
    6. import tensorflow as tf
    7. summ_scalar_list = []
    8. summ_scalar_list.append(tf.summary.scalar('F1_loss', loss[1]))
    9. summ_scalar_list.append(tf.summary.scalar('F2_loss', loss[2]))
    10. summ_scalar = tf.summary.merge(summ_scalar_list)
    11. writer = tf.summary.FileWriter(logpath, sess.graph)
    12. writer = tf.summary.FileWriter(logpath)
    13. writer.add_summary(sess.run(summ_scalar, feed_dict=feed_dict), iters)
    14. # 禁止输入特殊字符
    15. tensorboard --logdir=/home/yifei.wang/log
    16. ### tensorflow模型
    17. ## CheckPoint(*.ckpt):仅包含权重数据
    18. init = tf.global_variables_initializer()
    19. sess = tf.Session()
    20. sess.run(init)
    21. global_vars = tf.global_variables()
    22. saver = tf.train.Saver(var_list=global_vars)
    23. # 保存
    24. saver.save(sess, 'model.ckpt', epoch)
    25. saver.save(sess=sess, save_path=model_save_path, global_step=step) # step作为后缀加到模型名字中
    26. """
    27. ## 保存后文件:调用后创建3个数据文件1个检查点文件
    28. 1. checkpoint # 检查点文件
    29. 2.model.ckpt.data-00000-of-00001 # weights等参数以dict形式保存至该文件
    30. 3.model.ckpt.index # 内部需要的某种索引来正确映射1、2文件
    31. 4.model.ckpt.meta # MetaGraph数据保存至该文件,可使用tf.train.import_meta_graph加载
    32. """
    33. # 加载
    34. model_file=tf.train.latest_checkpoint('ckpt/') # 自动获取最后一次保存的模型
    35. saver.restore(sess, 'model.ckpt')
    36. ## GraphDef(*.pd): 表示MetaGraph的protocol buffer格式文件;MetaGraph包含计算图节点及相关张量
    37. import tensorflow as tf
    38. x = tf.placeholder(tf.float32, name="input")
    39. a = tf.Variable(tf.constant(5., shape=[1]), name="a")
    40. b = tf.Variable(tf.constant(6., shape=[1]), name="b")
    41. c = tf.Variable(tf.constant(10., shape=[1]), name="c")
    42. d = tf.Variable(tf.constant(2., shape=[1]), name="d")
    43. tensor1 = tf.multiply(a, b,"mul")
    44. tensor2 = tf.subtract(tensor1, c,"sub")
    45. tensor3 = tf.div(tensor2, d,"div")
    46. result = tf.add(tensor3, x, "add")
    47. inial = tf.global_variables_initializer()
    48. sess = tf.Session()
    49. sess.run(init)
    50. result = sess.run(result, feed_dict={x: 1.0})
    51. # 保存
    52. # 将计算图中变量转换为常量,并指定输出节点为'add'
    53. constant_graph = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, ['add'])
    54. with tf.gfile.FastGFile('model.pb', mode='wb') as fd:
    55. # 将计算图写入该pb文件
    56. fd.write(constant_graph.SerializeToString())
    57. # 加载
    58. with tf.gfile.FastGFile('model.pb', 'rb') as fd:
    59. graph_def = tf.GraphDef()
    60. graph_def.ParseFromString(fd.read())
    61. result, x = tf.import_graph_def(graph_def, return_elements=['add:0', 'input:0'])
    62. result = sess.run(result, feed_dict={x: 5.0})
    63. """
    64. ## checkpoint(*.ckpt)
    65. 由tf.train.Saver()对象调用saver.save()生成,只包含若干Variables对象序列化后的数据,不包含图结构.
    66. 仅有checkponit模型不提供代码无法重新构建计算图
    67. 加载checkpoint时,调用saver.restore(session, checkpoint_path)
    68. """
    69. """
    70. ## GraphDef(*.pd)
    71. 包含protobuf(Protocol Buffers,Google的序列化框架)对象序列化后的数据
    72. 包含计算图,可得到所有运算符(Operators)细节
    73. 包含张量(tensors)和Variables定义,但不包括Variable值,因此只能从中恢复图,但一些训练的权值仍需要从checkpoint恢复
    74. """
    75. # 利用*.pd文件构建计算图
    76. def load_graph(model_file):
    77. graph = tf.Graph()
    78. graph_def = tf.GraphDef()
    79. with open(model_file, 'rb') as f:
    80. graph_def.ParseFromString(f.read())
    81. with graph.as_default():
    82. tf.import_graph_def(graph_def)
    83. return graph
    84. ## 查看tf的版本及cpu or gpu
    85. tf.__version__()
    86. sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))