Tf-pose-estimation: How can I export the weights to a npy file?

Created on 11 Apr 2018  ·  2Comments  ·  Source: ildoonet/tf-pose-estimation

Hi @ildoonet
I want to retrain the model, and save the model into a npy file as you do.
So how to save the network model to a npy file, and It can be load by BaseNetwork class.

Thanks for your help.

Most helpful comment

Hello @segatecm
I am sorry you had to wait so long, but maybe someone else will need it. To save trained weights in the npy format, modify the run_checkpoint.py script slightly:

import argparse
import logging

import tensorflow as tf
import numpy as np

from networks import get_network

logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')

config = tf.ConfigProto()
config.gpu_options.allocator_type = 'BFC'
config.gpu_options.per_process_gpu_memory_fraction = 0.95
config.gpu_options.allow_growth = True


if __name__ == '__main__':
    """
    Use this script to just save weights of model
    """
    parser = argparse.ArgumentParser(description='Tensorflow Pose Estimation Graph Extractor')
    parser.add_argument('--model', type=str, default='cmu', help='cmu / mobilenet / mobilenet_thin')
    args = parser.parse_args()

    input_node = tf.placeholder(tf.float32, shape=(None, None, None, 3), name='image')
    model = {}
    with tf.Session(config=config) as sess:
        net, _, last_layer = get_network(args.model, input_node, sess, trainable=False)
        sess.run(tf.global_variables_initializer())
        variables = tf.get_collection('variables')
        for var in variables:
            name = var.name
            name = name.split(':')[0]
            layer, parameter = name.split('/')
            if layer not in model.keys():
                model[layer] = dict()
            model[layer][parameter] = var.eval()

    np.save('{}.npy'.format(args.model), model)

All 2 comments

Hello @segatecm
I am sorry you had to wait so long, but maybe someone else will need it. To save trained weights in the npy format, modify the run_checkpoint.py script slightly:

import argparse
import logging

import tensorflow as tf
import numpy as np

from networks import get_network

logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')

config = tf.ConfigProto()
config.gpu_options.allocator_type = 'BFC'
config.gpu_options.per_process_gpu_memory_fraction = 0.95
config.gpu_options.allow_growth = True


if __name__ == '__main__':
    """
    Use this script to just save weights of model
    """
    parser = argparse.ArgumentParser(description='Tensorflow Pose Estimation Graph Extractor')
    parser.add_argument('--model', type=str, default='cmu', help='cmu / mobilenet / mobilenet_thin')
    args = parser.parse_args()

    input_node = tf.placeholder(tf.float32, shape=(None, None, None, 3), name='image')
    model = {}
    with tf.Session(config=config) as sess:
        net, _, last_layer = get_network(args.model, input_node, sess, trainable=False)
        sess.run(tf.global_variables_initializer())
        variables = tf.get_collection('variables')
        for var in variables:
            name = var.name
            name = name.split(':')[0]
            layer, parameter = name.split('/')
            if layer not in model.keys():
                model[layer] = dict()
            model[layer][parameter] = var.eval()

    np.save('{}.npy'.format(args.model), model)

thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

khaerulumam42 picture khaerulumam42  ·  6Comments

16534165 picture 16534165  ·  3Comments

jpizarrom picture jpizarrom  ·  6Comments

zerolim820 picture zerolim820  ·  3Comments

khan958 picture khan958  ·  3Comments