简体   繁体   English

类型错误:将形状转换为 TensorShape 时出错:int() 参数必须是字符串或数字,而不是“元组”

[英]TypeError: Error converting shape to a TensorShape: int() argument must be a string or a number, not 'tuple'

In my detection script I get the following error :在我的检测脚本中,我收到以下错误:

Error converting shape to a TensorShape: int() argument must be a string or a number, not 'tuple'.将形状转换为 TensorShape 时出错:int() 参数必须是字符串或数字,而不是“元组”。

Tensorflow Version : 2.0 Tensorflow 版本:2.0

Code:代码:

"""Yolo v3 detection script.

Saves the detections in the `detection` folder.

Usage:
    python detect.py <images/video> <iou threshold> <confidence threshold> <filenames>

Example:
    python detect.py images 0.5 0.5 data/images/dog.jpg data/images/office.jpg
    python detect.py video 0.5 0.5 data/video/shinjuku.mp4

Note that only one video can be processed at one run.
"""

import tensorflow as tf
import sys
import cv2

from yolo_v3 import Yolo_v3
from utils import load_images, load_class_names, draw_boxes, draw_frame

_MODEL_SIZE = (416, 416)
_CLASS_NAMES_FILE = './data/labels/coco.names'
_MAX_OUTPUT_SIZE = 20


def main(type, iou_threshold, confidence_threshold, input_names):
    class_names = load_class_names(_CLASS_NAMES_FILE)
    n_classes = len(class_names)

    model = Yolo_v3(n_classes=n_classes, model_size=_MODEL_SIZE,
                    max_output_size=_MAX_OUTPUT_SIZE,
                    iou_threshold=iou_threshold,
                    confidence_threshold=confidence_threshold)

    if type == 'images':
        batch_size = len(input_names)
        batch = load_images(input_names, model_size=_MODEL_SIZE)
        inputs = tf.placeholder(tf.float32, [batch_size, _MODEL_SIZE, 3])
        detections = model(inputs, training=False)
        saver = tf.train.Saver(tf.global_variables(scope='yolo_v3_model'))

        with tf.Session() as sess:
            saver.restore(sess, './weights/model.ckpt')
            detection_result = sess.run(detections, feed_dict={inputs: batch})

        draw_boxes(input_names, detection_result, class_names, _MODEL_SIZE)

        print('Detections have been saved successfully.')

    elif type == 'video':
        inputs = tf.placeholder(tf.float32, [1, _MODEL_SIZE, 3])
        detections = model(inputs, training=False)
        saver = tf.train.Saver(tf.global_variables(scope='yolo_v3_model'))

        with tf.Session() as sess:
            saver.restore(sess, './weights/model.ckpt')

            win_name = 'Video detection'
            cv2.namedWindow(win_name)
            cap = cv2.VideoCapture(input_names[0])
            frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH),
                          cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
            fourcc = cv2.VideoWriter_fourcc(*'X264')
            fps = cap.get(cv2.CAP_PROP_FPS)
            out = cv2.VideoWriter('./detections/detections.mp4', fourcc, fps,
                                  (int(frame_size[0]), int(frame_size[1])))

            try:
                while True:
                    ret, frame = cap.read()
                    if not ret:
                        break
                    resized_frame = cv2.resize(frame, dsize=_MODEL_SIZE[::-1],
                                               interpolation=cv2.INTER_NEAREST)
                    detection_result = sess.run(detections,
                                                feed_dict={inputs: [resized_frame]})

                    draw_frame(frame, frame_size, detection_result,
                               class_names, _MODEL_SIZE)

                    cv2.imshow(win_name, frame)

                    key = cv2.waitKey(1) & 0xFF

                    if key == ord('q'):
                        break

                    out.write(frame)
            finally:
                cv2.destroyAllWindows()
                cap.release()
                print('Detections have been saved successfully.')

    else:
        raise ValueError("Inappropriate data type. Please choose either 'video' or 'images'.")


if __name__ == '__main__':
    main(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), sys.argv[4:])

Problem with line :线路问题:

main(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), sys.argv[4:])

Error log :错误日志:

Traceback (most recent call last):   File "detect.py", line 100, in <module>
     main(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), sys.argv[4:])   File "detect.py", line 39, in main
     inputs = tf.placeholder(tf.float32, [batch_size, _MODEL_SIZE, 3])   File
 "/home/christie/.local/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py",
 line 2143, in placeholder
     return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)   File
 "/home/christie/.local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py",
 line 6260, in placeholder
     shape = _execute.make_shape(shape, "shape")   File "/home/christie/.local/lib/python2.7/site-packages/tensorflow/python/eager/execute.py",
 line 148, in make_shape
     raise TypeError("Error converting %s to a TensorShape: %s." % (arg_name, e)) TypeError: Error converting shape to a TensorShape:
 int() argument must be a string or a number, not 'tuple'.

From the traceback, your TypeError derives from this line in your code:从回溯中,您的TypeError源自代码中的这一行:

inputs = tf.placeholder(tf.float32, [batch_size, _MODEL_SIZE, 3])

The list your are passing to placeholder should be a list of integers;您传递给placeholder的列表应该是一个整数列表; however, _MODEL_SIZE is a tuple ( (416, 416) ).然而, _MODEL_SIZE是一个元组 ( (416, 416) )。 You should unpack that tuple to generate a flat list of integers, eg:您应该解压缩该元组以生成整数的平面列表,例如:

inputs = tf.placeholder(tf.float32, [batch_size, *_MODEL_SIZE, 3])

Since this tuple unpacking syntax is only available in python > 3.0, if you are using python 2.7, you may achieve the same via:由于此元组解包语法仅在 python > 3.0 中可用,如果您使用的是 python 2.7,您可以通过以下方式实现:

inputs = tf.placeholder(tf.float32, [batch_size, _MODEL_SIZE[0], _MODEL_SIZE[1], 3])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:将形状转换为 TensorShape 时出错:int() 参数必须是字符串、类似字节的对象或数字,而不是“元组”。 在蟒蛇 - TypeError: Error converting shape to a TensorShape: int() argument must be a string, a bytes-like object or a number, not 'tuple'. in python TypeError:int()参数必须是字符串或数字,而不是&#39;tuple&#39; - TypeError: int() argument must be a string or a number, not 'tuple' TypeError:int()参数必须是字符串或数字,而不是使用ouchdb-python的“元组” - TypeError: int() argument must be a string or a number, not 'tuple' with couchdb-python TypeError:float()参数必须是字符串或数字,而不是“ tuple” - TypeError: float() argument must be a string or a number, not 'tuple' django - int参数必须是字符串或数字,而不是&#39;元组&#39; - django - int argument must be a string or a number, not 'Tuple' 将元组转换为 integer Python,错误信息:TypeError: int() argument must be a string, a bytes-like object or a real number, - Convert a tupple into integer Python, error message: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'tuple' Django错误:TypeError:int()参数必须是字符串或数字,而不是“ BuildsTable” - Django Error: TypeError: int() argument must be a string or a number, not 'BuildsTable' TypeError:将形状转换为 TensorShape 时出错:仅接受尺寸 1 arrays - TypeError: Error converting shape to a TensorShape: Only size 1 arrays are accepted 类型错误:INT()参数必须是字符串或数字,而不是“字典” - TypeError: int() argument must be a string or a number, not 'dict' TypeError:int()参数必须是字符串或数字,而不是&#39;Binary&#39; - TypeError: int() argument must be a string or a number, not 'Binary'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM