Giter Site home page Giter Site logo

zhanguochang / keras_to_tensorflow Goto Github PK

View Code? Open in Web Editor NEW

This project forked from amir-abdi/keras_to_tensorflow

0.0 1.0 0.0 148 KB

General code to convert a trained keras model into an inference tensorflow model

License: MIT License

Python 50.98% Jupyter Notebook 49.02%

keras_to_tensorflow's Introduction

Keras to TensorFlow

The keras_to_tensorflow is a tool that converts a trained keras model into a ready-for-inference TensorFlow model. The tool is NOT tailored for TensorFlow 2.0 it SEEMS to be working fine.

Summary

  • In the default behaviour, this tool freezes the nodes (converts all TF variables to TF constants), and saves the inference graph and weights into a binary protobuf (.pb) file. During freezing, TensorFlow also applies node pruning which removes nodes with no contribution to the output tensor.

  • This tool supports multiple output networks and enables the user to rename the output tensors via the --output_nodes_prefix flag.

  • If the --output_meta_ckpt flag is set, the checkpoint and metagraph files for TensorFlow will also be exported which can later be used in the tf.train.Saver class to continue training.

How to use

Keras models can be saved as a single [.hdf5 or h5] file, which stores both the architecture and weights, using the model.save() function. This model can be then converted to a TensorFlow model by calling this tool as follows:

python keras_to_tensorflow.py 
    --input_model="path/to/keras/model.h5" 
    --output_model="path/to/save/model.pb"

Keras models can also be saved in two separate files where a [.hdf5 or h5] file stores the weights, using the model.save_weights() function, and another .json file stores the network architecture using the model.to_json() function. In this case, the model can be converted as follows:

python keras_to_tensorflow.py 
    --input_model="path/to/keras/model.h5" 
    --input_model_json="path/to/keras/model.json" 
    --output_model="path/to/save/model.pb"

Try

python keras_to_tensorflow.py --help

to learn about other supported flags (quantize, output_nodes_prefix, save_graph_def).

Dependencies

  • keras
  • tensorflow
  • absl
  • pathlib

Legacy code

The code on how to freeze and save keras models in previous versions of tensorflow is also available. Back then, the freeze_graph tool (/tensorflow/python/tools/freeze_graph.py) was used to convert the variables into constants. This functionality is now handled by graph_util.convert_variables_to_constants

keras_to_tensorflow's People

Contributors

amir-abdi avatar anujanegi avatar aswathkiruba avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.