cifar数据集介绍及到图像转换的实现

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数据集到图像转换实现的代码如下:

  1. static void write_image_cifar(const cv::Mat& bgr, const std::string& image_save_path, const std::vector<int>& label_count, int label_class)  
  2. {  
  3.     std::string str = std::to_string(label_count[label_class]);  
  4.   
  5.     if (label_count[label_class] < 10) {  
  6.         str = "0000" + str;  
  7.     } else if (label_count[label_class] < 100) {  
  8.         str = "000" + str;  
  9.     } else if (label_count[label_class] < 1000) {  
  10.         str = "00" + str;  
  11.     } else if (label_count[label_class] < 10000) {  
  12.         str = "0" + str;  
  13.     } else {  
  14.         fprintf(stderr, "save image name fail\n");  
  15.         return;  
  16.     }  
  17.   
  18.     str = std::to_string(label_class) + "_" + str + ".png";  
  19.     str = image_save_path + str;  
  20.   
  21.     cv::imwrite(str, bgr);  
  22. }  
  23.   
  24. static void read_cifar_10(const std::string& bin_name, const std::string& image_save_path, int image_count, std::vector<int>& label_count)  
  25. {  
  26.     int image_width = 32;  
  27.     int image_height = 32;  
  28.   
  29.     std::ifstream file(bin_name, std::ios::binary);  
  30.     if (file.is_open()) {  
  31.         for (int i = 0; i < image_count; ++i) {  
  32.             cv::Mat red = cv::Mat::zeros(image_height, image_width, CV_8UC1);  
  33.             cv::Mat green = cv::Mat::zeros(image_height, image_width, CV_8UC1);  
  34.             cv::Mat blue = cv::Mat::zeros(image_height, image_width, CV_8UC1);  
  35.   
  36.             int label_class = 0;  
  37.             file.read((char*)&label_class, 1);  
  38.             label_count[label_class]++;  
  39.   
  40.             file.read((char*)red.data, 1024);  
  41.             file.read((char*)green.data, 1024);  
  42.             file.read((char*)blue.data, 1024);  
  43.   
  44.             std::vector<cv::Mat> tmp{ blue, green, red };  
  45.             cv::Mat bgr;  
  46.             cv::merge(tmp, bgr);  
  47.   
  48.             write_image_cifar(bgr, image_save_path, label_count, label_class);  
  49.         }  
  50.   
  51.         file.close();  
  52.     }  
  53. }  
  54.   
  55. int CIFAR10toImage()  
  56. {  
  57.     std::string images_path = "E:/GitCode/NN_Test/data/database/CIFAR/CIFAR-10/";  
  58.     // train image  
  59.     std::vector<int> label_count(10, 0);  
  60.     for (int i = 1; i <= 5; i++) {  
  61.         std::string bin_name = images_path + "data_batch_" + std::to_string(i) + ".bin";  
  62.         std::string image_save_path = "E:/GitCode/NN_Test/data/tmp/cifar-10_train/";  
  63.         int image_count = 10000;  
  64.   
  65.         read_cifar_10(bin_name, image_save_path, image_count, label_count);  
  66.     }  
  67.   
  68.     // test image  
  69.     std::fill(&label_count[0], &label_count[0] + 10, 0);  
  70.     std::string bin_name = images_path + "test_batch.bin";  
  71.     std::string image_save_path = "E:/GitCode/NN_Test/data/tmp/cifar-10_test/";  
  72.     int image_count = 10000;  
  73.   
  74.     read_cifar_10(bin_name, image_save_path, image_count, label_count);  
  75.   
  76.     // save big imags  
  77.     images_path = "E:/GitCode/NN_Test/data/tmp/cifar-10_train/";  
  78.     int width = 32 * 20;  
  79.     int height = 32 * 10;  
  80.     cv::Mat dst(height, width, CV_8UC3);  
  81.   
  82.     for (int i = 0; i < 10; i++) {  
  83.         for (int j = 1; j <= 20; j++) {  
  84.             int x = (j - 1) * 32;  
  85.             int y = i * 32;  
  86.             cv::Mat part = dst(cv::Rect(x, y, 32, 32));  
  87.   
  88.             std::string str = std::to_string(j);  
  89.             if (j < 10)  
  90.                 str = "0000" + str;  
  91.             else  
  92.                 str = "000" + str;  
  93.   
  94.             str = std::to_string(i) + "_" + str + ".png";  
  95.             std::string input_image = images_path + str;  
  96.   
  97.             cv::Mat src = cv::imread(input_image, 1);  
  98.             if (src.empty()) {  
  99.                 fprintf(stderr, "read image error: %s\n", input_image.c_str());  
  100.                 return -1;  
  101.             }  
  102.   
  103.             src.copyTo(part);  
  104.         }  
  105.     }  
  106.   
  107.     std::string output_image = images_path + "result.png";  
  108.     cv::imwrite(output_image, dst);  
  109.   
  110.     return 0;  
  111. }  
  112.   
  113. static void write_image_cifar(const cv::Mat& bgr, const std::string& image_save_path,  
  114.     const std::vector<std::vector<int>>& label_count, int label_class_coarse, int label_class_fine)  
  115. {  
  116.     std::string str = std::to_string(label_count[label_class_coarse][label_class_fine]);  
  117.   
  118.     if (label_count[label_class_coarse][label_class_fine] < 10) {  
  119.         str = "0000" + str;  
  120.     } else if (label_count[label_class_coarse][label_class_fine] < 100) {  
  121.         str = "000" + str;  
  122.     } else if (label_count[label_class_coarse][label_class_fine] < 1000) {  
  123.         str = "00" + str;  
  124.     } else if (label_count[label_class_coarse][label_class_fine] < 10000) {  
  125.         str = "0" + str;  
  126.     } else {  
  127.         fprintf(stderr, "save image name fail\n");  
  128.         return;  
  129.     }  
  130.   
  131.     str = std::to_string(label_class_coarse) + "_" + std::to_string(label_class_fine) + "_" + str + ".png";  
  132.     str = image_save_path + str;  
  133.   
  134.     cv::imwrite(str, bgr);  
  135. }  
  136.   
  137. 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)  
  138. {  
  139.     int image_width = 32;  
  140.     int image_height = 32;  
  141.   
  142.     std::ifstream file(bin_name, std::ios::binary);  
  143.     if (file.is_open()) {  
  144.         for (int i = 0; i < image_count; ++i) {  
  145.             cv::Mat red = cv::Mat::zeros(image_height, image_width, CV_8UC1);  
  146.             cv::Mat green = cv::Mat::zeros(image_height, image_width, CV_8UC1);  
  147.             cv::Mat blue = cv::Mat::zeros(image_height, image_width, CV_8UC1);  
  148.   
  149.             int label_class_coarse = 0;  
  150.             file.read((char*)&label_class_coarse, 1);  
  151.             int label_class_fine = 0;  
  152.             file.read((char*)&label_class_fine, 1);  
  153.             label_count[label_class_coarse][label_class_fine]++;  
  154.   
  155.             file.read((char*)red.data, 1024);  
  156.             file.read((char*)green.data, 1024);  
  157.             file.read((char*)blue.data, 1024);  
  158.   
  159.             std::vector<cv::Mat> tmp{ blue, green, red };  
  160.             cv::Mat bgr;  
  161.             cv::merge(tmp, bgr);  
  162.   
  163.             write_image_cifar(bgr, image_save_path, label_count, label_class_coarse, label_class_fine);  
  164.         }  
  165.   
  166.         file.close();  
  167.     }  
  168. }  
  169.   
  170. int CIFAR100toImage()  
  171. {  
  172.     std::string images_path = "E:/GitCode/NN_Test/data/database/CIFAR/CIFAR-100/";  
  173.     // train image  
  174.     std::vector<std::vector<int>> label_count;  
  175.     label_count.resize(20);  
  176.     for (int i = 0; i < 20; i++) {  
  177.         label_count[i].resize(100);  
  178.         std::fill(&label_count[i][0], &label_count[i][0] + 100, 0);  
  179.     }  
  180.   
  181.     std::string bin_name = images_path + "train.bin";  
  182.     std::string image_save_path = "E:/GitCode/NN_Test/data/tmp/cifar-100_train/";  
  183.     int image_count = 50000;  
  184.   
  185.     read_cifar_100(bin_name, image_save_path, image_count, label_count);  
  186.   
  187.     // test image  
  188.     for (int i = 0; i < 20; i++) {  
  189.         label_count[i].resize(100);  
  190.         std::fill(&label_count[i][0], &label_count[i][0] + 100, 0);  
  191.     }  
  192.     bin_name = images_path + "test.bin";  
  193.     image_save_path = "E:/GitCode/NN_Test/data/tmp/cifar-100_test/";  
  194.     image_count = 10000;  
  195.   
  196.     read_cifar_100(bin_name, image_save_path, image_count, label_count);  
  197.   
  198.     // save big imags  
  199.     images_path = "E:/GitCode/NN_Test/data/tmp/cifar-100_train/";  
  200.     int width = 32 * 20;  
  201.     int height = 32 * 100;  
  202.     cv::Mat dst(height, width, CV_8UC3);  
  203.     std::vector<std::string> image_names;  
  204.   
  205.     for (int j = 0; j < 20; j++) {  
  206.         for (int i = 0; i < 100; i++) {  
  207.             std::string str1 = std::to_string(j);  
  208.             std::string str2 = std::to_string(i);  
  209.             std::string str = images_path + str1 + "_" + str2 + "_00001.png";  
  210.             cv::Mat src = cv::imread(str, 1);  
  211.             if (src.data) {  
  212.                 for (int t = 1; t < 21; t++) {  
  213.                     if (t < 10)  
  214.                         str = "0000" + std::to_string(t);  
  215.                     else  
  216.                         str = "000" + std::to_string(t);  
  217.   
  218.                     str = images_path + str1 + "_" + str2 + "_" + str + ".png";  
  219.                     image_names.push_back(str);  
  220.                 }  
  221.             }  
  222.         }  
  223.     }  
  224.   
  225.     for (int i = 0; i < 100; i++) {  
  226.         for (int j = 0; j < 20; j++) {  
  227.             int x = j * 32;  
  228.             int y = i * 32;  
  229.             cv::Mat part = dst(cv::Rect(x, y, 32, 32));  
  230.             cv::Mat src = cv::imread(image_names[i * 20 + j], 1);  
  231.             if (src.empty()) {  
  232.                 fprintf(stderr, "read image fail: %s\n", image_names[i * 20 + j].c_str());  
  233.                 return -1;  
  234.             }  
  235.   
  236.             src.copyTo(part);  
  237.         }  
  238.     }  
  239.   
  240.     std::string output_image = images_path + "result.png";  
  241.     cv::imwrite(output_image, dst);  
  242.   
  243.     cv::Mat src = cv::imread(output_image, 1);  
  244.     if (src.empty()) {  
  245.         fprintf(stderr, "read result image fail: %s\n", output_image.c_str());  
  246.         return -1;  
  247.     }  
  248.     for (int i = 0; i < 4; i++) {  
  249.         cv::Mat dst = src(cv::Rect(0, i * 800, 640, 800));  
  250.         std::string str = images_path + "result_" + std::to_string(i + 1) + ".png";  
  251.         cv::imwrite(str, dst);  
  252.     }  
  253.   
  254.     return 0;  
  255. }  
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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CIFAR10数据集是一个二进制文件,包含了一些32x32像素的彩色图片。下面是一个MATLAB代码示例,演示如何将CIFAR10数据集转换成图片集。 ```matlab % 定义文件路径和名称 dataFile = 'cifar-10-batches-mat\data_batch_1.mat'; % 加载数据 data = load(dataFile); % 获取图像数据 images = data.data; % 获取标签数据 labels = data.labels; % 定义图像文件夹路径 imgFolder = 'cifar10_images'; % 如果文件夹不存在则创建文件夹 if ~exist(imgFolder, 'dir') mkdir(imgFolder); end % 循环处理每张图像 for i = 1:size(images,1) % 生成图像文件名 imgName = strcat(imgFolder, '\', num2str(i), '_', num2str(labels(i)), '.png'); % 获取图像数据 imgData = images(i,:); % 重塑图像数据形状 imgData = reshape(imgData, [32,32,3]); % 将数据类型转换为无符号8位整数 imgData = uint8(imgData); % 保存图像文件 imwrite(imgData, imgName); end ``` 在上面的示例中,我们首先定义了CIFAR10数据集文件的路径和名称,然后使用MATLAB的load函数加载数据。接下来,我们将图像数据和标签数据分别存储在变量images和labels中。 我们还定义了一个图像文件夹路径,用于存储转换后的图像。如果该文件夹不存在,则使用MATLAB的mkdir函数创建该文件夹。 然后,我们使用循环处理每张图像,生成一个唯一的图像文件名,获取图像数据,将图像数据重塑为32x32x3的形状,并将数据类型转换为无符号8位整数。最后,我们使用MATLAB的imwrite函数保存图像文件。 请注意,上面的代码示例只处理了CIFAR10数据集的一个批次。如果您想转换整个数据集,请在循环中使用所有批次的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值