Quantized ssd_mobilenet_v1_coco_11_06_2017 model is giving incorrect results
System information
- OS Platform and Distribution: Linux Ubuntu 16.04
- TensorFlow installed from: source
- TensorFlow version: r1.2
- Bazel version: compiling from source, using bazel release 0.4.5
Describe the problem
The frozen graph file downloaded is working as expected, however after quantization the accuracy drops significantly (mostly random predictions)
Source code / logs
I built tensorflow r1.2 (for CPU only) from source, and used following method to quantize:
bazel-bin/tensorflow/tools/graph_transforms/transform_graph --in_graph=frozen_inference_graph.pb --out_graph=optimized_graph.pb --inputs='image_tensor' --outputs='detection_boxes','detection_scores','detection_classes','num_detections' --transforms='add_default_attributes strip_unused_nodes(type=float, shape="1,224,224,3") fold_constants(ignore_errors=true) fold_batch_norms fold_old_batch_norms quantize_weights strip_unused_nodes sort_by_execution_order'
I tried various combinations in the "transforms" part, and the transforms mentioned above gave sometimes correct predictions, however no where close to the original model.
Is there any way to improve performance of the quantized model or to quantize this model?
Here is the test script used to generate the results:
import cv2 import tensorflow as tf import numpy as np from tensorflow.python.framework import graph_util
detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile("frozen_inference_graph.pb", 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='')
sess = tf.Session(graph=detection_graph)
img = 'IMG_1.jpg' frame = cv2.imread(img) image_np = frame[:,:,::-1]
image_np_expanded = np.expand_dims(image_np, axis=0) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') boxes = detection_graph.get_tensor_by_name('detection_boxes:0') scores = detection_graph.get_tensor_by_name('detection_scores:0') classes = detection_graph.get_tensor_by_name('detection_classes:0')
(boxes, scores, classes) = sess.run( [boxes, scores, classes], feed_dict={image_tensor: image_np_expanded})
@jch1, it's not clear to me whether you ever intended this model to be quantized, but it seems worth asking whether you have any suggestions for @chandrakantkhandelwal
could you give me the quantize_graph.pb ,I want it so long. my email adress is :[email protected] Thanks very much
sorry I want your optimized_graph.pb file very much ,could you give it to me ,I need it very much ,my email is :[email protected] !
You could use the round_weights(num_steps=512) ,I just test the result is nearly the real result.
@wujiahongPKU did you manage to quantize the frozen graph?
Please try this updated set of instructions: https://medium.com/tensorflow/training-and-serving-a-realtime-mobile-object-detector-in-30-minutes-with-cloud-tpus-b78971cf1193