Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

your .pbtxt file isn't useful "as is", you have to edit it a bit:

# Read the graph.
with tf.gfile.FastGFile('lenet.pb', 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

with tf.Session() as sess:
    # Restore session
    sess.graph.as_default()
    tf.import_graph_def(graph_def, name='')

# Strip Const nodes.
for i in reversed(range(len(graph_def.node))):
    if graph_def.node[i].op == 'Const':
        del graph_def.node[i]
    for attr in ['T', 'data_format', 'Tshape', 'N', 'Tidx', 'Tdim',
                  'use_cudnn_on_gpu', 'Index', 'Tperm', 'is_training',
                  'Tpaddings']:
        if attr in graph_def.node[i].attr:
             del graph_def.node[i].attr[attr]

# Save stripped model.
tf.train.write_graph(graph_def, "", 'lenet2.pbtxt', as_text=True)

then, you have to manually replace all flatten nodes with a single Flatten one,and rewire the inputs. the end of your .pbtxt should look like this:

node {
  name: "max_pooling2d_1/MaxPool"
  op: "MaxPool"
  input: "activation_1/Relu"
  attr {
    key: "ksize"
    value {
      list {
        i: 1
        i: 2
        i: 2
        i: 1
      }
    }
  }
  attr {
    key: "padding"
    value {
      s: "VALID"
    }
  }
  attr {
    key: "strides"
    value {
      list {
        i: 1
        i: 2
        i: 2
        i: 1
      }
    }
  }
}
node {
  name: "flatten/Shape"
  op: "Flatten"
  input: "max_pooling2d_1/MaxPool"
  attr {
    key: "out_type"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "dense/kernel/read"
  op: "Identity"
  input: "dense/kernel"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@dense/kernel"
      }
    }
  }
}
node {
  name: "dense/bias/read"
  op: "Identity"
  input: "dense/bias"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@dense/bias"
      }
    }
  }
}
node {
  name: "dense/MatMul"
  op: "MatMul"
  input: "flatten/Shape"
  input: "dense/kernel/read"
  attr {
    key: "transpose_a"
    value {
      b: false
    }
  }
  attr {
    key: "transpose_b"
    value {
      b: false
    }
  }
}
node {
  name: "dense/BiasAdd"
  op: "BiasAdd"
  input: "dense/MatMul"
  input: "dense/bias/read"
}
node {
  name: "activation_2/Relu"
  op: "Relu"
  input: "dense/BiasAdd"
}
node {
  name: "dense_1/kernel/read"
  op: "Identity"
  input: "dense_1/kernel"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@dense_1/kernel"
      }
    }
  }
}
node {
  name: "dense_1/bias/read"
  op: "Identity"
  input: "dense_1/bias"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@dense_1/bias"
      }
    }
  }
}
node {
  name: "dense_1/MatMul"
  op: "MatMul"
  input: "activation_2/Relu"
  input: "dense_1/kernel/read"
  attr {
    key: "transpose_a"
    value {
      b: false
    }
  }
  attr {
    key: "transpose_b"
    value {
      b: false
    }
  }
}
node {
  name: "dense_1/BiasAdd"
  op: "BiasAdd"
  input: "dense_1/MatMul"
  input: "dense_1/bias/read"
}
node {
  name: "activation_3/Softmax"
  op: "Softmax"
  input: "dense_1/BiasAdd"
}

then we can run it from opencv:

import cv2 as cv

cvNet = cv.dnn.readNetFromTensorflow('lenet.pb', 'lenet2.pbtxt')

img = cv.imread('blue_car.jpg')
cvNet.setInput(cv.dnn.blobFromImage(img, size=(40, 40), swapRB=False, crop=False))
cvOut = cvNet.forward()    

cvOut = cvOut.flatten()
classId = np.argmax(cvOut)
confidence = cvOut[classId]
labels = ["black", "blue", "gray", "green", "red", "white", "yellow"]
label = labels[classId]

cv.putText(img, label, (100, 130), cv.FONT_HERSHEY_SIMPLEX,  3.7, (0, 0, 0), 5)
cv.imshow('img', img)
cv.waitKey()

your .pbtxt file isn't useful "as is", you have to edit it a bit:

# Read the graph.
with tf.gfile.FastGFile('lenet.pb', 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

with tf.Session() as sess:
    # Restore session
    sess.graph.as_default()
    tf.import_graph_def(graph_def, name='')

# Strip Const nodes.
for i in reversed(range(len(graph_def.node))):
    if graph_def.node[i].op == 'Const':
        del graph_def.node[i]
    for attr in ['T', 'data_format', 'Tshape', 'N', 'Tidx', 'Tdim',
                  'use_cudnn_on_gpu', 'Index', 'Tperm', 'is_training',
                  'Tpaddings']:
        if attr in graph_def.node[i].attr:
             del graph_def.node[i].attr[attr]

# Save stripped model.
tf.train.write_graph(graph_def, "", 'lenet2.pbtxt', as_text=True)

then, you have to manually replace all flatten nodes with a single Flatten one,and rewire the inputs. the end of your .pbtxt should look like this:

node {
  name: "max_pooling2d_1/MaxPool"
  op: "MaxPool"
  input: "activation_1/Relu"
  attr {
    key: "ksize"
    value {
      list {
        i: 1
        i: 2
        i: 2
        i: 1
      }
    }
  }
  attr {
    key: "padding"
    value {
      s: "VALID"
    }
  }
  attr {
    key: "strides"
    value {
      list {
        i: 1
        i: 2
        i: 2
        i: 1
      }
    }
  }
}
node {
  name: "flatten/Shape"
  op: "Flatten"
  input: "max_pooling2d_1/MaxPool"
  attr {
    key: "out_type"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "dense/kernel/read"
  op: "Identity"
  input: "dense/kernel"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@dense/kernel"
      }
    }
  }
}
node {
  name: "dense/bias/read"
  op: "Identity"
  input: "dense/bias"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@dense/bias"
      }
    }
  }
}
node {
  name: "dense/MatMul"
  op: "MatMul"
  input: "flatten/Shape"
  input: "dense/kernel/read"
  attr {
    key: "transpose_a"
    value {
      b: false
    }
  }
  attr {
    key: "transpose_b"
    value {
      b: false
    }
  }
}
node {
  name: "dense/BiasAdd"
  op: "BiasAdd"
  input: "dense/MatMul"
  input: "dense/bias/read"
}
node {
  name: "activation_2/Relu"
  op: "Relu"
  input: "dense/BiasAdd"
}
node {
  name: "dense_1/kernel/read"
  op: "Identity"
  input: "dense_1/kernel"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@dense_1/kernel"
      }
    }
  }
}
node {
  name: "dense_1/bias/read"
  op: "Identity"
  input: "dense_1/bias"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@dense_1/bias"
      }
    }
  }
}
node {
  name: "dense_1/MatMul"
  op: "MatMul"
  input: "activation_2/Relu"
  input: "dense_1/kernel/read"
  attr {
    key: "transpose_a"
    value {
      b: false
    }
  }
  attr {
    key: "transpose_b"
    value {
      b: false
    }
  }
}
node {
  name: "dense_1/BiasAdd"
  op: "BiasAdd"
  input: "dense_1/MatMul"
  input: "dense_1/bias/read"
}
node {
  name: "activation_3/Softmax"
  op: "Softmax"
  input: "dense_1/BiasAdd"
}

then we can run it from opencv:

import cv2 as cv

cvNet = cv.dnn.readNetFromTensorflow('lenet.pb', 'lenet2.pbtxt')

img = cv.imread('blue_car.jpg')
cvNet.setInput(cv.dnn.blobFromImage(img, size=(40, 40), swapRB=False, crop=False))
cvOut = cvNet.forward()    

cvOut = cvOut.flatten()
classId = np.argmax(cvOut)
confidence = cvOut[classId]
labels = ["black", "blue", "gray", "green", "red", "white", "yellow"]
label = labels[classId]

cv.putText(img, label, (100, 130), cv.FONT_HERSHEY_SIMPLEX,  3.7, (0, 0, 0), 5)
cv.imshow('img', img)
cv.waitKey()

please also have a look at the answer here , where most of this came from.

your .pbtxt file isn't useful "as is", you have to edit it a bit:

# Read the graph.
with tf.gfile.FastGFile('lenet.pb', 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

with tf.Session() as sess:
    # Restore session
    sess.graph.as_default()
    tf.import_graph_def(graph_def, name='')

# Strip Const nodes.
for i in reversed(range(len(graph_def.node))):
    if graph_def.node[i].op == 'Const':
        del graph_def.node[i]
    for attr in ['T', 'data_format', 'Tshape', 'N', 'Tidx', 'Tdim',
                  'use_cudnn_on_gpu', 'Index', 'Tperm', 'is_training',
                  'Tpaddings']:
        if attr in graph_def.node[i].attr:
             del graph_def.node[i].attr[attr]

# Save stripped model.
tf.train.write_graph(graph_def, "", 'lenet2.pbtxt', as_text=True)

then, you have to manually replace all flatten nodes with a single Flatten one,and rewire the inputs. the end of your .pbtxt should look like this:

node {
  name: "max_pooling2d_1/MaxPool"
  op: "MaxPool"
  input: "activation_1/Relu"
  attr {
    key: "ksize"
    value {
      list {
        i: 1
        i: 2
        i: 2
        i: 1
      }
    }
  }
  attr {
    key: "padding"
    value {
      s: "VALID"
    }
  }
  attr {
    key: "strides"
    value {
      list {
        i: 1
        i: 2
        i: 2
        i: 1
      }
    }
  }
}
node {
  name: "flatten/Shape"
  op: "Flatten"
  input: "max_pooling2d_1/MaxPool"
  attr {
    key: "out_type"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "dense/kernel/read"
  op: "Identity"
  input: "dense/kernel"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@dense/kernel"
      }
    }
  }
}
node {
  name: "dense/bias/read"
  op: "Identity"
  input: "dense/bias"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@dense/bias"
      }
    }
  }
}
node {
  name: "dense/MatMul"
  op: "MatMul"
  input: "flatten/Shape"
  input: "dense/kernel/read"
  attr {
    key: "transpose_a"
    value {
      b: false
    }
  }
  attr {
    key: "transpose_b"
    value {
      b: false
    }
  }
}
node {
  name: "dense/BiasAdd"
  op: "BiasAdd"
  input: "dense/MatMul"
  input: "dense/bias/read"
}
node {
  name: "activation_2/Relu"
  op: "Relu"
  input: "dense/BiasAdd"
}
node {
  name: "dense_1/kernel/read"
  op: "Identity"
  input: "dense_1/kernel"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@dense_1/kernel"
      }
    }
  }
}
node {
  name: "dense_1/bias/read"
  op: "Identity"
  input: "dense_1/bias"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@dense_1/bias"
      }
    }
  }
}
node {
  name: "dense_1/MatMul"
  op: "MatMul"
  input: "activation_2/Relu"
  input: "dense_1/kernel/read"
  attr {
    key: "transpose_a"
    value {
      b: false
    }
  }
  attr {
    key: "transpose_b"
    value {
      b: false
    }
  }
}
node {
  name: "dense_1/BiasAdd"
  op: "BiasAdd"
  input: "dense_1/MatMul"
  input: "dense_1/bias/read"
}
node {
  name: "activation_3/Softmax"
  op: "Softmax"
  input: "dense_1/BiasAdd"
}

(the whole file can be found on a gist)

then we can run it from opencv:

import cv2 as cv

cvNet = cv.dnn.readNetFromTensorflow('lenet.pb', 'lenet2.pbtxt')

img = cv.imread('blue_car.jpg')
cvNet.setInput(cv.dnn.blobFromImage(img, size=(40, 40), swapRB=False, crop=False))
cvOut = cvNet.forward()    

cvOut = cvOut.flatten()
classId = np.argmax(cvOut)
confidence = cvOut[classId]
labels = ["black", "blue", "gray", "green", "red", "white", "yellow"]
label = labels[classId]

cv.putText(img, label, (100, 130), cv.FONT_HERSHEY_SIMPLEX,  3.7, (0, 0, 0), 5)
cv.imshow('img', img)
cv.waitKey()

please also have a look at the answer here , where most of this came from.

your .pbtxt file isn't useful "as is", you have to edit it a bit:

