Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 用两个张量流模型进行推理_Python 3.x_Tensorflow - Fatal编程技术网

Python 3.x 用两个张量流模型进行推理

Python 3.x 用两个张量流模型进行推理,python-3.x,tensorflow,Python 3.x,Tensorflow,我有推理应用程序。应用程序使用了两种推理模型 这两个模型将恢复为两个不同的图形,并在两个不同的会话中运行 检测推理产生物体的位置,二次推理识别作物图像中的分离物体 错误发生在第二次推理时 错误是 2019-06-17 22:08:34.454728: F tensorflow/core/framework/tensor_shape.cc: 44]Check failed:NDIMS==dims() (2 vs. 3)Asking for tensor of 2 dimensions from a

我有推理应用程序。应用程序使用了两种推理模型

这两个模型将恢复为两个不同的图形,并在两个不同的会话中运行

检测推理产生物体的位置,二次推理识别作物图像中的分离物体

错误发生在第二次推理时

错误是

2019-06-17 22:08:34.454728: F tensorflow/core/framework/tensor_shape.cc: 44]Check failed:NDIMS==dims() (2 vs. 3)Asking for tensor of 2 dimensions from a tensor of 3 dimensions
Aborted (core dumped)
错误在

b, s, c = sess.run([self.box, self.score, self.cls], feed_dict={self.image_tensor: image_data})
有什么不对劲吗?我的张量流量是1.9

整个代码如下所示

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'  # or any {'0', '1', '2'}
import tensorflow as tf
import cv2
import numpy as np
import time


class Recognition:
   def __init__(self):
      self.reg_graph = tf.Graph()
      with self.reg_graph.as_default():
          od_graph_def = tf.GraphDef()
          with tf.gfile.GFile("recognition_tf_model/frozen_model.pb", 'rb') as fid:
             serialized_graph = fid.read()
             od_graph_def.ParseFromString(serialized_graph)
             tf.import_graph_def(od_graph_def, name='')
             self.image_tensor = self.reg_graph.get_tensor_by_name('image_tensor:0')
             self.box = self.reg_graph.get_tensor_by_name('detection_boxes:0')
             self.score = self.reg_graph.get_tensor_by_name('detection_scores:0')
             self.cls = self.reg_graph.get_tensor_by_name('detection_classes:0')

   def infer(self, crop, frame, w, h):
      with tf.Session(graph=self.reg_graph) as sess:
         image = cv2.resize(crop,(512,300))
         image_data = np.expand_dims(image, axis=0).astype(np.uint8)
         b, s, c = sess.run([self.box, self.score, self.cls], feed_dict={self.image_tensor: image_data})
         if(len(b)==0 or len(s)==0 or len(c)==0):
            return
         boxes = b[0]
         conf = s[0]
         clses = c[0]
         for i in (range(len(boxes))):
            bx = boxes[i]
            if conf[i] < 0.5:
              continue
            p1 = (int(w * bx[1]), int(h * bx[0]))
            p2 = (int(w * bx[3]) ,int(h * bx[2]))
            cv2.rectangle(frame, p1, p2, (0,255,0))
      cv2.imshow("Numplate recognition", frame)
      cv2.waitKey(1)

def main():
   dr=Recognition()
   vidoe_path = "../../../Data/plates/IMG_5715.MOV"
   det_graph = tf.Graph()
   with det_graph.as_default():
      with tf.Session(graph=det_graph) as sess:
         graph_def = tf.GraphDef()
         with tf.gfile.GFile("detection_tf_model/frozen_model.pb", 'rb') as fid:
            serialized_graph = fid.read()
            graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(graph_def, name='')
            image_tensor = det_graph.get_tensor_by_name('image_tensor:0')
            box = det_graph.get_tensor_by_name('detection_boxes:0')
            score = det_graph.get_tensor_by_name('detection_scores:0')
            cls = det_graph.get_tensor_by_name('detection_classes:0')
         cap = cv2.VideoCapture(vidoe_path)
         width = cap.get(3)  # float
         height = cap.get(4) # float
         while True:
           fps_time=time.time()
           ret, frame = cap.read()
           if(ret==0):
             break
           image = cv2.resize(frame,(300,300))
           image_data = np.expand_dims(image, axis=0).astype(np.uint8)
           b, s, c = sess.run([box, score, cls], {image_tensor: image_data})
           if(len(b)==0 or len(s)==0 or len(c)==0):
              continue
           boxes = b[0]
           conf = s[0]
           clses = c[0]
           #writer = tf.summary.FileWriter('debug', sess.graph)
           for i in (range(5)):
              bx = boxes[i]
              if conf[i] < 0.5:
                  continue
              p1 = (int(width * bx[1]), int(height * bx[0]))
              p2 = (int(width * bx[3]) ,int(height * bx[2]))
              cv2.rectangle(frame, p1, p2, (0,255,0))
              dr.infer(frame[p1[1]:p2[1], p1[0]:p2[0]], frame, width, height)
           print('FPS: %f' % (1.0/(time.time() - fps_time)))

