Skip to content

Instantly share code, notes, and snippets.

View dkurt's full-sized avatar

Dmitry Kurtaev dkurt

View GitHub Profile
cd ~/opencv/build
cmake -DCMAKE_BUILD_TYPE=RELEASE \
-DWITH_OPENCL=NO \
-DWITH_CUDA=NO \
-DBUILD_TESTS=OFF \
-DBUILD_PERF_TESTS=OFF \
-DBUILD_opencv_aruco=OFF \
-DBUILD_opencv_bgsegm=OFF \
-DBUILD_opencv_bioinspired=OFF \
@dkurt
dkurt / CMakeLists.txt
Created July 4, 2017 11:08
CMakeLists.txt
cmake_minimum_required(VERSION 3.4.3)
project(test_halide CXX)
include_directories(
~/Halide/build/include
)
link_directories(
~/Halide/build/lib
)
@dkurt
dkurt / sample.cpp
Last active August 8, 2017 11:20
sample
{
Halide::Var x("x"), xo("xo"), xi("xi");
Halide::Func f("f");
f(x) = x + 777;
f.bound(x, 0, 16);
f.split(x, xo, xi, 8).gpu_threads(xi).gpu_blocks(xo);
f.output_buffer().dim(0).set_bounds(0, 16);
Halide::Target target = Halide::get_host_target();
import tensorflow as tf
# Read the graph.
with tf.gfile.FastGFile('opt_graph.pb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
# Remove Const nodes.
for i in reversed(range(len(graph_def.node))):
if graph_def.node[i].op == 'Const':
@dkurt
dkurt / tmp.cpp
Last active November 19, 2017 11:48
cv::Mat img = cv::imread("toucan.jpg");
cv::resize(img, img, cv::Size(224, 224));
img.convertTo(img, CV_32F);
cv::subtract(img, cv::Scalar(104.0f, 117.0f, 123.0f), img);
cv::divide(img, cv::Scalar(127.5f, 127.5f, 127.5f), img);
std::vector<cv::Mat> channels(3);
cv::split(img, channels);
cv::Mat blob({1, 3, 224, 224}, CV_32F);
for (int i = 0; i < 3; ++i) {
cv::Mat dst(224, 224, CV_32F, blob.ptr<float>(0, i));
@dkurt
dkurt / dnn.tf.md
Last active August 15, 2018 07:35

Let's discuss how to deploy deep learning models trained in TensorFlow framework both in TensorFlow and using OpenCV.

Requirements

  • TensorFlow
  • TensorBoard (optional but hardly recommended)
  • OpenCV with python bindings

Define a graph

This is how we define a network with a single convolution layer followed by ReLU activation function:

node {
name: "image_tensor"
op: "Placeholder"
}
node {
name: "FeatureExtractor/MobilenetV1/MobilenetV1/Conv2d_0/convolution"
op: "Conv2D"
input: "image_tensor"
input: "FeatureExtractor/MobilenetV1/Conv2d_0/weights"
attr {
node {
name: "image_tensor"
op: "Placeholder"
attr {
key: "dtype"
value {
type: DT_UINT8
}
}
attr {
// Wiki page about how to create .pbtxt files: https://github.com/opencv/opencv/wiki/TensorFlow-Object-Detection-API
// This page is referenced to a single script https://github.com/opencv/opencv/blob/master/samples/dnn/tf_text_graph_ssd.py
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
using namespace cv;
using namespace dnn;
const char* classes[] = {"background", "person", "bicycle", "car", "motorcycle",
"airplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant",
# http://answers.opencv.org/s/answers/186571
node {
name: "input_image"
op: "Placeholder"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}