tensorflow / models

Models and examples built with TensorFlow
Other
76.92k stars 45.81k forks source link

use the fine-tune ssdlite mobilenet v2 checkpoints, but got an error: KeyError: "The name 'image_tensor:0' refers to a Tensor which does not exist #5720

Closed zjbaby closed 5 years ago

zjbaby commented 5 years ago

System information What is the top-level directory of the model you are using: research/object_detection Have I written custom code: no OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 16.04 TensorFlow installed from (source or binary): source TensorFlow version (use command below): 1.11 Bazel version (if compiling from source):1.18 CUDA/cuDNN version: 9.0/7.1 GPU model and memory:[GeForce 1080Ti] Exact command to reproduce: using the object_detection/model_main.py as described in tutorials

Describe the problem I try to fine-tune the ssdlite_mobilenet_v2_coco_2018_05_09 model, and I use the checkpoint files (model.ckpt) to predict objects after training, but I got an error: KeyError: "The name 'image_tensor:0' refers to a Tensor which does not exist. The operation, 'image_tensor', does not exist in the graph."

Source code / logs the pipeline.config I used is: (models/research/object_detection/samples/configs/ssdlite_mobilenet_v2_coco.config) the pretrain_weight is from: (https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md) I follow the training steps discribed in (https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/running_locally.md)

the test script I use is as follows: `

with tf.Session() as sess:
    saver = tf.train.import_meta_graph('models/my_train/model.ckpt.meta')
    saver.restore(sess, 'models/my_train/model.ckpt')

    for tensor in sess.graph.get_operations():
        logger.error(tensor.name)

    image_tensor = sess.graph.get_tensor_by_name('image_tensor:0')
    boxes = sess.graph.get_tensor_by_name('detection_boxes:0')
    scores = sess.graph.get_tensor_by_name('detection_scores:0')
    cls = sess.graph.get_tensor_by_name('detection_classes:0')
    num_detections = sess.graph.get_tensor_by_name('num_detections:0')

    image = cv2.imread("data/Image_16.jpg")
    image_data = np.expand_dims(image, axis=0).astype(np.uint8)
    print(image_data.shape)

    start = time.clock()
    (box, score, clses, num_detection) = sess.run([boxes, scores, cls, num_detections], {image_tensor:image_data})

    boxes = box[0]
    scores = score[0]
    classes = clses[0]
    num_detections = num_detection[0]
    print("the count of objects is: ", num_detections)
    im_width = image.shape[1]
    im_height = image.shape[0]

    for i in range((int)(num_detections)):
        print("object ", i, " class id: " , classes[i], "  score: " , scores[i], "  boxes: ", boxes[i])

        y_min = (int)(boxes[i][0] * im_height)
        x_min = (int)(boxes[i][1] * im_width)
        y_max = (int)(boxes[i][2] * im_height)
        x_max = (int)(boxes[i][3] * im_width)
        x = int((x_min + x_max) / 2)
        y = int((y_min + y_max) / 2)
        cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 0, 255), 2)

    cv2.imshow("result", image)
    cv2.waitKey(0)

` and it got an error: Traceback (most recent call last): File "test_tf.py", line 54, in image_tensor = sess.graph.get_tensor_by_name('image_tensor:0') File "/home/cyj/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3664, in get_tensor_by_name return self.as_graph_element(name, allow_tensor=True, allow_operation=False) File "/home/cyj/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3488, in as_graph_element return self._as_graph_element_locked(obj, allow_tensor, allow_operation) File "/home/cyj/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3530, in _as_graph_element_locked "graph." % (repr(name), repr(op_name))) KeyError: "The name 'image_tensor:0' refers to a Tensor which does not exist. The operation, 'image_tensor', does not exist in the graph."

my training process went smoothly, but why the model.ckpt files after training seemed wrong?
I print all the Tensor names in my trained model.ckpt files, Its really haven't the "image_tensor:0" Tensor. its different from the model.ckpt files that provided by (https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md)

netanel-s commented 5 years ago

Have you exported the graph?

zjbaby commented 5 years ago

@netanel-s I export the graph, and the problem is solved, Thank you very much~~~

netanel-s commented 5 years ago

@zjbaby , happy to help :) You can close the issue.

zjbaby commented 5 years ago

OK~

Gavinic commented 5 years ago

Do I need to add additional code in the train.py or trainer.py of the object detection API files? i do not understand "export the graph" could you show me how to do it ? thank you very much!

zjbaby commented 5 years ago

