CIFAR是一个用于普通物体识别的数据集。CIFAR数据集分为两种:CIFAR-10和CIFAR-100。The CIFAR-10 and CIFAR-100 are labeled subsets of the 80 million tiny images dataset. They were collected by Alex Krizhevsky, Vinod Nair, and Geoffrey Hinton.

CIFAR-10由60000张大小为32*32的三通道彩色图像组成,被分为10类,分别为airplane、automobile、bird、cat、deer、dog、frog、horse、ship、truck。每类由6000张图像。其中50000张图像用来训练,10000张图像用来测试。数据集分为5个训练块和1个测试块,每个块包含10000张图像.训练集每类包含5000张图像,测试集每类包含1000张图像.

CIFAR-100由60000张大小为32*32的三通道彩色图像组成,分为20个大类,每个大类又包含5个小类,总共100个小类。每个小类包含600张图像,其中500张用于训练,100张用于测试。

https://www.cs.toronto.edu/~kriz/cifar.html 下载CIFAR C版本的二进制数据:

(1)、CIFAR-10:下载cifar-10-binary.tar.gz,解压缩,共8个文件,batches.meta.txt中存放10个种类名,data_batch_1.bin… data_batch_5.bin、test_batch.bin共6个文件,每个文件中存放10000张图像数据。

(2)、CIFAR-100:下载cifar-100-binary.tar.gz,解压缩,共5个文件,coarse_label_names.txt中存放20个大类名,fine_label_names.txt中存放100个小类名,train.bin中存放50000张训练图像,test.bin中存放10000张测试图像。

CIFAR数据集到图像转换实现的代码如下:

static void write_image_cifar(const cv::Mat& bgr, const std::string& image_save_path, const std::vector<int>& label_count, int label_class)
{
	std::string str = std::to_string(label_count[label_class]);

	if (label_count[label_class] < 10) {
		str = "0000" + str;
	} else if (label_count[label_class] < 100) {
		str = "000" + str;
	} else if (label_count[label_class] < 1000) {
		str = "00" + str;
	} else if (label_count[label_class] < 10000) {
		str = "0" + str;
	} else {
		fprintf(stderr, "save image name fail\n");
		return;
	}

	str = std::to_string(label_class) + "_" + str + ".png";
	str = image_save_path + str;

	cv::imwrite(str, bgr);
}

static void read_cifar_10(const std::string& bin_name, const std::string& image_save_path, int image_count, std::vector<int>& label_count)
{
	int image_width = 32;
	int image_height = 32;

	std::ifstream file(bin_name, std::ios::binary);
	if (file.is_open()) {
		for (int i = 0; i < image_count; ++i) {
			cv::Mat red = cv::Mat::zeros(image_height, image_width, CV_8UC1);
			cv::Mat green = cv::Mat::zeros(image_height, image_width, CV_8UC1);
			cv::Mat blue = cv::Mat::zeros(image_height, image_width, CV_8UC1);

			int label_class = 0;
			file.read((char*)&label_class, 1);
			label_count[label_class]++;

			file.read((char*)red.data, 1024);
			file.read((char*)green.data, 1024);
			file.read((char*)blue.data, 1024);

			std::vector<cv::Mat> tmp{ blue, green, red };
			cv::Mat bgr;
			cv::merge(tmp, bgr);

			write_image_cifar(bgr, image_save_path, label_count, label_class);
		}

		file.close();
	}
}

