尝试从.export_savedmmodel()获取预测时遇到错误

时间:2018-02-16 09:05:46

标签: python tensorflow machine-learning

样本数据

X_test.head()


Alarm_First_Notify_Delay

Duration_Difference

Customer_Same

Site_Same

SameDevice



100746
701 631 1 0 0 

87012
0 35 1 1 1 

167585
0 0 1 1 1 

178924
0 35 1 1 1 

76793
0 0 1 1 1 
DNNCLassifier的

列功能

  feature_spec2 = {
                   'Alarm_First_Notify_Delay': tf.FixedLenFeature([X_test.Alarm_First_Notify_Delay.count()],tf.float32),
                   'Customer_Same':tf.FixedLenFeature([X_test.Customer_Same.count()],tf.float32),
                   'Duration_Difference':tf.FixedLenFeature([X_test.Duration_Difference.count()],tf.float32),
                   'SameDevice':tf.FixedLenFeature([X_test.SameDevice.count()],tf.float32),
                   'Site_Same':tf.FixedLenFeature([X_test.Site_Same.count()],tf.float32)

               }



value of feature_spec2 is as follows :-

{'Alarm_First_Notify_Delay': FixedLenFeature(shape=[66849], dtype=tf.float32, default_value=None),
 'Customer_Same': FixedLenFeature(shape=[66849], dtype=tf.float32, default_value=None),
 'Duration_Difference': FixedLenFeature(shape=[66849], dtype=tf.float32, default_value=None),
 'SameDevice': FixedLenFeature(shape=[66849], dtype=tf.float32, default_value=None),
 'Site_Same': FixedLenFeature(shape=[66849], dtype=tf.float32, default_value=None)}

恢复使用.export_savedmodel()

保存的DNNCLASSIFIER模型
import tensorflow as tf
exported_path = 'D:\\dnnclassifier\\1518769534'

res_pred =[]
import time
from datetime import datetime
start = time.time()
start_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start))
print('start time : ',start_time)

with tf.Session() as sess:
     #load the saved model
     tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], exported_path)
     #Prepare model input, the model expects a float array to be passed to x
     # check line 28 serving_input_receiver_fn


     model_input= tf.train.Example(features= feature_spec2) 

       #get the predictor , refer tf.contrib.predicdtor
     predictor= tf.contrib.predictor.from_saved_model(exported_path)

       #get the input_tensor tensor from the model graph
       # name is input_tensor defined in input_receiver function refer to tf.dnn.classifier
     input_tensor=tf.get_default_graph().get_tensor_by_name("input_tensors:0")
       #get the output dict
       # do not forget [] around model_input or else it will complain shape() for Tensor shape(?,)
       # since its of shape(?,) when we trained it
     model_input=model_input.SerializeToString()
     print(model)
     output_dict= predictor({"inputs":[model_input]})
        #print(" prediction is " , output_dict['scores'])



     end = time.time()
     end_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end))
     print('end time : ',end_time)

error encountered:-

start time :  2018-02-16 14:08:05
INFO:tensorflow:Restoring parameters from b'D:\\dnnclassifier\\1518769534\\variables\\variables'
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
C:\ProgramData\Anaconda3\envs\tfdeeplearning\lib\site-packages\google\protobuf\internal\python_message.py in _GetFieldByName(message_descriptor, field_name)
    544   try:
--> 545     return message_descriptor.fields_by_name[field_name]
    546   except KeyError:

KeyError: 'SameDevice'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-136-02d93ae3ed5d> in <module>()
     15 
     16 
---> 17      model_input= tf.train.Example(features= feature_spec2)
     18 
     19        #get the predictor , refer tf.contrib.predicdtor

C:\ProgramData\Anaconda3\envs\tfdeeplearning\lib\site-packages\google\protobuf\internal\python_message.py in init(self, **kwargs)
    514         new_val = field_value
    515         if isinstance(field_value, dict):
--> 516           new_val = field.message_type._concrete_class(**field_value)
    517         try:
    518           copy.MergeFrom(new_val)

C:\ProgramData\Anaconda3\envs\tfdeeplearning\lib\site-packages\google\protobuf\internal\python_message.py in init(self, **kwargs)
    482     self._listener_for_children = _Listener(self)
    483     for field_name, field_value in kwargs.items():