if __name__ == "__main__":
     main()
导入操作系统
os.environ['TF_CPP_MIN_LOG_LEVEL']=“3”或任何{'0','1','2'}
导入tensorflow作为tf
进口cv2
将numpy作为np导入
导入时间
类别识别:
定义初始化(自):
self.reg_graph=tf.graph()
使用self.reg\u graph.as\u default():
od_graph_def=tf.GraphDef()
将tf.gfile.gfile(“recognition_tf_model/freezed_model.pb”,“rb”)作为fid:
序列化的_图=fid.read()
od_图形_def.ParseFromString(序列化_图形)
tf.import_graph_def(od_graph_def,name='')
self.image\u tensor=self.reg\u graph.get\u tensor\u by\u name('image\u tensor:0'))
self.box=self.reg\u graph.get\u tensor\u by\u name('detection\u box:0'))
self.score=self.reg\u graph.get\u tensor\u by\u name('detection\u scores:0'))
self.cls=self.reg_graph.get_tensor_by_name('detection_classes:0'))
def推断(自身、裁剪、帧、w、h):
将tf.Session(graph=self.reg_graph)作为sess:
图像=cv2。调整大小(裁剪,(512300))
图像\数据=np.expand\ dims(图像,轴=0).astype(np.uint8)
b、 s,c=sess.run([self.box,self.score,self.cls],feed\u dict={self.image\u tensor:image\u data})
如果(len(b)=0或len(s)=0或len(c)=0):
返回
框=b[0]
conf=s[0]
clses=c[0]
对于i(范围(len(框)):
bx=框[i]
如果conf[i]<0.5:
持续
p1=(int(w*bx[1]),int(h*bx[0]))
p2=(int(w*bx[3]),int(h*bx[2]))
cv2.矩形(框架,p1,p2,(0255,0))
cv2.imshow(“模板识别”,帧)
cv2.等待键(1)
def main():
dr=识别()
vidoe_path=“../../../Data/plates/IMG_5715.MOV”
det_graph=tf.graph()
使用det_graph.as_default():
将tf.Session(graph=det_graph)作为sess:
graph_def=tf.GraphDef()
将tf.gfile.gfile(“detection_tf_model/freezed_model.pb”,“rb”)作为fid:
序列化的_图=fid.read()
graph_def.ParseFromString(序列化图)
tf.import_graph_def(graph_def,name='')
image\u tensor=det\u图。通过名称获取(image\u tensor:0)
box=det\u图。通过名称获取张量(检测框:0)
score=det\u图。通过名称获取张量('detection\u scores:0')
cls=det\u图。通过名称获取张量('detection\u classes:0')
cap=cv2.视频捕获(视频路径)
宽度=上限。获得(3)#浮动
高度=盖。获取(4)#浮动
尽管如此:
fps_time=time.time()
ret,frame=cap.read()
如果(ret==0):
打破
图像=cv2。调整大小(帧,(300300))
图像\数据=np.expand\ dims(图像,轴=0).astype(np.uint8)
b、 s,c=sess.run([box,score,cls],{image\u tensor:image\u data})
如果(len(b)=0或len(s)=0或len(c)=0):
持续
框=b[0]
conf=s[0]
clses=c[0]
#writer=tf.summary.FileWriter('debug',sess.graph)
对于(范围(5))中的i:
bx=框[i]
如果conf[i]<0.5:
持续
p1=(int(宽*bx[1]),int(高*bx[0]))
p2=(int(宽*bx[3]),int(高*bx[2]))
cv2.矩形(框架,p1,p2,(0255,0))
推断博士(帧[p1[1]:p2[1]、p1[0]:p2[0]、帧、宽度、高度)
打印('FPS:%f'(1.0/(time.time()-FPS_time)))
如果名称=“\uuuuu main\uuuuuuuu”:
main()

TX2提供了Tensorflow1.9。Tensorflow1.9有这个问题。现在从源代码构建Tensorflow1.12,问题就解决了。

TX2提供了Tensorflow1.9。Tensorflow1.9有这个问题。现在从源代码构建Tensorflow1.12,问题就解决了