graph_def.ParseFromString(f.read()) google.protobuf.message.DecodeError:解析消息时出错

如何解决graph_def.ParseFromString(f.read()) google.protobuf.message.DecodeError:解析消息时出错

我试图使用我训练有素的 ssd_resnet50_v1_fpn saved_model.pb 来推断视频,这是由 TensorFlow Object Detection API 2.0 版训练的,我的 TF 版本是 2.4.0。我使用附加的脚本来运行推理,但出现了令人头疼的错误消息:

File "ObjDet_Test_Video.py",line 35,in <module>
    od_graph_def.ParseFromString(serialized_graph)
google.protobuf.message.DecodeError: Error parsing message

下面是我的推理脚本。有大佬知道怎么解决吗?

import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import cv2
import zipfile
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
import numpy as np
import datetime as dt
from objectTracker import * 
from collections import deque
# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")
from object_detection.utils import ops as utils_ops

    
from utils import label_map_util
from utils import visualization_utils as vis_util

PATH_TO_CKPT = 'exported-models/my_model/saved_model/saved_model.pb'
PATH_TO_LABELS = os.path.join('annotations','label_map.pbtxt')
NUM_CLASSES = 17

# Load Tensorflow model to memory
detection_graph = tf.Graph()
with detection_graph.as_default():
  od_graph_def = tf.compat.v1.GraphDef() 
  with tf.io.gfile.GFile(PATH_TO_CKPT,'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def,name='')

# Loading label map
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map,max_num_classes=NUM_CLASSES,use_display_name=True)
category_index = label_map_util.create_category_index(categories)

# Helper code
def load_image_into_numpy_array(image):
  (im_width,im_height) = image.size
  return np.array(image.getdata()).reshape(
      (im_height,im_width,3)).astype(np.uint8)
  
def cvDrawBoundingBox(xLeft,yTop,width,height,bgr,img):
    topLeft = (xLeft,yTop)
    botRight = (xLeft+width,yTop+height)
    #bgr = (0,255)
    thickness = 2
    img = cv2.rectangle(img,topLeft,botRight,thickness)
    return img 

def cvDrawText(txt,x,y,img):    
    botLeft_xy = (x,y)
    font = cv2.FONT_HERSHEY_SIMPLEX
    font_size = 0.66
    color = bgr
    thickness = 1
    lineType = cv2.LINE_AA
    cv2.putText(img,txt,botLeft_xy,font,font_size,color,thickness,lineType)
    return img

def cvDrawTrackBlobs(trackList,img):
    for track in trackList:
        boxColor = (255,255,255)            #white
        if (track.confirmed == True):
            boxColor = (0,255)            #red
        x = track.blob.xLeft
        y = track.blob.yTop
        w = track.blob.width
        h = track.blob.height
        img = cvDrawBoundingBox(x,w,h,boxColor,img)
        blobClass = " "        
        txt = str(track.id)+blobClass
        img = cvDrawText(txt,x-2,y-5,img)
    return img

# Detection

IMAGE_SIZE = (12,8)
with detection_graph.as_default():
  with tf.Session(graph=detection_graph) as sess:
    # Definite input and output Tensors for detection_graph
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
    # Each box represents a part of the image where a particular object was detected.
    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
    # Each score represent how level of confidence for each of the objects.
    # Score is shown on the result image,together with the class label.
    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')    
    cap = cv2.VideoCapture('N_Oakland.avi')
    delay_ms = -1
    #plt.figure(figsize=IMAGE_SIZE)
    #Dictionary for tracks indexed by Id and their state
    trackDict = {id: False for id in range(1,251)}
    trackedObjList = []
    trackTraceQueue = deque([],30)
    frameNum = 0
    while(cap.isOpened()):
          numBlobs = 0          
          blobList = []
          ret,frame = cap.read()
          blobFrame = frame.copy()
          im_height,im_depth = frame.shape
          # Convert open cv color image format (BGR) to RGB as expected by Tensorflow
          image_np = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)          
          # Expand dimensions since the model expects images to have shape: [1,None,3]
          image_np_expanded = np.expand_dims(image_np,axis=0)
          time_start = dt.datetime.now()
          # Actual detection.
          (boxes,scores,classes,num) = sess.run(
                  [detection_boxes,detection_scores,detection_classes,num_detections],feed_dict={image_tensor: image_np_expanded})
          time_end = dt.datetime.now()
          elapsed_time = time_end - time_start
          # Object Tracking
          numObjs = int(num[0])
          print()
          print("FrameNo:",frameNum)
          print("Objects:",numObjs)
          for idx in range(numObjs):
              if (scores[0][idx] < 0.5):
                  continue
              ymin,xmin,ymax,xmax = boxes[0][idx]
              xmin = int(xmin*im_width)
              xmax = int(xmax*im_width)
              ymin = int(ymin*im_height)
              ymax = int(ymax*im_height)
              objW = xmax-xmin
              objH = ymax-ymin
              #print(idx,ymin,objW,objH,(xmax+xmin)/2,(ymax+ymin)/2,objW*objH)
              #print()
              if (ymax > 0 and objW < im_width/2 and objH < im_height/2):
                  blobList.append(blobAttr(numBlobs,idx,objW*objH))
                  numBlobs += 1
                  #print(idx,objW*objH,scores[0][idx])
              #print(xmin,xmax,ymax)
          """Track moving objects"""
          blobList,trackedObjList,trackDict = trackBlobs(blobList,trackDict)
          blobFrame = cvDrawTrackBlobs(trackedObjList,blobFrame)
          
          # Visualization of the results of a detection.
          vis_util.visualize_boxes_and_labels_on_image_array(
                  image_np,np.squeeze(boxes),np.squeeze(classes).astype(np.int32),np.squeeze(scores),category_index,use_normalized_coordinates=True,line_thickness=2)      
          #plt.imshow(image_np)
          opencv_image=cv2.cvtColor(image_np,cv2.COLOR_RGB2BGR)
          txt = 'Inference Time (secs): '+ str(elapsed_time.total_seconds())
          opencv_image = cvDrawText(txt,10,20,(0,255),opencv_image)
          txt = 'Model: '+ MODEL_NAME[:30]
          opencv_image = cvDrawText(txt,450,opencv_image)
          txt = 'Frame No: '+ str(frameNum)
          blobFrame = cvDrawText(txt,30,0),blobFrame)
          cv2.imshow('Objects',opencv_image)
          cv2.imshow('Tracks',blobFrame)
          frameNum = frameNum + 1
          k = cv2.waitKey(delay_ms) & 0xff          
          if (k == 13): #enter
                if (delay_ms < 0):
                    delay_ms = 30
                else:
                    delay_ms = -1
          if (k == 32): #space
              continue
          elif (k == 27): #Esc 
              break          
    cap.release()
    cv2.destroyAllWindows()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res