分类任务3——把弄好的数据制作成tfrecord

2023-12-30 12:18

本文主要是介绍分类任务3——把弄好的数据制作成tfrecord,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

既然都用了tensorflow,那干脆数据文件也弄成这个格式算了。
(我绝对不会说是因为直接读取图像太慢了)

没错又是这个

"""# 把图像数据制作成tfrecord"""import tensorflow as tf
import os
from PIL import Image
import random
from tqdm import tqdmdef _int64_feature(label):return tf.train.Feature(int64_list=tf.train.Int64List(value=[label]))def _bytes_feature(imgdir):return tf.train.Feature(bytes_list=tf.train.BytesList(value=[imgdir]))def float_list_feature(value):return tf.train.Feature(float_list=tf.train.FloatList(value=value))def get_example_nums(tf_records_filenames):nums = 0for record in tf.python_io.tf_record_iterator(tf_records_filenames):nums += 1return numsdef load_file(imagestxtdir, shuffle=False):images = []  # 存储各个集中图像地址的列表labels = []with open(imagestxtdir) as f:lines_list = f.readlines()  # 读取文件列表中所有的行if shuffle:random.shuffle(lines_list)for line in lines_list:line_list = line.rstrip().split(' ')  # rstrip函数删除指定字符,这里用的rstrip()因为括号内是空格,所以是删除空白部分label = []for i in range(1):label.append(int(line_list[i + 1]))# 这里本质就是要line_list[1],因为这个部分就是存label的,可以用下面一行直接替代# label.append(int(line_list[1]))# cur_img_dir=images_base_dir+'/'+line_list[0]images.append(line_list[0])labels.append(label)return images, labelsdef create_tf_records(image_base_dir, image_txt_dir, tfrecords_dir,resize_height, resize_width, log, shuffle):images_list, labels_list = load_file(image_txt_dir, shuffle)# 判断是否存在保存tfrecord文件的路径,如果没有,就创建一个。tf_dir, tf_name = os.path.split(tfrecords_dir)if not os.path.exists(tf_dir):os.makedirs(tf_dir)tfrecords_dir = tf_dir + '/' + tf_name# print(tfrecords_dir)writer = tf.python_io.TFRecordWriter(tfrecords_dir)# print('len is :', len(images_list))# image_name 这个函数虽然没有用到,但是作用仍十分关键。因为后面的zip要求有两个变量。print('\n#######################start to create %s###########################' % tf_name)for i, [image_name, single_label_list] in enumerate(zip(images_list, labels_list)):cur_image_dir = image_base_dir + '/' + images_list[i]if not os.path.exists(cur_image_dir):print('the image path is not exists')continueimage = Image.open(cur_image_dir)image = image.resize((resize_height, resize_width))image_raw = image.tobytes()single_label = single_label_list[0]if i % log == 0 or i == len(images_list) - 1:print('------------processing:%d-th------------' % i)example = tf.train.Example(features=tf.train.Features(feature={'image_raw': _bytes_feature(image_raw),'label': _int64_feature(single_label)}))writer.write(example.SerializeToString())print('#######################successfully create %s###########################\n' % tf_name)writer.close()if __name__ == '__main__':resize_height = 600resize_width = 600# shuffle = Truelog = 5train_image_dir = 'E:/111project/ship image/train'train_txt_dir = 'E:/111project/ship image/train.txt'train_records_dir = 'E:/111project/tfrecordss/train.tfrecords'create_tf_records(train_image_dir, train_txt_dir, train_records_dir,resize_height, resize_width, log, shuffle=True)train_nums = get_example_nums(train_records_dir)print('the train records number is:', train_nums)validation_image_dir = 'E:/111project/ship image/validation'validation_txt_dir = 'E:/111project/ship image/validation.txt'validation_records_dir = 'E:/111project/tfrecordss/validation.tfrecords'create_tf_records(validation_image_dir, validation_txt_dir, validation_records_dir,resize_height, resize_width, log, shuffle=True)validation_nums = get_example_nums(validation_records_dir)print('the validation records number is:', validation_nums)test_image_dir = 'E:/111project/ship image/test'test_txt_dir = 'E:/111project/ship image/test.txt'test_records_dir = 'E:/111project/tfrecordss/test.tfrecords'create_tf_records(test_image_dir, test_txt_dir, test_records_dir,resize_height, resize_width, log, shuffle=False)test_nums = get_example_nums(test_records_dir)print('the test records number is:', test_nums)

这篇关于分类任务3——把弄好的数据制作成tfrecord的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/552822

相关文章

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

在Golang中实现定时任务的几种高效方法

《在Golang中实现定时任务的几种高效方法》本文将详细介绍在Golang中实现定时任务的几种高效方法,包括time包中的Ticker和Timer、第三方库cron的使用,以及基于channel和go... 目录背景介绍目的和范围预期读者文档结构概述术语表核心概念与联系故事引入核心概念解释核心概念之间的关系

springboot项目打jar制作成镜像并指定配置文件位置方式

《springboot项目打jar制作成镜像并指定配置文件位置方式》:本文主要介绍springboot项目打jar制作成镜像并指定配置文件位置方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录一、上传jar到服务器二、编写dockerfile三、新建对应配置文件所存放的数据卷目录四、将配置文

springboot如何通过http动态操作xxl-job任务

《springboot如何通过http动态操作xxl-job任务》:本文主要介绍springboot如何通过http动态操作xxl-job任务的问题,具有很好的参考价值,希望对大家有所帮助,如有错... 目录springboot通过http动态操作xxl-job任务一、maven依赖二、配置文件三、xxl-

Navicat数据表的数据添加,删除及使用sql完成数据的添加过程

《Navicat数据表的数据添加,删除及使用sql完成数据的添加过程》:本文主要介绍Navicat数据表的数据添加,删除及使用sql完成数据的添加过程,具有很好的参考价值,希望对大家有所帮助,如有... 目录Navicat数据表数据添加,删除及使用sql完成数据添加选中操作的表则出现如下界面,查看左下角从左

MySQL中的索引结构和分类实战案例详解

《MySQL中的索引结构和分类实战案例详解》本文详解MySQL索引结构与分类,涵盖B树、B+树、哈希及全文索引,分析其原理与优劣势,并结合实战案例探讨创建、管理及优化技巧,助力提升查询性能,感兴趣的朋... 目录一、索引概述1.1 索引的定义与作用1.2 索引的基本原理二、索引结构详解2.1 B树索引2.2