关闭程序后如何保存Tensorflow会话?

时间:2018-07-31 15:50:51

标签: python tensorflow session facial-identification

这是我的代码:我想保存“年龄,性别,sess,images_pl和train_mode”以在以后的通话中使用。我尝试腌制,但似乎无法腌制TensorFlow对象。

def eval(aligned_images, model_path):

    with tf.Graph().as_default():
    sess = tf.Session()
    images_pl = tf.placeholder(tf.float32, shape=[None, 160, 160, 3], name='input_image')
    images = tf.map_fn(lambda frame: tf.reverse_v2(frame, [-1]), images_pl) #BGR TO RGB
    images_norm = tf.map_fn(lambda frame: tf.image.per_image_standardization(frame), images)
    train_mode = tf.placeholder(tf.bool)
    age_logits, gender_logits, _ = inception_resnet_v1.inference(images_norm, keep_probability=0.8,
                                                                 phase_train=train_mode,
                                                                 weight_decay=1e-5)
    gender = tf.argmax(tf.nn.softmax(gender_logits), 1)
    age_ = tf.cast(tf.constant([i for i in range(0, 101)]), tf.float32)
    age = tf.reduce_sum(tf.multiply(tf.nn.softmax(age_logits), age_), axis=1)
    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())
    sess.run(init_op)
    saver = tf.train.Saver()

    #save final model 
    saver.save(sess, model_path)
    saver.restore(sess,model_path)

    ckpt = tf.train.get_checkpoint_state(model_path)

    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)

    else:
        pass

    return age, gender, sess, images_pl, train_mode

0 个答案:

没有答案