2017-08-02 6 views
5

मेरे पास एक नए डेटासेट के साथ ठीक-ठीक शुरुआत मॉडल है और इसे केरास में ".h5" मॉडल के रूप में सहेजा गया है। अब मेरा लक्ष्य एंड्रॉइड टेन्सफोर्लो पर अपना मॉडल चलाने के लिए है जो केवल ".pb" एक्सटेंशन स्वीकार करता है। सवाल यह है कि क्या इस रूपांतरण को करने के लिए केरास या टेन्सफोर्लो में कोई पुस्तकालय है? मैंने इस पोस्ट को अब तक देखा है: https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html लेकिन अभी तक पता नहीं लगाया जा सकता है।कैसे tasorflow .pb करने के लिए Keras .h5 निर्यात करने के लिए?

उत्तर

9

केरास में एक टेंसरफ्लो ग्राफ को प्रोटोकॉल बफर फ़ाइल के रूप में निर्यात करने का कोई मतलब नहीं है, लेकिन आप इसे नियमित टेंसरफ्लो उपयोगिताओं का उपयोग करके कर सकते हैं। Here एक ब्लॉग पोस्ट है जो यह बताता है कि उपयोगिता स्क्रिप्ट freeze_graph.py का उपयोग करके टेंसरफ्लो में शामिल किया गया है, जो "सामान्य" तरीका है।

हालांकि, मैं व्यक्तिगत रूप से एक बाधा एक चौकी बनाने के लिए और फिर एक मॉडल प्राप्त करने के लिए एक बाहरी स्क्रिप्ट चलाने, और बदले अपने ही अजगर कोड से यह करने के लिए पसंद करते हैं होने लगता है, इसलिए मैं इस तरह एक समारोह का उपयोग करें:

def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True): 
    """ 
    Freezes the state of a session into a pruned computation graph. 

    Creates a new computation graph where variable nodes are replaced by 
    constants taking their current value in the session. The new graph will be 
    pruned so subgraphs that are not necessary to compute the requested 
    outputs are removed. 
    @param session The TensorFlow session to be frozen. 
    @param keep_var_names A list of variable names that should not be frozen, 
          or None to freeze all the variables in the graph. 
    @param output_names Names of the relevant graph outputs. 
    @param clear_devices Remove the device directives from the graph for better portability. 
    @return The frozen graph definition. 
    """ 
    from tensorflow.python.framework.graph_util import convert_variables_to_constants 
    graph = session.graph 
    with graph.as_default(): 
     freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or [])) 
     output_names = output_names or [] 
     output_names += [v.op.name for v in tf.global_variables()] 
     input_graph_def = graph.as_graph_def() 
     if clear_devices: 
      for node in input_graph_def.node: 
       node.device = "" 
     frozen_graph = convert_variables_to_constants(session, input_graph_def, 
                 output_names, freeze_var_names) 
     return frozen_graph 

जो freeze_graph.py के कार्यान्वयन में प्रेरित है। पैरामीटर भी स्क्रिप्ट के समान हैं। session टेंसरफ्लो सत्र ऑब्जेक्ट है। keep_var_names केवल तभी जरूरी है जब आप कुछ चर को जमे हुए नहीं रखना चाहते हैं (उदा। राज्य के मॉडल के लिए), इसलिए आम तौर पर नहीं। output_names उन परिचालनों के नामों की एक सूची है जो आपके इच्छित आउटपुट का उत्पादन करती हैं। clear_devices ग्राफ़ को अधिक पोर्टेबल बनाने के लिए बस किसी भी डिवाइस निर्देश को हटा देता है।

from keras import backend as K 

# Create, compile and train model... 

frozen_graph = freeze_session(K.get_session(), output_names=[model.output.op.name]) 

तो फिर तुम tf.train.write_graph साथ हमेशा की तरह एक फाइल करने के लिए ग्राफ लिख सकते हैं:: तो, एक उत्पादन के साथ एक ठेठ Keras model के लिए, आप कुछ की तरह करना होगा

tf.train.write_graph(frozen_graph, "some_directory", "my_model.pb", as_text=False) 
1

freeze_session विधि ठीक काम करता है । लेकिन एक चेकपॉइंट फ़ाइल में सहेजने की तुलना में तो टेंसरफ्लो के साथ आने वाले फ्रीज_ग्राफ टूल का उपयोग करना मेरे लिए आसान लगता है, क्योंकि इसे बनाए रखना आसान है।

पहले, अपने Keras कोड model.fit(...) के बाद जोड़ सकते हैं और अपने मॉडल को प्रशिक्षित: तुम सब करने की ज़रूरत है निम्नलिखित दो कदम है

from keras import backend as K 
import tensorflow as tf 
print(model.output.op.name) 
saver = tf.train.Saver() 
saver.save(K.get_session(), '/tmp/keras_model.ckpt') 

तो सीडी अपने TensorFlow रूट निर्देशिका में, चलाएँ:

python tensorflow/python/tools/freeze_graph.py \ 
--input_meta_graph=/tmp/keras_model.ckpt.meta \ 
--input_checkpoint=/tmp/keras_model.ckpt \ 
--output_graph=/tmp/keras_frozen.pb \ 
--output_node_names="<output_node_name_printed_in_step_1>" \ 
--input_binary=true 
संबंधित मुद्दे