Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError: "The name 'net/images:0' refers to a Tensor which does not exist. The operation, 'net/images', does not exist in the graph." #22

Open
gasatig opened this issue Jun 28, 2020 · 27 comments

Comments

@gasatig
Copy link

gasatig commented Jun 28, 2020

getting this error

@AleBasso80
Copy link

I get the same error.

@ikoc
Copy link

ikoc commented Jul 10, 2020

i got the same error too

1 similar comment
@Abdktefane
Copy link

i got the same error too

@namwoo-konkuk
Copy link

namwoo-konkuk commented Jul 13, 2020

in my case, I was able to run with changing input&output node name.

Try to change "net/%s:0" => "%s:0" 83 & 85 lines in 'tools/generate_detections.py'

@ikoc
Copy link

ikoc commented Jul 13, 2020

I was using python 3.8 but some of the libs are not suitable with that. I installed python 3.7 , install requirements-gpu.txt then everything worked without problem.

@gasatig
Copy link
Author

gasatig commented Jul 13, 2020

in my case, I was able to run with changing input&output node name.

Try to change "net/%s:0" => "%s:0" 83 & 85 lines in 'tools/generate_detections.py'

this solved my error

@Kwonkyu
Copy link

Kwonkyu commented Aug 12, 2020

in my case, I was able to run with changing input&output node name.

Try to change "net/%s:0" => "%s:0" 83 & 85 lines in 'tools/generate_detections.py'

this solved my error too!

@duyluandethuong
Copy link

in my case, I was able to run with changing input&output node name.

Try to change "net/%s:0" => "%s:0" 83 & 85 lines in 'tools/generate_detections.py'

This solve my error running object_tracking.py on a MacBook Pro

@robisen1
Copy link

in my case, I was able to run with changing input&output node name.

Try to change "net/%s:0" => "%s:0" 83 & 85 lines in 'tools/generate_detections.py'

worked for me. thank you

@bharath5673
Copy link

bharath5673 commented Sep 19, 2020

changing this "net/%s:0" to "%s:0" got solved.................. #yolov4

@Shriram-Coder99
Copy link

in my case, I was able to run with changing input&output node name.

Try to change "net/%s:0" => "%s:0" 83 & 85 lines in 'tools/generate_detections.py'

Worked like a charm

@cristiandapp
Copy link

I tried it, but it doesn't work for me, by the way I'm using colab, what should I do?

@Bhaskar476
Copy link

encoder = gdet.create_box_encoder(model_filename, batch_size=1)

KeyError: "The name 'net/images:0' refers to a Tensor which does not exist. The operation, 'net/images', does not exist in the graph."

I am having this error in the given line.
I am doing this code in google colab.

I have also changed the code in line 84 & 86 with "%s:0". But showing same result again.

@Bhaskar476
Copy link

encoder = gdet.create_box_encoder(model_filename, batch_size=1)

KeyError: "The name 'net/images:0' refers to a Tensor which does not exist. The operation, 'net/images', does not exist in the graph."

I am having this error in the given line.
I am doing this code in google colab.

I have also changed the code in line 84 & 86 with "%s:0". But showing same result again.
Please help me to solve this error.

@Bhaskar476
Copy link

in my case, I was able to run with changing input&output node name.

Try to change "net/%s:0" => "%s:0" 83 & 85 lines in 'tools/generate_detections.py'

encoder = gdet.create_box_encoder(model_filename, batch_size=1)

KeyError: "The name 'net/images:0' refers to a Tensor which does not exist. The operation, 'net/images', does not exist in the graph."

I am having this error in the given line.
I am doing this code in google colab.

I have also changed the code in line 84 & 86 with "%s:0". But showing same result again.

@ektaarora3501
Copy link

If you are running on google colab, try restarting your runtime after making the above changes. It worked for me

@Teezq
Copy link

Teezq commented Jul 26, 2021

@namwoo-konkuk Your solution solves my problems, the output is able to run now. However, there is no detection or bounding box at my output video. I have manually printed out the boxs, scores and classes, but these parameters only return values on the first frame while other remaining frames just remains an empty list. Does anyone here encounter such issue?
@theAIGuysCode Could this be the reason that I were only able to generate the 3 files when converting yolo to tensorflow: checkpoint, yolov3.tf.data-00000-of-00001, yolov3.tf.index.

