tensorflow高效地推导pb模型,完整代码
from matplotlib import pyplot as plt
import numpy as np
import os import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
import glob
from collections import defaultdict
from io import StringIO from PIL import Image
import DrawBox
import cv2
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util SaveFilename='D:/FasterR-CNNImageTest/11/'
if not os.path.exists(SaveFilename):
os.mkdir(SaveFilename)
with tf.device('/cpu:0'):
cap = cv2.VideoCapture(0)
PATH_TO_CKPT = 'pb/frozen_inference_graph.pb'
PATH_TO_LABELS = os.path.join('dataset', 'pascal_label_map.pbtxt')
NUM_CLASSES = 2
# Load a (frozen) Tensorflow model into memory.
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.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='')
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) 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) # # Detection
cnt = 0
PATH_TO_TEST_IMAGES_DIR = 'E:/PythonOpenCVCode/BalanceGeneratePicture/TestSetSaveImage/Test/JPEGImages' with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
# while True: # for image_path in TEST_IMAGE_PATHS: #changed 20170825
# 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')
for pidImage in glob.glob(PATH_TO_TEST_IMAGES_DIR + "/*.jpg"):
TEST_IMAGE_PATHS = [os.path.join(PATH_TO_TEST_IMAGES_DIR, pidImage)]
# Size, in inches, of the output images.
IMAGE_SIZE = (12, 8)
for image_path in TEST_IMAGE_PATHS:
image = Image.open(image_path)
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = load_image_into_numpy_array(image)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
# Visualization of the results of a detection.
# print(boxes)
DrawBox.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
max_boxes_to_draw=400,
use_normalized_coordinates=True,
groundtruth_box_visualization_color='red',
line_thickness=8)
#plt.figure(figsize=IMAGE_SIZE)
cv2.imwrite(SaveFilename + os.path.basename(image_path), image_np)
# plt.imshow(image_np)
cnt = cnt + 1
print(image_path)
tensorflow高效地推导pb模型,完整代码的更多相关文章
- 使用redis的zset实现高效分页查询(附完整代码)
一.需求 移动端系统里有用户和文章,文章可设置权限对部分用户开放.现要实现的功能是,用户浏览自己能看的最新文章,并可以上滑分页查看. 二.数据库表设计 涉及到的数据库表有:用户表TbUser.文章表T ...
- tensorflow学习笔记——模型持久化的原理,将CKPT转为pb文件,使用pb模型预测
由题目就可以看出,本节内容分为三部分,第一部分就是如何将训练好的模型持久化,并学习模型持久化的原理,第二部分就是如何将CKPT转化为pb文件,第三部分就是如何使用pb模型进行预测. 一,模型持久化 为 ...
- 【5】TensorFlow光速入门-图片分类完整代码
本文地址:https://www.cnblogs.com/tujia/p/13862364.html 系列文章: [0]TensorFlow光速入门-序 [1]TensorFlow光速入门-tenso ...
- tensorflow C++接口调用图像分类pb模型代码
#include <fstream> #include <utility> #include <Eigen/Core> #include <Eigen/Den ...
- tensorflow C++接口调用目标检测pb模型代码
#include <iostream> #include "tensorflow/cc/ops/const_op.h" #include "tensorflo ...
- 导出pb模型之后测试的python代码
链接:https://blog.csdn.net/thriving_fcl/article/details/75213361 saved_model模块主要用于TensorFlow Serving.T ...
- 查看tensorflow pb模型文件的节点信息
查看tensorflow pb模型文件的节点信息: import tensorflow as tf with tf.Session() as sess: with open('./quantized_ ...
- tensorflow c++ API加载.pb模型文件并预测图片
tensorflow python创建模型,训练模型,得到.pb模型文件后,用c++ api进行预测 #include <iostream> #include <map> # ...
- 将keras的h5模型转换为tensorflow的pb模型
h5_to_pb.py from keras.models import load_model import tensorflow as tf import os import os.path as ...
随机推荐
- 如何在 JavaScript 中使用 C 程序
JavaScript 是个灵活的脚本语言,能方便的处理业务逻辑.当需要传输通信时,我们大多选择 JSON 或 XML 格式. 但在数据长度非常苛刻的情况下,文本协议的效率就非常低了,这时不得不使用二进 ...
- java获取服务器路径
java获取服务器一些信息的方法(服务器地址/相对路径/端口/项目名字 request.getServletContext().getRealPath("/") 获取项目所在服务 ...
- Java学习之Java历史版本
Java有三个版本,标准版Java SE,企业版Java EE,移动版Java ME.按理来说,每一种版本都会有自己的版本号,但是约定俗成:JDK版本号=Java SE版本号=Java版本号,这是因为 ...
- C++头文件记得加#pragma once
C++头文件记得加#pragma once不然可能会导致重定义类
- iOS逆向系列-Reveal
概述 Reveal是一款调试iOS程序UI界面的神器. 官网地址:https://revealall.com 下载:https://revealapp.com/download/ 建议下载Reveal ...
- JavaWeb读取资源文件的四种方式
1. ServletContext 1. 调用getResourcesAsStream方法获取输入流, 相对于webroot, 不用加/2. 读取任何类型的文件3. *只能在web环境下使用 Inpu ...
- 集合划分——cf1028D思维题
非常思维的一道题目,题意很长 给定s1,s2两个集合,s1维护最大值,s2维护最小值,s1的所有元素要比s2小 操作1:往两个集合里的任意一个添加x 操作2:把x从所在的集合里删掉:要求被删的x必须是 ...
- 服务器迁移部署OmsEdi
基本配置 绑定 高级设置
- dp练习集
动态规划(DP) // 以下题目来自牛客网 删括号 f[i][j][k] 表示序列s的前i个匹配序列t的前j个,序列s删除部分左括号与右括号数量差为k的情况是否可行 答案为 f[sl][tl][0] ...
- 第一周课堂笔记4th
1. if 对应着程序设计中的三种程序执行流程: 顺序结构,一条一条的按顺序执行,自上而下 选择结构,if else 单分支,双分支,多分支 循环结构 while for (后面学) 流程控制 ...