@Gavinic after you fine-tune the model, you can get the .ckpt files. Then you need to export the graph as follows: python object_detection/export_inference_graph.py \ --input_type image_tensor \ --pipeline_config_path object_detection/samples/configs/ssdlite_mobilenet_v2.config \ --trained_checkpoint_prefix model.ckpt-${CHECKPOINT_NUMBER} \ --output_directory exported_graphs Afterwards, you should see a directory named exported_graphs containing the SavedModel and frozen graph.

saastechops1 commented 4 years ago

After exporting the graph , how do i import the frozen graph model created in the output directory in tensorflowjs . Looking at the standard documentation , the tensorflowjs_converter is to be used , but after it it always gives me an error that "KeyError: 'The name "\'detection_boxes" refers to an Operation not in the graph.'"

Here is the conversion command below :

(tensorflowjs) c:>tensorflowjs_converter --input_format=tf_frozen_model --output_node_names='detection_boxes,detection_scores,detection_classes,num_detections' --saved_model_tags=serve C:/model_conv/inference_graph/frozen_inference_graph.pb C:/model_conv/inference_graph/web_model

and the Traceback call error below:

Traceback (most recent call last): File "c:\programdata\anaconda3\envs\tensorflowjs\lib\runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "c:\programdata\anaconda3\envs\tensorflowjs\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "C:\ProgramData\Anaconda3\envs\tensorflowjs\Scripts\tensorflowjs_converter.exe__main__.py", line 7, in File "c:\programdata\anaconda3\envs\tensorflowjs\lib\site-packages\tensorflowjs\converters\converter.py", line 671, in pip_main main([' '.join(sys.argv[1:])]) File "c:\programdata\anaconda3\envs\tensorflowjs\lib\site-packages\tensorflowjs\converters\converter.py", line 675, in main convert(argv[0].split(' ')) File "c:\programdata\anaconda3\envs\tensorflowjs\lib\site-packages\tensorflowjs\converters\converter.py", line 653, in convert tf_saved_model_conversion_v2.convert_tf_frozen_model( File "c:\programdata\anaconda3\envs\tensorflowjs\lib\site-packages\tensorflowjs\converters\tf_saved_model_conversion_v2.py", line 388, in convert_tf_frozen_model optimize_graph(graph, signature, File "c:\programdata\anaconda3\envs\tensorflowjs\lib\site-packages\tensorflowjs\converters\tf_saved_model_conversion_v2.py", line 134, in optimize_graph graph.add_to_collection('train_op', graph.get_operation_by_name(name)) File "c:\programdata\anaconda3\envs\tensorflowjs\lib\site-packages\tensorflow\python\framework\ops.py", line 3640, in get_operation_by_name return self.as_graph_element(name, allow_tensor=False, allow_operation=True) File "c:\programdata\anaconda3\envs\tensorflowjs\lib\site-packages\tensorflow\python\framework\ops.py", line 3512, in as_graph_element return self._as_graph_element_locked(obj, allow_tensor, allow_operation) File "c:\programdata\anaconda3\envs\tensorflowjs\lib\site-packages\tensorflow\python\framework\ops.py", line 3571, in _as_graph_element_locked raise KeyError("The name %s refers to an Operation not in the " KeyError: 'The name "\'detection_boxes" refers to an Operation not in the graph.'

My conda environment is: (tensorflowjs) c:>pip list Package Version


absl-py 0.9.0 astunparse 1.6.3 cachetools 4.1.0 certifi 2020.4.5.1 chardet 3.0.4 gast 0.3.3 google-auth 1.15.0 google-auth-oauthlib 0.4.1 google-pasta 0.2.0 grpcio 1.29.0 h5py 2.10.0 idna 2.9 Keras-Preprocessing 1.1.2 Markdown 3.2.2 numpy 1.18.4 oauthlib 3.1.0 opt-einsum 3.2.1 pip 20.0.2 prompt-toolkit 1.0.14 protobuf 3.11.3 pyasn1 0.4.8 pyasn1-modules 0.2.8 Pygments 2.6.1 PyInquirer 1.0.3 regex 2020.5.14 requests 2.23.0 requests-oauthlib 1.3.0 rsa 4.0 scipy 1.4.1 setuptools 46.4.0.post20200518 six 1.14.0 tensorboard 2.2.1 tensorboard-plugin-wit 1.6.0.post3 tensorflow-cpu 2.2.0 tensorflow-estimator 2.2.0 tensorflow-hub 0.7.0 tensorflowjs 1.7.4.post1 termcolor 1.1.0 urllib3 1.25.9 wcwidth 0.1.9 Werkzeug 1.0.1 wheel 0.34.2 wincertstore 0.2 wrapt 1.12.1

The environment in which object_detection_api was used is python=3.5 and tensorflow=1.5

Kindly Help , as I want to consume the trained model in tensorflowjs.