@BakingBrains
Copy link

@namwoo-konkuk Your solution solves my problems, the output is able to run now. However, there is no detection or bounding box at my output video. I have manually printed out the boxs, scores and classes, but these parameters only return values on the first frame while other remaining frames just remains an empty list. Does anyone here encounter such issue?
@theAIGuysCode Could this be the reason that I were only able to generate the 3 files when converting yolo to tensorflow: checkpoint, yolov3.tf.data-00000-of-00001, yolov3.tf.index.

Same problem for me. May I know how did you resolve?

@Teezq
Copy link

Teezq commented Aug 7, 2021

@BakingBrains I have resolved it by downgrading the python to 3.7 and install libraries that are older version

@senemaktas
Copy link

change generate_detections.py file in this part with below code , also import .:

import tensorflow.compat.v1 as tf

class ImageEncoder(object):

    def __init__(self, checkpoint_filename, input_name="images",
                 output_name="features"):
        self.session = tf.Session()
        with tf.gfile.GFile(checkpoint_filename, "rb") as file_handle:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(file_handle.read())
        tf.import_graph_def(graph_def, name="net")
        self.input_var = tf.get_default_graph().get_tensor_by_name(
            "%s:0" % input_name)
        self.output_var = tf.get_default_graph().get_tensor_by_name(
            "%s:0" % output_name)

        assert len(self.output_var.get_shape()) == 2
        assert len(self.input_var.get_shape()) == 4
        self.feature_dim = self.output_var.get_shape().as_list()[-1]
        self.image_shape = self.input_var.get_shape().as_list()[1:]

    def __call__(self, data_x, batch_size=32):
        out = np.zeros((len(data_x), self.feature_dim), np.float32)
        _run_in_batches(
            lambda x: self.session.run(self.output_var, feed_dict=x),
            {self.input_var: data_x}, out, batch_size)
        return out 

Haantzy added a commit to Haantzy/YOLO_for_Real-Time_Object_Counting that referenced this issue Sep 25, 2021
@chenzhuotoday
Copy link

i got the same error too

import tensorflow.compat.v1 as tf
tf.compat.v1.disable_v2_behavior()
it can fix the problem

@Lucio8930
Copy link

@BakingBrains I have resolved it by downgrading the python to 3.7 and install libraries that are older version

i got the same error
I couldn't solve the problem even with python3.7

@DaniloAlves1995
Copy link

in my case, I was able to run with changing input&output node name.

Try to change "net/%s:0" => "%s:0" 83 & 85 lines in 'tools/generate_detections.py'

This solved my problem. Thank you!

@Akfait
Copy link

Akfait commented Jun 22, 2022

in my case, I was able to run with changing input&output node name.

Try to change "net/%s:0" => "%s:0" 83 & 85 lines in 'tools/generate_detections.py'

