├── .gitignore
├── LICENSE
├── README.md
├── cpp
├── ai.props
├── libunet
│ ├── ai.cpp
│ ├── ai.h
│ ├── cvbind.cpp
│ ├── cvbind.h
│ ├── libcv.vcxitems
│ ├── libunet.vcxitems
│ ├── unet.cpp
│ ├── unet.h
│ ├── unetbind.cpp
│ └── unetbind.h
├── pycv
│ ├── dllmain.cpp
│ ├── pycv.vcxproj
│ ├── pycv.vcxproj.filters
│ ├── pycv.vcxproj.user
│ ├── result.png
│ └── test_pycv.py
├── thirdparty
├── unet.sln
└── unet
│ ├── main.cpp
│ ├── result.png
│ ├── test.png
│ ├── unet.mapping
│ ├── unet.vcxproj
│ ├── unet.vcxproj.filters
│ ├── unet.vcxproj.user
│ └── unet.xml
├── doc
├── 0_pred.png
├── 0_test.png
├── u-net-architecture.png
├── unet-openvino.png
├── unet-pycv.png
└── unet_20200229.png
└── py3
├── LICENSE
├── data.py
├── dataset
└── membrane
│ ├── test
│ ├── 0.png
│ ├── 0res.png
│ ├── 1.png
│ ├── 10.png
│ ├── 11.png
│ ├── 12.png
│ ├── 13.png
│ ├── 14.png
│ ├── 15.png
│ ├── 16.png
│ ├── 17.png
│ ├── 18.png
│ ├── 19.png
│ ├── 2.png
│ ├── 20.png
│ ├── 21.png
│ ├── 22.png
│ ├── 23.png
│ ├── 24.png
│ ├── 25.png
│ ├── 26.png
│ ├── 27.png
│ ├── 28.png
│ ├── 29.png
│ ├── 3.png
│ ├── 4.png
│ ├── 5.png
│ ├── 6.png
│ ├── 7.png
│ ├── 8.png
│ └── 9.png
│ └── train
│ ├── image
│ ├── 0.png
│ ├── 1.png
│ ├── 10.png
│ ├── 11.png
│ ├── 12.png
│ ├── 13.png
│ ├── 14.png
│ ├── 15.png
│ ├── 16.png
│ ├── 17.png
│ ├── 18.png
│ ├── 19.png
│ ├── 2.png
│ ├── 20.png
│ ├── 21.png
│ ├── 22.png
│ ├── 23.png
│ ├── 24.png
│ ├── 25.png
│ ├── 26.png
│ ├── 27.png
│ ├── 28.png
│ ├── 29.png
│ ├── 3.png
│ ├── 4.png
│ ├── 5.png
│ ├── 6.png
│ ├── 7.png
│ ├── 8.png
│ └── 9.png
│ └── label
│ ├── 0.png
│ ├── 1.png
│ ├── 10.png
│ ├── 11.png
│ ├── 12.png
│ ├── 13.png
│ ├── 14.png
│ ├── 15.png
│ ├── 16.png
│ ├── 17.png
│ ├── 18.png
│ ├── 19.png
│ ├── 2.png
│ ├── 20.png
│ ├── 21.png
│ ├── 22.png
│ ├── 23.png
│ ├── 24.png
│ ├── 25.png
│ ├── 26.png
│ ├── 27.png
│ ├── 28.png
│ ├── 29.png
│ ├── 3.png
│ ├── 4.png
│ ├── 5.png
│ ├── 6.png
│ ├── 7.png
│ ├── 8.png
│ └── 9.png
├── demo.py
├── model.py
├── models
├── myunet_64_1.2.h5
├── myunet_64_1.2.pb
├── myunet_64_1.2.tflite
├── unet.mapping
└── unet.xml
└── utils.py
/.gitignore:
--------------------------------------------------------------------------------
1 | # Cifar data
2 | *.bin
3 | *.vscode
4 | *.tar*
5 | *.tif*
6 | models/*
7 | dataset/*
8 | _*/
9 | .vs/
10 | x64/*
11 | cpp/x64/*
12 | thirdparty*/*
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Knight
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # UNet 网络训练和推理
2 |
3 | Train and test Unet in keras, deploy in different DL framework.
4 |
5 | 使用 Keras 训练和测试 UNet,并在深度学习推理框架下部署。
6 |
7 | 
8 |
9 | ## 简介
10 |
11 | * 借鉴 UNet 网络原理,搭建 U 形编解码(Encoder-Decoder)语义分割网络,实现细胞分割。
12 |
13 | * 使用 Keras 训练好网络后,得到 h5 模型, 并进行推理测试。
14 |
15 | * 使用 Keras 和 tensorflow 将 h5 模型转化为 pb 模型,并进行推理测试。
16 |
17 | * 使用 OpenVino 2020.R1 对 pb 模型进行优化,得到 IR 中间文件,然后分别利用 OpenVino 的 Python 版 和 C++ 版接口进行推理。
18 |
19 | * 使用 pybind11 封装 C++ 版的推理接口,供 Python 调用。
20 |
21 | * 【WARN】更改至 tf2.x,将 h5 模型转换为 tflite 模型并推理。(慢!)
22 |
23 | ### 关于网络
24 |
25 | 原始的 UNet 网络结构如图 :
26 |
27 | 
28 |
29 | 基于 UNet 网络原理,搭建类似 UNet 的 `U` 形 Encoder-Decoder 编解码网络,实现细胞分割。修改了网络输入大小,基本块的构建及通道个数等。
30 |
31 | 修改后网络输入和输出都是: `shape=(n, 256, 256, 1), dtype=float32, range in [0.0, 1.1]`;
32 |
33 | 使用 BCE(binary-cross-entropy) 二值交叉熵作为损失函数进行训练。
34 |
35 | ### 训练和测试
36 |
37 | 数据集来自 [isbi challenge](http://brainiac2.mit.edu/isbi_challenge/)。训练图片包含 30 张 512x512x1 的图像,使用 ImageDataGenerator 扩增数据量。
38 |
39 | 20 个 epochs 后, 网络精度达到 0.96。
40 |
41 | 网络测试结果如图,(语义)分割结果看上去还可以。
42 |
43 | 
44 | 
45 |
46 | ## 使用
47 |
48 | ### 训练和测试环境
49 |
50 | * Python 3.7
51 | * Keras 2.2.4 + Tensorflow 1.14.0 (注意 keras 需要和 tensorflow 匹配)
52 | * Keras 2.3.1 + Tensorflow 2.1.0 (仅当 h5转tflite, 或 tflite 推理时用 TF 2.x, 其他的用 1.x)
53 | * OpenVino 2020.R1
54 | * pybind11
55 |
56 | ### Python 版
57 |
58 | 进入 py3 目录,分别进行训练(train)、测试(test)、保存 pb(savepb)、测试 pb(testpb)。
59 |
60 | Include `train`, `test`, `savepb`, `testpb`.
61 |
62 | ```bash
63 | cd py3
64 | ## TF1.x
65 | python demo.py --op train -d 0
66 | python demo.py --op test
67 | python demo.py --op savepb
68 | python demo.py --op testpb
69 |
70 | ## TF2.x
71 | python demo.py --op savelite
72 | python demo.py --op testlite
73 |
74 | ```
75 |
76 | ### C++ OpenVino 推理
77 |
78 | 使用 OpenVino 进行推理的基本步骤:
79 |
80 | - 首先安装 g++/VS2015+ 和 OpenVino 工具包。
81 | - 将先前训练好的 h5 模型转为 pb 模型,然后使用 mo.py 优化 pb 模型得到 IR 文件(.xml/.bin/.mapping)。
82 | - 创建 C++ 项目,加载 IR 文件,并进行推理。
83 |
84 | 这里有 VS 项目供参考,使用方法:
85 |
86 | - 进入 cpp 文件夹
87 | - 从 [ausk/thirdparty](https://github.com/ausk/thirdparty) 下载依赖的 OpenVino 和 OpenCV 库, 解压 thirdparty 目录到 cpp 目录中。
88 | - 打开 unet.sln 解决方案文件,修改 pycv 项目中 Python 的路径。
89 | - 编译并运行 unet,直接使用 OpenVino C++ 版本推理。
90 |
91 | 
92 |
93 | - 编译并运行 pycv,则提供 pybind11 绑定的 OpenCV。test_pycv.py 是测试文件。
94 |
95 | 
96 |
97 | ## 参考
98 |
99 | * [U-Net: Convolutional Networks for Biomedical Image Segmentation](http://lmb.informatik.uni-freiburg.de/people/ronneber/u-net/)
100 | * keras 版 Unet 实现 [zhixuhao/unet](https://github.com/zhixuhao/unet)
101 | * OpenVino 开源版 [opencv/dldt](https://github.com/opencv/dldt)
102 | * 我的知乎文章 [191123 使用 Pybind11 和 OpenCV 创建 Python 库](https://zhuanlan.zhihu.com/p/93299698)
103 |
--------------------------------------------------------------------------------
/cpp/ai.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | D:\Programs\Python\Python37
6 |
7 |
8 | $(SolutionDir)$(Platform)\$(ProjectName)\$(Configuration)\
9 |
10 |
11 |
12 | $(PYTHONDIR)\include;$(SolutionDir)\thirdparty\openvino2020r1\include;$(SolutionDir)\thirdparty\opencv420\include;$(SolutionDir)\thirdparty\pybind11;%(AdditionalIncludeDirectories)
13 | WIN32;_WINDOWS;_SCL_SECURE_NO_WARNINGS;NOMINMAX;USE_OPENCV;_CRT_SECURE_NO_WARNINGS;GFLAGS_IS_A_DLL=0;_UNICODE_;UNICODE_;%(PreprocessorDefinitions)
14 |
15 |
16 | $(PYTHONDIR)\libs;$(SolutionDir)\thirdparty\openvino2020r1\lib;$(SolutionDir)\thirdparty\opencv420\lib;%(AdditionalLibraryDirectories)
17 | inference_engine.lib;opencv_core420.lib;opencv_highgui420.lib;opencv_imgproc420.lib;opencv_imgcodecs420.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
18 |
19 |
20 |
21 |
22 | $(PYTHONDIR)
23 | true
24 |
25 |
26 |
--------------------------------------------------------------------------------
/cpp/libunet/ai.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/cpp/libunet/ai.cpp
--------------------------------------------------------------------------------
/cpp/libunet/ai.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/cpp/libunet/ai.h
--------------------------------------------------------------------------------
/cpp/libunet/cvbind.cpp:
--------------------------------------------------------------------------------
1 | // Created by ausk @ 2019.11.23
2 | // Modified by ausk @ 2020.03.01
3 |
4 | #include "cvbind.h"
5 |
6 | // 基本的加法
7 | int add(int i, int j) {
8 | return i + j;
9 | }
10 |
11 | // 输出 hello
12 | void sayhello() {
13 | printf("Hello!\n");
14 | }
15 |
16 | //! 点相加
17 | cv::Point addpt(cv::Point& lhs, cv::Point& rhs) {
18 | return cv::Point(lhs.x + rhs.x, lhs.y + rhs.y);
19 | }
20 |
21 | //! 矩形相加
22 | cv::Rect addrect(cv::Rect& lhs, cv::Rect& rhs) {
23 | int x = std::min(lhs.x, rhs.x);
24 | int y = std::min(lhs.y, rhs.y);
25 | int width = std::max(lhs.x + lhs.width - 1, rhs.x + rhs.width - 1) - x + 1;
26 | int height = std::max(lhs.y + lhs.height - 1, rhs.y + rhs.height - 1) - y + 1;
27 | return cv::Rect(x, y, width, height);
28 | }
29 |
30 | //! 矩阵相加
31 | cv::Mat addmat(cv::Mat& lhs, cv::Mat& rhs) {
32 | return lhs + rhs;
33 | }
34 |
35 | //!读取图片
36 | cv::Mat imread(std::string fpath) {
37 | return cv::imread(fpath);
38 | }
39 |
40 | //! 写入图片
41 | void imwrite(std::string fpath, const cv::Mat& img) {
42 | cv::imwrite(fpath, img);
43 | }
44 |
45 | // 分离模块
46 | // https://pybind11.readthedocs.io/en/master/faq.html#how-can-i-reduce-the-build-time
47 | void bind_CV(py::module& m) {
48 | // Python + OpenCV
49 | m.def("add", &add, "A function which adds two numbers");
50 | m.def("sayhello", &sayhello, "Just sayhello!");
51 | m.def("addpt", &addpt, "add two point");
52 | m.def("addrect", &addrect, "add two rect");
53 | m.def("addmat", &addmat, "add two matrix");
54 | m.def("imread", &imread, "read the file into np.ndarray/cv::Mat");
55 | m.def("imwrite", &imwrite, "write np.ndarray/cv::Mat into the file");
56 | }
57 |
58 | /*
59 | PYBIND11_MODULE(pycv, m) {
60 | m.doc() = "pycv: A small OpenCV binding created using Pybind11"; // optional module docstring
61 | bind_CV(m);
62 | };
63 | */
--------------------------------------------------------------------------------
/cpp/libunet/cvbind.h:
--------------------------------------------------------------------------------
1 | // Created by ausk @ 2019.11.23
2 | // Modified by ausk @ 2020.03.01
3 |
4 | #pragma once
5 |
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 |
13 | namespace py = pybind11;
14 |
15 | // 绑定 cv 模块
16 | void bind_CV(py::module& m);
17 |
18 | // 对 cv::Point, cv::Rest, cv::
19 | namespace pybind11 {
20 | namespace detail {
21 |
22 | //! (1) 点与元组转换 cv::Point <=> tuple(x,y)
23 | #if 0
24 | template<>
25 | struct type_caster {
26 | PYBIND11_TYPE_CASTER(cv::Point, _("tuple_xy"));
27 | bool load(handle obj, bool) {
28 | py::tuple pt = reinterpret_borrow(obj);
29 | value = cv::Point(pt[0].cast(), pt[1].cast());
30 | return true;
31 | }
32 |
33 | static handle cast(const cv::Point& pt, return_value_policy, handle) {
34 | return py::make_tuple(pt.x, pt.y).release();
35 | }
36 | };
37 |
38 | #else
39 | template <>
40 | struct type_caster {
41 |
42 | //! 定义 cv::Point 类型名为 tuple_xy, 并声明类型为 cv::Point 的局部变量 value。
43 | PYBIND11_TYPE_CASTER(cv::Point, _("tuple_xy"));
44 |
45 | //! 步骤1:从 Python 转换到 C++。
46 | //! 将 Python tuple 对象转换为 C++ cv::Point 类型, 转换失败则返回 false。
47 | //! 其中参数2表示是否隐式类型转换。
48 | bool load(handle obj, bool) {
49 | // 确保传参是 tuple 类型
50 | if (!py::isinstance(obj)) {
51 | std::logic_error("Point(x,y) should be a tuple!");
52 | return false;
53 | }
54 |
55 | // 从 handle 提取 tuple 对象,确保其长度是2。
56 | py::tuple pt = reinterpret_borrow(obj);
57 | if (pt.size() != 2) {
58 | std::logic_error("Point(x,y) tuple should be size of 2");
59 | return false;
60 | }
61 |
62 | //! 将长度为2的 tuple 转换为 cv::Point。
63 | value = cv::Point(pt[0].cast(), pt[1].cast());
64 | return true;
65 | }
66 |
67 | //! 步骤2: 从 C++ 转换到 Python。
68 | //! 将 C++ cv::Mat 对象转换到 tuple,参数2和参数3常忽略。
69 | static handle cast(const cv::Point& pt, return_value_policy, handle) {
70 | return py::make_tuple(pt.x, pt.y).release();
71 | }
72 | };
73 | #endif
74 |
75 | // (2) 矩形与元组转换 cv::Rect <=> tuple(x,y,w,h)
76 | template<>
77 | struct type_caster {
78 |
79 | PYBIND11_TYPE_CASTER(cv::Rect, _("tuple_xywh"));
80 |
81 | bool load(handle obj, bool) {
82 | if (!py::isinstance(obj)) {
83 | std::logic_error("Rect should be a tuple!");
84 | return false;
85 | }
86 |
87 | py::tuple rect = reinterpret_borrow(obj);
88 | if (rect.size() != 4) {
89 | std::logic_error("Rect (x,y,w,h) tuple should be size of 4");
90 | return false;
91 | }
92 |
93 | value = cv::Rect(rect[0].cast(), rect[1].cast(), rect[2].cast(), rect[3].cast());
94 | return true;
95 | }
96 |
97 | static handle cast(const cv::Rect& rect, return_value_policy, handle) {
98 | return py::make_tuple(rect.x, rect.y, rect.width, rect.height).release();
99 | }
100 | };
101 |
102 | // (3) cv::Mat 与 numpy.ndarray 之间转换
103 | // Python 支持一种通用的插件间数据交换缓冲区协议(buffer protocol)。
104 | // 让类型暴露一个缓冲区视图(buffer view), 这样可以对原始内部数据进行直接访问,常用于矩阵类型。
105 | //
106 | //Pybind11 提供了 py::buffer_info 类型,来映射 Python 缓冲区协议(buffer protocol)。
107 | //
108 | //struct buffer_info {
109 | // void* ptr; /* Pointer to buffer */
110 | // ssize_t itemsize; /* Size of one scalar */
111 | // std::string format; /* Python struct-style format descriptor */
112 | // ssize_t ndim; /* Number of dimensions */
113 | // std::vector shape; /* Buffer dimensions */
114 | // std::vector strides; /* Strides (in bytes) for each index */
115 | //};
116 |
117 | template<>
118 | struct type_caster {
119 | public:
120 |
121 | PYBIND11_TYPE_CASTER(cv::Mat, _("numpy.ndarray"));
122 |
123 | //! 1. cast numpy.ndarray to cv::Mat
124 | bool load(handle obj, bool) {
125 | array b = reinterpret_borrow(obj);
126 | buffer_info info = b.request();
127 |
128 | int nh = 1;
129 | int nw = 1;
130 | int nc = 1;
131 | int ndims = info.ndim;
132 | if (ndims == 2) {
133 | nh = info.shape[0];
134 | nw = info.shape[1];
135 | }
136 | else if (ndims == 3) {
137 | nh = info.shape[0];
138 | nw = info.shape[1];
139 | nc = info.shape[2];
140 | }
141 | else {
142 | throw std::logic_error("Only support 2d, 2d matrix");
143 | return false;
144 | }
145 |
146 | int dtype;
147 | if (info.format == format_descriptor::format()) {
148 | dtype = CV_8UC(nc);
149 | }
150 | else if (info.format == format_descriptor::format()) {
151 | dtype = CV_32SC(nc);
152 | }
153 | else if (info.format == format_descriptor::format()) {
154 | dtype = CV_32FC(nc);
155 | }
156 | else {
157 | throw std::logic_error("Unsupported type, only support uchar, int32, float");
158 | return false;
159 | }
160 | value = cv::Mat(nh, nw, dtype, info.ptr);
161 | return true;
162 | }
163 |
164 | //! 2. cast cv::Mat to numpy.ndarray
165 | static handle cast(const cv::Mat& mat, return_value_policy, handle defval) {
166 | std::string format = format_descriptor::format();
167 | size_t elemsize = sizeof(unsigned char);
168 | int nw = mat.cols;
169 | int nh = mat.rows;
170 | int nc = mat.channels();
171 | int depth = mat.depth();
172 | int type = mat.type();
173 | int dim = (depth == type) ? 2 : 3;
174 | if (depth == CV_8U) {
175 | format = format_descriptor::format();
176 | elemsize = sizeof(unsigned char);
177 | }
178 | else if (depth == CV_32S) {
179 | format = format_descriptor::format();
180 | elemsize = sizeof(int);
181 | }
182 | else if (depth == CV_32F) {
183 | format = format_descriptor::format();
184 | elemsize = sizeof(float);
185 | }
186 | else {
187 | throw std::logic_error("Unsupport type, only support uchar, int32, float");
188 | }
189 |
190 | std::vector bufferdim;
191 | std::vector strides;
192 | if (dim == 2) {
193 | bufferdim = { (size_t)nh, (size_t)nw };
194 | strides = { elemsize * (size_t)nw, elemsize };
195 | }
196 | else if (dim == 3) {
197 | bufferdim = { (size_t)nh, (size_t)nw, (size_t)nc };
198 | strides = { (size_t)elemsize * nw * nc, (size_t)elemsize * nc, (size_t)elemsize };
199 | }
200 | return array(buffer_info(mat.data, elemsize, format, dim, bufferdim, strides)).release();
201 | }
202 | };
203 | }
204 | }//!
205 |
--------------------------------------------------------------------------------
/cpp/libunet/libcv.vcxitems:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
5 | true
6 | {cedae11d-b646-4a10-8fc0-9d68a54864b7}
7 |
8 |
9 |
10 | %(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory)
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/cpp/libunet/libunet.vcxitems:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
5 | true
6 | {46a879d4-4ac4-4fc0-9307-76caf8b8af5c}
7 |
8 |
9 |
10 | %(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory)
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/cpp/libunet/unet.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/cpp/libunet/unet.cpp
--------------------------------------------------------------------------------
/cpp/libunet/unet.h:
--------------------------------------------------------------------------------
1 | // Created by ausk @ 2020.03.01
2 |
3 | #pragma once
4 | #include "ai.h"
5 |
6 | class UNetInfer {
7 | IAI* punet;
8 | std::string model_xmlfpath;
9 |
10 | public:
11 |
12 | UNetInfer(const std::string& xmlfpath){
13 | model_xmlfpath = xmlfpath;
14 | punet = nullptr;
15 | init_UNet();
16 | }
17 |
18 | ~UNetInfer() {
19 | destroy_UNet();
20 | }
21 |
22 | cv::Mat operator()(const cv::Mat& src) {
23 | return run_UNet(src);
24 | }
25 |
26 | void init_UNet();
27 | void destroy_UNet();
28 | cv::Mat run_UNet(const cv::Mat&);
29 | };
30 |
--------------------------------------------------------------------------------
/cpp/libunet/unetbind.cpp:
--------------------------------------------------------------------------------
1 | // Created by ausk @ 2020.03.01
2 |
3 | #include "unetbind.h"
4 |
5 | void bind_UNet(py::module& m){
6 | py::class_(m, "UNetInfer")
7 | .def(py::init())
8 | .def("__call__", &UNetInfer::operator())
9 | .def("init_UNet", &UNetInfer::init_UNet)
10 | .def("destroy_UNet", &UNetInfer::destroy_UNet);
11 | }
--------------------------------------------------------------------------------
/cpp/libunet/unetbind.h:
--------------------------------------------------------------------------------
1 | // Created by ausk @ 2020.03.01
2 |
3 | #pragma once
4 | #include "cvbind.h"
5 | #include "unet.h"
6 |
7 | namespace py = pybind11;
8 |
9 | void bind_UNet(py::module& m);
10 |
--------------------------------------------------------------------------------
/cpp/pycv/dllmain.cpp:
--------------------------------------------------------------------------------
1 | // Created by ausk @ 2019.11.23
2 | // Modified by ausk @ 2020.03.01
3 |
4 | #include "cvbind.h"
5 | #include "unetbind.h"
6 |
7 | namespace py = pybind11;
8 |
9 | PYBIND11_MODULE(pycv, m) {
10 | m.doc() = "pycv: A binding created using pybind11";
11 | bind_CV(m);
12 | bind_UNet(m);
13 | };
14 |
--------------------------------------------------------------------------------
/cpp/pycv/pycv.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | x64
7 |
8 |
9 | Release
10 | x64
11 |
12 |
13 |
14 | 16.0
15 | {673832B6-F4E7-4CD7-88DC-B5A978DEFE32}
16 | Win32Proj
17 | pycv
18 | 10.0
19 | pycv
20 |
21 |
22 |
23 | DynamicLibrary
24 | true
25 | v142
26 | Unicode
27 |
28 |
29 | DynamicLibrary
30 | false
31 | v142
32 | true
33 | MultiByte
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 | false
53 | .pyd
54 | D:\Programs\Python\Python37\include;$(VC_IncludePath);$(WindowsSDK_IncludePath);
55 |
56 |
57 | true
58 |
59 |
60 |
61 | NotUsing
62 | Level3
63 | true
64 | true
65 | false
66 | NDEBUG;PYCV_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)
67 | true
68 | pch.h
69 |
70 |
71 | Windows
72 | true
73 | true
74 | true
75 | false
76 | D:\Programs\Python\Python37\libs;$(SolutionDir)\thirdparty\openvino2020r1\lib;$(SolutionDir)\thirdparty\opencv420\lib;%(AdditionalLibraryDirectories)
77 | python37.lib;inference_engine.lib;opencv_core420.lib;opencv_highgui420.lib;opencv_imgproc420.lib;opencv_imgcodecs420.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
78 |
79 |
80 |
81 |
82 | Use
83 | Level3
84 | true
85 | _DEBUG;PYCV_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)
86 | true
87 | pch.h
88 |
89 |
90 | Windows
91 | true
92 | false
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
--------------------------------------------------------------------------------
/cpp/pycv/pycv.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/cpp/pycv/pycv.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/cpp/pycv/result.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/cpp/pycv/result.png
--------------------------------------------------------------------------------
/cpp/pycv/test_pycv.py:
--------------------------------------------------------------------------------
1 | # 2019/11/23 初始版本
2 | # 2020/03/01 增加 unet 推理功能
3 | # Created by ausk @ 2019.11.23
4 | # Modified by ausk @ 2020.03.01
5 |
6 | # (1) https://pybind11.readthedocs.io/en/master/basics.html
7 | # (2) [191123 使用 Pybind11 和 OpenCV 创建 Python 库](https://zhuanlan.zhihu.com/p/93299698)
8 |
9 | import os, sys
10 | curdir = os.path.dirname(os.path.abspath(__file__))
11 |
12 | sys.path.append(os.path.join(curdir, "../x64/Release"))
13 | import pycv
14 | import numpy as np
15 |
16 | # 测试绑定的 OpenCV
17 | def test_cv():
18 | print("\n===== Test pybind11 for OpenCV =====")
19 | # 矩阵相加
20 | a = np.random.random((2,3)).astype(np.float32)
21 | b = np.ones((2,3)).astype(np.float32)
22 | c = pycv.addmat(a, b)
23 | print("a = \n", a)
24 | print("b = \n", b)
25 | print("c = \n", c)
26 |
27 | # 点相加
28 | a = (1,2)
29 | b = (3,-1)
30 | c = pycv.addpt(a, b)
31 | print("{} + {} = {}".format(a, b, c))
32 |
33 | # 矩形相加
34 | a = (10, 20, 20, 10) # lt(10, 20) => rb(30, 30)
35 | b = (5, 5, 20, 20) # lt(5, 5) => rb(25, 25)
36 | c = pycv.addrect(a, b) # lt(5, 5) => rb(30, 30) => wh(25, 25)
37 | print("a = \n", a)
38 | print("b = \n", b)
39 | print("c = \n", c)
40 | print("===== Finish =====\n")
41 |
42 | # 测试使用 pybind11 绑定的 OpenVino+OpenCV 实现 UNet 推理
43 | def test_unet():
44 | print("\n===== Test pybind11 for UNet (OpenCV+OpenVino) =====")
45 | xmlfpath = "../unet/unet.xml"
46 | imgfpath = "../unet/test.png"
47 |
48 | img = pycv.imread(imgfpath)
49 | unet = pycv.UNetInfer(xmlfpath)
50 | res = unet(img)
51 | pycv.imwrite("result.png", res)
52 | print("Saved into result.png")
53 | print("===== Finish =====\n")
54 |
55 | if __name__ == "__main__":
56 | test_cv()
57 | test_unet()
58 | print("\nDone!")
--------------------------------------------------------------------------------
/cpp/thirdparty:
--------------------------------------------------------------------------------
1 | D:/Programs/intel/install/thirdparty
--------------------------------------------------------------------------------
/cpp/unet.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.29806.167
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "unet", "unet\unet.vcxproj", "{8D0021F7-D7A7-47F5-8851-04EF8035533A}"
7 | EndProject
8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libunet", "libunet\libunet.vcxitems", "{46A879D4-4AC4-4FC0-9307-76CAF8B8AF5C}"
9 | EndProject
10 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pycv", "pycv\pycv.vcxproj", "{673832B6-F4E7-4CD7-88DC-B5A978DEFE32}"
11 | EndProject
12 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libcv", "libunet\libcv.vcxitems", "{CEDAE11D-B646-4A10-8FC0-9D68A54864B7}"
13 | EndProject
14 | Global
15 | GlobalSection(SharedMSBuildProjectFiles) = preSolution
16 | libunet\libunet.vcxitems*{46a879d4-4ac4-4fc0-9307-76caf8b8af5c}*SharedItemsImports = 9
17 | libunet\libcv.vcxitems*{673832b6-f4e7-4cd7-88dc-b5a978defe32}*SharedItemsImports = 4
18 | libunet\libunet.vcxitems*{673832b6-f4e7-4cd7-88dc-b5a978defe32}*SharedItemsImports = 4
19 | libunet\libunet.vcxitems*{8d0021f7-d7a7-47f5-8851-04ef8035533a}*SharedItemsImports = 4
20 | libunet\libcv.vcxitems*{cedae11d-b646-4a10-8fc0-9d68a54864b7}*SharedItemsImports = 9
21 | EndGlobalSection
22 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
23 | Debug|x64 = Debug|x64
24 | Debug|x86 = Debug|x86
25 | Release|x64 = Release|x64
26 | Release|x86 = Release|x86
27 | EndGlobalSection
28 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
29 | {8D0021F7-D7A7-47F5-8851-04EF8035533A}.Debug|x64.ActiveCfg = Debug|x64
30 | {8D0021F7-D7A7-47F5-8851-04EF8035533A}.Debug|x64.Build.0 = Debug|x64
31 | {8D0021F7-D7A7-47F5-8851-04EF8035533A}.Debug|x86.ActiveCfg = Debug|x64
32 | {8D0021F7-D7A7-47F5-8851-04EF8035533A}.Release|x64.ActiveCfg = Release|x64
33 | {8D0021F7-D7A7-47F5-8851-04EF8035533A}.Release|x64.Build.0 = Release|x64
34 | {8D0021F7-D7A7-47F5-8851-04EF8035533A}.Release|x86.ActiveCfg = Release|x64
35 | {673832B6-F4E7-4CD7-88DC-B5A978DEFE32}.Debug|x64.ActiveCfg = Debug|x64
36 | {673832B6-F4E7-4CD7-88DC-B5A978DEFE32}.Debug|x64.Build.0 = Debug|x64
37 | {673832B6-F4E7-4CD7-88DC-B5A978DEFE32}.Debug|x86.ActiveCfg = Debug|x64
38 | {673832B6-F4E7-4CD7-88DC-B5A978DEFE32}.Release|x64.ActiveCfg = Release|x64
39 | {673832B6-F4E7-4CD7-88DC-B5A978DEFE32}.Release|x64.Build.0 = Release|x64
40 | {673832B6-F4E7-4CD7-88DC-B5A978DEFE32}.Release|x86.ActiveCfg = Release|x64
41 | EndGlobalSection
42 | GlobalSection(SolutionProperties) = preSolution
43 | HideSolutionNode = FALSE
44 | EndGlobalSection
45 | GlobalSection(ExtensibilityGlobals) = postSolution
46 | SolutionGuid = {2C3C6793-7FC0-45F9-91A4-E1F641DD1500}
47 | EndGlobalSection
48 | EndGlobal
49 |
--------------------------------------------------------------------------------
/cpp/unet/main.cpp:
--------------------------------------------------------------------------------
1 | // Created by ausk @ 2020.03.01
2 |
3 | #include "unet.h"
4 |
5 | int main(int argc, char* argv[]) {
6 | printf("Usage: xxx unet.xml test.png\n");
7 | printf("Start test...");
8 | std::string xmlfpath = "unet.xml";
9 | std::string imgfpath = "test.png";
10 | if(argc==3){
11 | xmlfpath = argv[1];
12 | imgfpath = argv[2];
13 | }
14 |
15 | auto infer = UNetInfer(xmlfpath);
16 | cv::Mat img = cv::imread(imgfpath);
17 | cv::Mat prob = infer(img);
18 | cv::imwrite("result.png", prob);
19 |
20 | printf("Great Job!\n");
21 | return 0;
22 | }
--------------------------------------------------------------------------------
/cpp/unet/result.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/cpp/unet/result.png
--------------------------------------------------------------------------------
/cpp/unet/test.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/cpp/unet/test.png
--------------------------------------------------------------------------------
/cpp/unet/unet.mapping:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
11 |
15 |
19 |
23 |
27 |
31 |
35 |
39 |
43 |
47 |
51 |
55 |
59 |
63 |
67 |
71 |
75 |
79 |
83 |
87 |
91 |
95 |
99 |
103 |
107 |
111 |
115 |
119 |
123 |
127 |
131 |
135 |
139 |
143 |
147 |
151 |
155 |
159 |
163 |
167 |
171 |
175 |
179 |
183 |
187 |
191 |
195 |
199 |
203 |
207 |
211 |
215 |
219 |
223 |
227 |
231 |
235 |
239 |
243 |
247 |
251 |
255 |
259 |
263 |
267 |
271 |
275 |
279 |
283 |
287 |
291 |
295 |
299 |
303 |
307 |
311 |
315 |
319 |
323 |
327 |
331 |
335 |
339 |
343 |
347 |
351 |
355 |
359 |
360 |
--------------------------------------------------------------------------------
/cpp/unet/unet.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | x64
7 |
8 |
9 | Release
10 | x64
11 |
12 |
13 |
14 |
15 |
16 |
17 | 16.0
18 | {8D0021F7-D7A7-47F5-8851-04EF8035533A}
19 | unet
20 | 10.0
21 |
22 |
23 |
24 | Application
25 | true
26 | v142
27 | Unicode
28 |
29 |
30 | Application
31 | false
32 | v142
33 | true
34 | MultiByte
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 | true
53 |
54 |
55 | false
56 |
57 |
58 |
59 | Level3
60 | true
61 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
62 | true
63 |
64 |
65 | Console
66 | true
67 |
68 |
69 |
70 |
71 | Level3
72 | true
73 | true
74 | true
75 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
76 | true
77 |
78 |
79 | Console
80 | true
81 | true
82 | true
83 |
84 |
85 |
86 |
87 |
88 |
--------------------------------------------------------------------------------
/cpp/unet/unet.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/cpp/unet/unet.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/cpp/unet/unet.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
14 |
15 |
16 |
17 |
25 |
26 |
27 |
28 |
29 |
30 | 1
31 | 1
32 | 256
33 | 256
34 |
35 |
36 | 64
37 | 1
38 | 3
39 | 3
40 |
41 |
42 |
50 |
51 |
52 |
53 |
61 |
62 |
63 |
64 |
65 | 1
66 | 64
67 | 256
68 | 256
69 |
70 |
71 | 1
72 | 64
73 | 1
74 | 1
75 |
76 |
77 |
85 |
86 |
87 |
88 |
89 |
90 | 1
91 | 64
92 | 256
93 | 256
94 |
95 |
96 |
104 |
105 |
106 |
107 |
108 |
109 | 1
110 | 64
111 | 256
112 | 256
113 |
114 |
115 |
123 |
124 |
125 |
126 |
134 |
135 |
136 |
137 |
138 |
139 | 1
140 | 64
141 | 128
142 | 128
143 |
144 |
145 | 77
146 | 64
147 | 3
148 | 3
149 |
150 |
151 |
159 |
160 |
161 |
162 |
170 |
171 |
172 |
173 |
174 | 1
175 | 77
176 | 128
177 | 128
178 |
179 |
180 | 1
181 | 77
182 | 1
183 | 1
184 |
185 |
186 |
194 |
195 |
196 |
197 |
198 |
199 | 1
200 | 77
201 | 128
202 | 128
203 |
204 |
205 |
213 |
214 |
215 |
216 |
217 |
218 | 1
219 | 77
220 | 128
221 | 128
222 |
223 |
224 |
232 |
233 |
234 |
235 |
243 |
244 |
245 |
246 |
247 |
248 | 1
249 | 77
250 | 64
251 | 64
252 |
253 |
254 | 92
255 | 77
256 | 3
257 | 3
258 |
259 |
260 |
268 |
269 |
270 |
271 |
279 |
280 |
281 |
282 |
283 | 1
284 | 92
285 | 64
286 | 64
287 |
288 |
289 | 1
290 | 92
291 | 1
292 | 1
293 |
294 |
295 |
303 |
304 |
305 |
306 |
307 |
308 | 1
309 | 92
310 | 64
311 | 64
312 |
313 |
314 |
322 |
323 |
324 |
325 |
326 |
327 | 1
328 | 92
329 | 64
330 | 64
331 |
332 |
333 |
341 |
342 |
343 |
344 |
352 |
353 |
354 |
355 |
356 |
357 | 1
358 | 92
359 | 32
360 | 32
361 |
362 |
363 | 111
364 | 92
365 | 3
366 | 3
367 |
368 |
369 |
377 |
378 |
379 |
380 |
388 |
389 |
390 |
391 |
392 | 1
393 | 111
394 | 32
395 | 32
396 |
397 |
398 | 1
399 | 111
400 | 1
401 | 1
402 |
403 |
404 |
412 |
413 |
414 |
415 |
416 |
417 | 1
418 | 111
419 | 32
420 | 32
421 |
422 |
423 |
431 |
432 |
433 |
434 |
435 |
436 | 1
437 | 111
438 | 32
439 | 32
440 |
441 |
442 |
450 |
451 |
452 |
453 |
461 |
462 |
463 |
464 |
465 |
466 | 1
467 | 111
468 | 16
469 | 16
470 |
471 |
472 | 133
473 | 111
474 | 3
475 | 3
476 |
477 |
478 |
486 |
487 |
488 |
489 |
497 |
498 |
499 |
500 |
501 | 1
502 | 133
503 | 16
504 | 16
505 |
506 |
507 | 1
508 | 133
509 | 1
510 | 1
511 |
512 |
513 |
521 |
522 |
523 |
524 |
525 |
526 | 1
527 | 133
528 | 16
529 | 16
530 |
531 |
532 |
540 |
541 |
542 |
543 |
548 |
549 |
550 |
551 |
552 |
553 | 1
554 | 133
555 | 16
556 | 16
557 |
558 |
559 | 2
560 |
561 |
562 |
570 |
571 |
572 |
573 |
574 |
575 | 1
576 | 133
577 | 32
578 | 32
579 |
580 |
581 | 1
582 | 111
583 | 32
584 | 32
585 |
586 |
587 |
595 |
596 |
597 |
598 |
606 |
607 |
608 |
609 |
610 |
611 | 1
612 | 244
613 | 32
614 | 32
615 |
616 |
617 | 133
618 | 244
619 | 3
620 | 3
621 |
622 |
623 |
631 |
632 |
633 |
634 |
642 |
643 |
644 |
645 |
646 | 1
647 | 133
648 | 32
649 | 32
650 |
651 |
652 | 1
653 | 133
654 | 1
655 | 1
656 |
657 |
658 |
666 |
667 |
668 |
669 |
670 |
671 | 1
672 | 133
673 | 32
674 | 32
675 |
676 |
677 |
685 |
686 |
687 |
688 |
693 |
694 |
695 |
696 |
697 |
698 | 1
699 | 133
700 | 32
701 | 32
702 |
703 |
704 | 2
705 |
706 |
707 |
715 |
716 |
717 |
718 |
719 |
720 | 1
721 | 133
722 | 64
723 | 64
724 |
725 |
726 | 1
727 | 92
728 | 64
729 | 64
730 |
731 |
732 |
740 |
741 |
742 |
743 |
751 |
752 |
753 |
754 |
755 |
756 | 1
757 | 225
758 | 64
759 | 64
760 |
761 |
762 | 111
763 | 225
764 | 3
765 | 3
766 |
767 |
768 |
776 |
777 |
778 |
779 |
787 |
788 |
789 |
790 |
791 | 1
792 | 111
793 | 64
794 | 64
795 |
796 |
797 | 1
798 | 111
799 | 1
800 | 1
801 |
802 |
803 |
811 |
812 |
813 |
814 |
815 |
816 | 1
817 | 111
818 | 64
819 | 64
820 |
821 |
822 |
830 |
831 |
832 |
833 |
838 |
839 |
840 |
841 |
842 |
843 | 1
844 | 111
845 | 64
846 | 64
847 |
848 |
849 | 2
850 |
851 |
852 |
860 |
861 |
862 |
863 |
864 |
865 | 1
866 | 111
867 | 128
868 | 128
869 |
870 |
871 | 1
872 | 77
873 | 128
874 | 128
875 |
876 |
877 |
885 |
886 |
887 |
888 |
896 |
897 |
898 |
899 |
900 |
901 | 1
902 | 188
903 | 128
904 | 128
905 |
906 |
907 | 92
908 | 188
909 | 3
910 | 3
911 |
912 |
913 |
921 |
922 |
923 |
924 |
932 |
933 |
934 |
935 |
936 | 1
937 | 92
938 | 128
939 | 128
940 |
941 |
942 | 1
943 | 92
944 | 1
945 | 1
946 |
947 |
948 |
956 |
957 |
958 |
959 |
960 |
961 | 1
962 | 92
963 | 128
964 | 128
965 |
966 |
967 |
975 |
976 |
977 |
978 |
983 |
984 |
985 |
986 |
987 |
988 | 1
989 | 92
990 | 128
991 | 128
992 |
993 |
994 | 2
995 |
996 |
997 |
1005 |
1006 |
1007 |
1008 |
1016 |
1017 |
1018 |
1019 |
1020 |
1021 | 1
1022 | 92
1023 | 256
1024 | 256
1025 |
1026 |
1027 | 77
1028 | 92
1029 | 3
1030 | 3
1031 |
1032 |
1033 |
1041 |
1042 |
1043 |
1044 |
1052 |
1053 |
1054 |
1055 |
1056 | 1
1057 | 77
1058 | 256
1059 | 256
1060 |
1061 |
1062 | 1
1063 | 77
1064 | 1
1065 | 1
1066 |
1067 |
1068 |
1076 |
1077 |
1078 |
1079 |
1080 |
1081 | 1
1082 | 77
1083 | 256
1084 | 256
1085 |
1086 |
1087 |
1095 |
1096 |
1097 |
1098 |
1106 |
1107 |
1108 |
1109 |
1110 |
1111 | 1
1112 | 77
1113 | 256
1114 | 256
1115 |
1116 |
1117 | 64
1118 | 77
1119 | 3
1120 | 3
1121 |
1122 |
1123 |
1131 |
1132 |
1133 |
1134 |
1142 |
1143 |
1144 |
1145 |
1146 | 1
1147 | 64
1148 | 256
1149 | 256
1150 |
1151 |
1152 | 1
1153 | 64
1154 | 1
1155 | 1
1156 |
1157 |
1158 |
1166 |
1167 |
1168 |
1169 |
1170 |
1171 | 1
1172 | 64
1173 | 256
1174 | 256
1175 |
1176 |
1177 |
1185 |
1186 |
1187 |
1188 |
1196 |
1197 |
1198 |
1199 |
1200 |
1201 | 1
1202 | 64
1203 | 256
1204 | 256
1205 |
1206 |
1207 | 64
1208 | 64
1209 | 3
1210 | 3
1211 |
1212 |
1213 |
1221 |
1222 |
1223 |
1224 |
1232 |
1233 |
1234 |
1235 |
1236 | 1
1237 | 64
1238 | 256
1239 | 256
1240 |
1241 |
1242 | 1
1243 | 64
1244 | 1
1245 | 1
1246 |
1247 |
1248 |
1256 |
1257 |
1258 |
1259 |
1260 |
1261 | 1
1262 | 64
1263 | 256
1264 | 256
1265 |
1266 |
1267 |
1275 |
1276 |
1277 |
1278 |
1286 |
1287 |
1288 |
1289 |
1290 |
1291 | 1
1292 | 64
1293 | 256
1294 | 256
1295 |
1296 |
1297 | 1
1298 | 64
1299 | 1
1300 | 1
1301 |
1302 |
1303 |
1311 |
1312 |
1313 |
1314 |
1322 |
1323 |
1324 |
1325 |
1326 | 1
1327 | 1
1328 | 256
1329 | 256
1330 |
1331 |
1332 | 1
1333 | 1
1334 | 1
1335 | 1
1336 |
1337 |
1338 |
1346 |
1347 |
1348 |
1349 |
1350 | 1
1351 | 1
1352 | 256
1353 | 256
1354 |
1355 |
1356 |
1364 |
1365 |
1366 |
1367 |
1368 | 1
1369 | 1
1370 | 256
1371 | 256
1372 |
1373 |
1374 |
1375 |
1376 |
1377 |
1378 |
1379 |
1380 |
1381 |
1382 |
1383 |
1384 |
1385 |
1386 |
1387 |
1388 |
1389 |
1390 |
1391 |
1392 |
1393 |
1394 |
1395 |
1396 |
1397 |
1398 |
1399 |
1400 |
1401 |
1402 |
1403 |
1404 |
1405 |
1406 |
1407 |
1408 |
1409 |
1410 |
1411 |
1412 |
1413 |
1414 |
1415 |
1416 |
1417 |
1418 |
1419 |
1420 |
1421 |
1422 |
1423 |
1424 |
1425 |
1426 |
1427 |
1428 |
1429 |
1430 |
1431 |
1432 |
1433 |
1434 |
1435 |
1436 |
1437 |
1438 |
1439 |
1440 |
1441 |
1442 |
1443 |
1444 |
1445 |
1446 |
1447 |
1448 |
1449 |
1450 |
1451 |
1452 |
1453 |
1454 |
1455 |
1456 |
1457 |
1458 |
1459 |
1460 |
1461 |
1462 |
1463 |
1464 |
1465 |
1466 |
1467 |
1468 |
1469 |
1470 |
1471 |
1472 |
1473 |
1474 |
1475 |
1476 |
1477 |
1478 |
1479 |
1480 |
1481 |
1482 |
1483 |
1484 |
1485 |
1486 |
1487 |
1488 |
1489 |
1490 |
1491 |
1492 |
1493 |
1494 |
1495 |
1496 |
1497 |
1498 |
1499 |
1500 |
1501 |
1502 |
--------------------------------------------------------------------------------
/doc/0_pred.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/doc/0_pred.png
--------------------------------------------------------------------------------
/doc/0_test.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/doc/0_test.png
--------------------------------------------------------------------------------
/doc/u-net-architecture.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/doc/u-net-architecture.png
--------------------------------------------------------------------------------
/doc/unet-openvino.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/doc/unet-openvino.png
--------------------------------------------------------------------------------
/doc/unet-pycv.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/doc/unet-pycv.png
--------------------------------------------------------------------------------
/doc/unet_20200229.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/doc/unet_20200229.png
--------------------------------------------------------------------------------
/py3/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 jinlj(ausk)
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/py3/data.py:
--------------------------------------------------------------------------------
1 | # 2020/02/29 by ausk
2 |
3 | import os
4 | import cv2
5 | import numpy as np
6 | from keras.preprocessing.image import ImageDataGenerator
7 |
8 | def trainGenerator(batch_size,train_dpath,image_dname,mask_dname,aug_dict,
9 | image_color_mode = "grayscale", mask_color_mode = "grayscale",
10 | image_save_prefix = "image",mask_save_prefix = "mask",
11 | save_to_dir = None,target_size = (256,256),seed = 1):
12 |
13 | image_generator = ImageDataGenerator(**aug_dict).flow_from_directory(
14 | train_dpath,
15 | classes = [image_dname],
16 | class_mode = None,
17 | color_mode = image_color_mode,
18 | target_size = target_size,
19 | batch_size = batch_size,
20 | save_to_dir = save_to_dir,
21 | save_prefix = image_save_prefix,
22 | seed = seed)
23 |
24 | mask_generator = ImageDataGenerator(**aug_dict).flow_from_directory(
25 | train_dpath,
26 | classes = [mask_dname],
27 | class_mode = None,
28 | color_mode = mask_color_mode,
29 | target_size = target_size,
30 | batch_size = batch_size,
31 | save_to_dir = save_to_dir,
32 | save_prefix = mask_save_prefix,
33 | seed = seed)
34 |
35 | def preprocess(img,mask):
36 | if(np.max(img) > 1):
37 | img = img / 255
38 | mask = np.where(mask >127.5, 1.0, 0.0)
39 | return (img,mask)
40 |
41 | for (img,mask) in zip(image_generator, mask_generator):
42 | img,mask = preprocess(img,mask)
43 | yield (img,mask)
44 |
45 |
46 | #读图,灰度化,归一化,缩放,改变维度 [1, nh, nw, 1]
47 | def testGenerator(test_dpath, num_image = 30, input_shape = (256,256, 1)):
48 | for i in range(num_image):
49 | fpath = "{}/{}.png".format(test_dpath, i)
50 | img = cv2.imread(fpath, cv2.IMREAD_GRAYSCALE)
51 | nh, nw = img.shape[:2]
52 | if img.shape[0]>input_shape[0] or img.shape[1] > input_shape[1]:
53 | interpolation = cv2.INTER_AREA
54 | else:
55 | interpolation = cv2.INTER_CUBIC
56 | img = cv2.resize(img, (input_shape[1], input_shape[0]), interpolation = interpolation)
57 | img = np.float32(img)/255
58 | img = img[None, ..., None]
59 | yield img
60 |
61 | # 保存结果
62 | def saveResult(dpath, npyfile,num_class = 2, postfix=""):
63 | if not postfix:
64 | postfix = "_res"
65 | for i,item in enumerate(npyfile):
66 | img = np.clip(item[:,:,0]*255, 0, 255)
67 | fname = "{}{}.png".format(i, postfix)
68 | cv2.imwrite(os.path.join(dpath, fname),img)
69 |
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/0.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/0res.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/0res.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/1.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/10.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/11.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/11.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/12.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/12.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/13.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/13.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/14.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/14.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/15.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/15.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/16.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/17.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/17.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/18.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/18.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/19.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/19.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/2.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/20.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/20.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/21.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/21.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/22.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/22.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/23.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/23.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/24.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/24.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/25.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/25.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/26.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/26.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/27.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/27.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/28.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/28.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/29.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/29.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/3.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/4.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/5.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/6.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/7.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/8.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/test/9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/test/9.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/0.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/1.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/10.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/11.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/11.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/12.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/12.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/13.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/13.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/14.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/14.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/15.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/15.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/16.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/17.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/17.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/18.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/18.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/19.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/19.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/2.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/20.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/20.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/21.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/21.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/22.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/22.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/23.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/23.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/24.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/24.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/25.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/25.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/26.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/26.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/27.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/27.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/28.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/28.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/29.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/29.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/3.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/4.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/5.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/6.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/7.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/8.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/image/9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/image/9.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/0.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/1.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/10.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/11.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/11.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/12.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/12.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/13.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/13.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/14.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/14.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/15.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/15.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/16.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/17.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/17.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/18.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/18.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/19.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/19.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/2.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/20.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/20.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/21.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/21.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/22.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/22.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/23.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/23.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/24.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/24.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/25.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/25.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/26.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/26.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/27.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/27.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/28.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/28.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/29.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/29.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/3.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/4.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/5.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/6.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/7.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/8.png
--------------------------------------------------------------------------------
/py3/dataset/membrane/train/label/9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/dataset/membrane/train/label/9.png
--------------------------------------------------------------------------------
/py3/demo.py:
--------------------------------------------------------------------------------
1 | # 2020/02/29 by ausk
2 |
3 | import warnings
4 | warnings.filterwarnings("ignore")
5 |
6 | import os, sys
7 | import argparse
8 |
9 | import numpy as np
10 | import cv2
11 |
12 | import tensorflow as tf
13 | from tensorflow.keras import backend as K
14 | #from keras.losses import *
15 | from tensorflow.keras.optimizers import Adam
16 | from tensorflow.keras import models
17 | from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler
18 |
19 | from data import trainGenerator,testGenerator, saveResult
20 | from model import myunet, UNetH5, UNetPB, UNetTfLite
21 | import utils
22 |
23 |
24 | class cfg:
25 | losses = {
26 | #"dice": dice_coef_loss,
27 | "bce": "binary_crossentropy",
28 | }
29 | loss_name = "bce"
30 |
31 | custom_objects = {} # {"dice_coef_loss": dice_coef_loss}
32 | train_dpath = "dataset/membrane/train/"
33 | test_dpath = "dataset/membrane/test/"
34 |
35 | model_dpath = "models/{}".format(loss_name)
36 | pretrained_fpath = "{}/myunet_final.h5".format(model_dpath)
37 | mfpath = model_dpath + '/ep{epoch:03d}-loss{loss:.3f}-acc{accuracy:.3f}.h5'
38 |
39 | input_size = (256,256,1)
40 | epochs = 20
41 | tag = "myunet"
42 | prefix = "{}_".format(tag) if tag else ""
43 | postfix = "_{}".format(tag) if tag else ""
44 |
45 | @classmethod
46 | def setparam(cls, tag, loss_name, lr=1E-4):
47 | cls.tag=tag
48 | cls.prefix = "{}_".format(tag) if tag else ""
49 | cls.postfix = "_{}".format(tag) if tag else ""
50 | cls.loss_name = loss_name
51 | cls.loss = cls.losses.get(loss_name)
52 | cls.model_dpath = "models/{}{}".format(cls.prefix, loss_name)
53 | utils.mksured(cls.model_dpath)
54 | cls.pretrained_fpath = "{}/myunet_final.h5".format(cls.model_dpath)
55 | cls.mfpath = cls.model_dpath + '/ep{epoch:03d}-loss{loss:.3f}-acc{accuracy:.3f}.h5'
56 | cls.lr=lr
57 |
58 | @classmethod
59 | def get_lastepoch(cls):
60 | try:
61 | fpaths = glob.glob(cls.model_dpath+"/ep*.h5")
62 | print(fpaths)
63 | last_epoch = sorted(int(fpath.split("/")[-1][2:5]) for fpath in fpaths)[-1]
64 | except:
65 | last_epoch = 0
66 | return last_epoch
67 |
68 |
69 | # 训练 UNet
70 | def train_unet(cfg, resume=True):
71 | input_size = cfg.input_size
72 | pretrained_fpath = cfg.pretrained_fpath
73 | model_dpath = cfg.model_dpath
74 | loss = cfg.loss
75 | train_dpath = cfg.train_dpath
76 | lr = cfg.lr
77 | epochs = cfg.epochs
78 | mfpath = cfg.mfpath #model_dpath + '/ep{epoch:03d}-loss{loss:.3f}-acc{accuracy:.3f}.h5'
79 | last_epoch = cfg.get_lastepoch()
80 | print(last_epoch)
81 |
82 | # (1) 创建训练生成器
83 | augdict = dict(rotation_range=0.2,
84 | width_shift_range=0.05,
85 | height_shift_range=0.05,
86 | shear_range=0.05,
87 | zoom_range=0.05,
88 | horizontal_flip=True,
89 | fill_mode='nearest')
90 | train_gen = trainGenerator(4, train_dpath,'image','label',augdict, save_to_dir = None)
91 |
92 | if False:
93 | for i,batch in enumerate(train_gen):
94 | if i >= 3:
95 | break
96 |
97 | # (2) 编译和训练模型, (可选地加载预训练模型,继续训练)
98 | model = myunet(input_size)
99 | model.compile(optimizer = Adam(lr =lr), loss = loss, metrics = ['accuracy'])
100 | #model.summary()
101 |
102 | # 加载预训练模型
103 | if resume and (pretrained_fpath) and os.path.exists(pretrained_fpath):
104 | try:
105 | model.load_weights(pretrained_fpath)
106 | except Exception as exc:
107 | print("Exception: {}".format(exc))
108 |
109 | # 训练模型
110 | model_checkpoint = ModelCheckpoint(mfpath, monitor='loss', verbose=1, save_best_only=True)
111 | model_tb = keras.callbacks.TensorBoard(log_dir=model_dpath, histogram_freq=0, write_graph=False, write_images=False)
112 | model.fit_generator(train_gen, steps_per_epoch=1000, epochs=epochs, callbacks=[model_checkpoint, model_tb], initial_epoch=last_epoch)
113 | model.save(pretrained_fpath)
114 |
115 | # 测试 UNet
116 | def test_unet( cfg ):
117 | pretrained_fpath = cfg.pretrained_fpath
118 | test_dpath = cfg.test_dpath
119 | postfix = cfg.postfix
120 |
121 | tf.keras.backend.set_learning_phase(0)
122 | K.set_learning_phase(0)
123 |
124 | # 从文件中加载模型
125 | print("Loading: {}".format(pretrained_fpath))
126 | #model = models.load_model(pretrained_fpath, custom_objects=custom_objects)
127 | model = UNetH5(pretrained_fpath, (256, 256, 1))
128 |
129 | # 创建数据生成器,预测并保存结果
130 | test_gen = testGenerator(test_dpath)
131 | results = model.predict_generator(test_gen,30,verbose=1)
132 | saveResult(test_dpath,results, postfix=postfix)
133 |
134 |
135 | # 保存为 PB 文件
136 | def run_savepb(h5fpath, pbfpath=None):
137 | if pbfpath is None:
138 | pbfpath = h5fpath[:-2] + "pb"
139 | #K.clear_session()
140 | #K.set_session(tf.Session())
141 | print(h5fpath)
142 | sess = tf.Session()
143 | with sess.as_default():
144 | model = models.load_model(h5fpath)
145 | #model.load_weights(h5fpath, by_name=True)
146 | utils.savepb(model, sess, pbname = pbfpath)
147 |
148 | def test_loadpb(pbfpath):
149 | unet = UNetPB(pbfpath, (256, 256, 1))
150 | for i in range(30):
151 | fpath = "dataset/membrane/test/{}.png".format(i)
152 | fpath2 = "dataset/membrane/test/{}_pb.png".format(i)
153 | img = cv2.imread(fpath)
154 | print(fpath, img.shape)
155 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
156 |
157 | with utils.timeit():
158 | result = unet.predict(gray)
159 |
160 | print(result.min(), result.max(), result.shape)
161 | cv2.imwrite(fpath2, result)
162 |
163 | def run_savelite(h5fpath, litefpath=None):
164 | if litefpath is None:
165 | litefpath = h5fpath[:-2] + "lite"
166 | utils.keras2tflite(h5fpath, litefpath)
167 |
168 | def test_loadlite(litefpath):
169 | unet = UNetTfLite(litefpath)
170 | for i in range(30):
171 | fpath = "dataset/membrane/test/{}.png".format(i)
172 | fpath2 = "dataset/membrane/test/{}_pb.png".format(i)
173 | img = cv2.imread(fpath)
174 | print(fpath, img.shape)
175 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
176 |
177 | with utils.timeit():
178 | result = unet.predict(gray)
179 |
180 |
181 | def get_args():
182 | #tag = "myunet"
183 | parser = argparse.ArgumentParser(description="Train/Test Unet-like CNN")
184 | parser.add_argument("--devices", "-d", default="0", type=str)
185 | parser.add_argument("--op", default="test", type=str)
186 |
187 | args = parser.parse_args(sys.argv[1:])
188 | assert args.op in ("train", "test", "savepb", "testpb", "savelite", "testlite")
189 | return args
190 |
191 |
192 | if __name__ == "__main__":
193 | args = get_args()
194 | devices = args.devices
195 | op = args.op
196 |
197 | # (0) 设置参数
198 | #assert tag in ("myunet", )
199 | os.environ["CUDA_VISIBLE_DEVICES"] = devices
200 | cfg.setparam(tag="myunet",loss_name="bce", lr=1E-4)
201 |
202 | # (1) 训练
203 | if op == "train":
204 | train_unet(cfg)
205 |
206 | # (2) 测试
207 | if op == "test":
208 | cfg.pretrained_fpath = "models/myunet_64_1.2.h5"
209 | test_unet(cfg)
210 |
211 | # (3) 转 pb 模型
212 | if op == "savepb":
213 | h5fpath = cfg.pretrained_fpath
214 | h5fpath = "models/myunet_64_1.2.h5"
215 | pbfpath = "models/myunet_64_1.2.pb"
216 | run_savepb(h5fpath)
217 |
218 | # (4) 测试 pb 模型
219 | if op == "testpb":
220 | pbfpath = "models/myunet_64_1.2.pb"
221 | test_loadpb(pbfpath)
222 |
223 | # (5) 测试保存 lite
224 | if op == "savelite":
225 | h5fpath = "models/myunet_64_1.2.h5"
226 | run_savelite(h5fpath)
227 |
228 | # (6) 测试 tflite
229 | if op == "testlite":
230 | litefpath = "models/myunet_64_1.2.tflite"
231 | test_loadlite(litefpath)
232 |
--------------------------------------------------------------------------------
/py3/model.py:
--------------------------------------------------------------------------------
1 | # 2020/02/29 by ausk
2 |
3 | import utils
4 | import warnings
5 | warnings.filterwarnings("ignore")
6 |
7 | import os, sys, time
8 | import numpy as np
9 | import cv2
10 |
11 | import tensorflow as tf
12 | from tensorflow.keras import layers, models
13 | from tensorflow.keras import backend as K
14 |
15 | # 处理块的名字
16 | def handle_block_names(prefix, stage):
17 | name = "{}_{}".format(prefix, stage)
18 | conv_name = '{}_conv'.format(name)
19 | bn_name = '{}_bn'.format(name)
20 | relu_name = '{}_relu'.format(name)
21 | return conv_name, bn_name, relu_name
22 |
23 | # 默认的 ConvReLU6
24 | def ConvReLU( nfilter, kernel_size, prefix="block", stage=1, use_bn=False):
25 | conv_name, bn_name, relu_name = handle_block_names(prefix, stage)
26 | def layer(x):
27 | conv_name, bn_name, relu_name = handle_block_names(prefix, stage)
28 | x = layers.Conv2D( nfilter, kernel_size, padding="same", kernel_initializer = 'glorot_uniform', activation = None, name=conv_name, use_bias=not(use_bn))(x)
29 | if use_bn:
30 | x = layers.BatchNormalization(epsilon=1e-3, momentum=0.999, name=bn_name)(x)
31 | #x = layers.Activation('relu', name=relu_name)(x)
32 | x = layers.ReLU(6.0, name=relu_name)(x)
33 | return x
34 | return layer
35 |
36 | # 编码模块
37 | def EncoderBlock( nfilter, stage, kernel_size=(3,3), use_bn=False, use_drop=False, use_pool = True):
38 | prefix = "encoder"
39 | def layer(x):
40 | if use_pool:
41 | x = layers.MaxPooling2D(pool_size=(2, 2))(x)
42 | x = ConvReLU( nfilter, kernel_size, use_bn=use_bn, prefix=prefix, stage = str(stage)+"1")(x)
43 | #x = ConvReLU( nfilter, kernel_size, use_bn=use_bn, prefix=prefix, stage = str(stage)+"2")(x)
44 | if use_drop:
45 | x = layers.Dropout(0.5)(x)
46 | return x
47 | return layer
48 |
49 | # 解码模块
50 | def DecoderBlock( nfilter, stage, kernel_size=(3,3), use_bn=False, use_up=True, skip=None):
51 | prefix = "decoder"
52 | def layer(x):
53 | nonlocal skip
54 | if use_up:
55 | x = layers.UpSampling2D(size=(2,2), name="{}_{}_up".format(prefix, str(stage)))(x)
56 | #x = ConvReLU( nfilter, kernel_size, use_bn=use_bn, prefix=prefix, stage = str(stage)+"0")(x)
57 | if skip is not None:
58 | x = layers.Concatenate()([x, skip])
59 | x = ConvReLU( nfilter, kernel_size, use_bn=use_bn, prefix=prefix, stage = str(stage)+"1")(x)
60 | #x = ConvReLU( nfilter, kernel_size, use_bn=use_bn, prefix=prefix, stage = str(stage)+"2")(x)
61 | return x
62 | return layer
63 |
64 | # 搭建 UNet
65 | def myunet(input_size=(256, 256, 1), nfilter0=64,ratio=1.2):
66 | inputs = layers.Input(input_size)
67 | stages = 5
68 | nfilters = [int(nfilter0 * (ratio**i) + 0.5) for i in range(stages)]
69 | feats = []
70 | x = inputs
71 | for stage in range(1, stages+1):
72 | nfilter = nfilters[stage-1]
73 | use_drop = stage > 4
74 | use_pool = stage > 1
75 | use_bn = True #stage > 1
76 | x = EncoderBlock( nfilter, stage, kernel_size=(3,3), use_bn=use_bn, use_drop=use_drop, use_pool=use_pool)(x)
77 | feats.append(x)
78 |
79 | for stage in range(stages, 0, -1):
80 | nfilter = nfilters[stage-1]
81 | use_drop = False
82 | use_bn = True #stage > 1
83 | use_up = stage > 1
84 | skip = feats[stage-2] if stage>2 else None
85 | x = DecoderBlock( nfilter, stage, kernel_size=(3,3), use_bn=use_bn, use_up=use_up, skip=skip)(x)
86 |
87 | x = ConvReLU(nfilter0, kernel_size=(3,3), prefix="final", stage=1, use_bn=True)(x)
88 | x = layers.Conv2D(1, 1, activation = 'sigmoid', name="final")(x)
89 | model = models.Model(inputs = inputs, outputs = x)
90 | model.summary()
91 | return model
92 |
93 | # UNet in H5
94 | class UNetH5:
95 | def __init__(self, h5fpath, input_shape):
96 | assert len(input_shape)==2 or input_shape[2] == 1
97 | self.input_shape = input_shape
98 | self.input_size = (int(input_shape[1]), int(input_shape[0]))
99 | tf.keras.backend.set_learning_phase(0)
100 | K.set_learning_phase(0)
101 | self.model = models.load_model(h5fpath, custom_objects={})
102 |
103 | def predict_generator(self, test_gen, batch_size, verbose=0):
104 | return self.model.predict_generator(test_gen, batch_size, verbose=verbose)
105 |
106 | def predict(self, img):
107 | #assert img.shape == input_shape, "Make sure you have input {} ndarray"
108 | nh, nw = img.shape[:2]
109 | if img.ndim == 3 and img.shape[-1] !=1 :
110 | img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
111 | if img.shape[:2] != self.input_shape[:2]:
112 | img = cv2.resize(img, self.input_size)
113 | ximg = (np.float32(img)/255)[None, ..., None]
114 |
115 | xout = self.model.predict(ximg)[0] #, ..., 0]
116 | xout = np.clip(xout * 255, 0, 255).astype(np.uint8)
117 | return xout
118 |
119 | # UNet in PB
120 | class UNetPB:
121 | def __init__(self, pbfpath, input_shape):
122 | self.input_shape = input_shape
123 | self.pbfpath = pbfpath
124 | self.graph = utils.loadGraph(pbfpath)
125 | self.input_node = self.graph.get_tensor_by_name("input_1:0")
126 | self.output_node = self.graph.get_tensor_by_name("output_0:0")
127 | self.session = tf.Session(graph=self.graph)
128 |
129 | def predict(self, img):
130 | nh,nw = self.input_shape[:2]
131 | img = cv2.resize(img, (nw, nh))
132 |
133 | if img.ndim ==2:
134 | ximg = img[None, ..., None]
135 | else:
136 | ximg = img[None, ...]
137 | ximg = np.float32(ximg)/255.0
138 |
139 | xout = self.session.run(self.output_node, feed_dict={self.input_node: ximg})[0]
140 | xout = np.clip(xout * 255, 0, 255).astype(np.uint8)
141 | return xout
142 |
143 | class UNetTfLite:
144 | def __init__(self, litefpath):
145 | self.interpreter = tf.lite.Interpreter(litefpath)
146 | self.interpreter.allocate_tensors()
147 |
148 | def predict(self, img=None):
149 | input_details = self.interpreter.get_input_details()
150 | output_details = self.interpreter.get_output_details()
151 |
152 | _, nh, nw, _ = input_details[0]['shape']
153 | img = cv2.resize(img, (nw, nh))
154 |
155 | if img.ndim ==2:
156 | ximg = img[None, ..., None]
157 | else:
158 | ximg = img[None, ...]
159 | ximg = np.float32(ximg)/255.0
160 |
161 | self.interpreter.set_tensor(input_details[0]["index"], ximg)
162 | self.interpreter.invoke()
163 | xout = self.interpreter.get_tensor(output_details[0]["index"])[0,:,:,0]
164 |
165 | xout = np.clip(xout * 255, 0, 255).astype(np.uint8)
166 | return xout
167 |
168 |
169 | def __call__(self, *args, **kwargs):
170 | return self.infer(*args,**kwargs)
171 |
172 | def infer(self, img, verbose=False):
173 | interpreter = self.interpreter
174 | labels = self.labels
175 | input_details = interpreter.get_input_details()
176 | output_details = interpreter.get_output_details()
--------------------------------------------------------------------------------
/py3/models/myunet_64_1.2.h5:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/models/myunet_64_1.2.h5
--------------------------------------------------------------------------------
/py3/models/myunet_64_1.2.pb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/models/myunet_64_1.2.pb
--------------------------------------------------------------------------------
/py3/models/myunet_64_1.2.tflite:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ausk/keras-unet-deploy/487efc85b87b4c5dbff8b0f2cb2476d183ba2397/py3/models/myunet_64_1.2.tflite
--------------------------------------------------------------------------------
/py3/models/unet.mapping:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
11 |
15 |
19 |
23 |
27 |
31 |
35 |
39 |
43 |
47 |
51 |
55 |
59 |
63 |
67 |
71 |
75 |
79 |
83 |
87 |
91 |
95 |
99 |
103 |
107 |
111 |
115 |
119 |
123 |
127 |
131 |
135 |
139 |
143 |
147 |
151 |
155 |
159 |
163 |
167 |
171 |
175 |
179 |
183 |
187 |
191 |
195 |
199 |
203 |
207 |
211 |
215 |
219 |
223 |
227 |
231 |
235 |
239 |
243 |
247 |
251 |
255 |
259 |
263 |
267 |
271 |
275 |
279 |
283 |
287 |
291 |
295 |
299 |
303 |
307 |
311 |
315 |
319 |
323 |
327 |
331 |
335 |
339 |
343 |
347 |
351 |
355 |
359 |
360 |
--------------------------------------------------------------------------------
/py3/models/unet.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
14 |
15 |
16 |
17 |
25 |
26 |
27 |
28 |
29 |
30 | 1
31 | 1
32 | 256
33 | 256
34 |
35 |
36 | 64
37 | 1
38 | 3
39 | 3
40 |
41 |
42 |
50 |
51 |
52 |
53 |
61 |
62 |
63 |
64 |
65 | 1
66 | 64
67 | 256
68 | 256
69 |
70 |
71 | 1
72 | 64
73 | 1
74 | 1
75 |
76 |
77 |
85 |
86 |
87 |
88 |
89 |
90 | 1
91 | 64
92 | 256
93 | 256
94 |
95 |
96 |
104 |
105 |
106 |
107 |
108 |
109 | 1
110 | 64
111 | 256
112 | 256
113 |
114 |
115 |
123 |
124 |
125 |
126 |
134 |
135 |
136 |
137 |
138 |
139 | 1
140 | 64
141 | 128
142 | 128
143 |
144 |
145 | 77
146 | 64
147 | 3
148 | 3
149 |
150 |
151 |
159 |
160 |
161 |
162 |
170 |
171 |
172 |
173 |
174 | 1
175 | 77
176 | 128
177 | 128
178 |
179 |
180 | 1
181 | 77
182 | 1
183 | 1
184 |
185 |
186 |
194 |
195 |
196 |
197 |
198 |
199 | 1
200 | 77
201 | 128
202 | 128
203 |
204 |
205 |
213 |
214 |
215 |
216 |
217 |
218 | 1
219 | 77
220 | 128
221 | 128
222 |
223 |
224 |
232 |
233 |
234 |
235 |
243 |
244 |
245 |
246 |
247 |
248 | 1
249 | 77
250 | 64
251 | 64
252 |
253 |
254 | 92
255 | 77
256 | 3
257 | 3
258 |
259 |
260 |
268 |
269 |
270 |
271 |
279 |
280 |
281 |
282 |
283 | 1
284 | 92
285 | 64
286 | 64
287 |
288 |
289 | 1
290 | 92
291 | 1
292 | 1
293 |
294 |
295 |
303 |
304 |
305 |
306 |
307 |
308 | 1
309 | 92
310 | 64
311 | 64
312 |
313 |
314 |
322 |
323 |
324 |
325 |
326 |
327 | 1
328 | 92
329 | 64
330 | 64
331 |
332 |
333 |
341 |
342 |
343 |
344 |
352 |
353 |
354 |
355 |
356 |
357 | 1
358 | 92
359 | 32
360 | 32
361 |
362 |
363 | 111
364 | 92
365 | 3
366 | 3
367 |
368 |
369 |
377 |
378 |
379 |
380 |
388 |
389 |
390 |
391 |
392 | 1
393 | 111
394 | 32
395 | 32
396 |
397 |
398 | 1
399 | 111
400 | 1
401 | 1
402 |
403 |
404 |
412 |
413 |
414 |
415 |
416 |
417 | 1
418 | 111
419 | 32
420 | 32
421 |
422 |
423 |
431 |
432 |
433 |
434 |
435 |
436 | 1
437 | 111
438 | 32
439 | 32
440 |
441 |
442 |
450 |
451 |
452 |
453 |
461 |
462 |
463 |
464 |
465 |
466 | 1
467 | 111
468 | 16
469 | 16
470 |
471 |
472 | 133
473 | 111
474 | 3
475 | 3
476 |
477 |
478 |
486 |
487 |
488 |
489 |
497 |
498 |
499 |
500 |
501 | 1
502 | 133
503 | 16
504 | 16
505 |
506 |
507 | 1
508 | 133
509 | 1
510 | 1
511 |
512 |
513 |
521 |
522 |
523 |
524 |
525 |
526 | 1
527 | 133
528 | 16
529 | 16
530 |
531 |
532 |
540 |
541 |
542 |
543 |
548 |
549 |
550 |
551 |
552 |
553 | 1
554 | 133
555 | 16
556 | 16
557 |
558 |
559 | 2
560 |
561 |
562 |
570 |
571 |
572 |
573 |
574 |
575 | 1
576 | 133
577 | 32
578 | 32
579 |
580 |
581 | 1
582 | 111
583 | 32
584 | 32
585 |
586 |
587 |
595 |
596 |
597 |
598 |
606 |
607 |
608 |
609 |
610 |
611 | 1
612 | 244
613 | 32
614 | 32
615 |
616 |
617 | 133
618 | 244
619 | 3
620 | 3
621 |
622 |
623 |
631 |
632 |
633 |
634 |
642 |
643 |
644 |
645 |
646 | 1
647 | 133
648 | 32
649 | 32
650 |
651 |
652 | 1
653 | 133
654 | 1
655 | 1
656 |
657 |
658 |
666 |
667 |
668 |
669 |
670 |
671 | 1
672 | 133
673 | 32
674 | 32
675 |
676 |
677 |
685 |
686 |
687 |
688 |
693 |
694 |
695 |
696 |
697 |
698 | 1
699 | 133
700 | 32
701 | 32
702 |
703 |
704 | 2
705 |
706 |
707 |
715 |
716 |
717 |
718 |
719 |
720 | 1
721 | 133
722 | 64
723 | 64
724 |
725 |
726 | 1
727 | 92
728 | 64
729 | 64
730 |
731 |
732 |
740 |
741 |
742 |
743 |
751 |
752 |
753 |
754 |
755 |
756 | 1
757 | 225
758 | 64
759 | 64
760 |
761 |
762 | 111
763 | 225
764 | 3
765 | 3
766 |
767 |
768 |
776 |
777 |
778 |
779 |
787 |
788 |
789 |
790 |
791 | 1
792 | 111
793 | 64
794 | 64
795 |
796 |
797 | 1
798 | 111
799 | 1
800 | 1
801 |
802 |
803 |
811 |
812 |
813 |
814 |
815 |
816 | 1
817 | 111
818 | 64
819 | 64
820 |
821 |
822 |
830 |
831 |
832 |
833 |
838 |
839 |
840 |
841 |
842 |
843 | 1
844 | 111
845 | 64
846 | 64
847 |
848 |
849 | 2
850 |
851 |
852 |
860 |
861 |
862 |
863 |
864 |
865 | 1
866 | 111
867 | 128
868 | 128
869 |
870 |
871 | 1
872 | 77
873 | 128
874 | 128
875 |
876 |
877 |
885 |
886 |
887 |
888 |
896 |
897 |
898 |
899 |
900 |
901 | 1
902 | 188
903 | 128
904 | 128
905 |
906 |
907 | 92
908 | 188
909 | 3
910 | 3
911 |
912 |
913 |
921 |
922 |
923 |
924 |
932 |
933 |
934 |
935 |
936 | 1
937 | 92
938 | 128
939 | 128
940 |
941 |
942 | 1
943 | 92
944 | 1
945 | 1
946 |
947 |
948 |
956 |
957 |
958 |
959 |
960 |
961 | 1
962 | 92
963 | 128
964 | 128
965 |
966 |
967 |
975 |
976 |
977 |
978 |
983 |
984 |
985 |
986 |
987 |
988 | 1
989 | 92
990 | 128
991 | 128
992 |
993 |
994 | 2
995 |
996 |
997 |
1005 |
1006 |
1007 |
1008 |
1016 |
1017 |
1018 |
1019 |
1020 |
1021 | 1
1022 | 92
1023 | 256
1024 | 256
1025 |
1026 |
1027 | 77
1028 | 92
1029 | 3
1030 | 3
1031 |
1032 |
1033 |
1041 |
1042 |
1043 |
1044 |
1052 |
1053 |
1054 |
1055 |
1056 | 1
1057 | 77
1058 | 256
1059 | 256
1060 |
1061 |
1062 | 1
1063 | 77
1064 | 1
1065 | 1
1066 |
1067 |
1068 |
1076 |
1077 |
1078 |
1079 |
1080 |
1081 | 1
1082 | 77
1083 | 256
1084 | 256
1085 |
1086 |
1087 |
1095 |
1096 |
1097 |
1098 |
1106 |
1107 |
1108 |
1109 |
1110 |
1111 | 1
1112 | 77
1113 | 256
1114 | 256
1115 |
1116 |
1117 | 64
1118 | 77
1119 | 3
1120 | 3
1121 |
1122 |
1123 |
1131 |
1132 |
1133 |
1134 |
1142 |
1143 |
1144 |
1145 |
1146 | 1
1147 | 64
1148 | 256
1149 | 256
1150 |
1151 |
1152 | 1
1153 | 64
1154 | 1
1155 | 1
1156 |
1157 |
1158 |
1166 |
1167 |
1168 |
1169 |
1170 |
1171 | 1
1172 | 64
1173 | 256
1174 | 256
1175 |
1176 |
1177 |
1185 |
1186 |
1187 |
1188 |
1196 |
1197 |
1198 |
1199 |
1200 |
1201 | 1
1202 | 64
1203 | 256
1204 | 256
1205 |
1206 |
1207 | 64
1208 | 64
1209 | 3
1210 | 3
1211 |
1212 |
1213 |
1221 |
1222 |
1223 |
1224 |
1232 |
1233 |
1234 |
1235 |
1236 | 1
1237 | 64
1238 | 256
1239 | 256
1240 |
1241 |
1242 | 1
1243 | 64
1244 | 1
1245 | 1
1246 |
1247 |
1248 |
1256 |
1257 |
1258 |
1259 |
1260 |
1261 | 1
1262 | 64
1263 | 256
1264 | 256
1265 |
1266 |
1267 |
1275 |
1276 |
1277 |
1278 |
1286 |
1287 |
1288 |
1289 |
1290 |
1291 | 1
1292 | 64
1293 | 256
1294 | 256
1295 |
1296 |
1297 | 1
1298 | 64
1299 | 1
1300 | 1
1301 |
1302 |
1303 |
1311 |
1312 |
1313 |
1314 |
1322 |
1323 |
1324 |
1325 |
1326 | 1
1327 | 1
1328 | 256
1329 | 256
1330 |
1331 |
1332 | 1
1333 | 1
1334 | 1
1335 | 1
1336 |
1337 |
1338 |
1346 |
1347 |
1348 |
1349 |
1350 | 1
1351 | 1
1352 | 256
1353 | 256
1354 |
1355 |
1356 |
1364 |
1365 |
1366 |
1367 |
1368 | 1
1369 | 1
1370 | 256
1371 | 256
1372 |
1373 |
1374 |
1375 |
1376 |
1377 |
1378 |
1379 |
1380 |
1381 |
1382 |
1383 |
1384 |
1385 |
1386 |
1387 |
1388 |
1389 |
1390 |
1391 |
1392 |
1393 |
1394 |
1395 |
1396 |
1397 |
1398 |
1399 |
1400 |
1401 |
1402 |
1403 |
1404 |
1405 |
1406 |
1407 |
1408 |
1409 |
1410 |
1411 |
1412 |
1413 |
1414 |
1415 |
1416 |
1417 |
1418 |
1419 |
1420 |
1421 |
1422 |
1423 |
1424 |
1425 |
1426 |
1427 |
1428 |
1429 |
1430 |
1431 |
1432 |
1433 |
1434 |
1435 |
1436 |
1437 |
1438 |
1439 |
1440 |
1441 |
1442 |
1443 |
1444 |
1445 |
1446 |
1447 |
1448 |
1449 |
1450 |
1451 |
1452 |
1453 |
1454 |
1455 |
1456 |
1457 |
1458 |
1459 |
1460 |
1461 |
1462 |
1463 |
1464 |
1465 |
1466 |
1467 |
1468 |
1469 |
1470 |
1471 |
1472 |
1473 |
1474 |
1475 |
1476 |
1477 |
1478 |
1479 |
1480 |
1481 |
1482 |
1483 |
1484 |
1485 |
1486 |
1487 |
1488 |
1489 |
1490 |
1491 |
1492 |
1493 |
1494 |
1495 |
1496 |
1497 |
1498 |
1499 |
1500 |
1501 |
1502 |
--------------------------------------------------------------------------------
/py3/utils.py:
--------------------------------------------------------------------------------
1 | # 2020/02/29 by ausk
2 |
3 | import warnings
4 | warnings.filterwarnings("ignore")
5 |
6 | import os, sys, glob
7 | import contextlib
8 | import time
9 |
10 | os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "true"
11 | os.environ["TF_ENABLE_GPU_GARBAGE_COLLECTION"] = "true"
12 | os.environ["TF_CPP_MIN_LOG_LEVEL"] = "true"
13 | os.environ["CUDA_VISIBLE_DEVICES"] = ""
14 |
15 | import tensorflow as tf
16 | from tensorflow.python.framework import graph_util, graph_io
17 | from tensorflow.keras import models
18 | import tensorflow.keras.backend as K
19 |
20 | #tf.compat.v1.disable_eager_execution()
21 |
22 | #assert tf.__version__[:4] == "1.4.", "Tensorflow should be v1.4.x!"
23 |
24 | # Reset the GPU session
25 | def reset_gpu_session(devices="0", fraction=0.8):
26 | os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "true"
27 | os.environ["TF_ENABLE_GPU_GARBAGE_COLLECTION"] = "true"
28 | os.environ["TF_CPP_MIN_LOG_LEVEL"] = "true"
29 | os.environ["CUDA_VISIBLE_DEVICES"] = devices
30 | config = tf.ConfigProto()
31 | config.gpu_options.allow_growth = True # dynamically grow the memory used on the GPU
32 | config.gpu_options.per_process_gpu_memory_fraction = fraction
33 | config.log_device_placement = False
34 | sess = tf.Session(config=config)
35 | K.set_session(sess) # set this TensorFlow session as the default session for Keras
36 | return sess
37 |
38 | # To get the version.
39 | def get_version(version):
40 | assert isinstance(version, str)
41 | import re
42 | return list(map(int, re.findall("(\d+)", version)))[:3]
43 |
44 | # To make sure the directory exist.
45 | def mksured(dpath):
46 | if not os.path.exists(dpath):
47 | os.makedirs(dpath, exist_ok=True)
48 |
49 | # The contextmanager to avoid raise except.
50 | @contextlib.contextmanager
51 | def warnOnException():
52 | try:
53 | yield
54 | except Exception as exc:
55 | print("Exception: {}".format(exc))
56 | finally:
57 | pass
58 |
59 | # The contextmanager to time the code.
60 | @contextlib.contextmanager
61 | def timeit():
62 | ts = time.time()
63 | try:
64 | yield
65 | finally:
66 | pass
67 | te = time.time()
68 | print("dt: {:.3f} ms ".format( (te-ts)*1000) )
69 |
70 |
71 | # Save the graph in current session as binary protobuf file.
72 | def savepb(model, sess=None, pbname="result.pb"):
73 | #tf.keras.backend.set_learning_phase(0)
74 | #K.set_learning_phase(0)
75 | model_outputs = model.outputs
76 | output_names = []
77 | outputs = []
78 | for i, output in enumerate(model_outputs):
79 | output_names.append("output_{}".format(i))
80 | outputs.append(tf.identity(output, name = output_names[i]))
81 |
82 | input_names = [node.op.name for node in model.inputs]
83 |
84 | print("[ INFO ] inputs: ", input_names)
85 | print("[ INFO ] outputs: ", output_names)
86 |
87 | if sess is None:
88 | sess = K.get_session()
89 |
90 | graph_def = sess.graph.as_graph_def()
91 | #graphdef_inf = graph_util.remove_training_nodes(graph_def)
92 | #graphdef_frozen = graph_util.convert_variables_to_constants(sess, graphdef_inf, output_names)
93 | graphdef_frozen = graph_util.convert_variables_to_constants(sess, graph_def, output_names)
94 | graph_io.write_graph(graphdef_frozen, ".", pbname, as_text=False)
95 | print("[ INFO ] Saved pb into: ", pbname)
96 | print("[ INFO ] inputs: ", input_names)
97 | print("[ INFO ] outputs: ", output_names)
98 |
99 |
100 | def loadGraph(pbfpath, prefix=""):
101 | """
102 | (1) About prefix.
103 | If we set name(prefix) = None,then it will add "import/" prefix to each node.
104 | And we should use name(prefix) = "" to keep none prefix.
105 | (2) It may be faild when load pb graph if it contains BN layers.
106 | """
107 | tf.keras.backend.set_learning_phase(0)
108 | K.set_learning_phase(0)
109 | K.clear_session()
110 |
111 | # (1) Load the protobuf file from the disk and parse it to retrieve the unserialized graph_def.
112 | #with tf.gfile.GFile(pbfpath, "rb") as fin:
113 | with open(pbfpath, "rb") as fin:
114 | graph_def = tf.GraphDef()
115 | graph_def.ParseFromString(fin.read())
116 |
117 | # (2) Import a graph_def into the current default Graph.
118 | with tf.Graph().as_default() as graph:
119 | tf.import_graph_def(
120 | graph_def,
121 | input_map=None,
122 | return_elements=None,
123 | name=prefix,
124 | op_dict=None,
125 | producer_op_list=None
126 | )
127 |
128 | # We can verify that we can access the list of operations in the graph
129 | for op in graph.get_operations():
130 | pass
131 | #print(op.name)
132 |
133 | return graph
134 | """
135 | # Access the input and output nodes
136 | x = graph.get_tensor_by_name(prefix+'input_1:0')
137 | y = graph.get_tensor_by_name(prefix+'output_0:0')
138 |
139 | # Launch a Session and load the graph, feed and interence.
140 | with tf.Session(graph=graph) as sess:
141 | xinput = np.random.random((1, 256, 256,1))
142 | pred_y = sess.run( y, feed_dict={x: xinput} )
143 | print(pred_y)
144 | """
145 |
146 | def keras2tflite(h5fpath, litefpath=None):
147 | if litefpath is None:
148 | litefpath = h5fpath[:-2] + "tflite"
149 | model = models.load_model(h5fpath)
150 | c = tf.lite.TFLiteConverter.from_keras_model(model)
151 | data = c.convert()
152 | with open(litefpath, "wb") as fout:
153 | fout.write(data)
--------------------------------------------------------------------------------