Tensorflow常用方法

获取某个节点的张量

graph = tf.get_default_graph()
inputs = graph.get_tensor_by_name('placeholder:0')

获得所有操作节点

tensor_name_list = [tensor.name for tensor in graph.as_graph_def().node]
import tensorflow as tf
import numpy as np
import os

def vgg16_layer(image_batch):
    """ vgg16_conv5_3 layer """
    # Gray to color if necessary.
    if image_batch.shape[3] == 1:
        node_x = tf.nn.conv2d(image_batch, np.ones((1, 1, 1, 3)),
                              np.ones(4).tolist(), 'VALID')
    else:
        node_x = image_batch

    # Subtract trained average image.
    average_rgb = tf.get_variable('average_rgb1', 3, dtype=image_batch.dtype)
    node_x = node_x - average_rgb

    # VGG16-convolution FUNC
    def vgg_conv(inputs, layer_name, out_dim, with_relu):
        if with_relu:
            activation = tf.nn.relu
        else:
            activation = None
        result = tf.layers.conv2d(inputs, out_dim, [3, 3], 1, padding='same',
                                  activation=activation,
                                  name='conv1_%s' % layer_name)
        return result

    # VGG16-Pooling FUNC
    def vgg_pool(inputs):
        return tf.layers.max_pooling2d(inputs, 2, 2)

    node_x = vgg_conv(node_x, '1_1', 64, True)
    node_x = vgg_conv(node_x, '1_2', 64, False)
    node_x = vgg_pool(node_x)
    node_x = tf.nn.relu(node_x)

    node_x = vgg_conv(node_x, '2_1', 128, True)
    node_x = vgg_conv(node_x, '2_2', 128, False)
    node_x = vgg_pool(node_x)
    node_x = tf.nn.relu(node_x)
    #
    # node_x = vgg_conv(node_x, '3_1', 256, True)
    # node_x = vgg_conv(node_x, '3_2', 256, True)
    # node_x = vgg_conv(node_x, '3_3', 256, False)
    # node_x = vgg_pool(node_x)
    # node_x = tf.nn.relu(node_x)
    #
    # node_x = vgg_conv(node_x, '4_1', 512, True)
    # node_x = vgg_conv(node_x, '4_2', 512, True)
    # node_x = vgg_conv(node_x, '4_3', 512, False)
    # node_x = vgg_pool(node_x)
    # node_x = tf.nn.relu(node_x)
    #
    # node_x = vgg_conv(node_x, '5_1', 512, True)
    # node_x = vgg_conv(node_x, '5_2', 512, True)
    # node_x = vgg_conv(node_x, '5_3', 512, False)
    return node_x

if __name__ == '__main__':
    input = tf.placeholder(tf.float32,(None,640,480,3))
    output = vgg16_layer(input)
    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        saver.save(sess,'./model/model1.ckpt')

 

 

 

import tensorflow as tf
import numpy as np
import os

def vgg16_layers(image_batch):
    """ vgg16_conv5_3 layer """
    # Gray to color if necessary.
    # if image_batch.shape[3] == 1:
    #     node_x = tf.nn.conv2d(image_batch, np.ones((1, 1, 1, 3)),
    #                           np.ones(4).tolist(), 'VALID')
    # else:
    node_x = image_batch

    # Subtract trained average image.
    # average_rgb = tf.get_variable('average_rgb2', 3, dtype=image_batch.dtype)
    # node_x = node_x - average_rgb

    # VGG16-convolution FUNC
    def vgg_conv(inputs, layer_name, out_dim, with_relu):
        if with_relu:
            activation = tf.nn.relu
        else:
            activation = None
        result = tf.layers.conv2d(inputs, out_dim, [3, 3], 1, padding='same',
                                  activation=activation,
                                  name='conv2_%s' % layer_name)
        return result

    # VGG16-Pooling FUNC
    def vgg_pool(inputs):
        return tf.layers.max_pooling2d(inputs, 2, 2)

    node_x = vgg_conv(node_x, '3_1', 256, True)
    node_x = vgg_conv(node_x, '3_2', 256, True)
    node_x = vgg_conv(node_x, '3_3', 256, False)
    node_x = vgg_pool(node_x)
    node_x = tf.nn.relu(node_x)

    node_x = vgg_conv(node_x, '4_1', 512, True)
    node_x = vgg_conv(node_x, '4_2', 512, True)
    node_x = vgg_conv(node_x, '4_3', 512, False)
    node_x = vgg_pool(node_x)
    node_x = tf.nn.relu(node_x)

    node_x = vgg_conv(node_x, '5_1', 512, True)
    node_x = vgg_conv(node_x, '5_2', 512, True)
    node_x = vgg_conv(node_x, '5_3', 512, False)
    return node_x

if __name__ == '__main__':
    input = tf.placeholder(tf.float32,(None,160,120,128))
    output = vgg16_layers(input)
    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        saver.save(sess,'./model/model2.ckpt')

 

import tensorflow as tf
import os
import model1,model2



os.environ['CUDA_VISIBLE_DEVICES'] = '1'


inputs = tf.placeholder(tf.float32,(None,640,480,3))

output = model1.vgg16_layer(inputs)
output = model2.vgg16_layers(output)

var_list = tf.global_variables()

saver = tf.train.Saver(var_list[:9])
with tf.Session() as sess:
        sess.run(tf.variables_initializer(var_list[:9]))
        saver.restore(sess,'./model/model1.ckpt')

        saver = tf.train.Saver(var_list[9:])
        sess.run(tf.variables_initializer(var_list[9:]))
        saver.restore(sess,'./model/model2.ckpt')
        saver = tf.train.Saver(tf.global_variables())
        saver.save(sess,'./model/merge.ckpt')
print('done')
# graph1 = tf.train.import_meta_graph('./model/model1.ckpt.meta')
# var_list = tf.global_variables()
#
# with tf.Session() as sess:
#     saver = tf.train.Saver()
#     sess.run(tf.variables_initializer(var_list))
#     saver.restore(sess,'./model/model1.ckpt')

#
# graph2 = tf.train.import_meta_graph('./model/model2.ckpt.meta')
# var_list = tf.global_variables()[9:]
# with tf.Session() as sess:
#     sess.run(tf.variables_initializer(var_list))
#     saver.restore(sess, './model/model2.ckpt')
#     print('done')
#     saver.save(sess, './model/merge.ckpt')

 

你可能感兴趣的:(深度学习框架)