This is a repo showing various ways of using tensorflow and its variants.
pip install --upgrade --pre tensorflow-gpu
- ๊ฐ release์ ๋ง๋ cuda์ cudnn์ ์ค์นํด์ผํ๋ค
- ํ์ฌ latest release๋
tensorflow 1.13.1, ์ด์ ๋ง๋ ๊ฑดcuda 10.0,cudnn 7.x
https://www.tensorflow.org/install/lang_c ์์ Windows GPU only ๋ค์ด๋ก๋, ์์ถ ํด์
https://storage.googleapis.com/tensorflow ์ฌ๊ธฐ์ ์ ๋ฒ์ ๋ค์ prebuilt๋ฅผ ๋ฐ๊ฒฌํ ์ ์๋ค
prebuilt๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ๋น๋๋ ๋์ CUDA ๋ฒ์ ์ ์์์ผํ๋ค!!!
https://www.tensorflow.org/install/source_windows ์ฌ๊ธฐ์ ํ์ธ (1.12.0์ CUDA 9.0์ ๊น์์ผํจ, 9.2๋ ์ ๋๋ ์ฃผ์)
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2017\Visual Studio Tools์ ์๋ ๊ฐ๋ฐ์ ๋ช
๋ น ํ๋กฌํํธ๋ฅผ ์ฌ์ฉ
dumpbin /exports tensorflow.dll > dumpbin.txt
dumpbin.txt ์์ name๋ถ๋ถ๋ง ๋ฐ๋ก ๋นผ๊ณ , ๋งจ ์์ EXPORTS๋ฅผ ๋ฃ์ด์ tensorflow.def๋ก ์ ์ฅํ์ (๋์ ๊ฒฝ์ฐ๋ vs code์ Shift+Alt+Click ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ )
dumpbin.txt ์์
ordinal hint RVA name
1 0 02D78C80 ??0?$MaybeStackArray@D$0CI@@icu_62@@AEAA@AEBV01@@Z
2 1 035F9D10 ??0?$MaybeStackArray@D$0CI@@icu_62@@QEAA@$$QEAV01@@Z
3 2 035F9D70 ??0?$MaybeStackArray@D$0CI@@icu_62@@QEAA@H@Z
4 3 035F9DF0 ??0?$MaybeStackArray@D$0CI@@icu_62@@QEAA@XZ
5 4 03603B80 ??0Appendable@icu_62@@QEAA@AEBV01@@Z
6 5 03603B80 ??0Appendable@icu_62@@QEAA@XZ
tensorflow.def ์์
EXPORTS
??0?$MaybeStackArray@D$0CI@@icu_62@@AEAA@AEBV01@@Z
??0?$MaybeStackArray@D$0CI@@icu_62@@QEAA@$$QEAV01@@Z
??0?$MaybeStackArray@D$0CI@@icu_62@@QEAA@H@Z
??0?$MaybeStackArray@D$0CI@@icu_62@@QEAA@XZ
??0Appendable@icu_62@@QEAA@AEBV01@@Z
lib /def:tensorflow.def /OUT:tensorflow.lib /MACHINE:X64 ๋ก tensorflow.lib ์์ฑ
๊ทธ๋ ๋ค๋ฉด 3 ํ์ผ์ด ์ค๋น๊ฐ ๋์์ ๊ฒ์ด๋ค
tensorflow.lib- libtensorflow.dll- dllc_api.h- header
์์ง windows์ง์์ด ๋ง์ด ๋ฏธํกํ ์ํ๋ผ์ ์ค๋ฅ๊ฐ ๋ง์ผ๋ ์ ์
https://www.tensorflow.org/install/source_windows์์ Build the pip package ์ ๊น์ง์ ๊ณผ์ ์ ํด์ฃผ์
bazel 0.21.0 (๋๋ฌด ๋์ ๋ฒ์ ์ ๋ฐ์ผ๋ฉด ํ ์ํ๋ก์ฐ๊ฐ ํธํ์ด ์ ๋๋ค) ์ ๋ฐ์
-
https://docs.bazel.build/versions/master/install-windows.html
-
https://docs.bazel.build/versions/master/windows.html#build-c
-
์ฌ๊ธฐ์ ๋์์๋ ๊ณผ์ ์ ๊ทธ๋๋ก ํ๋ฉด ๋๋ค
bazel, msys2, Visual C++ Build Tools 2015(๋ด ๊ฒฝ์ฐ๋ VS2017์์ ์ถ๊ฐ ์ต์ ์ ์ฒดํฌํด์ ์ค์นํ์) ์ค์น
tensoflow 1.13.1 : https://github.com/tensorflow/tensorflow/releases
python configure.py
- windows์์ XLA JIT support ๋๊ธฐ (์์ง ์ง์์ด ์ ๋๋๋ฏ) : tensorflow/tensorflow#24218
- ROCm์ AMD gpu์ฉ์ด๋ผ๊ณ ํ๋ ๋๊ธฐ
- ์ข์ cpu (์ผ๋จ์ 6์ธ๋ ์ด์?)์์ optimization flag์
/arch:AVX2๋ฅผ ์จ์ฃผ์ (์ด์ฐจํผ ์ค์ํ๊ฑด gpu์ด๊ธฐ ๋๋ฌธ์ ๊ทธ๋ฅ ์ฐจ์ด๋ ์์๋ฏ) - ์ปดํ์ผ ํ์ ์ค์ด๋ eigen strong inline์ ์ผ๋ ๋๋๋ฐ, ๋ง์ฝ ๋น๋๊ฐ ์คํจํ๋ฉด ๊บผ๋ณด์
- CUDA๋ ์ํ๋ ๋ฒ์ ์ ์ ์ (1.13.1์ 10.0์ด default๋ค), ์ฌ๊ธฐ์ ์ ํํ๊ฒ ์์ ํ์๋ฆฌ๊น์ง ์ ์ด์ผ ๋์ค์ dll์ ์ฐพ์ ์ ์๋ค
- cudnn์ 7.4.2๋ผ๊ณ ํด์ค๋ ๋๋๋ฐ 7์ด๋ผ๊ณ ๋ง ํด๋ ๋๋๋ฏ(default)
- RTX 2080์ CUDA compute capability๋ 7.5์ด๋ 7.5๊น์ง ํฌํจํด์ฃผ์
3.5,7.5
bazel build --config opt //tensorflow/tools/lib_package:libtensorflow ํ๋ฉด gpu support ๋จ
์ด๋ฏธ python configure.py์์ CUDA์ต์
์ ์คฌ๊ธฐ ๋๋ฌธ์ --config=cuda๋ฅผ ๋ฐ๋ก ์ ํด๋ ๋๋๋ฏํ๋ค. ํํ์ด์ง์ ๋์์๋ ์ปค๋งจ๋๋ ์์ ๋ฒ์ ์ธ๋ฏ.
๊ทธ๋ ๋ค๋ฉด 3 ํ์ผ์ด ์ค๋น๊ฐ ๋์์ ๊ฒ์ด๋ค
-
liblibtensorflow.so.ifso- lib -bazel-bin/tensorflow/liblibtensorflow.so.ifso -
libtensorflow.so- dll -bazel-bin/tensorflow/libtensorflow.so -
c_api.h- header -bazel-bin/tensorflow/tools/lib_package/libtensorflow.tar.gz์์ถ ํด์ ํinclude/tensorflow/c/c_api.h
์์ ๋งํ lib, dll, header๋ฅผ ์ถ๊ฐํด์ ์จ์ฃผ๋ฉด ๋๋ค.
C Api๋ฅผ ์ด์ฉํ๊ธฐ ์ํด์๋ graph definition์ protobuf(.pb) ํ์์ผ๋ก ๋นผ๋ด์ผ ํ๋ค. ๋ ํ์ํ operation์ด ์์ผ๋ฉด operation์ ์ด๋ฆ, operation์ ํ์ํ input ๋๋ output tensor์ shape๊ณผ type์ ์๊ณ ์์ด์ผ ํ๋ค.
์ด๋ฐ ์์ ์์๋ก ๋ง์ด๋ค.
import tensorflow as tf
# Batch of input and target output (1x1 matrices)
x = tf.placeholder(tf.float32, shape=[None, 1, 1], name='input')
y = tf.placeholder(tf.float32, shape=[None, 1, 1], name='target')
# Trivial linear model
y_ = tf.identity(tf.layers.dense(x, 1), name='output')
# Optimize loss
loss = tf.reduce_mean(tf.square(y_ - y), name='loss')
optimizer = tf.train.AdamOptimizer()
train_op = optimizer.minimize(loss, name='train')
init = tf.global_variables_initializer()
# tf.train.Saver.__init__ adds operations to the graph to save
# and restore variables.
saver_def = tf.train.Saver().as_saver_def()
print('Run this operation to initialize variables : ', init.name)
print('Run this operation for a train step : ', train_op.name)
print('Feed this tensor to set the checkpoint filename: ', saver_def.filename_tensor_name)
print('Run this operation to save a checkpoint : ', saver_def.save_tensor_name)
print('Run this operation to restore a checkpoint : ', saver_def.restore_op_name)
# Write the graph out to a file.
with open('graph.pb', 'wb') as f:
f.write(tf.get_default_graph().as_graph_def().SerializeToString())๋ checkpoint ํ์ผ๋ก weight๋ฅผ ๋ณต๊ตฌํด์ฃผ๊ณ ์ถ๋ค๋ฉด, checkpoint ํ์ผ๋ ํ์ํ๋ค. (.index, .data~)
ํ์ํ operation์ ์ด๋ฆ๋ค์ ๋ณดํต input, output, train, initializer, checkpoint save, checkpoint restore, checkpoint filename set ํ๋ operation์ด๋ค.
๊ทธ๋์ ๊ฒฐ๊ตญ ์์๋ธ operation์ ์ด๋ฆ๋ค๋ก C api์์ operation๋ค์ ์ฐพ๊ณ , TF_SessionRun์ผ๋ก ์คํํ๋ฉด ๋๋ค.
Tensor๋ ์ฐ์๋ ๋ฉ๋ชจ๋ฆฌ์ dimension์ ๋ช ์ํด์ฃผ๋ฉด ๋ง๋ค์ด๋ผ ์ ์๋ค.
๋ค์์ ์ ๊ฐ ๊ตฌํํด๋ธ ์์์ ๋๋ค. ๊ทธ๋๋ก ์ฐ์๋ฉด ๋ฉ๋๋ค.
fcn_model()์
logs๋ผ๋ ๋๋ ํ ๋ฆฌ๊ฐ ์์ผ๋ฉด ๊ฑฐ๊ธฐ์์ restore๋ฅผ ํ ํ, prediction์ ๋ณด์ฌ์ค ํ training์ ์ผ์ ๋ ํ ํ ๋ค์ ๊ฐ์ ๋ prediction์ ๋ณด์ฌ์ค๋๋ค.logs๋ผ๋ ๋๋ ํ ๋ฆฌ๊ฐ ์์ผ๋ฉด weight initialization์ ํ ํ, prediction์ ๋ณด์ฌ์ค ํ training์ ์ผ์ ๋ ํ ํ ๋ค์ ๊ฐ์ ๋ prediction์ ๋ณด์ฌ์ค๋๋ค.
- Tensor์ shape๊ณผ type์ tensorboard๋ python์์
print(t.shape, t.dtype)๋ฑ์ผ๋ก ํ์ธํ์. ๊ฐ๋์ฉt.dtype์ด ๊ทธ๋ํ์ ์๋ ๊ฒ๊ณผ ๋ค๋ฅด๊ฒ ์ถ๋ ฅ๋๋๊ฒ ๊ฐ๊ธด ํ๋(float32 -> float64๊ฐ์ด ์ฌ์ํ๊ฒ) ๊ทธ๋ํ๋ฅผ ๋ณด๋๊ฒ ์ ์ผ ํ์คํ ๋ฏ - Graph op ์ด๋ฆ๋ tensorboard๋ python์์ ํ์ธํด์ ์ ์ฉ
- DT_BOOL์ int ๋ฐฐ์ด๋ก ๋จน์ฌ์ค๋ ์ ์ธ์๋๋ค. ๋๋๋ก int๋ก ์ฃผ์ (vector ๊ด๋ จํ ์ด์ ๋๋ฌธ์)
- DT_FLOAT : float
- DT_INT32 : int
- DT_INT64 : int64_t
tensorflow๊ฐ windows์์ c++ api ์ง์์ ์์ง ์ ์ ํด์ ์ด๋ ์ ๋์ hack์ด ํ์ํ๋ค.
์์ง ์์ ๋จ๊ณ : tensorflow/tensorflow#26152
cuda 7.0~8.0์ ์ฐ๋ ๊ณผ๊ฑฐ ๋ฒ์ ์์๋ Cmake๋ฅผ ์ง์ํ์ง๋ง, ํ์ฌ ๋ฒ์ ์์๋ Cmake ์ง์์ด ๋๊ฒจ ์ ๋๊ณ , bazel์ ์ด์ฉํ ์ปดํ์ผ์ ์ง์ํ๋ค.
์ฌ์ง์ด tensorflow๊ฐ ๊ณต์์ผ๋ก ์ง์ํ๋ C++ api๋ tensorflow project๋ฅผ ์ ๋ถ compileํ๋ฉด์ tensorflow ๋ด๋ถ์ ๋ด ํ๋ก์ ํธ๋ฅผ ๋ฃ์ด tensorflow์ ๋ฐฉ๋ํ ์ฝ๋๋ฅผ ์ ๋ถ ์ปดํ์ผํด์ผํ๋ ๋จ์ ์ด ์์ด ๋ฐฐํฌ์ฉ์ผ๋ก๋ ๋ถ์ ํฉํ๋ค.
๋คํํ ์ด๋ ์ ๋์ hack์ ํตํด shared library๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์์๋์ผ๋ ๊ทธ๊ฒ์ ์ฐ๋ฉด ๋ ๊ฒ ๊ฐ๋ค.
์ด repo๋ฅผ ๋ฐ๋ฅธ๋ค : https://github.com/guikarist/tensorflow-windows-build-script
์ด repo์์๋ tensorflow์์ ์ง์ํ๋ bazel build์๋ค ์ถ๊ฐ๋ก ์๋์ฐ์์ shared library์ ํํ๋ก ์ฐ๊ธฐ ์ํ ํจ์น ์์ ์ ๋ชจ์๋์ repo์ด๋ค.
์ด repo์ ๋ด์ฉ๋๋ก ์ปดํ์ผ์ ํ์ผ๋ฉด ํด์ผ ํ ์ผ์ bazel build์ ๊ฒฐ๊ณผ์์ ์ ์ ํ ํ์ผ๋ค์ includeํด์ฃผ๋ ๊ฒ์ธ๋ฐ, ์์ง๊น์ง ์ถฉ๋ถํ symbol์ ๋ค ๋ชจ์๋์ static lib๊ฐ ์๋ ์ํ๋ผ์ ๋ด๊ฐ ํ์ํ symbol์ ํ์ ํ๊ณ ๋ค์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋น๋ํด์ผํ๋ค. (์ด๊ฒ ๋ฌด์จ ์๋ฆฌ์ธ์ง๋ ์ถํ์ ์ค๋ช )
๋น๋๊ฐ ๋๋๋ฉด c api์ฒ๋ผ
bazel-bin/tensorflow/libtensorflow_cc.so -> tensorflow_cc.dll
bazel-bin/tensorflow/liblibtensorflow_cc.so.ifso -> tensorflow_cc.lib
๋ก ์ถ๊ฐํด์ค๋ค.
๋ฐ๋ก includeํด์ผํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ์ถ์ถํด์ฃผ๋ ์คํฌ๋ฆฝํธ๋ฅผ ๋ง๋ค์ด๋ณด์๋ค.
Set-StrictMode -Version latest
$ErrorActionPreference = "Stop"
$tfLibDir = "$pwd\tensorflow-1.13.1_cc"
Remove-Item $tfLibDir -ErrorAction SilentlyContinue -Force -Recurse
mkdir $tfLibDir | Out-Null
$tfSourceDir = "D:\tf-win\source"
# Tensorflow lib and dll
Copy-Item $tfSourceDir\bazel-bin\tensorflow\libtensorflow_cc.so $tfLibDir\tensorflow_cc.dll
Copy-Item $tfSourceDir\bazel-bin\tensorflow\liblibtensorflow_cc.so.ifso $tfLibDir\tensorflow_cc.lib
# Tensorflow includes
Copy-Item $tfSourceDir\tensorflow\core $tfLibDir\include\tensorflow\core -Recurse -Filter "*.h"
Copy-Item $tfSourceDir\tensorflow\cc $tfLibDir\include\tensorflow\cc -Recurse -Filter "*.h"
Copy-Item $tfSourceDir\bazel-genfiles\tensorflow\core $tfLibDir\include_pb\tensorflow\core -Recurse -Filter "*.h"
Copy-Item $tfSourceDir\bazel-genfiles\tensorflow\cc $tfLibDir\include_pb\tensorflow\cc -Recurse -Filter "*.h"
# Protobuf includes.
Copy-Item $tfSourceDir\bazel-source\external\protobuf_archive\src\google $tfLibDir\include_proto\google -Recurse -Filter "*.h"
# Absl includes.
Copy-Item $tfSourceDir\bazel-source\external\com_google_absl\absl $tfLibDir\include_absl\absl -Recurse -Filter "*.h"
# Eigen includes
Copy-Item $tfSourceDir\bazel-source\external\eigen_archive\ $tfLibDir\include_eigen_archive -Recurse
Copy-Item $tfSourceDir\third_party\eigen3 $tfLibDir\include_eigen\third_party\eigen3\ -Recurse
COMPILER_MSVC
NOMINMAX
์คํํด๋ณด๊ณ external symbol์ด ์๋ค๊ณ ๋จ๋ฉด ๊ทธ ์ฌ๋ณผ๋ค์ ๊ฐ์ง๊ณ tf_exported_symbols_msvc.lds์ ๋ฃ๊ณ ๋ค์ ๋น๋ํ๋ฉด ๋๋ค.
https://www.tensorflow.org/lite/guide/get_started
TF Lite model๋ก ๋ฐ๊พธ๊ธฐ ์ํด์๋ ๋ช๋ช operation, type๋ฑ์ ์ ์ฝ ์กฐ๊ฑด์ด ์๋ค.
fcn ๋ชจ๋ธ์ ๊ฒฝ์ฐ์๋ shape์ด scalar์ธ๊ฒ ์ ์ฝ ์กฐ๊ฑด์ ๊ฑธ๋ ค์, (1) shape์ผ๋ก ๋ฐ๊ฟ์ ํด๊ฒฐํ๋ค.
TF_BOOL ์ง์ ์ ํจ : tensorflow/tensorflow#20741
TF Lite๋ android java api๋ฅผ ์จ์ application(apk)๋ฅผ ๋ง๋ค ์ ์๋ค.
์ด๋ก ์์ผ๋ก๋ .tflite ํ์ผ๋ก ๋ฐ๊พธ๊ธฐ๋ง ํ๋ฉด ๋ค ์คํํ ์ ์์ง๋ง, ๋ชจ๋ธ ํฌ๊ธฐ๊ฐ ๋๋ฌด ํฌ๋ฉด interpreter๊ฐ ๋ป์ด๋ฒ๋ ค์ ํ์ค์ ์ผ๋ก๋ ๋ถ๊ฐ๋ฅํ๋ค. (8 bit quantization ์ต์ ํ๋ฅผ ์ ์ฉํด๋ 100MB ์ด์์)
๊ตฌ๊ธ์์ ์ถ์ฒํ๋ ๋ชจ๋ธ์ธ ๋ชจ๋ฐ์ผ์ฉ์ผ๋ก ์ต์ ํ๋ deeplab์ผ๋ก ํ๋ฉด ์ ๋๋ค.
https://www.tensorflow.org/lite/models/segmentation/overview
์ค์๊ฐ segmentation apk ์์ : https://github.com/tantara/JejuNet
tensorflow js๋ ๋ธ๋ผ์ฐ์ ๋ก ๊ฐ๋๋๋ฏ๋ก ๋งค์ฐ ์ ๊ทผ์ฑ์ด ํธ๋ฆฌํ๋ค.
์ด๊ฒ ์ญ์ tfjs๊ฐ ์๊ตฌํ๋ ํ์์ผ๋ก model์ ๋ณํํด์ผ ํ๋ฉฐ, operation์ด๋ type๋ฑ์ ์ ์ฝ์ ์์ง๋ง, ์ด๋ฒ fcn model์ ๊ฒฝ์ฐ์๋ ๊ฑธ๋ฆฌ๋ ์ ์ฝ์ด ์์ด์ ์ ๋๋ค.
๋ค๋ง ์ด ๊ฒฝ์ฐ ๋ฌธ์ ์ ์ ์ญ์ ๋๋ฌด๋๋ ํฐ fcn model์ด๋ค. ์ด๋ก ์์ผ๋ก๋ ์๋ฌด ๋ชจ๋ธ์ด๋ ๋ณํ ๊ฐ๋ฅํ์ง๋ง, ํ์ค์ ์ผ๋ก ์น ํ๊ฒฝ์์ ์ฐ๊ธฐ์ ๋ถํธํ ์ ๋๋ก model์ด ์ปค์ ๋ค์ด๋ก๋ ๋ฐ๋ ์๊ฐ์ด ๋๋ฌด ๋๋ฆฌ๋ค.
๋ opencv๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋์์ด ์์ผ๋ฏ๋ก tensor๋ผ๋ฆฌ์ ์ฐ์ฐ์ด ๊ตฌํ๋์ด ์๋ ๋ฐฉ์์ด ๊น๋ค๋ก์, image Overlay๋ฅผ ๋น๊ต์ ํ์ ํ๊ฒ ํ ์๋ฐ์ ์์๋ค. (ํ์ฌ ์ ๊ฐ ์์๋ก ํด๋์ visualization์ด ๋ถ์ ํํจ) ์ด๊ฒ์ ๋์ค์ ์ง์ง๋ก ์ฌ์ฉํ ์ผ์ด ์์๋๋ ์ ๋๋ก ๋ง๋ค์ด์ผ ํ ๊ฒ์ด๋ค.
http://imnotkind.tk/~imnotkind/tfjs/
this is NOT freezing, so we can use this pb for training!
init = tf.global_variables_initializer()
saver_def = tf.train.Saver().as_saver_def()
print('Run this operation to initialize variables : ', init.name)
print('Run this operation for a train step : ', train_op.name)
print('Feed this tensor to set the checkpoint filename: ', saver_def.filename_tensor_name)
print('Run this operation to save a checkpoint : ', saver_def.save_tensor_name)
print('Run this operation to restore a checkpoint : ', saver_def.restore_op_name)
with open('fcn.pb', 'wb') as f:
f.write(tf.get_default_graph().as_graph_def().SerializeToString())freeze_graph --input_graph=/tmp/mobilenet_v1_224.pb \
--input_checkpoint=/tmp/checkpoints/mobilenet-10202.ckpt \
--input_binary=true \
--output_graph=/tmp/frozen_mobilenet_v1_224.pb \
--output_node_names=MobileNetV1/Predictions/Reshape_1import sys
import tensorflow as tf
from tensorflow.python.tools import freeze_graph
from tensorflow.python.tools import optimize_for_inference_lib
# Freeze the graph
input_graph_path = 'model/fcn.pb'
checkpoint_path = 'model/fcn-ckpt/model.ckpt-haebin' #prefix of checkpoint, only need .index and .data-???, not .meta
input_saver_def_path = ""
input_binary = True
output_node_names = "Pred"
restore_op_name = "save/restore_all"
filename_tensor_name = "save/Const:0"
output_frozen_graph_name = 'model/frozen_fcn.pb'
#output_optimized_graph_name = 'optimized_'+MODEL_NAME+'.pb'
clear_devices = True
freeze_graph.freeze_graph(input_graph_path, input_saver_def_path,
input_binary, checkpoint_path, output_node_names,
restore_op_name, filename_tensor_name,
output_frozen_graph_name, clear_devices, "")tflite_convert `
--output_file=model/frozen_fcn.tflite `
--graph_def_file=model/frozen_fcn.pb `
--input_arrays=input_image,keep_probability `
--output_arrays=Pred `
--input_shapes=1,256,256,3:1 `
--output_format=TFLITE `
--inference_type=QUANTIZED_UINT8 `
--std_dev_values=128,0 --mean_values=128,1 `
--default_ranges_min=-6 --default_ranges_max=6import tensorflow as tf
graph_def_file = "model/mymodel.pb"
input_arrays = ["input/Placeholder", "input/Placeholder_2"]
output_arrays = ["output/ArgMax"]
converter = tf.lite.TFLiteConverter.from_frozen_graph(
graph_def_file, input_arrays, output_arrays)
tflite_model = converter.convert()
open("model/mymodel.tflite", "wb").write(tflite_model)tflite_convert `
--output_file=model/frozen_fcn.tflite `
--graph_def_file=model/frozen_fcn.pb `
--input_arrays=input_image `
--output_arrays=Pred `
--input_shapes=1,256,256,3 `
--output_format=TFLITE `
--inference_type=QUANTIZED_UINT8 `
--std_dev_values=128 --mean_values=128 `
--default_ranges_min=-6 --default_ranges_max=6import/์ :0๋ ๋นผ๋ ๋๋ค
tensorflow/tensorflow#23932 : scalar value๋ [1]๋ก ๋์ฒด
tensorflowjs_converter `
--input_format=tf_frozen_model `
--output_node_names='Pred' `
--saved_model_tags=serve `
--output_json=true `
frozen_fcn.pb `
frozen_fcn.jspip install tensorflowjs==0.8.5 tensorflow/tfjs#1541
pb์์ ๋ณํํ ์ ์๋ ๊ฑด ๊ตฌ๋ฒ์ ๋ฟ์ด๋ค. ๊ตฌ๋ฒ์ ์ ๋ฐ์์ผ ํ๋ค.
์ค๋ฅ ๋จ๋ ๊ฒฝ์ฐ : pip install numpy --upgrade : https://stackoverflow.com/questions/54665842/when-importing-tensorflow-i-get-the-following-error-no-module-named-numpy-cor
https://www.tensorflow.org/js/tutorials/conversion/import_saved_model
from keras import backend as K
from keras.models import load_model
import tensorflow as tf
model = load_model('model/my_model.h5')
#print(model.summary()) (None,300,300,3) -> (None, 5)
print(model.input)
print(model.output)
print(model.targets)
#print(dir(model))
#print(K.learning_phase())
K.set_learning_phase(0) #0 : test, 1 : train
#print(K.learning_phase())
sess = K.get_session()
saver = tf.train.Saver()
saver.save(sess, 'keras/keras.ckpt')
sess.graph.as_default()
graph = sess.graph
saver_def = saver.as_saver_def()
print('Feed this tensor to set the checkpoint filename: ', saver_def.filename_tensor_name)
print('Run this operation to save a checkpoint : ', saver_def.save_tensor_name)
print('Run this operation to restore a checkpoint : ', saver_def.restore_op_name)
with open('keras/keras.pb', 'wb') as f:
f.write(graph.as_graph_def().SerializeToString())์ด๋ ๊ฒ ํ๋ฉด inference๋ ์๋ฒฝํ๋ฐ, ๋ฌธ์ ๋ training์ด๋ค. ์ผ๋ฐ์ ์ผ๋ก tensorflow model์์๋ training operation์ ๊ฐ๋ํ๋ฉด training์ด ๋์ง๋ง, keras์ ๊ฒฝ์ฐ ๊ทธ๋ ์ง ์๊ณ ์ผ์ผํ keras๊ฐ model์ ๊ณณ๊ณณ์ ๋ณํํ๋ ๋ฐฉ์์ผ๋ก set_learning_phase()ํจ์๊ฐ ์ด๋ฃจ์ด์ ธ ์๋ ๊ฒ ๊ฐ๋ค. ๊ทธ๋์ ๋ง์ฝ keras๊ฐ ๋ด๋ถ์ ์ผ๋ก ๋ชจ๋ธ์ ๋ณํํ๋ ๋ฐฉ๋ฒ์ ์๋ค ํ๋๋ผ๋, ๊ทธ๋ํ ๋ณํ์ c api์์๋ ํ ์ ์๋ ์ผ์ด๋ผ์ ๋ถ๊ฐ๋ฅ์ธ ๊ฒ ๊ฐ๋ค. ๋ง์ฝ keras๊ฐ ๋ด๋ถ์ ์ผ๋ก ์ฐ๋ train op๊ฐ ์์ด์ ๊ทธ๊ฒ๋ง ์คํํ๋ฉด ๋๋ค๋ฉด, ๊ฐ๋ฅํ ๊ฒ์ด๋ค.
์ด์๋ก ์ฌ๋ ค๋์ ์ํ : tensorflow/tensorflow#28681