int CIFAR10toImage()
{
	std::string images_path = "E:/GitCode/NN_Test/data/database/CIFAR/CIFAR-10/";
	// train image
	std::vector<int> label_count(10, 0);
	for (int i = 1; i <= 5; i++) {
		std::string bin_name = images_path + "data_batch_" + std::to_string(i) + ".bin";
		std::string image_save_path = "E:/GitCode/NN_Test/data/tmp/cifar-10_train/";
		int image_count = 10000;

		read_cifar_10(bin_name, image_save_path, image_count, label_count);
	}

	// test image
	std::fill(&label_count[0], &label_count[0] + 10, 0);
	std::string bin_name = images_path + "test_batch.bin";
	std::string image_save_path = "E:/GitCode/NN_Test/data/tmp/cifar-10_test/";
	int image_count = 10000;

	read_cifar_10(bin_name, image_save_path, image_count, label_count);

	// save big imags
	images_path = "E:/GitCode/NN_Test/data/tmp/cifar-10_train/";
	int width = 32 * 20;
	int height = 32 * 10;
	cv::Mat dst(height, width, CV_8UC3);

	for (int i = 0; i < 10; i++) {
		for (int j = 1; j <= 20; j++) {
			int x = (j - 1) * 32;
			int y = i * 32;
			cv::Mat part = dst(cv::Rect(x, y, 32, 32));

			std::string str = std::to_string(j);
			if (j < 10)
				str = "0000" + str;
			else
				str = "000" + str;

			str = std::to_string(i) + "_" + str + ".png";
			std::string input_image = images_path + str;

			cv::Mat src = cv::imread(input_image, 1);
			if (src.empty()) {
				fprintf(stderr, "read image error: %s\n", input_image.c_str());
				return -1;
			}

			src.copyTo(part);
		}
	}

	std::string output_image = images_path + "result.png";
	cv::imwrite(output_image, dst);

	return 0;
}

static void write_image_cifar(const cv::Mat& bgr, const std::string& image_save_path,
	const std::vector<std::vector<int>>& label_count, int label_class_coarse, int label_class_fine)
{
	std::string str = std::to_string(label_count[label_class_coarse][label_class_fine]);

	if (label_count[label_class_coarse][label_class_fine] < 10) {
		str = "0000" + str;
	} else if (label_count[label_class_coarse][label_class_fine] < 100) {
		str = "000" + str;
	} else if (label_count[label_class_coarse][label_class_fine] < 1000) {
		str = "00" + str;
	} else if (label_count[label_class_coarse][label_class_fine] < 10000) {
		str = "0" + str;
	} else {
		fprintf(stderr, "save image name fail\n");
		return;
	}

	str = std::to_string(label_class_coarse) + "_" + std::to_string(label_class_fine) + "_" + str + ".png";
	str = image_save_path + str;

	cv::imwrite(str, bgr);
}

static void read_cifar_100(const std::string& bin_name, const std::string& image_save_path, int image_count, std::vector<std::vector<int>>& label_count)
{
	int image_width = 32;
	int image_height = 32;

	std::ifstream file(bin_name, std::ios::binary);
	if (file.is_open()) {
		for (int i = 0; i < image_count; ++i) {
			cv::Mat red = cv::Mat::zeros(image_height, image_width, CV_8UC1);
			cv::Mat green = cv::Mat::zeros(image_height, image_width, CV_8UC1);
			cv::Mat blue = cv::Mat::zeros(image_height, image_width, CV_8UC1);

			int label_class_coarse = 0;
			file.read((char*)&label_class_coarse, 1);
			int label_class_fine = 0;
			file.read((char*)&label_class_fine, 1);
			label_count[label_class_coarse][label_class_fine]++;

			file.read((char*)red.data, 1024);
			file.read((char*)green.data, 1024);
			file.read((char*)blue.data, 1024);

			std::vector<cv::Mat> tmp{ blue, green, red };
			cv::Mat bgr;
			cv::merge(tmp, bgr);

			write_image_cifar(bgr, image_save_path, label_count, label_class_coarse, label_class_fine);
		}

		file.close();
	}
}

int CIFAR100toImage()
{
	std::string images_path = "E:/GitCode/NN_Test/data/database/CIFAR/CIFAR-100/";
	// train image
	std::vector<std::vector<int>> label_count;
	label_count.resize(20);
	for (int i = 0; i < 20; i++) {
		label_count[i].resize(100);
		std::fill(&label_count[i][0], &label_count[i][0] + 100, 0);
	}

	std::string bin_name = images_path + "train.bin";
	std::string image_save_path = "E:/GitCode/NN_Test/data/tmp/cifar-100_train/";
	int image_count = 50000;

	read_cifar_100(bin_name, image_save_path, image_count, label_count);

	// test image
	for (int i = 0; i < 20; i++) {
		label_count[i].resize(100);
		std::fill(&label_count[i][0], &label_count[i][0] + 100, 0);
	}
	bin_name = images_path + "test.bin";
	image_save_path = "E:/GitCode/NN_Test/data/tmp/cifar-100_test/";
	image_count = 10000;

	read_cifar_100(bin_name, image_save_path, image_count, label_count);

	// save big imags
	images_path = "E:/GitCode/NN_Test/data/tmp/cifar-100_train/";
	int width = 32 * 20;
	int height = 32 * 100;
	cv::Mat dst(height, width, CV_8UC3);
	std::vector<std::string> image_names;

	for (int j = 0; j < 20; j++) {
		for (int i = 0; i < 100; i++) {
			std::string str1 = std::to_string(j);
			std::string str2 = std::to_string(i);
			std::string str = images_path + str1 + "_" + str2 + "_00001.png";
			cv::Mat src = cv::imread(str, 1);
			if (src.data) {
				for (int t = 1; t < 21; t++) {
					if (t < 10)
						str = "0000" + std::to_string(t);
					else
						str = "000" + std::to_string(t);

					str = images_path + str1 + "_" + str2 + "_" + str + ".png";
					image_names.push_back(str);
				}
			}
		}
	}

	for (int i = 0; i < 100; i++) {
		for (int j = 0; j < 20; j++) {
			int x = j * 32;
			int y = i * 32;
			cv::Mat part = dst(cv::Rect(x, y, 32, 32));
			cv::Mat src = cv::imread(image_names[i * 20 + j], 1);
			if (src.empty()) {
				fprintf(stderr, "read image fail: %s\n", image_names[i * 20 + j].c_str());
				return -1;
			}

			src.copyTo(part);
		}
	}

	std::string output_image = images_path + "result.png";
	cv::imwrite(output_image, dst);

	cv::Mat src = cv::imread(output_image, 1);
	if (src.empty()) {
		fprintf(stderr, "read result image fail: %s\n", output_image.c_str());
		return -1;
	}
	for (int i = 0; i < 4; i++) {
		cv::Mat dst = src(cv::Rect(0, i * 800, 640, 800));
		std::string str = images_path + "result_" + std::to_string(i + 1) + ".png";
		cv::imwrite(str, dst);
	}

	return 0;
}

cifar-10转换的结果如下:

cifar-100转换的结果如下:


GitHubhttps://github.com/fengbingchun/NN_Test

cifar数据集介绍及到图像转换的实现的更多相关文章

  1. Pascal VOC & COCO数据集介绍 & 转换

    目录 Pascal VOC & COCO数据集介绍 Pascal VOC数据集介绍 1. JPEGImages 2. Annotations 3. ImageSets 4. Segmentat ...

  2. OpenCV 编程简单介绍(矩阵/图像/视频的基本读写操作)

    PS. 因为csdn博客文章长度有限制,本文有部分内容被截掉了.在OpenCV中文站点的wiki上有可读性更好.而且是完整的版本号,欢迎浏览. OpenCV Wiki :<OpenCV 编程简单 ...

  3. 机器学习数据集,主数据集不能通过,人脸数据集介绍,从r包中获取数据集,中国河流数据集

    机器学习数据集,主数据集不能通过,人脸数据集介绍,从r包中获取数据集,中国河流数据集   选自Microsoft www.tz365.Cn 作者:Lee Scott 机器之心编译 参与:李亚洲.吴攀. ...

  4. Base64图片编码原理,base64图片工具介绍,图片在线转换Base64

    Base64图片编码原理,base64图片工具介绍,图片在线转换Base64 DataURI 允许在HTML文档中嵌入小文件,可以使用 img 标签或 CSS 嵌入转换后的 Base64 编码,减少  ...

  5. caffe之数据集介绍

    数据集:http://bigdata.51cto.com/art/201702/531276.htm 计算机视觉 MNIST: 最通用的健全检查.25x25 的数据集,中心化,B&W 手写数字 ...

  6. 详细介绍Base64的编码转换方式

    下面,详细介绍Base64的编码转换方式. 所谓Base64,就是说选出64个字符----小写字母a-z.大写字母A-Z.数字0-9.符号"+"."/"(再加上 ...

  7. <p><span style="font-size:14px">近期须要批量将PNM&#26684;式的文件转换成GIF文件。我尝试了例如以下的图像转换工具:</span></p>

    近期须要批量将PNM格式的文件转换成GIF文件.我尝试了例如以下的图像转换工具: ImageBatch:全然免费,但只支持PNG JPEG BMP GIF四种格式 OfficeConverter:在线 ...

  8. TensorFlow 卷积神经网络手写数字识别数据集介绍

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! 手写数字识别 接下来将会以 MNIST 数据集为例,使用卷积层和池 ...

  9. 将图标LOGO之类的图形图像转换成字体调用方法大全

    借鉴百度对此标题的评价: 使用字体图标的优势 字体图标除了图像清晰度之外,比位图还有哪些优势呢. 适用性:一个图标字体比一系列的图像(特别是在Retina屏中使用双倍大小的图像)要小.一旦图标字体加载 ...

随机推荐

  1. 用以替换系统NSLog的YouXianMingLog

    用以替换系统NSLog的YouXianMingLog 这是本人自己使用并改良的用以替换系统NSLog的类,非常好用,以下是使用示例,现在开源出来并提供源码,好用的话顶一下吧^_^ 效果: YouXia ...

  2. webform ajax 上传文件+参数

    今天维护webform项目时,有个需求需要在一个ajax中上传excel和多个参数.网上没怎么找到答案,这边做个笔记. 首先上页面大体这样 <form id= "uploadForm& ...

  3. (1)访问控制 (2)final关键字 (3)对象创建的过程 (4)多态

    1.访问控制(笔试题)1.1 常用的访问控制符 public - 公有的 protected - 保护的 啥也不写 - 默认的 private - 私有的 1.2 访问控制符的比较 访问控制符 访问权 ...

  4. mysql的表和数据类型

    一.查看当前数据库所有表 mysql> use db Database changed mysql> show tables; Empty set (0.00 sec) #表示db数据库下 ...

  5. MVC5开发环境的配置

    如果你打算在VS2012上开发MVC5,请通过WPI来安装此组件:ASP.NET and Web Tools 2013.1 version

  6. 【实战项目】【FLEX】#900 实现拖控件功能

    一.功能说明:拖控件的功能(类似FLEX,VS 里面的拖控件). 提示:大家对事件的注册和派发的说法可能不一样.因为在FLEX中和在Java中,叫法有的区别.但是本质是一样的. 注册事件  == 设置 ...

  7. 用windows自带的fsutil来创建1G稀疏文件(sparse file)

    fsutils file createnew  a.dat 1073741824 fsutil sparse setflag a.dat fsutil sparse setrange a.dat 0  ...

  8. swift内存管理:值类型与引用类型

    Use struct to create a structure. Structures support many of the same behaviors as classes, includin ...

  9. Kubernetes-dns 服务搭建

    DNS 服务不是独立的系统服务,而是一种 addon ,作为插件来安装的,不是 kubernetes 集群必须的(但是非常推荐安装).可以把它看做运行在集群上的应用,只不过这个应用比较特殊而已. DN ...

  10. 【CF662C】Binary Table

    题目 好吧,我连板子都不会了 有一个非常显然的做法就是\(O(2^nm)\)做法就是枚举每一行的状态,之后我们贪心去看看每一列是否需要翻转就好啦 显然这个做法非常垃圾过不去 首先我们发现每一列都不超过 ...