Ask Your Question

AyushP123's profile - activity

2020-03-11 12:11:40 -0600 received badge  Popular Question (source)
2018-11-05 10:38:59 -0600 marked best answer How should a tensorflow model be saved so that it can be loaded in opencv3.3.1

Hi, I am using Tensorflow to train a neural network ( The neural network doesn't contain any variables ). This is my neural network graph in Tensorflow.

X =  tf.placeholder(tf.float32, [None,training_set.shape[1]],name = 'X')
Y = tf.placeholder(tf.float32,[None,training_labels.shape[1]], name = 'Y')

A1 = tf.contrib.layers.fully_connected(X, num_outputs = 50, activation_fn = tf.nn.relu)
A1 = tf.nn.dropout(A1, 0.8)
A2 = tf.contrib.layers.fully_connected(A1, num_outputs = 2, activation_fn = None)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = A2, labels = Y))
global_step = tf.Variable(0, trainable=False)
start_learning_rate = 0.001
learning_rate = tf.train.exponential_decay(start_learning_rate, global_step, 100, 0.1, True )
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

I wanted to know how this graph should be saved in tensorflow so as to load it using readNetFromTensorflow

2018-07-08 05:25:10 -0600 asked a question Does having a tf.nn.dropout layer in a tensorflow frozen graph affect dnn::forward

Does having a tf.nn.dropout layer in a tensorflow frozen graph affect dnn::forward I wanted to know if frozen tensorflow

2018-06-01 09:57:17 -0600 asked a question How to infer stride, padding information of a convolution layer after importing from a frozen graph (readNetFromTensorFlow)