--> 484       field = _GetFieldByName(message_descriptor, field_name)
    485       if field is None:
    486         raise TypeError("%s() got an unexpected keyword argument '%s'" %

C:\ProgramData\Anaconda3\envs\tfdeeplearning\lib\site-packages\google\protobuf\internal\python_message.py in _GetFieldByName(message_descriptor, field_name)
    546   except KeyError:
    547     raise ValueError('Protocol message %s has no "%s" field.' %
--> 548                      (message_descriptor.name, field_name))
    549 
    550 

ValueError: Protocol message Features has no "SameDevice" field.




encounter the below error while trying to get predictions from a stored DNNCLASSIFIER model
Plaban · 15 minutes ago
feature_spec2 = {
               'Alarm_First_Notify_Delay': tf.FixedLenFeature([X_test.Alarm_First_Notify_Delay.count()],tf.float32),
               'Customer_Same':tf.FixedLenFeature([X_test.Customer_Same.count()],tf.float32),
               'Duration_Difference':tf.FixedLenFeature([X_test.Duration_Difference.count()],tf.float32),
               'SameDevice':tf.FixedLenFeature([X_test.SameDevice.count()],tf.float32),
               'Site_Same':tf.FixedLenFeature([X_test.Site_Same.count()],tf.float32)

               }



value of feature_spec2 is as follows :-

{'Alarm_First_Notify_Delay': FixedLenFeature(shape=[66849], dtype=tf.float32, default_value=None),
 'Customer_Same': FixedLenFeature(shape=[66849], dtype=tf.float32, default_value=None),
 'Duration_Difference': FixedLenFeature(shape=[66849], dtype=tf.float32, default_value=None),
 'SameDevice': FixedLenFeature(shape=[66849], dtype=tf.float32, default_value=None),
 'Site_Same': FixedLenFeature(shape=[66849], dtype=tf.float32, default_value=None)}
Code to get predictions 

import tensorflow as tf
exported_path = 'D:\\dnnclassifier\\1518769534'
res_pred =[]
import time
from datetime import datetime
start = time.time()
start_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start))
print('start time : ',start_time)

with tf.Session() as sess:
     #load the saved model
     tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], exported_path)
     #Prepare model input, the model expects a float array to be passed to x
     # check line 28 serving_input_receiver_fn


     model_input= tf.train.Example(features= feature_spec2) 

       #get the predictor , refer tf.contrib.predicdtor
     predictor= tf.contrib.predictor.from_saved_model(exported_path)

       #get the input_tensor tensor from the model graph
       # name is input_tensor defined in input_receiver function refer to tf.dnn.classifier
     input_tensor=tf.get_default_graph().get_tensor_by_name("input_tensors:0")
       #get the output dict
       # do not forget [] around model_input or else it will complain shape() for Tensor shape(?,)
       # since its of shape(?,) when we trained it
     model_input=model_input.SerializeToString()
     print(model)
     output_dict= predictor({"inputs":[model_input]})
        #print(" prediction is " , output_dict['scores'])
     res_pred.append([items[0],items[1],items[2],items[3],items[4],output_dict['classes'],output_dict['scores']])


     end = time.time()
     end_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end))
     print('end time : ',end_time)

error encountered:-

start time :  2018-02-16 14:08:05
INFO:tensorflow:Restoring parameters from b'D:\\dnnclassifier\\1518769534\\variables\\variables'
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
C:\ProgramData\Anaconda3\envs\tfdeeplearning\lib\site-packages\google\protobuf\internal\python_message.py in _GetFieldByName(message_descriptor, field_name)
    544   try:
--> 545     return message_descriptor.fields_by_name[field_name]
    546   except KeyError:

KeyError: 'SameDevice'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-136-02d93ae3ed5d> in <module>()
     15 
     16 
---> 17      model_input= tf.train.Example(features= feature_spec2)
     18 
     19        #get the predictor , refer tf.contrib.predicdtor

C:\ProgramData\Anaconda3\envs\tfdeeplearning\lib\site-packages\google\protobuf\internal\python_message.py in init(self, **kwargs)
    514         new_val = field_value
    515         if isinstance(field_value, dict):
--> 516           new_val = field.message_type._concrete_class(**field_value)
    517         try:
    518           copy.MergeFrom(new_val)

C:\ProgramData\Anaconda3\envs\tfdeeplearning\lib\site-packages\google\protobuf\internal\python_message.py in init(self, **kwargs)
    482     self._listener_for_children = _Listener(self)
    483     for field_name, field_value in kwargs.items():
--> 484       field = _GetFieldByName(message_descriptor, field_name)
    485       if field is None:
    486         raise TypeError("%s() got an unexpected keyword argument '%s'" %

C:\ProgramData\Anaconda3\envs\tfdeeplearning\lib\site-packages\google\protobuf\internal\python_message.py in _GetFieldByName(message_descriptor, field_name)
    546   except KeyError:
    547     raise ValueError('Protocol message %s has no "%s" field.' %
--> 548                      (message_descriptor.name, field_name))
    549 
    550 

ValueError: Protocol message Features has no "SameDevice" field.

Could you please advise what is wrong over here, the model was exported with same input features.

1 个答案:

答案 0 :(得分:0)

似乎您的CASE(和X_test)是协议缓冲区消息。您的代码尝试获取名为feature_spec2的字段,但此协议缓冲区消息没有这样的字段。检查定义消息的.proto文件。