본문 바로가기

Deep Learning

[tensorflow] how to load and use the saved trained network in python

반응형

** tensorflow에서 미리 저장한 CNN로드하고 사용하기 **

** See '[tensorflow] how to save trained network' first.



// PSEUDO CODE ////////////////////////////////////////

import tensorflow as tf

import something


# save trained net

net_dir = 'path_to_saved_trained_network'

image_path = 'path_to_input_images'


with tf.Session() as sess:


    # Step1) Load graph from meta file (meta file contains the graph that I had defined before)

    saver = tf.train.import_meta_graph(os.path.join(net_dir, 'saved_checkpoint-0.meta'))


    # Step2) Restore all the weights values

    saver.restore(sess, os.path.join(net_dir, 'saved_checkpoint-0'))

    print('Trained Deep Network is restored')


    # Step3) Recall placeholder and operation

    graph = tf.get_default_graph()

    x = graph.get_tensor_by_name("input_node:0") # placeholder for input 

    r = graph.get_tensor_by_name("output_node:0") # regression output


    c1 = graph.get_tensor_by_name("conv1/norm1:0")  # output of conv1 layer of size (1, height/2, width/2, 24)

    c2 = graph.get_tensor_by_name("conv2/norm2:0")

    c3 = graph.get_tensor_by_name("conv3/norm3:0")

    c4 = graph.get_tensor_by_name("conv4/Relu:0")

    c5 = graph.get_tensor_by_name("conv5/Relu:0")


    # # EVALUATION

    overal_cnt = 0

    for step in range(0, num_images):


        if (overal_cnt > -1):


            # # Load test data

            img = io.imread(parse_file_name_read(image_path, overal_cnt))

            X_ = make_cnn_input(img) # crop and normalize input image of size [1, height, width, channel]


            # # Do regression

            regression, Conv = sess.run([r, c1], feed_dict={x: X_})


        overal_cnt = overal_cnt + 1


sess.close()