TensorFlow推论 [英] TensorFlow Inference

查看:67
本文介绍了TensorFlow推论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了一段时间了。我发现了很多文章;但没有人真正将张量流推论显示为纯推论。它总是使用服务引擎或使用预先编码/定义的图形。



这里是问题所在:我有一台设备偶尔会检查更新的模型。然后,它需要加载该模型并通过该模型运行输入预测。



在keras中,这很简单:建立一个模型;训练模型并调用model.predict()。



我能够抓取一个新模型并加载它。我可以打印出所有的砝码;但是我到底该如何推断呢?



加载模型和打印权重的代码:

 ,其中tf.Session()为sess:
new_saver = tf.train.import_meta_graph(MODEL_PATH +'.meta',clear_devices = True)
new_saver.restore(sess ,MODEL_PATH)
for tf.trainable_variables():
print(sess.run(var))

我打印了所有收藏集,并且拥有:
['queue_runners','variables','loss','summary','train_op','cond_context','trainable_variables ']



我尝试使用 sess.run(train_op);但是,这刚刚开始了一次完整的培训课程;这不是我想做的。我只是想对我提供的一组不同的输入进行推断,这些输入不是TF记录。



只是一点点细节:



设备可以使用C ++或Python;只要我可以产生一个.exe。如果我想给系统喂食,我可以设置一个喂食字典。我使用 TFRecords 进行了培训;但在生产环境中,我不会使用 TFRecords ;它是一个实时/近实时系统。



感谢您的输入。我正在此仓库中发布示例代码:



要杀死的部分:





所以我认为问题就来了:如何杀死图形的输入部分并进行替换它与 feed_dict



后续操作是:这真的是正确的方法吗? ?



---- END UPDATE 3 ----



---链接到检查点文件---



https://drcdata.blob.core.windows.net/checkpoints/CIFAR_10_VGG3_50neuron_1pool_1e-3lr_adam.model.zip?st=2017-05-01T21%3A56%3A00Z&se=2020-05-02T21%3A56% 3A00Z& sp = rl& sv = 2015-12-11& sr = b& sig = oBCGxlOusB4NOEKnSnD%2FTlRYa5NKNIwAX1IyuZXAr9o%3D



-链接至检查点文件---



-----更新4 -----



假设我可以让科学家只是对他们的模型进行腌制,而我们可以抓住模型腌制,则只是对正常的推理方式进行了介绍。解压缩它,然后对其进行推断。因此,为了进行测试,我假设我们已经打开包装,尝试了正常的方式...它也不值钱……

 以tf 
导入常量
的方式导入tensorflow Vgg3CIFAR10以NP
的方式从scipy导入misc
导入时间

MODEL_PATH = '模型/'+ CONSTANTS.MODEL_NAME +'.model'
imgs_bsdir ='C:/ data / cifar_10 / train /'

图片= tf.placeholder(tf.float32,shape = (1、32、32、3))

logits = Vgg3CIFAR10.inference(images)

def run_inference():
'''对a进行推断加载的模型'''
,其中tf.Session()为sess:
sess.run(tf.global_variables_initializer())
new_saver = tf.train.import_meta_graph(MODEL_PATH +'.meta' )#,import_scope ='1',input_map = {'input:0':images})
new_saver.restore(sess,MODEL_PATH)
pred = tf.get_default_graph()。get_operation_by_name('prediction' )
enq = sess.graph.get_operation_by_name(enqueue_op)
#tf.train.start_queue_runners(sess)
打印(rand)
打印(pred)
打印(enq)
for i in range(1,25):
img = misc.imread(imgs_bsdir + str(i)+'.png')。astype(np.float32)/ 255.0
img = img.reshape(1、32、32、3)
print(sess.run(logits,feed_dict = {images:img}))
time.sleep(3)
print('done')

run_inference()

Tensorflow最终使用来自加载模型的推断函数构建了一个新图;然后将其他图表中的所有其他内容附加到其末尾。因此,当我填充 feed_dict 期望返回推理时;我只是得到了一堆随机垃圾,好像它是第一次通过网络一样。



再次;这似乎很疯狂;我真的需要编写自己的框架来对随机网络进行序列化和反序列化吗?这必须在...



----- UPDATE 4 -----



之前完成。

再次;谢谢!

解决方案

好吧,这花了太多时间才弄清楚。



快速提醒:我需要坚持一个可以动态加载和推断的模型



第1步:将模型创建为Class,理想情况下使用界面定义

  class Vgg3Model:

NUM_DENSE_NEURONS = 50
DENSE_RESHAPE = 32 *(常量.IMAGE_SHAPE [0] // 2)*(CONSTANTS.IMAGE_SHAPE [1] // 2)

def推断(自己,图像):
'''
部分接受输入并将其转换为Y输出的计算图
'''
以tf.variable_scope('Conv1')作为范围:
C_1_1 = ld.cnn_layer(images,( 5,5,3,32),(1,1,1,1),范围,name_postfix ='1')
C_1_2 = ld.cnn_layer(C_1_1,(5,5,32,32),( 1,1,1,1),作用域,name_postfix ='2')
P_1 = ld.pool_laye r(C_1_2,(1、2、2、1),(1、2、2、1),范围)
,其中tf.variable_scope('Dense1')作为范围:
P_1 = tf。 reshape(P_1,(-1,self.DENSE_RESHAPE))
dim = P_1.get_shape()[1] .value
D_1 = ld.mlp_layer(P_1,dim,self.NUM_DENSE_NEURONS,scope,act_func = tf.nn.relu)
,其中tf.variable_scope('Dense2')作为范围:
D_2 = ld.mlp_layer(D_1,self.NUM_DENSE_NEURONS,CONSTANTS.NUM_CLASSES,范围)
H = tf.nn.softmax(D_2,name ='prediction')
return H

def损失(自身,对数,标签):
'''
将损失加到所有变量
'''
cross_entr = tf.nn.sparse_softmax_cross_entropy_with_logits(logits = logits,标签=标签)
cross_entr = tf.reduce_mean(cross_entr)
tf。 summary.scalar('cost',cross_entr)
tf.add_to_collection('losses',cross_entr)
return tf.add_n(tf.get_collection('losses'),name ='total_loss')

第2步:使用所需的任何输入来训练网络;就我而言,我使用了队列运行器和 TF记录。请注意,此步骤由另一个团队进行,该团队迭代,构建,设计和优化模型。这也可能随着时间而改变。它们产生的输出必须能够从远程位置拉出,以便我们可以在设备上动态加载更新的模型(刷新硬件是一件很麻烦的事情,尤其是在地理位置分散的情况下)。在这种情况下;小组删除了与图形保护程序关联的3个文件;也是用于该培训课程的模型的酱菜

  model = vgg3.Vgg3Model()

def create_sess_ops():
'''
创建并返回运行
a tensorflow培训课程所需的操作
'''
GRAPH = tf.Graph()$带GRAPH.as_default()的b $ b:
示例,标签= Inputs.read_inputs(CONSTANTS.RecordPaths,
batch_size = CONSTANTS.BATCH_SIZE,
img_shape = CONSTANTS.IMAGE_SHAPE,
num_threads = CONSTANTS.INPUT_PIPELINE_THREADS)
的示例= tf.reshape(示例,[-1,CONSTANTS.IMAGE_SHAPE [0],
CONSTANTS.IMAGE_SHAPE [1],CONSTANTS.IMAGE_SHAPE [2]],名称= '推断/输入')
logits = model.inference(示例)
损失= model.loss(logits,标签)
OPTIMIZER = tf.train.AdamOptimizer(CONST ANTS.LEARNING_RATE)
梯度= OPTIMIZER.compute_gradients(损失)
apply_gradient_op = OPTIMIZER.apply_gradients(梯度)
gradients_summary(梯度)
summaries_op = tf.summary.merge_all()
return [apply_gradient_op,summaries_op,loss,logits],GRAPH

def main():
'''
跑步和训练CIFAR 10
'' '
print('starting ...')
ops,GRAPH = create_sess_ops()
total_duration = 0.0
with tf.Session(graph = GRAPH)作为会话:
COORDINATOR = tf.train.Coordinator()
螺纹= tf.train.start_queue_runners(SESSION,COORDINATOR)
SESSION.run(tf.global_variables_initializer())
summary_WRITER = tf.summary .FileWriter('Tensorboard /'+ CONSTANTS.MODEL_NAME,graph = GRAPH)
GRAPH_SAVER = tf.train.Saver()

范围内的EPOCH(CONSTANTS.EPOCHS):
持续时间= 0
错误= 0.0
start_time = time.time()
用于范围内的批处理(CONSTANTS.MINI_BATCHES):
_,摘要,cost_val,预测= SESSION.run(ops)
错误+ = cost_val
持续时间+ = time.time()-开始时间
total_duration + =持续时间
summary_WRITER.add_summary(summary,EPOCH)
print('Epoch%d:损失=%.2f(%。 3f sec)'%(EPOCH,错误,持续时间))
如果EPOCH == CONSTANTS.EPOCHS-1或错误< 0.005:
print(
'完成%d个时期的训练。(%。3f秒)'%(EPOCH,total_duration)

中断
GRAPH_SAVER.save (SESSION,'models /'+ CONSTANTS.MODEL_NAME +'.model')
以open('models /'+ CONSTANTS.MODEL_NAME +'.pkl,'wb')作为输出:
泡菜.dump(模型,输出)
COORDINATOR.request_stop()
COORDINATOR.join(螺纹)

第3步:运行一些推断。加载您的腌制模型;通过将新占位符管道输送到logit来创建新图;然后调用会话还原。不要还原整个图形;只是变量。

  MODEL_PATH ='models /'+ CONSTANTS.MODEL_NAME +'.model'
imgs_bsdir =' C:/ data / cifar_10 / train /'

图片= tf.placeholder(tf.float32,shape =(1、32、32、3))
with open('models / vgg3.pkl','rb')as model_in:
model = pickle.load(model_in)
logits = model.inference(images)

def run_inference():
'''对加载的模型进行推断''
,其中tf.Session()为sess:
sess.run(tf.global_variables_initializer())
new_saver = tf。 train.Saver()
new_saver.restore(sess,MODEL_PATH)
print( Starting ...)
for i in range(20,30):
print( str(i)+'.png')
img = misc.imread(imgs_bsdir + str(i)+'.png')。astype(np.float32)/ 255.0
img = img.reshape (1、32、32、3)
pred = sess.run(logits,feed_dict = {images:img})
max_node = np.argmax(pred)
print('p要求标签:'+ str(max_node))
print('done')

run_inference()

肯定有方法可以使用界面进行改进,甚至可以更好地打包所有内容;



最终提示,当我们最终将其投入生产时,我们结束了这一步。必须将愚蠢的`mymodel_model.py文件与所有内容一起发送以构建图形。因此,我们现在为所有模型强制执行命名约定,并且还为生产模型运行提供了编码标准,因此我们可以正确地执行此操作。



祝你好运!


I've been digging around on this for a while. I have found a ton of articles; but none really show just tensorflow inference as a plain inference. Its always "use the serving engine" or using a graph that is pre-coded/defined.

Here is the problem: I have a device which occasionally checks for updated models. It then needs to load that model and run input predictions through the model.

In keras this was simple: build a model; train the model and the call model.predict(). In scikit-learn same thing.

I am able to grab a new model and load it; I can print out all of the weights; but how in the world do I run inference against it?

Code to load model and print weights:

    with tf.Session() as sess:
        new_saver = tf.train.import_meta_graph(MODEL_PATH + '.meta', clear_devices=True)
        new_saver.restore(sess, MODEL_PATH)
        for var in tf.trainable_variables():
            print(sess.run(var))

I printed out all of my collections and I have: ['queue_runners', 'variables', 'losses', 'summaries', 'train_op', 'cond_context', 'trainable_variables']

I tried using sess.run(train_op); however that just started kicking up a full training session; which is not what I want to do. I just want to run inference against a different set of inputs that I provide which are not TF Records.

Just a little more detail:

The device can use C++ or Python; as long as I can produce a .exe. I can set up a feed dict if I want to feed the system. I trained with TFRecords; but in production I'm not going to use TFRecords; its a real/near real time system.

Thanks for any input. I am posting sample code to this repo: https://github.com/drcrook1/CIFAR10/TensorFlow which does all the training and sample inference.

Any hints are greatly appreciated!

------------EDITS----------------- I rebuilt the model to be as below:

def inference(images):
    '''
    Portion of the compute graph that takes an input and converts it into a Y output
    '''
    with tf.variable_scope('Conv1') as scope:
        C_1_1 = ld.cnn_layer(images, (5, 5, 3, 32), (1, 1, 1, 1), scope, name_postfix='1')
        C_1_2 = ld.cnn_layer(C_1_1, (5, 5, 32, 32), (1, 1, 1, 1), scope, name_postfix='2')
        P_1 = ld.pool_layer(C_1_2, (1, 2, 2, 1), (1, 2, 2, 1), scope)
    with tf.variable_scope('Dense1') as scope:
        P_1 = tf.reshape(C_1_2, (CONSTANTS.BATCH_SIZE, -1))
        dim = P_1.get_shape()[1].value
        D_1 = ld.mlp_layer(P_1, dim, NUM_DENSE_NEURONS, scope, act_func=tf.nn.relu)
    with tf.variable_scope('Dense2') as scope:
        D_2 = ld.mlp_layer(D_1, NUM_DENSE_NEURONS, CONSTANTS.NUM_CLASSES, scope)
    H = tf.nn.softmax(D_2, name='prediction')
    return H

notice I add the name 'prediction' to the TF operation so I can retrieve it later.

When training I used the input pipeline for tfrecords and input queues.

GRAPH = tf.Graph()
with GRAPH.as_default():
    examples, labels = Inputs.read_inputs(CONSTANTS.RecordPaths,
                                          batch_size=CONSTANTS.BATCH_SIZE,
                                          img_shape=CONSTANTS.IMAGE_SHAPE,
                                          num_threads=CONSTANTS.INPUT_PIPELINE_THREADS)
    examples = tf.reshape(examples, [CONSTANTS.BATCH_SIZE, CONSTANTS.IMAGE_SHAPE[0],
                                     CONSTANTS.IMAGE_SHAPE[1], CONSTANTS.IMAGE_SHAPE[2]])
    logits = Vgg3CIFAR10.inference(examples)
    loss = Vgg3CIFAR10.loss(logits, labels)
    OPTIMIZER = tf.train.AdamOptimizer(CONSTANTS.LEARNING_RATE)

I am attempting to use feed_dict on the loaded operation in the graph; however now it is just simply hanging....

MODEL_PATH = 'models/' + CONSTANTS.MODEL_NAME + '.model'

images = tf.placeholder(tf.float32, shape=(1, 32, 32, 3))

def run_inference():
    '''Runs inference against a loaded model'''
    with tf.Session() as sess:
        #sess.run(tf.global_variables_initializer())
        new_saver = tf.train.import_meta_graph(MODEL_PATH + '.meta', clear_devices=True)
        new_saver.restore(sess, MODEL_PATH)
        pred = tf.get_default_graph().get_operation_by_name('prediction')
        rand = np.random.rand(1, 32, 32, 3)
        print(rand)
        print(pred)
        print(sess.run(pred, feed_dict={images: rand}))
        print('done')

run_inference()

I believe this is not working because the original network was trained using TFRecords. In the sample CIFAR data set the data is small; our real data set is huge and it is my understanding TFRecords the the default best practice for training a network. The feed_dict makes great perfect sense from a productionizing perspective; we can spin up some threads and populate that thing from our input systems.

So I guess I have a network that is trained, I can get the predict operation; but how do I tell it to stop using the input queues and start using the feed_dict? Remember that from the production perspective I do not have access to whatever the scientists did to make it. They do their thing; and we stick it in production using whatever agreed upon standard.

-------INPUT OPS--------

tf.Operation 'input/input_producer/Const' type=Const, tf.Operation 'input/input_producer/Size' type=Const, tf.Operation 'input/input_producer/Greater/y' type=Const, tf.Operation 'input/input_producer/Greater' type=Greater, tf.Operation 'input/input_producer/Assert/Const' type=Const, tf.Operation 'input/input_producer/Assert/Assert/data_0' type=Const, tf.Operation 'input/input_producer/Assert/Assert' type=Assert, tf.Operation 'input/input_producer/Identity' type=Identity, tf.Operation 'input/input_producer/RandomShuffle' type=RandomShuffle, tf.Operation 'input/input_producer' type=FIFOQueueV2, tf.Operation 'input/input_producer/input_producer_EnqueueMany' type=QueueEnqueueManyV2, tf.Operation 'input/input_producer/input_producer_Close' type=QueueCloseV2, tf.Operation 'input/input_producer/input_producer_Close_1' type=QueueCloseV2, tf.Operation 'input/input_producer/input_producer_Size' type=QueueSizeV2, tf.Operation 'input/input_producer/Cast' type=Cast, tf.Operation 'input/input_producer/mul/y' type=Const, tf.Operation 'input/input_producer/mul' type=Mul, tf.Operation 'input/input_producer/fraction_of_32_full/tags' type=Const, tf.Operation 'input/input_producer/fraction_of_32_full' type=ScalarSummary, tf.Operation 'input/TFRecordReaderV2' type=TFRecordReaderV2, tf.Operation 'input/ReaderReadV2' type=ReaderReadV2,

------END INPUT OPS-----

----UPDATE 3----

I believe what I need to do is to kill the input section of the graph trained with TF Records and rewire the input to the first layer to a new input. Its kinda like performing surgery; but this is the only way I can find to do inference if I trained using TFRecords as crazy as it sounds...

Full Graph:

Section to kill:

So I think the question becomes: How does one kill the input section of the graph and replace it with a feed_dict?

A follow up to this would be: is this really the right way to do it? This seems bonkers.

----END UPDATE 3----

---link to checkpoint files---

https://drcdata.blob.core.windows.net/checkpoints/CIFAR_10_VGG3_50neuron_1pool_1e-3lr_adam.model.zip?st=2017-05-01T21%3A56%3A00Z&se=2020-05-02T21%3A56%3A00Z&sp=rl&sv=2015-12-11&sr=b&sig=oBCGxlOusB4NOEKnSnD%2FTlRYa5NKNIwAX1IyuZXAr9o%3D

--end link to checkpoint files---

-----UPDATE 4 -----

I gave in and just gave a shot at the 'normal' way of performing inference assuming I could have the scientists simply just pickle their models and we could grab the model pickle; unpack it and then run inference on it. So to test I tried the normal way assuming we already unpacked it...It doesn't work worth a beans either...

import tensorflow as tf
import CONSTANTS
import Vgg3CIFAR10
import numpy as np
from scipy import misc
import time

MODEL_PATH = 'models/' + CONSTANTS.MODEL_NAME + '.model'
imgs_bsdir = 'C:/data/cifar_10/train/'

images = tf.placeholder(tf.float32, shape=(1, 32, 32, 3))

logits = Vgg3CIFAR10.inference(images)

def run_inference():
'''Runs inference against a loaded model'''
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        new_saver = tf.train.import_meta_graph(MODEL_PATH + '.meta')#, import_scope='1', input_map={'input:0': images})
        new_saver.restore(sess, MODEL_PATH)
        pred = tf.get_default_graph().get_operation_by_name('prediction')
        enq = sess.graph.get_operation_by_name(enqueue_op)
        #tf.train.start_queue_runners(sess)
        print(rand)
        print(pred)
        print(enq)
        for i in range(1, 25):
            img = misc.imread(imgs_bsdir + str(i) + '.png').astype(np.float32) / 255.0
            img = img.reshape(1, 32, 32, 3)
            print(sess.run(logits, feed_dict={images : img}))
            time.sleep(3)
        print('done')

run_inference()

Tensorflow ends up building a new graph with the inference function from the loaded model; then it appends all the other stuff from the other graph to the end of it. So then when I populate a feed_dict expecting to get inferences back; I just get a bunch of random garbage as if it were the first pass through the network...

Again; this seems nuts; do I really need to write my own framework for serializing and deserializing random networks? This has had to have been done before...

-----UPDATE 4 -----

Again; thanks!

解决方案

Alright, this took way too much time to figure out; so here is the answer for the rest of the world.

Quick Reminder: I needed to persist a model that can be dynamically loaded and inferred against without knowledge as to the under pinnings or insides of how it works.

Step 1: Create a model as a Class and ideally use an interface definition

class Vgg3Model:

    NUM_DENSE_NEURONS = 50
    DENSE_RESHAPE = 32 * (CONSTANTS.IMAGE_SHAPE[0] // 2) * (CONSTANTS.IMAGE_SHAPE[1] // 2)

    def inference(self, images):
        '''
        Portion of the compute graph that takes an input and converts it into a Y output
        '''
        with tf.variable_scope('Conv1') as scope:
            C_1_1 = ld.cnn_layer(images, (5, 5, 3, 32), (1, 1, 1, 1), scope, name_postfix='1')
            C_1_2 = ld.cnn_layer(C_1_1, (5, 5, 32, 32), (1, 1, 1, 1), scope, name_postfix='2')
            P_1 = ld.pool_layer(C_1_2, (1, 2, 2, 1), (1, 2, 2, 1), scope)
        with tf.variable_scope('Dense1') as scope:
            P_1 = tf.reshape(P_1, (-1, self.DENSE_RESHAPE))
            dim = P_1.get_shape()[1].value
            D_1 = ld.mlp_layer(P_1, dim, self.NUM_DENSE_NEURONS, scope, act_func=tf.nn.relu)
        with tf.variable_scope('Dense2') as scope:
            D_2 = ld.mlp_layer(D_1, self.NUM_DENSE_NEURONS, CONSTANTS.NUM_CLASSES, scope)
        H = tf.nn.softmax(D_2, name='prediction')
        return H

    def loss(self, logits, labels):
        '''
        Adds Loss to all variables
        '''
        cross_entr = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels)
        cross_entr = tf.reduce_mean(cross_entr)
        tf.summary.scalar('cost', cross_entr)
        tf.add_to_collection('losses', cross_entr)
        return tf.add_n(tf.get_collection('losses'), name='total_loss')

Step 2: Train your network with whatever inputs you want; in my case I used Queue Runners and TF Records. Note that this step is done by a different team which iterates, builds, designs and optimizes models. This can also change over time. The output they produce must be able to be pulled from a remote location so we can dynamically load the updated models on devices (reflashing hardware is a pain especially if it is geographically distributed). In this instance; the team drops the 3 files associated with a graph saver; but also a pickle of the model used for that training session

model = vgg3.Vgg3Model()

def create_sess_ops():
    '''
    Creates and returns operations needed for running
    a tensorflow training session
    '''
    GRAPH = tf.Graph()
    with GRAPH.as_default():
        examples, labels = Inputs.read_inputs(CONSTANTS.RecordPaths,
                                          batch_size=CONSTANTS.BATCH_SIZE,
                                          img_shape=CONSTANTS.IMAGE_SHAPE,
                                          num_threads=CONSTANTS.INPUT_PIPELINE_THREADS)
        examples = tf.reshape(examples, [-1, CONSTANTS.IMAGE_SHAPE[0],
                                     CONSTANTS.IMAGE_SHAPE[1], CONSTANTS.IMAGE_SHAPE[2]], name='infer/input')
        logits = model.inference(examples)
        loss = model.loss(logits, labels)
        OPTIMIZER = tf.train.AdamOptimizer(CONSTANTS.LEARNING_RATE)
        gradients = OPTIMIZER.compute_gradients(loss)
        apply_gradient_op = OPTIMIZER.apply_gradients(gradients)
        gradients_summary(gradients)
        summaries_op = tf.summary.merge_all()
        return [apply_gradient_op, summaries_op, loss, logits], GRAPH

def main():
    '''
    Run and Train CIFAR 10
    '''
    print('starting...')
    ops, GRAPH = create_sess_ops()
    total_duration = 0.0
    with tf.Session(graph=GRAPH) as SESSION:
        COORDINATOR = tf.train.Coordinator()
        THREADS = tf.train.start_queue_runners(SESSION, COORDINATOR)
        SESSION.run(tf.global_variables_initializer())
        SUMMARY_WRITER = tf.summary.FileWriter('Tensorboard/' + CONSTANTS.MODEL_NAME, graph=GRAPH)
        GRAPH_SAVER = tf.train.Saver()

        for EPOCH in range(CONSTANTS.EPOCHS):
            duration = 0
            error = 0.0
            start_time = time.time()
            for batch in range(CONSTANTS.MINI_BATCHES):
                _, summaries, cost_val, prediction = SESSION.run(ops)
                error += cost_val
            duration += time.time() - start_time
            total_duration += duration
            SUMMARY_WRITER.add_summary(summaries, EPOCH)
            print('Epoch %d: loss = %.2f (%.3f sec)' % (EPOCH, error, duration))
            if EPOCH == CONSTANTS.EPOCHS - 1 or error < 0.005:
                print(
                'Done training for %d epochs. (%.3f sec)' % (EPOCH, total_duration)
            )
                break
        GRAPH_SAVER.save(SESSION, 'models/' + CONSTANTS.MODEL_NAME + '.model')
        with open('models/' + CONSTANTS.MODEL_NAME + '.pkl', 'wb') as output:
            pickle.dump(model, output)
        COORDINATOR.request_stop()
        COORDINATOR.join(THREADS)

Step 3: Run some Inference. Load your pickled model; create a new graph by piping in the new placeholder to the logits; and then call session restore. DO NOT RESTORE THE WHOLE GRAPH; JUST THE VARIABLES.

MODEL_PATH = 'models/' + CONSTANTS.MODEL_NAME + '.model'
imgs_bsdir = 'C:/data/cifar_10/train/'

images = tf.placeholder(tf.float32, shape=(1, 32, 32, 3))
with open('models/vgg3.pkl', 'rb') as model_in:
model = pickle.load(model_in)
logits = model.inference(images)

def run_inference():
    '''Runs inference against a loaded model'''
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        new_saver = tf.train.Saver()
        new_saver.restore(sess, MODEL_PATH)
        print("Starting...")
        for i in range(20, 30):
            print(str(i) + '.png')
            img = misc.imread(imgs_bsdir + str(i) + '.png').astype(np.float32) / 255.0
            img = img.reshape(1, 32, 32, 3)
            pred = sess.run(logits, feed_dict={images : img})
            max_node = np.argmax(pred)
            print('predicted label: ' + str(max_node))
        print('done')

run_inference()

There definitely ways to improve on this using interfaces and maybe packaging up everything better; but this is working and sets the stage for how we will be moving forward.

FINAL NOTE When we finally pushed this to production, we ended up having to ship the stupid `mymodel_model.py file down with everything to build up the graph. So we now enforce a naming convention for all models and there is also a coding standard for production model runs so we can do this properly.

Good Luck!

这篇关于TensorFlow推论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