分类任务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

相关文章

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入

SpringQuartz定时任务核心组件JobDetail与Trigger配置

《SpringQuartz定时任务核心组件JobDetail与Trigger配置》Spring框架与Quartz调度器的集成提供了强大而灵活的定时任务解决方案,本文主要介绍了SpringQuartz定... 目录引言一、Spring Quartz基础架构1.1 核心组件概述1.2 Spring集成优势二、J

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

Mysql如何将数据按照年月分组的统计

《Mysql如何将数据按照年月分组的统计》:本文主要介绍Mysql如何将数据按照年月分组的统计方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql将数据按照年月分组的统计要的效果方案总结Mysql将数据按照年月分组的统计要的效果方案① 使用 DA

鸿蒙中Axios数据请求的封装和配置方法

《鸿蒙中Axios数据请求的封装和配置方法》:本文主要介绍鸿蒙中Axios数据请求的封装和配置方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.配置权限 应用级权限和系统级权限2.配置网络请求的代码3.下载在Entry中 下载AxIOS4.封装Htt

Redis实现延迟任务的三种方法详解

《Redis实现延迟任务的三种方法详解》延迟任务(DelayedTask)是指在未来的某个时间点,执行相应的任务,本文为大家整理了三种常见的实现方法,感兴趣的小伙伴可以参考一下... 目录1.前言2.Redis如何实现延迟任务3.代码实现3.1. 过期键通知事件实现3.2. 使用ZSet实现延迟任务3.3