This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tips:keras [2018/08/23 15:01] – [Export protobuf] scipio | tips:keras [2019/12/17 15:48] (current) – [tensorflow] scipio | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Keras ====== | ====== Keras ====== | ||
| + | |||
| + | ===== tensorflow ===== | ||
| + | |||
| + | * yay bazelisk | ||
| + | |||
| + | < | ||
| + | pip install -U pip six numpy wheel setuptools mock | ||
| + | pip install -U keras_applications --no-deps | ||
| + | pip install -U keras_preprocessing --no-deps | ||
| + | |||
| + | # tf 2 | ||
| + | git clone https:// | ||
| + | cd tensorflow | ||
| + | |||
| + | # tf 1.15 | ||
| + | wget https:// | ||
| + | tar zxvf v1.15.0.tar.gz | ||
| + | </ | ||
| + | |||
| + | only for tf 2 | ||
| + | < | ||
| + | wget https:// | ||
| + | patch -Np1 -i Add-grpc-fix-for-gettid.patch | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | export TF_IGNORE_MAX_BAZEL_VERSION=1 | ||
| + | export TF_NEED_CUDA=1 | ||
| + | ./configure | ||
| + | </ | ||
| + | |||
| + | for atf 1.15 | ||
| + | < | ||
| + | echo 0.26.1 > .bazelversion | ||
| + | </ | ||
| + | |||
| + | this produce .tf_configure.bazelrc | ||
| + | < | ||
| + | build --action_env PYTHON_BIN_PATH="/ | ||
| + | build --action_env PYTHON_LIB_PATH="/ | ||
| + | build --python_path="/ | ||
| + | build:xla --define with_xla_support=true | ||
| + | build --config=xla | ||
| + | build --action_env CUDA_TOOLKIT_PATH="/ | ||
| + | build --action_env TF_CUDA_COMPUTE_CAPABILITIES=" | ||
| + | build --action_env LD_LIBRARY_PATH="/ | ||
| + | build --action_env GCC_HOST_COMPILER_PATH="/ | ||
| + | build --config=cuda | ||
| + | build:opt --copt=-march=native | ||
| + | build:opt --copt=-Wno-sign-compare | ||
| + | build:opt --host_copt=-march=native | ||
| + | build:opt --define with_default_optimizations=true | ||
| + | test --flaky_test_attempts=3 | ||
| + | test --test_size_filters=small, | ||
| + | test --test_tag_filters=-benchmark-test, | ||
| + | test --build_tag_filters=-benchmark-test, | ||
| + | build --action_env TF_CONFIGURE_IOS=" | ||
| + | </ | ||
| + | |||
| + | compile | ||
| + | < | ||
| + | bazel build // | ||
| + | |||
| + | # ??? | ||
| + | bazelisk \ | ||
| + | build --config=opt \ | ||
| + | // | ||
| + | // | ||
| + | // | ||
| + | // | ||
| + | ./ | ||
| + | pip install / | ||
| + | </ | ||
| + | |||
| + | ===== from keras to opencv | ||
| + | |||
| + | * https:// | ||
| + | |||
| + | |||
| ===== Export protobuf ===== | ===== Export protobuf ===== | ||
| + | prereq | ||
| < | < | ||
| + | bazel build tensorflow/ | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | import argparse | ||
| + | ap = argparse.ArgumentParser() | ||
| + | ap.add_argument(' | ||
| + | ap.add_argument(' | ||
| + | args = ap.parse_args() | ||
| + | |||
| from keras import backend as K | from keras import backend as K | ||
| from keras.models import load_model | from keras.models import load_model | ||
| Line 12: | Line 102: | ||
| # create model and load weights or ... | # create model and load weights or ... | ||
| # model = create_model() | # model = create_model() | ||
| - | # model.load_weights(path_to_h5) | + | # model.load_weights(args.input) |
| # ... load h5 model | # ... load h5 model | ||
| - | load_model(path_to_h5) | + | load_model(args.input) |
| - | print(" | + | |
| saver = tf.train.Saver() | saver = tf.train.Saver() | ||
| - | saver.save(K.get_session(), | + | saver.save(K.get_session(), |
| + | print("Output node needed in freeze_graph: | ||
| + | print(" | ||
| + | </ | ||
| + | |||
| + | result | ||
| + | < | ||
| + | /tmp/model | ||
| + | ├── checkpoint | ||
| + | ├── ckpt.data-00000-of-00001 | ||
| + | ├── | ||
| + | └── ckpt.meta | ||
| </ | </ | ||
| Line 24: | Line 125: | ||
| < | < | ||
| bazel-bin/ | bazel-bin/ | ||
| - | --input_meta_graph=model.ckpt/model.ckpt.meta | + | --input_meta_graph=/tmp/model/ |
| - | --input_checkpoint=model.ckpt/model.ckpt \ | + | --input_checkpoint=/tmp/model/ckpt \ |
| - | --output_graph=frozen_graph.pb --input_binary \ | + | --output_graph=frozen_graph.pb |
| + | --input_binary \ | ||
| --output_node_names=" | --output_node_names=" | ||
| </ | </ | ||