After I've done it I am having a new issue:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\User\PycharmProjects\DeepSortV3_pip\deepsort_v3\tracker.py", line 101, in runTracker
    yolo = YoloV3()
  File "C:\Users\User\PycharmProjects\DeepSortV3_pip\deepsort_v3\yolov3_tf2\models.py", line 232, in YoloV3
    outputs = Lambda(lambda x: yolo_nms(x, anchors, masks, classes),
  File "C:\Users\User\PycharmProjects\DeepSortV3_pip\venv\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\User\PycharmProjects\DeepSortV3_pip\deepsort_v3\yolov3_tf2\models.py", line 232, in <lambda>
    outputs = Lambda(lambda x: yolo_nms(x, anchors, masks, classes),
  File "C:\Users\User\PycharmProjects\DeepSortV3_pip\deepsort_v3\yolov3_tf2\models.py", line 195, in yolo_nms
    max_output_size_per_class=FLAGS.yolo_max_boxes,
  File "C:\Users\User\PycharmProjects\DeepSortV3_pip\venv\lib\site-packages\absl\flags\_flagvalues.py", line 478, in __getattr__
    raise _exceptions.UnparsedFlagAccessError(
absl.flags._exceptions.UnparsedFlagAccessError: Exception encountered when calling layer "yolo_nms" (type Lambda).

Trying to access flag --yolo_max_boxes before flags were parsed.

Call arguments received by layer "yolo_nms" (type Lambda):
  • inputs=(('tf.Tensor(shape=(None, None, None, 3, 4), dtype=float32)', 'tf.Tensor(shape=(None, None, None, 3, 1), dtype=float32)', 'tf.Tensor(shape=(None, None, None, 3, 80), dtype=float32)'), ('tf.Tensor(shape=(None, None, None, 3, 4), dtype=float32)', 'tf.Tensor(shape=(None, None, None, 3, 1), dtype=float32)', 'tf.Tensor(shape=(None, None, None, 3, 80), dtype=float32)'), ('tf.Tensor(shape=(None, None, None, 3, 4), dtype=float32)', 'tf.Tensor(shape=(None, None, None, 3, 1), dtype=float32)', 'tf.Tensor(shape=(None, None, None, 3, 80), dtype=float32)'))
  • mask=None
  • training=None

@Karthiktmv
Copy link

@BakingBrains I have resolved it by downgrading the python to 3.7 and install libraries that are older version

@Teezq Can you share the other library versions

@mukeshnaidu
Copy link

vim: expandtab:ts=4:sw=4

import os
import errno
import argparse
import numpy as np
import cv2
import tensorflow as tf

def _run_in_batches(f, data_dict, out, batch_size):
data_len = len(out)
num_batches = int(data_len / batch_size)

s, e = 0, 0
for i in range(num_batches):
    s, e = i * batch_size, (i + 1) * batch_size
    batch_data_dict = {k: v[s:e] for k, v in data_dict.items()}
    out[s:e] = f(batch_data_dict)
if e < len(out):
    batch_data_dict = {k: v[e:] for k, v in data_dict.items()}
    out[e:] = f(batch_data_dict)

def extract_image_patch(image, bbox, patch_shape):
"""Extract image patch from bounding box.

Parameters
----------
image : ndarray
    The full image.
bbox : array_like
    The bounding box in format (x, y, width, height).
patch_shape : Optional[array_like]
    This parameter can be used to enforce a desired patch shape
    (height, width). First, the `bbox` is adapted to the aspect ratio
    of the patch shape, then it is clipped at the image boundaries.
    If None, the shape is computed from :arg:`bbox`.

Returns
-------
ndarray | NoneType
    An image patch showing the :arg:`bbox`, optionally reshaped to
    :arg:`patch_shape`.
    Returns None if the bounding box is empty or fully outside of the image
    boundaries.

"""
bbox = np.array(bbox)
if patch_shape is not None:
    # correct aspect ratio to patch shape
    target_aspect = float(patch_shape[1]) / patch_shape[0]
    new_width = target_aspect * bbox[3]
    bbox[0] -= (new_width - bbox[2]) / 2
    bbox[2] = new_width

# convert to top left, bottom right
bbox[2:] += bbox[:2]
bbox = bbox.astype(np.int)

# clip at image boundaries
bbox[:2] = np.maximum(0, bbox[:2])
bbox[2:] = np.minimum(np.asarray(image.shape[:2][::-1]) - 1, bbox[2:])
if np.any(bbox[:2] >= bbox[2:]):
    return None
sx, sy, ex, ey = bbox
image = image[sy:ey, sx:ex]
image = cv2.resize(image, tuple(patch_shape[::-1]))
return image

class ImageEncoder(object):

def __init__(self, checkpoint_filename, input_name="images",
             output_name="features"):
    self.session = tf.compat.v1.Session()
    with tf.compat.v1.gfile.GFile(checkpoint_filename, "rb") as file_handle:
        graph_def = tf.compat.v1.GraphDef()
        graph_def.ParseFromString(file_handle.read())
    tf.compat.v1.import_graph_def(graph_def, name="net")
    self.input_var = tf.compat.v1.get_default_graph().get_tensor_by_name(
        "%s:0" % input_name)
    self.output_var = tf.compat.v1.get_default_graph().get_tensor_by_name(
        "%s:0" % output_name)

    assert len(self.output_var.get_shape()) == 2
    assert len(self.input_var.get_shape()) == 4
    self.feature_dim = self.output_var.get_shape().as_list()[-1]
    self.image_shape = self.input_var.get_shape().as_list()[1:]

def __call__(self, data_x, batch_size=32):
    out = np.zeros((len(data_x), self.feature_dim), np.float32)
    _run_in_batches(
        lambda x: self.session.run(self.output_var, feed_dict=x),
        {self.input_var: data_x}, out, batch_size)
    return out

def create_box_encoder(model_filename, input_name="images",
output_name="features", batch_size=32):
image_encoder = ImageEncoder(model_filename, input_name, output_name)
image_shape = image_encoder.image_shape

def encoder(image, boxes):
    image_patches = []
    for box in boxes:
        patch = extract_image_patch(image, box, image_shape[:2])
        if patch is None:
            print("WARNING: Failed to extract image patch: %s." % str(box))
            patch = np.random.uniform(
                0., 255., image_shape).astype(np.uint8)
        image_patches.append(patch)
    image_patches = np.asarray(image_patches)
    return image_encoder(image_patches, batch_size)

return encoder

def generate_detections(encoder, mot_dir, output_dir, detection_dir=None):
"""Generate detections with features.

Parameters
----------
encoder : Callable[image, ndarray] -> ndarray
    The encoder function takes as input a BGR color image and a matrix of
    bounding boxes in format `(x, y, w, h)` and returns a matrix of
    corresponding feature vectors.
mot_dir : str
    Path to the MOTChallenge directory (can be either train or test).
output_dir
    Path to the output directory. Will be created if it does not exist.
detection_dir
    Path to custom detections. The directory structure should be the default
    MOTChallenge structure: `[sequence]/det/det.txt`. If None, uses the
    standard MOTChallenge detections.

"""
if detection_dir is None:
    detection_dir = mot_dir
try:
    os.makedirs(output_dir)
except OSError as exception:
    if exception.errno == errno.EEXIST and os.path.isdir(output_dir):
        pass
    else:
        raise ValueError(
            "Failed to created output directory '%s'" % output_dir)

for sequence in os.listdir(mot_dir):
    print("Processing %s" % sequence)
    sequence_dir = os.path.join(mot_dir, sequence)

    image_dir = os.path.join(sequence_dir, "img1")
    image_filenames = {
        int(os.path.splitext(f)[0]): os.path.join(image_dir, f)
        for f in os.listdir(image_dir)}

    detection_file = os.path.join(
        detection_dir, sequence, "det/det.txt")
    detections_in = np.loadtxt(detection_file, delimiter=',')
    detections_out = []

    frame_indices = detections_in[:, 0].astype(np.int)
    min_frame_idx = frame_indices.astype(np.int).min()
    max_frame_idx = frame_indices.astype(np.int).max()
    for frame_idx in range(min_frame_idx, max_frame_idx + 1):
        print("Frame %05d/%05d" % (frame_idx, max_frame_idx))
        mask = frame_indices == frame_idx
        rows = detections_in[mask]

        if frame_idx not in image_filenames:
            print("WARNING could not find image for frame %d" % frame_idx)
            continue
        bgr_image = cv2.imread(
            image_filenames[frame_idx], cv2.IMREAD_COLOR)
        features = encoder(bgr_image, rows[:, 2:6].copy())
        detections_out += [np.r_[(row, feature)] for row, feature
                           in zip(rows, features)]

    output_filename = os.path.join(output_dir, "%s.npy" % sequence)
    np.save(
        output_filename, np.asarray(detections_out), allow_pickle=False)

def parse_args():
"""Parse command line arguments.
"""
parser = argparse.ArgumentParser(description="Re-ID feature extractor")
parser.add_argument(
"--model",
default="resources/networks/mars-small128.pb",
help="Path to freezed inference graph protobuf.")
parser.add_argument(
"--mot_dir", help="Path to MOTChallenge directory (train or test)",
required=True)
parser.add_argument(
"--detection_dir", help="Path to custom detections. Defaults to "
"standard MOT detections Directory structure should be the default "
"MOTChallenge structure: [sequence]/det/det.txt", default=None)
parser.add_argument(
"--output_dir", help="Output directory. Will be created if it does not"
" exist.", default="detections")
return parser.parse_args()

def main():
args = parse_args()
encoder = create_box_encoder(args.model, batch_size=32)
generate_detections(encoder, args.mot_dir, args.output_dir,
args.detection_dir)

if name == "main":
main()

@mukeshnaidu
Copy link

Replace generate_detections.py with above code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests