当前位置:网站首页>Tensorflow---TFRecord文件的创建与读取
Tensorflow---TFRecord文件的创建与读取
2022-06-11 19:27:00 【水哥很水】
一、环境要求
Python==3.7
Tensorflow==2.4.0
二、数据准备
本博客演示的数据集可以通过以下链接进行下载
百度网盘提取码:9q0f
三、TFRecord文件的创建
import os
import cv2
import tensorflow as tf
import tqdm as tqdm
if __name__ == '__main__':
# 需要处理的数据集根目录
Dataset_path = 'Split_Dataset'
# 用于生成需要写入的文件路径
file_names = [i.split('.')[0] for i in os.listdir(os.path.join(Dataset_path, 'DSM'))]
dsm_filename = [os.path.join(Dataset_path, 'DSM', i + '.tif') for i in file_names]
label_filename = [os.path.join(Dataset_path, 'Label', i + '.png') for i in file_names]
rgb_filename = [os.path.join(Dataset_path, 'RGB', i + '.png') for i in file_names]
# 开始进行TFRecords文件的构建
with tf.io.TFRecordWriter('Potsdam.tfrecords') as writer:
tqdm_file = tqdm.tqdm(iterable=zip(dsm_filename, rgb_filename, label_filename), total=len(dsm_filename))
for dsm, rgb, label in tqdm_file:
# 进行高程图的读取
dsm_data = cv2.imread(dsm, -1)
dsm_data = dsm_data.ravel()
# 进行光学图的读取
rgb_data = open(rgb, 'rb').read()
# 进行标签的读取
label_data = open(label, 'rb').read()
# 建立tf.train.Feature字典
feature = {
'dsm': tf.train.Feature(float_list=tf.train.FloatList(value=dsm_data)),
'rgb': tf.train.Feature(bytes_list=tf.train.BytesList(value=[rgb_data])),
'label': tf.train.Feature(bytes_list=tf.train.BytesList(value=[label_data]))
}
# 通过字典创建Example
example = tf.train.Example(features=tf.train.Features(feature=feature))
# 将Example序列化并写入TFRecords文件
writer.write(example.SerializeToString())
tqdm_file.close()
四、TFRecord文件的读取
import tensorflow as tf
import matplotlib.pyplot as plt
# 构造Feature结构,告诉解码器每个Feature是什么
feature_description = {
'dsm': tf.io.FixedLenFeature([512, 512, 1], tf.float32),
'rgb': tf.io.FixedLenFeature([], tf.string),
'label': tf.io.FixedLenFeature([], tf.string)
}
# Example的解析函数
def parse_example(example_string):
feature = tf.io.parse_single_example(serialized=example_string, features=feature_description)
feature['rgb'] = tf.image.decode_png(feature['rgb'], channels=3)
feature['rgb'] = tf.image.resize(feature['rgb'], [512, 512])
feature['rgb'] = tf.cast(feature['rgb'], tf.float32)
feature['rgb'] = feature['rgb'] / 255
feature['label'] = tf.image.decode_png(feature['label'], channels=1)
feature['label'] = tf.image.resize(feature['label'], [512, 512])
feature['label'] = tf.cast(feature['label'], tf.int64)
feature['label'] = tf.squeeze(feature['label'])
return feature['dsm'], feature['rgb'], feature['label']
if __name__ == '__main__':
dataset = tf.data.TFRecordDataset(r'Potsdam.tfrecords')
dataset = dataset.map(parse_example)
dataset = dataset.shuffle(buffer_size=500)
for dsm, rgb, label in dataset.take(1):
temp = (dsm - tf.reduce_min(input_tensor=dsm, axis=(0, 1, 2))) / (
tf.reduce_max(input_tensor=dsm, axis=(0, 1, 2)) - tf.reduce_min(input_tensor=dsm, axis=(0, 1, 2)))
plt.figure(figsize=(15, 5), dpi=150)
plt.subplot(1, 3, 1)
plt.imshow(temp)
plt.axis('off')
plt.subplot(1, 3, 2)
plt.imshow(rgb)
plt.axis('off')
plt.subplot(1, 3, 3)
plt.imshow(label)
plt.axis('off')
plt.show()
五、TFRecord文件的读取结果

六、项目文件的下载
本文的项目源文件点击以下链接进行获取
项目源文件
边栏推荐
- Activate function formulas, derivatives, image notes
- YOLOv3 Pytorch代码及原理分析(一):跑通代码
- 无监督图像分类《SCAN:Learning to Classify Images without》代码分析笔记(1):simclr
- Crop disease detection using image processing technology and convolutional neural network (CNN)
- NR LDPC punched
- Cf:g. count the trains [sortedset + bisect + simulation maintaining strict decreasing sequence]
- leetcode:926. Flip the string to monotonically increasing [prefix and + analog analysis]
- LDPC 7 - simple example of decoding
- 构建Web应用程序
- Leetcode: sword finger offer 59 - ii Maximum value of queue [deque + sortedlist]
猜你喜欢

更换目标检测的backbone(以Faster RCNN为例)

postman配置中文

干货丨MapReduce的工作流程是怎样的?

图床:PicGo+腾讯云+typora

Yolov3 pytoch code and principle analysis (I): runthrough code

Use Mysql to determine the day of the week
![[video denoising] video denoising based on salt with matlab code](/img/79/e1f8255061a342e02232ac1e393217.png)
[video denoising] video denoising based on salt with matlab code

Operator new and placement new

Chrome tips - browser web page setting coding, solving the problem of web page garbled code, obtaining the latest version of charset plug-in, UTF-8 coding setting

E-commerce (njupt)
随机推荐
Review of software testing technology
Internet_ Business Analysis Overview
leetcode:剑指 Offer 59 - II. 队列的最大值[deque + sortedlist]
pstack和dmesg
Common - name of conference room
What started mongodb? What are the application scenarios?
今天睡眠质量记录60分
Use canvas to add text watermark to the page
Merge multiple binary search trees
Software requirements engineering review
Web3 Games: exploring and reshaping the game experience
[signal denoising] speech adaptive denoising based on nonlinear filter with matlab code
MongoDB 什么兴起的?应用场景有哪些?
leetcode:926. Flip the string to monotonically increasing [prefix and + analog analysis]
Pymysql uses cursor operation database method to encapsulate!!!
【信号去噪】基于FFT和FIR实现信号去噪附matlab代码
CMU 15-445 database course lesson 5 text version - buffer pool
The US inflation rate reached a 41 year high of 8.6%! High inflation fever? The stock and encryption markets fell first!
[C language questions -- 10 simple questions for leetcode]
Multimodal learning toolkit paddlemm based on propeller