mnist文件解析(C语言)


点击查看 (人工智能) 系列文章


  mnist本身是将数字手写体图像,归一化后信息合成的文件,分训练集和测试集两部分,每部分包含图片和标签两个文件,本文使用C++写的Demo将文件解码,并通过opencv将数据存储为常规的图像文件。源码下载地址:https://github.com/zacSuo/mnist

  • t10k-images.idx3-ubyte
  • t10k-labels.idx1-ubyte
  • train-images.idx3-ubyte
  • train-labels.idx1-ubyte

使用说明

环境要求

  • visual studio 2012及以上版本
  • opencv 2.4及以上版本

配置

启动前先通过项目属性,设置本地opencv的路径。

执行结果

  在目录的两个文件夹,分别将测试集(test_images)和训练集(train_images)的图片保存到其中。

程序说明

解码图像

int read_Mnist(std::string filename, std::vector<cv::Mat> &vec)
{
   
	int number_of_images = 0;
	std::ifstream file(filename, std::ios::binary);
	if (file.is_open()) {
   
		int magic_number = 0;		
		int n_rows = 0;
		int n_cols = 0;
		file.read((char*)&magic_number, sizeof(magic_number));
		magic_number = ReverseInt(magic_number);
		file.read((char*)&number_of_images, sizeof(number_of_images));
		number_of_images = ReverseInt(number_of_images);
		file.read((char*)&n_rows, sizeof(n_rows)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MNIST数据集转换为Yolo格式需要进行以下步骤: 1. 将MNIST数据集的图像和标签转换为Yolo格式的标注文件。 2. 将Yolo格式的标注文件转换为Yolo需要的训练文件。 下面是一个简单的示例代码: ```python import os import numpy as np from PIL import Image # 标签列表 labels = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] # MNIST数据集路径 mnist_path = "path/to/mnist/" # Yolo格式的标注文件路径 labels_path = "path/to/labels.txt" # Yolo格式的训练文件路径 train_path = "path/to/train.txt" # 打开标注文件 with open(labels_path, "w") as labels_file: # 遍历MNIST数据集 for i in range(60000): # 加载图像和标签 img_path = os.path.join(mnist_path, "train", str(i) + ".png") label_path = os.path.join(mnist_path, "train-labels", str(i) + ".txt") img = Image.open(img_path) label = np.loadtxt(label_path) # 转换为Yolo格式的标注 label_yolo = [] for j in range(label.shape[0]): x_center = label[j, 0] + label[j, 2] / 2 y_center = label[j, 1] + label[j, 3] / 2 w = label[j, 2] h = label[j, 3] label_yolo.append(labels.index(str(int(label[j, 4]))) + 1) label_yolo.append(x_center / img.width) label_yolo.append(y_center / img.height) label_yolo.append(w / img.width) label_yolo.append(h / img.height) label_yolo = [str(l) for l in label_yolo] # 写入标注文件 labels_file.write(" ".join(label_yolo) + "\n") # 写入训练文件 train_file = os.path.join(mnist_path, "train.txt") with open(train_file, "a") as f: f.write(os.path.abspath(img_path) + "\n") ``` 这将生成一个名为“train.txt”的文件,其中包含MNIST数据集图像的绝对路径,以及一个名为“labels.txt”的文件,其中包含MNIST数据集图像的Yolo格式标注。注意,这只是一个简单的示例代码,你可能需要根据自己的需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

放羊郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值