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

相关文章

Redis的数据过期策略和数据淘汰策略

《Redis的数据过期策略和数据淘汰策略》本文主要介绍了Redis的数据过期策略和数据淘汰策略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录一、数据过期策略1、惰性删除2、定期删除二、数据淘汰策略1、数据淘汰策略概念2、8种数据淘汰策略

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE

Python给Excel写入数据的四种方法小结

《Python给Excel写入数据的四种方法小结》本文主要介绍了Python给Excel写入数据的四种方法小结,包含openpyxl库、xlsxwriter库、pandas库和win32com库,具有... 目录1. 使用 openpyxl 库2. 使用 xlsxwriter 库3. 使用 pandas 库

SpringBoot定制JSON响应数据的实现

《SpringBoot定制JSON响应数据的实现》本文主要介绍了SpringBoot定制JSON响应数据的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录前言一、如何使用@jsonView这个注解?二、应用场景三、实战案例注解方式编程方式总结 前言

使用Python在Excel中创建和取消数据分组

《使用Python在Excel中创建和取消数据分组》Excel中的分组是一种通过添加层级结构将相邻行或列组织在一起的功能,当分组完成后,用户可以通过折叠或展开数据组来简化数据视图,这篇博客将介绍如何使... 目录引言使用工具python在Excel中创建行和列分组Python在Excel中创建嵌套分组Pyt

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

在Mysql环境下对数据进行增删改查的操作方法

《在Mysql环境下对数据进行增删改查的操作方法》本文介绍了在MySQL环境下对数据进行增删改查的基本操作,包括插入数据、修改数据、删除数据、数据查询(基本查询、连接查询、聚合函数查询、子查询)等,并... 目录一、插入数据:二、修改数据:三、删除数据:1、delete from 表名;2、truncate

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

Java实现Elasticsearch查询当前索引全部数据的完整代码

《Java实现Elasticsearch查询当前索引全部数据的完整代码》:本文主要介绍如何在Java中实现查询Elasticsearch索引中指定条件下的全部数据,通过设置滚动查询参数(scrol... 目录需求背景通常情况Java 实现查询 Elasticsearch 全部数据写在最后需求背景通常情况下

Spring Boot 整合 ShedLock 处理定时任务重复执行的问题小结

《SpringBoot整合ShedLock处理定时任务重复执行的问题小结》ShedLock是解决分布式系统中定时任务重复执行问题的Java库,通过在数据库中加锁,确保只有一个节点在指定时间执行... 目录前言什么是 ShedLock?ShedLock 的工作原理:定时任务重复执行China编程的问题使用 Shed