Cifar-10資料集的視覺化儲存
阿新 • • 發佈:2018-12-09
學習Tensorflow或深度學習,難免用到各種資料集, 最近用到cifar10資料集,簡單研究了下,然後把cifar-10資料集儲存為jpg圖片,分別利用python和c++做了實現。
關於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 np from 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; }
結果截圖: