Ask Your Question

Dobiasd's profile - activity

2019-12-06 09:01:16 -0500 received badge  Famous Question (source)
2018-06-19 06:32:24 -0500 received badge  Notable Question (source)
2018-02-13 09:43:48 -0500 received badge  Popular Question (source)
2017-08-24 09:53:06 -0500 commented question Keras -> TensorFlow -> OpenCV/dnn

OK, thanks. However it looks like the Keras interface does not provide these fine-grained options. Is it planned to support Keras models natively without going through the indirection of another model format like TensorFlow's?

2017-08-24 08:01:47 -0500 commented question Keras -> TensorFlow -> OpenCV/dnn

Hi @dkurtaev. Thanks for the quick respone.

wilcoschoneveld uses Keras via tensorflow.contrib.keras. But my Keras model is backend agnostic. So I guess tf.reshape would not be an optinal solution/workaround in my case.

However, as a test I removed the Flattenpart completely: https://ideone.com/ZhXwhT This results in OpenCV Error: Unspecified error (Unknown layer type Mean in op batch_normalization_1/moments/Mean)

Using selu instead of relu as the activation in a conv layer results in OpenCV Error: Unspecified error (Unknown layer type Greater in op conv2d_1/Greater)

Do you know if it is planned to support all these things in the future? And if so is there already a roadmap?

2017-08-23 10:40:33 -0500 asked a question Keras -> TensorFlow -> OpenCV/dnn

I try to use a Tensorflow model (created with Keras) in OpenCV/dnn (3.3.0). But I get the following error message:

OpenCV Error: Unspecified error (Unknown layer type Shape in op flatten_1/Shape)

This is the python code to create and export the model.

import os
import sys
import shutil
import subprocess

import numpy as np

import tensorflow as tf

from keras.models import Model
from keras import backend as K

from keras.layers import Dense, Input, Conv2D, Flatten, MaxPooling2D, BatchNormalization


sess = tf.Session()
K.set_session(sess)

# create model
inputs = Input(shape=(64, 64, 3), name='input_layer')
x = Conv2D(8, (3, 3), padding='same', activation='relu')(inputs)
x = MaxPooling2D((2,2))(x)
x = Conv2D(16, (3, 3), padding='same', activation='relu')(x)
x = MaxPooling2D((2,2))(x)
x = Flatten()(x)
x = Dense(64, activation='sigmoid')(x)
x = BatchNormalization()(x)
x = Dense(32, activation='sigmoid')(x)
predictions = Dense(10, activation='softmax', name='output_layer')(x)
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# train model
data = np.random.random((100, 64, 64, 3))
labels = np.random.randint(2, size=(100, 10))
model.fit(data, labels, epochs=10, batch_size=32)

# create temp directory for export
temp_export_dir = './temp_export/'
if os.path.exists(temp_export_dir):
    shutil.rmtree(temp_export_dir)
os.makedirs(temp_export_dir)

# export model to temp directory
with sess.graph.as_default():
    saver = tf.train.Saver()
    saver.save(sess, temp_export_dir + 'model.ckpt')
    tf.train.write_graph(sess.graph.as_graph_def(), temp_export_dir, "graph.pbtxt")

# freeze graph and optimize for inference
_ = subprocess.call("python freeze_graph.py --input_graph=" + temp_export_dir + "graph.pbtxt --input_checkpoint=" + temp_export_dir + "model.ckpt --output_graph=" + temp_export_dir + "frozen_graph.pb --output_node_names=output_layer/Softmax")
_ = subprocess.call("python optimize_for_inference.py --input=" + temp_export_dir + "frozen_graph.pb --output=model.pb --frozen_graph=True --input_names=input_layer --output_names=output_layer/Softmax")

# remove temp directory
#shutil.rmtree(temp_export_dir)

The two external scripts freeze_graph.py and optimize_for_inference.py come from tensorflow/python/tools

And this is the C++ code to load the model:

#include <opencv2/dnn.hpp>
int main()
{
    cv::dnn::Net net = cv::dnn::readNetFromTensorflow( "model.pb" );
}

I uploaded the file model.pbhere: http://daiw.de/share/opencv_question_...

Am I doing something wrong or can OpenCV/dnn simply not import such a model?

2016-04-21 07:55:31 -0500 received badge  Student (source)
2016-04-21 05:16:43 -0500 asked a question How to set parameters for DeepFlow optical flow algorithm

The documentation of cv::optflow::createOptFlow_DeepFlow states:

Parameters - class fields - that may be modified after creating a class instance

How can one modify these parameters? All one gets is a Ptr<DenseOpticalFlow> and cv::optflow::OpticalFlowDeepFlow is not in the public interface, so one can not even dynamic_cast to it.

2015-09-10 07:58:45 -0500 commented question GridAdaptedFeatureDetector missing in OpenCV 3.0??

It was removed with this commit. I miss it too and hope it will come back.

2015-09-10 07:46:42 -0500 received badge  Supporter (source)
2015-05-26 04:24:49 -0500 asked a question findContours, number of elements in hierarchy (Python)

x-post (SO)

Given this minimal example code

import numpy as np
import cv2

img = np.zeros((512,512,1), np.uint8)
cv2.rectangle(img, (100,100), (200,200), 255, -1)
cv2.rectangle(img, (300,300), (400,400), 255, -1)
#cv2.imwrite('img.png', img)
contours,hierarchy = cv2.findContours(
    img,
    cv2.RETR_TREE,
    cv2.CHAIN_APPROX_SIMPLE)
print hierarchy
print len(hierarchy)

that produces the following image:

enter image description here

I would expect hierarchy to look like this

[[ 1 -1 -1 -1]
 [-1  0 -1 -1]]

because the documentation clearly states

hierarchy –
Optional output vector, containing information about the image topology.
It has as many elements as the number of contours.
For each i-th contour contours[i] , the elements
hierarchy[i][0] , hiearchy[i][1] , hiearchy[i][2] , and hiearchy[i][3]
are set to 0-based indices in contours of the next and previous contours
at the same hierarchical level,
the first child contour and the parent contour, respectively.

But actually it looks like this:

[[[ 1 -1 -1 -1]
  [-1  0 -1 -1]]]

This means that hierarchy does not have 2 elements (like the documentation suggests), but only 1 element. Thus I do not have to use

hierarchy[i][0]
hierarchy[i][1]
...

to access the data but

hierarchy[0][i][0]
hierarchy[0][i][1]
...

Is there a deeper meaning behind this that I am missing, am I doing something wrong, or is the documentation just incorrect / the function broken?