Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hi, @Robb! It's a quite easy:

Let's retrained graph is called graph.pb. Call optimize_for_inference.py tool to remove an Identity nodes, some training-only nodes, make some fusion (conv+bn):

python ~/tensorflow/tensorflow/python/tools/optimize_for_inference.py \
  --input graph.pb \
  --output opt_graph.pb \
  --frozen_graph True \
  --input_names DecodeJpeg/contents \
  --output_names final_result

Before: image description After: image description

Then remove PlaceholderWithDefault node and preprocessing subgraph by

~/tensorflow/bazel-bin/tensorflow/tools/graph_transforms/transform_graph \
  --in_graph=opt_graph.pb \
  --out_graph=final_graph.pb \
  --inputs=Mul \
  --outputs=final_result \
  --transforms="remove_nodes(op=PlaceholderWithDefault) strip_unused_nodes(type=float, shape=\"1,299,299,3\") sort_by_execution_order"

PlaceholderWithDefault PlaceholderWithDefault

preprocessing subgraph preprocessing subgraph

Enjoy!

import cv2 as cv
import numpy as np

net = cv.dnn.readNetFromTensorflow('final_graph.pb')
inp = np.random.standard_normal([299, 299, 3]).astype(np.float32)
net.setInput(cv.dnn.blobFromImage(inp))
out = net.forward()

Thank you for the interest in OpenCV! All images are done using TensorBoard.