How to infer stride, padding information of a convolution layer after importing from a frozen graph (readNetFromTensorFl

2018-05-17 04:24:47 -0600 asked a question Is it possible to use GPU for forward pass of a tensorflow model in opencv 3.4.1 dnn

Is it possible to use GPU for forward pass of a tensorflow model in opencv 3.4.1 dnn Was trying to find a GPU implementa

2018-05-17 04:12:54 -0600 received badge  Enthusiast
2018-04-13 07:13:09 -0600 received badge  Student (source)
2018-04-10 07:06:16 -0600 marked best answer How to import a tensorflow model with dropout in opencv

Hi, I tried using the advice here -> https://github.com/opencv/opencv/issu.... Saving the checkpoint with dropout and saving the graph without dropout. Here is the issue that I face, along with the name of all the layers read by OpenCV 3.4.1:

Conv1/convolution1/Conv2D

Conv1/convolution1/Relu

Conv1/pooling_1

Conv2/convolution2/Conv2D

Conv2/convolution2/Relu

Conv2/pooling_2

Conv3/convolution3/Conv2D

Conv3/convolution3/Relu

Conv3/Reshape/nchw Conv3/Reshape

Dense1/fully_connected5/MatMul

Dense1/fully_connected5/Relu

Dense2/fully_connected6/MatMul

Dense2/fully_connected6/Relu

Dense2/output_node [ INFO:0]

Initialize OpenCL runtime...

OpenCV(3.4.1) Error: Backtrace (Can't infer a dim denoted by -1) in computeShapeByReshapeMask, file /home/ayushpandey/opencv/modules/dnn/src/layers/reshape_layer.cpp, line 136 terminate called after throwing an instance of 'cv::Exception'

what(): OpenCV(3.4.1) /home/ayushpandey/opencv/modules/dnn/src/layers/reshape_layer.cpp:136: error: (-1) Can't infer a dim denoted by -1 in function computeShapeByReshapeMask

Aborted (core dumped)

Here is my tensorflow code ->https://pastebin.com/mMgRbHV0 ( Commented lines are uncommented and the lines corresponding to uncommented pool1, pool2 and saver.save is commented for saving graph without dropout ).

Then I run the following commands to freeze the graph.

python freeze_graph.py --input_graph=./input_graph.pb --input_checkpoint=./with_dropout.ckpt --output_graph=./output_graph.pb --output_node=Dense2/output_node

followed by

python optimize_for_inference.py --input output_graph.pb --output without_dropout.pb --frozen_graph True --input_names input_node --output_names Dense2/output_node

and then I try to load the "without_dropout.pb" with OpenCV where I face the issue.

2018-04-10 05:40:44 -0600 commented answer How to import a tensorflow model with dropout in opencv

Sorry for wasting your time .... it turns out that I was using the wrong image size .... should I remove this question a

2018-04-10 04:44:26 -0600 commented question How to import a tensorflow model with dropout in opencv

Sorry for the mistake .... the input size is [NONE, 250, 250, 1]

2018-04-10 03:36:53 -0600 commented question How to import a tensorflow model with dropout in opencv

[NONE, 125, 125, 1]

2018-04-10 03:09:02 -0600 commented question How to import a tensorflow model with dropout in opencv

https://drive.google.com/file/d/1fIppXXv_FJEiWKP79xJ92ar0uFO_1iFj/view?usp=sharing

2018-04-09 09:42:02 -0600 asked a question How to import a tensorflow model with dropout in opencv

How to import a tensorflow model with dropout in opencv Hi, I tried using the advice here -> https://github.com/openc

2018-04-03 09:05:34 -0600 commented answer Running tensorflow graph through OpenCV gives different result

Should I close this question then .... since its an issue of using the latest version??

2018-04-03 08:45:39 -0600 commented answer Running tensorflow graph through OpenCV gives different result

What version should I use ... 3.3 or 3.4 ??

2018-04-03 08:41:41 -0600 commented answer Running tensorflow graph through OpenCV gives different result

Ohh .... thank you for the response :) .... will update OpenCV then.

2018-04-03 08:18:33 -0600 commented answer Running tensorflow graph through OpenCV gives different result

@dkurt .... here is the link -> https://drive.google.com/drive/folders/16nWtIdXJ4hjBJLBO3FABKZScfwXCyoRz?usp=sharing

2018-04-03 07:49:04 -0600 commented answer Running tensorflow graph through OpenCV gives different result

@dkurt ... its not working ... I checked the model midway and saw the values improved and thought that the problem got s

2018-04-03 07:46:57 -0600 received badge  Critic (source)
2018-04-03 05:15:57 -0600 commented answer Running tensorflow graph through OpenCV gives different result

Your answer solved it ..... initially I was saving using freeze_graph.py from tensorflow .... I saved the model as an in

2018-04-03 05:07:47 -0600 marked best answer Running tensorflow graph through OpenCV gives different result

I am trying to run an single channel image of size = ( 125, 125 ) through a frozen tensorflow graph. The result that I am expecting is a vector which is close to [ 125, 131, 203, 203 ].

My Tensorflow Graph is as follows -> https://pastebin.com/tDw3kTWw

OpenCV code that I am using for running the graph:

Mat image = imread("0_1_125_131_203.png", CV_8UC1);

Mat blob = cv::dnn::blobFromImage(image);

cv::dnn::Net net = cv::dnn::readNetFromTensorflow("output_graph.pb");

net.setInput(blob, "input_node");

Mat result = net.forward("Dense4/fully_connected6/MatMul");

cout << result << endl;

The result that I get using OpenCV forward pass is [47.357735, 49.290573, 81.015984, 87.777328].

This is the Tensorflow code that I am using to run the forward pass:

import cv2

import numpy as np

import tensorflow as tf

import argparse

def load_graph(frozen_graph_filename):

with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

with tf.Graph().as_default() as graph:
    tf.import_graph_def(graph_def, name="prefix")
return graph

image = cv2.imread("0_1_125_131_203.png", cv2.CV_8UC1)
a = np.asarray(image[:,:])
a = np.reshape(a, [1,125,125,1])
parser = argparse.ArgumentParser()
parser.add_argument("--frozen_model_filename", default="output_graph.pb", type=str, help="Frozen model file to import")

args = parser.parse_args()

graph = load_graph(args.frozen_model_filename)

x = graph.get_tensor_by_name('prefix/input_node:0')
y = graph.get_tensor_by_name('prefix/Dense4/fully_connected6/BiasAdd:0')

# We launch a Session
with tf.Session(graph=graph) as sess:
    y_out = sess.run( y, feed_dict={
        x: a
    })
    print("Result", y_out)

The result that I am getting with tensorflow is relatively quite closer to the actual answer i.e. [[ 139.59773254 135.82391357 217.75576782 214.71946716]]. I am confused as to why there are separate results for after running the same graph.

2018-04-03 03:30:52 -0600 commented answer Running tensorflow graph through OpenCV gives different result

Really sorry for wasting your time with wrong edits @dkurt.

2018-04-03 03:30:24 -0600 edited question Running tensorflow graph through OpenCV gives different result

Running tensorflow graph through OpenCV gives different result I am trying to run an single channel image of size = ( 12

2018-04-03 03:29:52 -0600 commented answer Running tensorflow graph through OpenCV gives different result

I apologize for the wrong tensorflow code @dkurt ... the tensorflow code also gets the image of size ( 125, 125 )

2018-04-03 03:16:53 -0600 edited question Running tensorflow graph through OpenCV gives different result

Running tensorflow graph through OpenCV gives different result I am trying to run an single channel image of size = ( 12

2018-04-03 03:15:35 -0600 commented answer Running tensorflow graph through OpenCV gives different result

I apologize for the wrong tensorflow code @dkurt ... the tensorflow code resizes the image first to ( 125, 125 )

2018-04-03 03:15:02 -0600 commented answer Running tensorflow graph through OpenCV gives different result

I apologize for the wrong tensorflow code @dkurt ... the tensorflow code reshapes it to ( 125, 125 )

2018-04-03 03:13:58 -0600 edited question Running tensorflow graph through OpenCV gives different result

Running tensorflow graph through OpenCV gives different result I am trying to run an single channel image of size = ( 12

2018-04-03 02:43:29 -0600 commented answer Running tensorflow graph through OpenCV gives different result

@dkurt .... Have made the edits .... I found adding tensorflow code to the question quite difficilt, hence have added th

2018-04-02 09:18:33 -0600 commented answer Running tensorflow graph through OpenCV gives different result

dkurt .... Have made the edits .... I found adding tensorflow code to the question quite difficilt, hence have added the

2018-04-02 09:16:57 -0600 edited question Running tensorflow graph through OpenCV gives different result

How to set a 1 channel image as an input for cv::dnn::net I am trying to run an single channel image of size = ( 125, 1

2018-04-02 08:45:22 -0600 edited question Running tensorflow graph through OpenCV gives different result

How to set a 1 channel image as an input for cv::dnn::net I am trying to run an single channel image of size = ( 125, 1

2018-04-02 07:39:41 -0600 commented answer Running tensorflow graph through OpenCV gives different result

Thanks for the response dkurt .... I have added the graph reference on the stack overflow link .... Should I add another

2018-04-02 05:29:59 -0600 commented answer Running tensorflow graph through OpenCV gives different result

Thank you for pointing out the error. But I am still facing issue while running the image through OpenCV. I have asked t

2018-04-02 05:28:59 -0600 edited question Running tensorflow graph through OpenCV gives different result

How to set a 1 channel image as an input for cv::dnn::net I am trying to run an single channel image of size = ( 125, 1

2018-04-02 00:51:50 -0600 edited question Running tensorflow graph through OpenCV gives different result

How to set a 1 channel image as an input for cv::dnn::net I am trying to run an single channel image of size = ( 125, 1

2018-04-02 00:51:50 -0600 received badge  Editor (source)
2018-04-02 00:49:02 -0600 asked a question Running tensorflow graph through OpenCV gives different result

How to set a 1 channel image as an input for cv::dnn::net I am trying to run an single channel image of size = ( 125, 1