Ask Your Question
0

(dnn/tensorflow) Very different results tf X dnn

asked 2017-11-09 11:00:15 -0600

Hello, I created a keras model and converted to tensorflow, I had some troubles but after couples days the dnn module imported the network from tensorflow sucessfull. Now I got other problem that I dont find any solution, Using tensorflow I got good predictions, same predictions using keras, but using the DNN module I got very different results, using this module I tried to create the blobs manually and using the blob function (dnn.blobFromImage), each method got really differents results, and I dont know why.

using "manual" blob

CLASS | SCORE

3 0.278939

5 0.49932

5 0.742838

7 0.504909

using blob function

CLASS | SCORE

5 0.257985

5 0.372841

5 0.511958

7 0.233953

using tensorflow

CLASS | SCORE

6 0.999519

8 0.999179

7 0.999713

6 0.999958

For a better investigation I created a simples network based on LeNet to predict the MNIST dataset, but again after all training I got differentes results too.

here is a piece of my code.

to use tensorflow model

   versions:
    Python 64bits for Windows=3.5.3
    Tensorflow=1.4.0
    sklearn=0.19.0
    numpy=1.13.3
    opencv=3.2.0 (for python 64bits)   

from tensorflow.python.platform import gfile
from sklearn import datasets
import tensorflow as tf
import numpy as np
import time
import cv2

dataset = datasets.fetch_mldata("MNIST Original")

data = dataset.data.reshape((dataset.data.shape[0], 28, 28))
data = data[:, :, :, np.newaxis] #For tensorflow image ordering
data = np.float32(data/255.0)
with tf.Session() as persisted_sess:
  print("load graph")
  with gfile.FastGFile("Models/MNISTLenet_final.pb",'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    persisted_sess.graph.as_default()
    tf.import_graph_def(graph_def, name='')
    output=persisted_sess.graph.get_tensor_by_name("activation_4/Softmax:0")
    inputCnn=persisted_sess.graph.get_tensor_by_name("conv2d_1_input:0")
    np.random.seed(0)
    t=time.time()    
    for _ in range(10):
        i=np.uint32(np.random.rand()*data.shape[0])
        im=data[i]
        im=im[np.newaxis,:]
        result = persisted_sess.run(output, {inputCnn: im})
        print(result.argmax(),result.max())

        im=im[0,:,:,0]
        im*=255
        im=np.uint8(im)
        cv2.imshow("number",im)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    print("speed",time.time()-t,"secs")

and to use with Opencv DNN module:

versions:

Python 32bits for Windows=3.5.3 Tensorflow=1.4.0 sklearn=0.18.2 numpy=1.13.3 opencv=3.3.1

from sklearn import datasets
import numpy as np
import cv2
import time

dataset = datasets.fetch_mldata("MNIST Original")

data = dataset.data.reshape((dataset.data.shape[0], 28, 28))
data = data[:,np.newaxis,:,:] #For dnn module image ordering
data = np.float32(data/255.0)

net = cv2.dnn.readNetFromTensorflow("Models/MNISTLenet.pb")
np.random.seed(0)
t=time.time()

#using "manual" blob creation
for _ in range(10):
    i=np.uint32(np.random.rand()*data.shape[0])
    im=data[i]
    im=im[np.newaxis,:]
    net.setInput(im,"conv2d_1_input")
    prob = net.forward("activation_4/Softmax")
    print(prob[0,:].argmax(),prob[0,:].max())

    im=im[0,0,:,:]
    im*=255
    im=np.uint8(im)
    cv2.imshow ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-11-13 08:53:54 -0600

here it is the problem: #9177

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2017-11-09 11:00:15 -0600

Seen: 97 times

Last updated: Nov 13