1、相关资料

tensorflow的ckpt文件总结

1.TensorFlow的模型文件

--checkpoint_dir
  | |--checkpoint
  | |--MyModel.meta
  | |--MyModel.data-00000-of-00001
  | |--MyModel.index

2.meta文件

  该文件保存的是图结构,meta文件是pb格式,包含变量、结合、OP

3.ckpt文件

  二进制文件,存储了weights,biases,gradients等变量

4.checkpoint文件

  文本文件,该文件记录了保存的最新的checkpoint文件以及其他checkpoint文件列表,可以修改这个文件,制定使用哪个model

5.保存TensorFlow

  使用tf.train.Saver(),TensorFlow中变量都是存储在Session环境中,只有Session环境下才会存有变量值,因此保存模型时需要传入Session。

  saver=tf.train.Saver()

  saver.save(sess, './checkpoint_dir/myModel')

6.在实际训练中,我们可能会在每1000次迭代中保存一次模型数据,但是由于图是不变的,没必要每次都去保存,可以通过如下方式指定不保存图

  saver.save(sess, './checkpoint_dir/MyModel', write_meta_graph=False)

谷歌推荐的保存模型的方式是保存模型为 PB 文件

2、运行脚本及命令

在models/research下执行命令,如果命令太长可以输出为.sh文件

echo  (命令)   >> 名称.sh

python3 object_detection/export_inference_graph.py --input_type=image_tensor --pipeline_config_path=/root/tf/models/research/object_detection/samples/configs/ssd_resnet50_v1_fpn_shared_box_predictor_640x640_coco14_sync_face.config --trained_checkpoint_prefix=//root/tf/widerface/resnet50v1-fpn/model.ckpt-6214 --output_directory=/root/tf/widerface/resnet50v1-fpn/pb

运行的脚本为/models/research/object_detection/export_inference_graph.py

配置文件--> pipeline_config_path

--pipeline_config_path=/root/tf/models/research/object_detection/samples/configs/ssd_resnet50_v1_fpn_shared_box_predictor_640x640_coco14_sync_face.config

模型--> trained_checkpoint_prefix,后面的数字要保留,显示这是训练了多少次的模型

--trained_checkpoint_prefix=//root/tf/widerface/resnet50v1-fpn/model.ckpt-6214

输出文件夹--> output_directory

--output_directory=/root/tf/widerface/resnet50v1-fpn/pb

3、输出结果

4、应用pb文件

先修改相应pb文件,label_map文件

PATH_TO_FROZEN_GRAPH = "/home/roy/TF/widerface/pb/frozen_inference_graph.pb"PATH_TOLABELS = "/home/roy/models/research/object_detection/data/face_label_map.pbtxt"

其中frozen_inference_graph.pb为第3步输出结果中的pb文件

脚本文件

import numpy as np
import sys
import tensorflow as tf
import glob
import cv2
sys.path.append("..")PATH_TO_FROZEN_GRAPH = "/home/roy/TF/widerface/pb/frozen_inference_graph.pb"PATH_TOLABELS = "/home/roy/models/research/object_detection/data/face_label_map.pbtxt"# 构图代码
detection_graph = tf.Graph()
with detection_graph.as_default():od_graph_def = tf.GraphDef()with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:serialized_graph = fid.read()od_graph_def.ParseFromString(serialized_graph)tf.import_graph_def(od_graph_def, name='')im_path_list = glob.glob("/home/roy/TF/widerface/test-images/*")  # 获取图片测试的路径
IMAGE_SIZE=(256, 256)
def run_inference_for_single_image(image, graph):with graph.as_default():with tf.Session() as sess:# Get handles to input and output tensorsops = tf.get_default_graph().get_operations()all_tensor_names = {output.name for op in ops for output in op.outputs}tensor_dict = {}for key in ['num_detections', 'detection_boxes', 'detection_scores','detection_classes', 'detection_masks']:tensor_name = key + ':0'if tensor_name in all_tensor_names:tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(tensor_name)if 'detection_masks' in tensor_dict:# The following processing is only for single image\n",detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])# Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(detection_masks, detection_boxes, image.shape[1], image.shape[2])detection_masks_reframed = tf.cast(tf.greater(detection_masks_reframed, 0.5), tf.uint8)# Follow the convention by adding back the batch dimensiontensor_dict['detection_masks'] = tf.expand_dims(detection_masks_reframed, 0)image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')# Run inferenceoutput_dict = sess.run(tensor_dict,feed_dict={image_tensor: np.expand_dims(image, 0)})# all outputs are float32 numpy arrays, so convert types as appropriateoutput_dict['num_detections'] = int(output_dict['num_detections'][0])output_dict['detection_classes'] = output_dict['detection_classes'][0].astype(np.int64)output_dict['detection_boxes'] = output_dict['detection_boxes'][0]output_dict['detection_scores'] = output_dict['detection_scores'][0]if 'detection_masks' in output_dict:output_dict['detection_masks'] = output_dict['detection_masks'][0]return output_dictfor image_path in im_path_list:imdata = cv2.imread(image_path)sp = imdata.shapeimdata = cv2.resize(imdata, IMAGE_SIZE)  # 重新定义图片尺寸output_dict = run_inference_for_single_image(imdata, detection_graph)for i in range(len(output_dict['detection_scores'])):if output_dict['detection_scores'][i] > 0.6:  # 人脸框的预值要大于0.6我们才认为它是一个人脸框bbox = output_dict['detection_boxes'][i]  # box 人脸框y1 = int(IMAGE_SIZE[0] * bbox[0])x1 = int(IMAGE_SIZE[1] * bbox[1])y2 = int(IMAGE_SIZE[0] * bbox[2])x2 = int(IMAGE_SIZE[1] * bbox[3])cv2.rectangle(imdata, (x1, y1), (x2, y2), (0, 255, 0), 2) # 绘制人脸框,最后一个值2为线条宽度cv2.imshow(image_path, imdata)cv2.waitKey(0)cv2.destroyWindow(image_path)

结果:

将训练好的模型转化为pb文件及pb应用相关推荐

  1. python 解析pb文件_将tensorflow模型打包成PB文件及PB文件读取方式

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

  2. VS2015+OpenCV3.4.5+QT5.12+WINDOWS10用c++调用tensorflow训练好的.pb文件图像检测

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_31806049/article/ ...

  3. 使用SSD训练自己的模型(从图片标注开始)

    此文章参考了https://blog.csdn.net/zzZ_CMing/article/details/81131101  在此表示感谢,如果有侵权的地方可联系本人删除 训练手表模型步骤 未经允许 ...

  4. Pytorch训练Bilinear CNN模型笔记

    Pytorch训练Bilinear CNN模型笔记 注:一个项目需要用到机器学习,而本人又是一个python小白,根据老师的推荐,然后在网上查找了一些资料,终于实现了目的. 参考文献: Caltech ...

  5. 深度学习(六)——CNN识别典型地标建筑,并制作pb文件,部署在android studio端,制作app实现该功能

    一.背景 两年前的一个项目,识别典型地标,并对图片进行标记.这里只写一下简单思路,最开始是打算用特征点匹配来做的,可以参考:基于特征点匹配方法--SIFT, SURF, ORB的图像识别 ,后来发现效 ...

  6. TF之TFOD-API:基于tensorflow框架利用TFOD-API脚本文件将YoloV3训练好的.ckpt模型文件转换为推理时采用的.pb文件

    TF之TFOD-API:基于tensorflow框架利用TFOD-API脚本文件将YoloV3训练好的.ckpt模型文件转换为推理时采用的frozen_inference_graph.pb文件 目录 ...

  7. h5模型转化为pb模型,代码及排坑

    我是在实际工程中要用到tensorflow训练的pb模型,但是训练的代码是用keras写的,所以生成keras特定的h5模型,所以用到了h5_to_pb.py函数. 附上h5_to_pb.py(pyt ...

  8. TensorFlow 工程实战(一):在TFhub中下载预训练的pb文件,并使用 TF-Hub 库微调模型评估人物年龄

    实例描述 有一组照片,每个文件夹的名称为具体的年龄,里面放的是该年纪的人物图片. 微调 TF-Hub 库,让模型学习这些样本,找到其中的规律,可以根据具体人物的图片来评估人物的年龄. 即便是通过人眼来 ...

  9. tensorflow加载预训练好的模型图(.pb文件)

    千万不要试图在jupyter notebook中打开.pb模型文件,否则你会得到: 这时候我以为shi编码的问题,开始转换编码,转换完成后发现shi乱ma. 后来网上查了,.pb文件里面存储的shi模 ...

最新文章

  1. 给Java新手的一些建议——Java知识点归纳(Java基础部分)
  2. git提交代码,合并同步分支
  3. 程序员的六种境界(摘抄)
  4. 转发:Ajax动态画EChart图表
  5. 计算机网络原理(第二章)课后题答案
  6. 榨取kkksc03(洛谷-P1855)
  7. android注册文件打开,Android项目实战系列—基于博学谷(三)注册与登录模块
  8. Ranger开源流水线docker化实践案例
  9. combox 增加请选择_好消息!阜阳机动车互联网选号增加新号段!
  10. parseConf(配置文件解析器)
  11. TPLINK-WR720N刷openwrt
  12. Bailian4030 统计单词数【文本处理】
  13. Oracle中for update和for update nowait的区别
  14. mysql80连接不上本地服务器_干货教程:如何在服务器上安装Mysql8.0
  15. 使用Outlook Connector插件之后 qq发送过来的邮件为乱码
  16. 计算机考证创建文本文档
  17. 2022手机商城源码h5运营版本
  18. windows下的文件服务器监控
  19. linux vim下自动补全,linux-python在vim下的自动补全功能
  20. openlayers动态添加自定义div图层 具有筛选功能 和浮窗

热门文章

  1. 网络安全入门:不可不知的8款免费Web安全测试工具
  2. 02高级语言及其语法描述
  3. OpenHarmony-标准设备系统代码操作梳理
  4. vue使用javascript动态创建script - 动态引入外部js文件
  5. LimeSDR入门之软硬件安装
  6. Shell实现自动添加新行
  7. 西门子300PLC连接组态王KingSCADA实现ModbusTCP通信
  8. 打印100-200以内的素数
  9. 中国石油大学《钢结构》第一阶段在线作业
  10. Ubuntu最新版本(Ubuntu22.04LTS)安装nfs服务器及使用教程