기타

KISTI-INTEL Data School example files

lovegbt 2018. 8. 20. 14:19

KISTI-INTEL Data School example files


capmouse.cpp

command.txt

convert_mnist_data.cpp

createdb.cpp

emnist2png.cpp

mnist2png.cpp



#include <iostream>
#include <string>
#include <sys/stat.h>
#include "opencv2/videoio.hpp"
#include "opencv2/opencv.hpp"
 
using namespace std;
using namespace cv;
 
unsigned int swap_endian(unsigned int val) {
  val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
  return (val << 16) | (val >> 16);
}
 
int main()
{
  struct stat sb = { 0 };
  string path = "/home/logic/work/caffe/data/mnist/";
  string work_dir;
 
  for (int i = 0; i < 10; i++)
  {
    work_dir = path;
    work_dir += i+'0';
 
    cout << "data path : " << work_dir << endl;
    if (stat(work_dir.c_str(), &sb) == -1)
    {
      mkdir(work_dir.c_str(), 0700);
    }
  }
 
  string image_name[2] = {"train-images-idx3-ubyte", "t10k-images-idx3-ubyte"};
  string label_name[2] = {"train-labels-idx1-ubyte","t10k-labels-idx1-ubyte"};
 
  char label;  
  int rows(28), cols(28);
  Mat mat(rows, cols, CV_8UC1);
  unsigned char* pixels = mat.data;
  int num_label[10] = { 0 };
  
  string png_name;
  vector<int> compression_params;
  compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
  compression_params.push_back(0);
  char filename[1024];
 
  for (int i = 0; i < 2; i++)
  {
    ifstream image_file(path+image_name[i], ios::in | ios::binary);
    ifstream label_file(path+label_name[i], ios::in | ios::binary);
 
    if (!image_file.is_open()) {
      cout << "Unable to open file " << image_name[i] << endl;
      return -1;
    }
    if (!label_file.is_open()) {
      cout << "Unable to open file " << label_name[i] << endl;
      return -1;
    }
 
    unsigned int head;
    unsigned int num_items; 
    unsigned int num_labels;
    unsigned int rows;
    unsigned int cols;
 
    image_file.read(reinterpret_cast<char*>(&head), 4);
    head = swap_endian(head);
    if (head != 2051)
    {
      cout << "Incorrect image file header." << endl;
      return -1;
    }
 
    image_file.read(reinterpret_cast<char*>(&num_items), 4);
    num_items = swap_endian(num_items);
    image_file.read(reinterpret_cast<char*>(&rows), 4);
    rows = swap_endian(rows);
    image_file.read(reinterpret_cast<char*>(&cols), 4);
    cols = swap_endian(cols);
 
    label_file.read(reinterpret_cast<char*>(&head), 4);
    head = swap_endian(head);
    if (head != 2049)
    {
      cout << "Incorrect index file header." << endl;
      return -1;
    }
    label_file.read(reinterpret_cast<char*>(&num_labels), 4);
    num_labels = swap_endian(num_labels);
    
    if (num_items != num_labels) 
    {
      cout << "Invalid data." << endl;
      return -1;
    }
  
    for (int item_id = 0; item_id < num_items; ++item_id) {
      image_file.read(reinterpret_cast<char*>(pixels), rows * cols);
      label_file.read(&label, 1);
 
      sprintf(filename, "%s/%d/%d_%05d.png", path.c_str(), label, label, num_label[label]++);
      imwrite(filename, mat, compression_params);
    }
  }
 
  return 0;
}