Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Added image reading function for runtime #577

Merged
merged 55 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from 54 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
a083bf2
fixing to README.md
yoya Jul 16, 2019
01ae182
Tensor dump layout refine.
yoya Jul 16, 2019
be30069
post-process implementation.
yoya Jul 16, 2019
fbf4c8d
enable to building without OpenCV.
yoya Jul 16, 2019
dd3a037
apply google coding styles with cpplint check.
yoya Jul 16, 2019
86b185c
add copyright to src/include/test, cpp/hpp
yoya Jul 17, 2019
f17fb64
apply google coding styles with cpplint check. examples/run.cpp
yoya Jul 17, 2019
a005d64
Merge branch 'master' into dev/runtime/postprocess
yoya Jul 24, 2019
242e182
fixing to not work even if OpenCV enabled
yoya Jul 24, 2019
d3a026b
Merge branch 'master' into dev/runtime/postprocess
yoya Jul 31, 2019
332b6e0
add function Tensor_loadImage to blueoil namespace.
yoya Jul 31, 2019
192e4a7
OpenCV default off, for examples/run.cpp
yoya Jul 31, 2019
691fc5e
Merge branch 'dev/runtime/postprocess' into dev/runtime/readimage
yoya Jul 31, 2019
f518f41
Merge branch 'master' into dev/runtime/postprocess
iizukak Aug 8, 2019
efac9d3
fixing to blueoil::image::LoadImage build failed without OpenCV.
yoya Aug 9, 2019
57f4f85
Merge branch 'dev/runtime/postprocess' into dev/runtime/readimage
yoya Aug 14, 2019
199bee9
Merge branch 'master' into dev/runtime/readimage
yoya Aug 14, 2019
5949886
adding PNG library rules to runtime CMake
yoya Aug 14, 2019
b403b93
a mechanism that can read image files as possible depend on the libra…
yoya Sep 4, 2019
86eab04
add NPY reader.
yoya Sep 18, 2019
e833509
add test for npy file reading
yoya Sep 25, 2019
33fa8ce
fix comment, function name.
yoya Sep 25, 2019
045bb5d
PNG image reader with libpng.
yoya Sep 27, 2019
b6442e7
refine with cpplint
yoya Sep 27, 2019
4aff09c
Merge branch 'master' into dev/runtime/readimage
yoya Oct 2, 2019
73d10f4
use Tensor_loadImage function.
yoya Oct 2, 2019
97cf571
list(FILTER... was introduced in CMake 3.6.
yoya-awm Oct 9, 2019
dc8d123
change link library target, "-lpthread" to pthread
yoya Oct 9, 2019
73edb31
fix build failed condition. USE_OPENCV=OFF & USE_LIBPNG=ON
yoya Oct 9, 2019
6faa87c
libpng, opencv support.
yoya Oct 9, 2019
832bb6e
Merge branch 'master' into dev/runtime/readimage
yoya Oct 16, 2019
956d0f0
Merge branch 'master' into dev/runtime/readimage
yoya Oct 23, 2019
d584fa6
fix cmake rules for LIBPNG
yoya Oct 29, 2019
1e7b64f
Usage: a.out -i <imagefile> -c <configfile>
yoya Oct 29, 2019
49df6fe
fixing to read image PNG.
yoya Oct 29, 2019
51c630e
change dlk examples something to classification/lmnet_quantize_cifar10
yoya Oct 30, 2019
cbd8813
update README a.out output log with the lmnet_quantize_cifar10 dlklib
yoya Oct 30, 2019
611f552
Merge branch 'master' into dev/runtime/readimage
yoya Oct 30, 2019
b9855b0
cat.png image (from cat.npy)
yoya Oct 30, 2019
db7eb85
Merge branch 'master' into dev/runtime/readimage
yoya Nov 6, 2019
5455041
README update to make test current situation.
yoya Nov 6, 2019
f8683c2
improved run.cpp result output.
yoya Nov 6, 2019
4b33fd9
Merge branch 'master' into dev/runtime/readimage
yoya Nov 12, 2019
5fd7749
let test_resize using blueoil::Tensor_loadImage, instead of opencv::i…
yoya Nov 13, 2019
90444ac
Merge branch 'master' into dev/runtime/readimage
yoya Nov 20, 2019
1a947dc
Let Tensor_fromNPYFile more generic function, not limited to image data.
yoya Nov 22, 2019
bb92228
cpplint check.
yoya Nov 22, 2019
54f68cd
remove empty line between namespace keywords.
yoya Nov 22, 2019
15130c1
remove dlk_lib/libdlk_x86.a & meta.yaml
yoya Nov 22, 2019
efdc6e6
support float32 value in npy file.
yoya Nov 23, 2019
a5dd48d
fix variable name. imagedata => data.
yoya Nov 23, 2019
21688c9
change variable name. valuetype => datatype.
yoya Nov 23, 2019
ac1e423
fixing to coding style.
yoya Nov 26, 2019
506e97a
refine CMakeList.txt
yoya Nov 26, 2019
9e49ab2
Merge branch 'master' into dev/runtime/readimage
tkng Nov 26, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ if(USE_OPENCV)
add_definitions(-DUSE_OPENCV=1)
endif()

option(USE_LIBPNG "use PNG library" OFF)
if(NOT USE_OPENCV)
if(USE_LIBPNG)
find_package(PNG REQUIRED)
if(NOT PNG_FOUND)
message(SEND_ERROR "PNG library not found.")
endif()
add_definitions(-DUSE_LIBPNG=1)
endif()
endif()

# Add yaml cpp
option(YAML_CPP_BUILD_TESTS "Enable testing" OFF)
Expand All @@ -49,13 +59,22 @@ file(GLOB SRCS "src/[a-zA-Z]*.cpp")
file(GLOB PUBLIC_HEADERS "include/blueoil.hpp")
file(GLOB PRIVATE_HEADERS "include/[a-zA-Z]*.hpp")
if(NOT USE_OPENCV)
list(FILTER SRCS EXCLUDE REGEX ".*opencv.cpp$")
list(FILTER PRIVATE_HEADERS EXCLUDE REGEX ".*opencv.hpp$")
file(GLOB OPENCV_SRCS "src/*_opencv.cpp")
file(GLOB OPENCV_HEADERS "include/*_opencv.hpp")
list(REMOVE_ITEM SRCS ${OPENCV_SRCS})
list(REMOVE_ITEM PRIVATE_HEADERS ${OPENCV_HEADERS})
endif()
if(NOT USE_LIBPNG)
file(GLOB PNG_SRCS "src/*_png.cpp")
file(GLOB PNG_HEADERS "include/*_png.hpp")
list(REMOVE_ITEM SRCS ${PNG_SRCS})
list(REMOVE_ITEM PRIVATE_HEADERS ${PNG_HEADERS})
endif()


if(VERBOSE)
message(STATUS "use_opencv: ${USE_OPENCV}")
message(STATUS "use_libpng: ${USE_LIBPNG}")
message(STATUS "sources: ${SRCS}")
message(STATUS "public_headers: ${PUBLIC_HEADERS}")
message(STATUS "private_headers: ${PRIVATE_HEADERS}")
Expand All @@ -67,6 +86,9 @@ include_directories(include yaml-cpp/include)
if(USE_OPENCV)
include_directories(${OpenCV_INCLUDE_DIRS})
endif()
if(USE_PNG)
include_directories(${PNG_INCLUDE_DIRS})
endif()


# Add dlk static lib.
Expand Down
65 changes: 30 additions & 35 deletions runtime/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# Import model with dlk library

- Choose a model to try from blueoil/dlk/examples/
- for example: classification/lmnet_quantize_cifar10
- make libdlk_x86.a with reference blueoil/dlk/README.md
- put libdlk_x86.a & meta.yaml to blueoil/runtime/examples/

# Build blueoil static lib
```
$ mkdir build
$ cd build
# You can set `DLK_LIB_DIR` environment.
$ DLK_LIB_DIR=`pwd`/../examples/dlk_lib/ cmake ../
$ DLK_LIB_DIR=`pwd`/../examples/ cmake ../
$ make
$ make install
$ tree output/
Expand All @@ -20,7 +27,7 @@ output/
$ mkdir build
$ cd build
# -DBUILD_SHARED_LIBS=ON
$ DLK_LIB_DIR=`pwd`/../examples/dlk_lib/ cmake -DBUILD_SHARED_LIBS=ON ../
$ DLK_LIB_DIR=`pwd`/../examples/ cmake -DBUILD_SHARED_LIBS=ON ../
$ make
$ make install
$ tree output/
Expand All @@ -41,59 +48,47 @@ $ cd examples
$ cp -R ../build/output/* ./
$ cmake .
$ make
$ ./a.out
$ ./run -i cat.npy -c meta.yaml
classes:
0
1
2
3
4
5
6
7
8
9
airplane
automobile
bird
cat
deer
dog
frog
horse
ship
truck
task: IMAGE.CLASSIFICATION
expected input shape:
1
128
128
32
32
3
Run
Results !
0.100382
0.0998879
0.101126
0.098727
0.100418
0.0999296
0.100612
0.0995238
0.0996848
0.0997096
shape:1 10
0.000105945 6.23502e-05 0.0323531 0.00360625 0.0124029 0.000231775 0.951004 8.7062e-05 9.84179e-05 4.80589e-05
```

Currentlly pre/post-process functions are NOT correctly implemented.
The value of Results! 0.100382, 0.0998879 will be changed after correct implementation.


# Unit tests

```
$ cd build
$ DLK_LIB_DIR=`pwd`/../examples/dlk_lib/ cmake -DBUILD_SHARED_LIBS=ON ../
$ DLK_LIB_DIR=`pwd`/../examples/ cmake -DBUILD_SHARED_LIBS=ON ../
$ make
$ make test
Running tests...
Test project <repos_dir>/blueoil/runtime/build
Start 1: blueoil-test-tensor
1/4 Test #1: blueoil-test-tensor .............. Passed 0.00 sec
1/5 Test #1: blueoil-test-tensor .............. Passed 0.00 sec
Start 2: blueoil-test-image
2/4 Test #2: blueoil-test-image ............... Passed 0.00 sec
Start 3: blueoil-test-opencv
3/4 Test #3: blueoil-test-opencv .............. Passed 0.04 sec
2/5 Test #2: blueoil-test-image ............... Passed 0.00 sec
Start 3: blueoil-test-npy
3/5 Test #3: blueoil-test-npy ................. Passed 0.00 sec
Start 4: blueoil-test-resize
4/4 Test #4: blueoil-test-resize .............. Passed 0.04 sec
4/5 Test #4: blueoil-test-resize .............. Passed 0.00 sec
Start 5: blueoil-test-data_processor
5/5 Test #5: blueoil-test-data_processor ...... Passed 0.00 sec

Expand Down
28 changes: 24 additions & 4 deletions runtime/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,30 @@ set(CMAKE_CXX_COMPILER g++)
set(CMAKE_CXX_FLAGS "-Wall -O3")
set(CMAKE_CXX_STANDARD "11")

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/meta.yaml meta.yaml COPYONLY)

link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

add_executable(a.out run.cpp)
target_link_libraries(a.out blueoil pthread)
add_executable(run run.cpp)

if(USE_OPENCV)
find_package(OpenCV REQUIRED)
if(NOT OpenCV_FOUND)
message(SEND_ERROR "OpenCV library not found.")
endif()
endif()
if(USE_LIBPNG)
find_package(PNG REQUIRED)
if(NOT PNG_FOUND)
message(SEND_ERROR "PNG library not found.")
endif()
endif()

if(USE_OPENCV)
target_link_libraries(run blueoil ${OpenCV_LIBS})
else()
if(USE_LIBPNG)
target_link_libraries(run blueoil png pthread)
else()
target_link_libraries(run blueoil pthread)
endif()
endif()
Binary file added runtime/examples/cat.npy
Binary file not shown.
Binary file added runtime/examples/cat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed runtime/examples/dlk_lib/libdlk_x86.a
Binary file not shown.
23 changes: 0 additions & 23 deletions runtime/examples/meta.yaml

This file was deleted.

48 changes: 27 additions & 21 deletions runtime/examples/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,34 @@ limitations under the License.
#include <string>
#include <iostream>
#include <vector>
#include <cstring>

#include "blueoil.hpp"

// TODO: delete this func. it is for debug.
blueoil::Tensor RandomImage(int height, int width, int channel) {
blueoil::Tensor t({height, width, channel});
std::vector<float>& data = t.data();
unsigned seed = 0;

for (size_t i = 0; i < data.size(); ++i) {
const float f_rand = static_cast <float> (rand_r(&seed)) / static_cast <float> (RAND_MAX) * 255;
data[i] = f_rand;
int main(int argc, char **argv) {
std::string imagefile, meta_yaml;

for (int i = 1; i < (argc-1); i++) {
char *arg = argv[i];
char *arg2 = argv[i+1];
if ((arg[0] == '-') && (std::strlen(arg) == 2) && (arg2[0] != '-')) {
switch (arg[1]) {
case 'i': // image file (ex. raw_image.npy)
imagefile = std::string(arg2);
break;
case 'c': // config file (meta.yaml)
meta_yaml = std::string(arg2);
break;
}
}
}

return t;
}



int main() {
std::string meta_yaml = "meta.yaml";
if (imagefile.empty() || meta_yaml.empty()) {
std::cerr << "Usage: a.out -i <imagefile> -c <configfile>" << std::endl;
std::cerr << "ex) a.out -i raw_image.npy -c meta.yaml" << std::endl;
std::exit(1);
}

blueoil::Predictor predictor = blueoil::Predictor(meta_yaml);

Expand All @@ -51,14 +58,13 @@ int main() {
for (int j : predictor.expected_input_shape) {
std::cout << j << "\n";
}

blueoil::Tensor random_image = RandomImage(256, 256, 3);
blueoil::Tensor image = blueoil::Tensor_loadImage(imagefile);

std::cout << "Run" << std::endl;
blueoil::Tensor output = predictor.Run(random_image);
blueoil::Tensor output = predictor.Run(image);

std::cout << "Results !" << std::endl;
for (float j : output.data()) {
std::cout << j << std::endl;
}
output.dump();

std::exit(0);
}
2 changes: 2 additions & 0 deletions runtime/include/blueoil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class Tensor {
bool allclose(const Tensor &tensor, float rtol, float atol) const;
};

Tensor Tensor_loadImage(std::string filename);


// typedef Tensor (*TensorFunction)(Tensor&);
typedef std::function<Tensor(const Tensor& input)> Processor;
Expand Down
3 changes: 3 additions & 0 deletions runtime/include/blueoil_image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
#ifndef RUNTIME_INCLUDE_BLUEOIL_IMAGE_HPP_
#define RUNTIME_INCLUDE_BLUEOIL_IMAGE_HPP_

#include <string>
#include "blueoil.hpp"

namespace blueoil {
Expand All @@ -26,6 +27,8 @@ enum ResizeFilter {
RESIZE_FILTER_BI_LINEAR = 2,
};

Tensor LoadImage(const std::string filename);

Tensor Resize(const Tensor& image, const int width, const int height,
const enum ResizeFilter filter);

Expand Down
30 changes: 30 additions & 0 deletions runtime/include/blueoil_npy.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Copyright 2019 The Blueoil Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=============================================================================*/

#ifndef RUNTIME_INCLUDE_BLUEOIL_NPY_HPP_
#define RUNTIME_INCLUDE_BLUEOIL_NPY_HPP_

#include <string>
#include "blueoil.hpp"

namespace blueoil {
namespace npy {

Tensor Tensor_fromNPYFile(const std::string filename);

} // namespace npy
} // namespace blueoil

#endif // RUNTIME_INCLUDE_BLUEOIL_NPY_HPP_
30 changes: 30 additions & 0 deletions runtime/include/blueoil_png.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Copyright 2019 The Blueoil Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=============================================================================*/

#ifndef RUNTIME_INCLUDE_BLUEOIL_PNG_HPP_
#define RUNTIME_INCLUDE_BLUEOIL_PNG_HPP_

#include <string>
#include "blueoil.hpp"

namespace blueoil {
namespace png {

Tensor Tensor_fromPNGFile(const std::string filename);

} // namespace png
} // namespace blueoil

#endif // RUNTIME_INCLUDE_BLUEOIL_PNG_HPP_
5 changes: 5 additions & 0 deletions runtime/src/blueoil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ limitations under the License.
#include <functional>

#include "blueoil.hpp"
#include "blueoil_image.hpp"
#include "blueoil_data_processor.hpp"

#include "yaml-cpp/yaml.h"
Expand Down Expand Up @@ -242,6 +243,10 @@ bool Tensor::allclose(const Tensor &tensor, float rtol, float atol) const {
return true;
}

Tensor Tensor_loadImage(std::string filename) {
return blueoil::image::LoadImage(filename);
}


// mapping process node to functions vector.
void MappingProcess(const YAML::Node processors_node, std::vector<Processor>* functions) {
Expand Down
Loading