ubyte 数据集的制作【2月更文挑战第07天】

简介: 【2月更文挑战第07天】

image.png
t10k-images-idx3-ubyte
将文件夹中的图片处理成二进制数据集(如 MNIST 数据集的格式)是一个常见的需求,特别是在需要将数据集用于某些特定的框架或工具时。以下是一个详细的步骤指南,展示如何将文件夹中的图片和对应的标签转换为二进制格式。
步骤 1:准备数据
假设你的数据集结构如下:
复制
dataset/
├── train/
│ ├── 0/
│ ├── 1/
│ ├── 2/
│ └── ...
└── val/
├── 0/
├── 1/
├── 2/
└── ...
每个文件夹中的图片对应一个类别。
步骤 2:读取图片和标签
使用 Python 的 PIL 或 OpenCV 库读取图片,并将图片和标签存储到内存中。
Python复制
import os
from PIL import Image
import numpy as np

def load_images_and_labels(data_dir, target_size=(28, 28)):
images = []
labels = []
for label, folder in enumerate(sorted(os.listdir(data_dir))):
folder_path = os.path.join(data_dir, folder)
if not os.path.isdir(folder_path):
continue
for filename in os.listdir(folder_path):
if filename.endswith('.png') or filename.endswith('.jpg'):
image_path = os.path.join(folder_path, filename)
image = Image.open(image_path).convert('L') # 转换为灰度图
image = image.resize(target_size) # 调整大小
images.append(np.array(image, dtype=np.uint8))
labels.append(label)
return np.array(images), np.array(labels)

示例

train_images, train_labels = load_images_and_labels('dataset/train')
val_images, val_labels = load_images_and_labels('dataset/val')
步骤 3:保存为二进制文件
将图片和标签保存为二进制文件,格式类似于 MNIST 数据集。
Python复制
import struct

def save_mnist(images, labels, image_file_path, label_file_path):
with open(image_file_path, 'wb') as image_file, open(label_file_path, 'wb') as label_file:

    # 写入图片文件头
    image_file.write(struct.pack('>IIII', 2051, len(images), images.shape[1], images.shape[2]))
    # 写入标签文件头
    label_file.write(struct.pack('>II', 2049, len(labels)))
    # 写入图片数据
    for image in images:
        image_file.write(image.tobytes())
    # 写入标签数据
    label_file.write(labels.tobytes())

示例

save_mnist(train_images, train_labels, 'train-images-idx3-ubyte', 'train-labels-idx1-ubyte')
save_mnist(val_images, val_labels, 't10k-images-idx3-ubyte', 't10k-labels-idx1-ubyte')
步骤 4:验证保存的文件
你可以使用之前提到的读取 MNIST 数据集的代码来验证保存的文件是否正确。
Python复制
def read_mnist_images(file_path):
with open(file_path, 'rb') as f:
magic, num, rows, cols = struct.unpack(">IIII", f.read(16))
images = np.frombuffer(f.read(), dtype=np.uint8).reshape(num, rows, cols)
return images

def read_mnist_labels(file_path):
with open(file_path, 'rb') as f:
magic, num = struct.unpack(">II", f.read(8))
labels = np.frombuffer(f.read(), dtype=np.uint8)
return labels

示例

train_images = read_mnist_images('train-images-idx3-ubyte')
train_labels = read_mnist_labels('train-labels-idx1-ubyte')
val_images = read_mnist_images('t10k-images-idx3-ubyte')
val_labels = read_mnist_labels('t10k-labels-idx1-ubyte')

print(train_images.shape, train_labels.shape)
print(val_images.shape, val_labels.shape)

6个月前
t10k-labels-idx1-ubyte

6个月前
train-images-idx3-ubyte

6个月前
train-labels-idx1-ubyte

6个月前
t10k-images.idx3-ubyte

7.84MB
6个月前
下载
t10k-labels.idx1-ubyte

10.01KB
6个月前
下载
train-images.idx3-ubyte

目录
相关文章
|
NoSQL Java Redis
SpringBoot-引入Redis依赖
本文介绍如何在IDEA里将SpringBoot整合Redis。
730 0
|
25天前
|
人工智能 自然语言处理 运维
2026年阿里云OpenClaw(Clawdbot)海外部署+Slack深度接入全指南
在全球化协作日益频繁的2026年,OpenClaw(原Clawdbot)作为企业级AI自动化代理工具,凭借**跨平台协作、轻量化部署、插件化扩展**三大核心优势,成为远程办公、跨国团队协作场景下的效率利器。2026年阿里云重磅推出OpenClaw专属海外云端部署方案,深度适配Slack在海外企业协作场景的高渗透率,实现“Slack频道/私信下达自然语言指令,阿里云海外服务器运行的OpenClaw自动执行自动化任务”的高效协作模式,彻底解决跨境网络不稳定、本地部署易断联、多语言协作障碍等痛点。
540 12
|
机器学习/深度学习 API
机器学习入门(七):线性回归原理,损失函数和正规方程
机器学习入门(七):线性回归原理,损失函数和正规方程
1650 1
|
7月前
|
Linux iOS开发 开发者
Linux桌面版,又被喷了!
罗马不是一天建成的,想要拥有更多的大众用户,在市场份额突破个位数,就需要虚心倾听他们的声音,学习赛道中的优秀者。
|
机器学习/深度学习
YOLOv11改进策略【Neck】| GSConv+Slim Neck:混合深度可分离卷积和标准卷积的轻量化网络设计
YOLOv11改进策略【Neck】| GSConv+Slim Neck:混合深度可分离卷积和标准卷积的轻量化网络设计
1135 8
YOLOv11改进策略【Neck】| GSConv+Slim Neck:混合深度可分离卷积和标准卷积的轻量化网络设计
|
机器学习/深度学习 并行计算 调度
CuPy:将 NumPy 数组调度到 GPU 上运行
CuPy:将 NumPy 数组调度到 GPU 上运行
684 1
|
NoSQL API Redis
如何使用 C++ 开发 Redis 模块
如何使用 C++ 开发 Redis 模块
|
机器学习/深度学习 算法 Unix
循环编码:时间序列中周期性特征的一种常用编码方式
循环编码是深度学习中处理周期性数据的一种技术,常用于时间序列预测。它将周期性特征(如小时、日、月)转换为网络可理解的形式,帮助模型识别周期性变化。传统的one-hot编码将时间特征转换为分类特征,而循环编码利用正弦和余弦转换,保持时间顺序信息。通过将时间戳转换为弧度并应用sin和cos,每个原始特征只映射到两个新特征,减少了特征数量。这种方法在神经网络中有效,但在树模型中可能需谨慎使用。
1985 5
|
机器学习/深度学习 数据可视化 数据库
R语言对MNIST数据集分析:探索手写数字分类
R语言对MNIST数据集分析:探索手写数字分类
|
机器学习/深度学习 算法 IDE
一行代码加速sklearn运算上千倍
一行代码加速sklearn运算上千倍
389 0

热门文章

最新文章