(转)Cifar-10数据集的可视化保存
https://blog.csdn.net/hyqwmxsh/article/details/82629961
学习Tensorflow或深度学习,难免用到各种数据集, 最近用到cifar10数据集,简单研究了下,然后把cifar-10数据集保存为jpg图片,分别利用python和c++做了实现。
CIFAR-10下载
关于cifar-10,网上介绍很多,这里主要用了python和binary版本:
python版
每个batch包含一个字典,该字典有data和labels两个key。其中,data是1000*3072( 3 *32 *32)的图像数据。1000即图片数量,前1024个数据是red通道像素值,然后1024是个green通道像素值,最后啥blue通道。labels是1000个0~9表示数据类别的数据。
代码如下:
import numpy as npfrom PIL import Image
import pickle
import os
CHANNEL = 3
WIDTH = 32
HEIGHT = 32
data = []
labels=[]
classification = ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']
for i in range(5):
with open("data/cifar-10-batches-py/data_batch_"+ str(i+1),mode='rb') as file:
data_dict = pickle.load(file, encoding='bytes')
data+= list(data_dict[b'data'])
labels+= list(data_dict[b'labels'])
img = np.reshape(data,[-1,CHANNEL, WIDTH, HEIGHT])
data_path = "data/images/"
if not os.path.exists(data_path):
os.makedirs(data_path)
for i in range(img.shape[0]):
r = img[i][0]
g = img[i][1]
b = img[i][2]
ir = Image.fromarray(r)
ig = Image.fromarray(g)
ib = Image.fromarray(b)
rgb = Image.merge("RGB", (ir, ig, ib))
name = "img-" + str(i) +"-"+ classification[labels[i]]+ ".png"
rgb.save(data_path + name, "PNG")
结果截图:
C++版
每个batch包括10000*(1 + 3072)大小数据,1代表label大小,3072是图像数据。存储方式同上。
代码如下:
#include<iostream>#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
#define WIDTH 32
#define HEIGHT 32
#define CHANNEL 3
#define PERNUM 1000
#define CLASS 10
char classification[CLASS][256] = { "airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck" };
int main(){
FILE *pBatch = fopen("data_batch_1.bin","rb");
if (!pBatch)
return -1;
unsigned char buf[CHANNEL * WIDTH * HEIGHT + 1];
memset(buf,0,sizeof(buf));
Mat bgr;
bgr.create(WIDTH,HEIGHT,CV_8UC3);
int index = 0;
while (!feof(pBatch)){
fread(buf, 1, CHANNEL * WIDTH * HEIGHT + 1, pBatch);
unsigned char* pBuf = buf + 1;
for (int i = 0; i < bgr.rows;i++){
Vec3b *pbgr = bgr.ptr<Vec3b>(i);
for (int j = 0; j < bgr.cols;j++){
//pBuf += (i * bgr.rows + j * bgr.cols);
for (int c = 0; c < 3;c++){
pbgr[j][c] = pBuf[(2 - c)* bgr.rows * bgr.cols + i * bgr.rows + j ];
}
}
}
imwrite("image/img" + to_string(index)+".jpg",bgr);
index++;
}
fclose(pBatch);
return 0;
}
结果截图:
以上是 (转)Cifar-10数据集的可视化保存 的全部内容, 来源链接: utcz.com/a/53380.html