tips:keras

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tips:keras [2018/08/23 15:00] – created scipiotips:keras [2019/12/17 15:48] (current) – [tensorflow] scipio
Line 1: Line 1:
 ====== Keras ====== ====== Keras ======
 +
 +===== tensorflow =====
 +
 +  * yay bazelisk
 +
 +<code>
 +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://github.com/tensorflow/tensorflow.git
 +cd tensorflow
 +
 +# tf 1.15
 +wget https://github.com/tensorflow/tensorflow/archive/v1.15.0.tar.gz
 +tar zxvf v1.15.0.tar.gz
 +</code>
 +
 +only for tf 2
 +<code>
 +wget https://git.archlinux.org/svntogit/community.git/plain/trunk/Add-grpc-fix-for-gettid.patch\?h\=packages/tensorflow -O Add-grpc-fix-for-gettid.patch
 +patch -Np1  -i Add-grpc-fix-for-gettid.patch
 +</code>
 +
 +<code>
 +export TF_IGNORE_MAX_BAZEL_VERSION=1
 +export TF_NEED_CUDA=1
 +./configure
 +</code>
 +
 +for atf 1.15
 +<code>
 +echo 0.26.1 > .bazelversion
 +</code>
 +
 +this produce .tf_configure.bazelrc
 +<code>
 +build --action_env PYTHON_BIN_PATH="/lab/gianoobserver/lib/bin/python"
 +build --action_env PYTHON_LIB_PATH="/lab/gianoobserver/lib/lib/python3.8/site-packages"
 +build --python_path="/lab/gianoobserver/lib/bin/python"
 +build:xla --define with_xla_support=true
 +build --config=xla
 +build --action_env CUDA_TOOLKIT_PATH="/usr/local/cuda"
 +build --action_env TF_CUDA_COMPUTE_CAPABILITIES="6.1"
 +build --action_env LD_LIBRARY_PATH="/lab/dnn/lib/lib"
 +build --action_env GCC_HOST_COMPILER_PATH="/usr/bin/gcc-8"
 +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,medium
 +test --test_tag_filters=-benchmark-test,-no_oss,-gpu,-oss_serial
 +test --build_tag_filters=-benchmark-test,-no_oss,-gpu
 +build --action_env TF_CONFIGURE_IOS="0"
 +</code>
 +
 +compile
 +<code>
 +bazel build //tensorflow/tools/pip_package:build_pip_package
 +
 +# ???
 +bazelisk \
 +    build --config=opt \
 +      //tensorflow:libtensorflow.so \
 +      //tensorflow:libtensorflow_cc.so \
 +      //tensorflow:install_headers \
 +      //tensorflow/tools/pip_package:build_pip_package
 +./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
 +pip install /tmp/tensorflow_pkg/tensorflow*.whl
 +</code>
 +
 +===== from keras to opencv  =====
 +
 +  * https://github.com/adoval4/keras_to_OpenCV_tensorflow
 +
 +
  
 ===== Export protobuf ===== ===== Export protobuf =====
  
 +prereq
 <code> <code>
 +bazel build tensorflow/python/tools:freeze_graph
 +</code>
 +
 +<code>
 +import argparse
 +ap = argparse.ArgumentParser()
 +ap.add_argument('--input', required=True)
 +ap.add_argument('--output', default="/tmp/model/ckpt")
 +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("Output node: %s" % model.output.op.name)+
 saver = tf.train.Saver() saver = tf.train.Saver()
-saver.save(K.get_session(), "/tmp/model.ckpt")+saver.save(K.get_session(), args.default) 
 +print("Output node needed in freeze_graph: %s" % model.output.op.name) 
 +print("Model created in: %s" % args.output) 
 +</code> 
 + 
 +result 
 +<code> 
 +/tmp/model 
 +├── checkpoint 
 +├── ckpt.data-00000-of-00001 
 +├── ckpt.index 
 +└── ckpt.meta
 </code> </code>
  
Line 24: Line 125:
 <code> <code>
 bazel-bin/tensorflow/python/tools/freeze_graph \ bazel-bin/tensorflow/python/tools/freeze_graph \
---input_meta_graph=model.ckpt/model.ckpt.meta +--input_meta_graph=/tmp/model/ckpt.meta 
---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="previous output node name" --output_node_names="previous output node name"
 </code> </code>
  
- 
-     
-    print("Output node: %s" % model.output.op.name) 
-    saver = tf.train.Saver() 
-    saver.save(K.get_session(), "./"+export_path+"/model.ckpt") 
  • tips/keras.1535029245.txt.gz
  • Last modified: 2018/08/23 15:00
  • by scipio