# Read the graph.
with tf.gfile.FastGFile('lenet.pb', 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

with tf.Session() as sess:
    # Restore session
    sess.graph.as_default()
    tf.import_graph_def(graph_def, name='')

# Strip Const nodes.
for i in reversed(range(len(graph_def.node))):
    if graph_def.node[i].op == 'Const':
        del graph_def.node[i]
    for attr in ['T', 'data_format', 'Tshape', 'N', 'Tidx', 'Tdim',
                  'use_cudnn_on_gpu', 'Index', 'Tperm', 'is_training',
                  'Tpaddings']:
        if attr in graph_def.node[i].attr:
             del graph_def.node[i].attr[attr]

# Save stripped model.
tf.train.write_graph(graph_def, "", 'lenet2.pbtxt', as_text=True)

then, you have to manually replace all flatten nodes with a single Flatten one,and rewire the inputs. the end of your .pbtxt should look like this:

node {
  name: "max_pooling2d_1/MaxPool"
  op: "MaxPool"
  input: "activation_1/Relu"
  attr {
    key: "ksize"
    value {
      list {
        i: 1
        i: 2
        i: 2
        i: 1
      }
    }
  }
  attr {
    key: "padding"
    value {
      s: "VALID"
    }
  }
  attr {
    key: "strides"
    value {
      list {
        i: 1
        i: 2
        i: 2
        i: 1
      }
    }
  }
}
node {
  name: "flatten/Shape"
  op: "Flatten"
  input: "max_pooling2d_1/MaxPool"
  attr {
    key: "out_type"
    value {
      type: DT_INT32
    }
  }
}
node {
  name: "dense/kernel/read"
  op: "Identity"
  input: "dense/kernel"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@dense/kernel"
      }
    }
  }
}
node {
  name: "dense/bias/read"
  op: "Identity"
  input: "dense/bias"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@dense/bias"
      }
    }
  }
}
node {
  name: "dense/MatMul"
  op: "MatMul"
  input: "flatten/Shape"
  input: "dense/kernel/read"
  attr {
    key: "transpose_a"
    value {
      b: false
    }
  }
  attr {
    key: "transpose_b"
    value {
      b: false
    }
  }
}
node {
  name: "dense/BiasAdd"
  op: "BiasAdd"
  input: "dense/MatMul"
  input: "dense/bias/read"
}
node {
  name: "activation_2/Relu"
  op: "Relu"
  input: "dense/BiasAdd"
}
node {
  name: "dense_1/kernel/read"
  op: "Identity"
  input: "dense_1/kernel"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@dense_1/kernel"
      }
    }
  }
}
node {
  name: "dense_1/bias/read"
  op: "Identity"
  input: "dense_1/bias"
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@dense_1/bias"
      }
    }
  }
}
node {
  name: "dense_1/MatMul"
  op: "MatMul"
  input: "activation_2/Relu"
  input: "dense_1/kernel/read"
  attr {
    key: "transpose_a"
    value {
      b: false
    }
  }
  attr {
    key: "transpose_b"
    value {
      b: false
    }
  }
}
node {
  name: "dense_1/BiasAdd"
  op: "BiasAdd"
  input: "dense_1/MatMul"
  input: "dense_1/bias/read"
}
node {
  name: "activation_3/Softmax"
  op: "Softmax"
  input: "dense_1/BiasAdd"
}

(the whole file can be found on a gist)

then we can run it from opencv:

import cv2 as cv

cvNet = cv.dnn.readNetFromTensorflow('lenet.pb', 'lenet2.pbtxt')

img = cv.imread('blue_car.jpg')
cvNet.setInput(cv.dnn.blobFromImage(img, size=(40, 40), swapRB=False, crop=False))
cvOut = cvNet.forward()    

cvOut = cvOut.flatten()
classId = np.argmax(cvOut)
confidence = cvOut[classId]
labels = ["black", "blue", "gray", "green", "red", "white", "yellow"]
label = labels[classId]

cv.putText(img, label, (100, 130), cv.FONT_HERSHEY_SIMPLEX,  3.7, (0, 0, 0), 5)
cv.imshow('img', img)
cv.waitKey()

import matplotlib.pyplot as plt
orig = cv.cvtColor(img, cv.COLOR_BGR2RGB)
plt.imshow(orig)

hey, it got it wrong ;)

please also have a look at the answer here , where most of this came from.