Skip to content

Instantly share code, notes, and snippets.

View dkurt's full-sized avatar

Dmitry Kurtaev dkurt

View GitHub Profile
import cv2 as cv
import numpy as np
img = cv.imread('/home/dkurt/Pictures/download.jpeg')
inp = cv.dnn.blobFromImage(img, 0.017, (224, 224), (103.94,116.78,123.68), swapRB=False, crop=False)
net = cv.dnn.readNet('shufflenet_1x_g3_deploy.prototxt', 'shufflenet_1x_g3.caffemodel')
net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
net.setInput(inp)
out = net.forward()
from math import sqrt
min_scale = 0.2
max_scale = 0.95
num_layers = 6
aspect_ratios = [1.0, 2.0, 0.5, 3.0, 0.333]
image_width = 300
image_height = 300
scales = [min_scale + (max_scale - min_scale) * i / (num_layers - 1) for i in range(num_layers)] + [1.0]
node {
name: "image_tensor"
op: "Placeholder"
attr {
key: "dtype"
value {
type: DT_UINT8
}
}
attr {
node {
name: "image_tensor"
op: "Placeholder"
attr {
key: "dtype"
value {
type: DT_UINT8
}
}
attr {
node {
name: "image_tensor"
op: "Placeholder"
attr {
key: "dtype"
value {
type: DT_UINT8
}
}
attr {
# http://answers.opencv.org/s/answers/186571
node {
name: "input_image"
op: "Placeholder"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
// 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",
node {
name: "image_tensor"
op: "Placeholder"
attr {
key: "dtype"
value {
type: DT_UINT8
}
}
attr {
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 {
@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: