Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Database \ MNIST presentation and database conversion     - The minimum initial use of the Linux operating system RancherOS feelings (Linux)

- Oracle 11g + RAC + RHEL6.5 + udev + ASM + PSU installation summary (Database)

- VMware difference in three network connection (Linux)

- Some safety precautions of Linux servers (Linux)

- Oracle 11g Export guide problem not an empty table (Database)

- Use Epoll develop high-performance application server on Linux (Server)

- Oracle 11g creates virtual private directory RMAN-06004 ORA-00942 error handling (Database)

- ctop: monitor container performance Linux command line artifact (Linux)

- Ubuntu 15.04 and CentOS 7 to deploy .NET, Mono and DNX (Server)

- TWiki LDAP error appears the problem is solved (Linux)

- MNIST presentation and database conversion (Database)

- Commentary Apache + Tomcat + JK implement Tomcat clustering and load (Server)

- Cygwin install Python MySQLdb (Linux)

- OpenJDK7 source compiler installation on CentOS 6.5 (Linux)

- Install Jetty on CentOS / RHEL 6.X (Server)

- JEdit 5.2 Pro Edition installation on Ubuntu 14.04 (Linux)

- Linux operating system security tools of the Logs (Linux)

- Java regular expressions examples (Programming)

- Java 8 perspective annotation types (Programming)

- libnet list of functions (Programming)

 
         
  MNIST presentation and database conversion
     
  Add Date : 2017-08-31      
         
         
         
  MNIST database Introduction: MNIST is a handwritten digital database, which has 60 000 and 10 000 training set test sample set. It is a subset of the NIST database.

MNIST official database at: http: //yann.lecun.com/exdb/mnist/, can also be downloaded directly under the windows, train-images-idx3-ubyte.gz, train-labels-idx1-ubyte.gz like. Download four files, unzip. After extracting these files are not found in standard image format. These image data are stored in a binary file. Each sample image width and height is 28 * 28.

The following is to convert it into a normal jpg image format code:

#include < iostream >
#include < fstream >

#include "opencv2 / core / core.hpp"
#include "opencv2 / highgui / highgui.hpp"
#include "opencv2 / imgproc / imgproc.hpp"

using namespace std;

int ReverseInt (int i)
{
 unsigned char ch1, ch2, ch3, ch4;
 ch1 = i & 255;
 ch2 = (i >> 8) & 255;
 ch3 = (i >> 16) & 255;
 ch4 = (i >> 24) & 255;
 return ((int) ch1 << 24) + ((int) ch2 << 16) + ((int) ch3 << 8) + ch4;
}

void read_Mnist (string filename, vector & vec)
{
 ifstream file (filename, ios :: binary);
 if (file.is_open ()) {
  int magic_number = 0;
  int number_of_images = 0;
  int n_rows = 0;
  int n_cols = 0;
  file.read ((char *) & magic_number, sizeof (magic_number));
  magic_number = ReverseInt (magic_number);
  file.read ((char *) & number_of_images, sizeof (number_of_images));
  number_of_images = ReverseInt (number_of_images);
  file.read ((char *) & n_rows, sizeof (n_rows));
  n_rows = ReverseInt (n_rows);
  file.read ((char *) & n_cols, sizeof (n_cols));
  n_cols = ReverseInt (n_cols);

  for (int i = 0; i    cv :: Mat tp = cv :: Mat :: zeros (n_rows, n_cols, CV_8UC1);
   for (int r = 0; r     for (int c = 0; c      unsigned char temp = 0;
     file.read ((char *) & temp, sizeof (temp));
     tp.at (r, c) = (int) temp;
    }
   }
   vec.push_back (tp);
  }
 }
}

void read_Mnist_Label (string filename, vector & vec)
{
 ifstream file (filename, ios :: binary);
 if (file.is_open ()) {
  int magic_number = 0;
  int number_of_images = 0;
  int n_rows = 0;
  int n_cols = 0;
  file.read ((char *) & magic_number, sizeof (magic_number));
  magic_number = ReverseInt (magic_number);
  file.read ((char *) & number_of_images, sizeof (number_of_images));
  number_of_images = ReverseInt (number_of_images);

  for (int i = 0; i    unsigned char temp = 0;
   file.read ((char *) & temp, sizeof (temp));
   vec [i] = (int) temp;
  }
 }
}

string GetImageName (int number, int arr [])
{
 string str1, str2;

 for (int i = 0; i <10; i ++) {
  if (number == i) {
   arr [i] ++;
   char ch1 [10];
   sprintf (ch1, "% d", arr [i]);
   str1 = std :: string (ch1);

   if (arr [i] <10) {
    str1 = "0000" + str1;
   } Else if (arr [i] <100) {
    str1 = "000" + str1;
   } Else if (arr [i] <1000) {
    str1 = "00" + str1;
   } Else if (arr [i] <10000) {
    str1 = "0" + str1;
   }

   break;
  }
 }

 char ch2 [10];
 sprintf (ch2, "% d", number);
 str2 = std :: string (ch2);

 str2 = str2 + "_" + str1;

 return str2;
}

int main ()
{
 // Reference: http://eric-yuan.me/cpp-read-mnist/
 // Test images and test labels
 // Read MNIST image into OpenCV Mat vector
 string filename_test_images = "D: /Download/t10k-images-idx3-ubyte/t10k-images.idx3-ubyte";
 int number_of_test_images = 10000;
    vector vec_test_images;

    read_Mnist (filename_test_images, vec_test_images);

 // Read MNIST label into int vector
    string filename_test_labels = "D: /Download/t10k-labels-idx1-ubyte/t10k-labels.idx1-ubyte";
    vector vec_test_labels (number_of_test_images);

    read_Mnist_Label (filename_test_labels, vec_test_labels);

 if (vec_test_images.size ()! = vec_test_labels.size ()) {
  cout << "parse MNIST test file error" << endl;
  return -1;
 }

 // Save test images
 int count_digits [10];
 for (int i = 0; i <10; i ++)
  count_digits [i] = 0;

 string save_test_images_path = "D: / Download / MNIST / test_images /";

 for (int i = 0; i   int number = vec_test_labels [i];
  string image_name = GetImageName (number, count_digits);
  image_name = save_test_images_path + image_name + ".jpg";

  cv :: imwrite (image_name, vec_test_images [i]);
 }

 // Train images and train labels
 // Read MNIST image into OpenCV Mat vector
 string filename_train_images = "D: /Download/train-images-idx3-ubyte/train-images.idx3-ubyte";
 int number_of_train_images = 60000;
 vector vec_train_images;

 read_Mnist (filename_train_images, vec_train_images);

 // Read MNIST label into int vector
 string filename_train_labels = "D: /Download/train-labels-idx1-ubyte/train-labels.idx1-ubyte";
 vector vec_train_labels (number_of_train_images);

 read_Mnist_Label (filename_train_labels, vec_train_labels);

 if (vec_train_images.size ()! = vec_train_labels.size ()) {
  cout << "parse MNIST train file error" << endl;
  return -1;
 }

 // Save train images
 for (int i = 0; i <10; i ++)
  count_digits [i] = 0;

 string save_train_images_path = "D: / Download / MNIST / train_images /";

 for (int i = 0; i   int number = vec_train_labels [i];
  string image_name = GetImageName (number, count_digits);
  image_name = save_train_images_path + image_name + ".jpg";

  cv :: imwrite (image_name, vec_train_images [i]);
 }

 return 0;
}
     
         
         
         
  More:      
 
- MongoDB Study Notes (1) - Install MongoDB on Windows systems (Database)
- CentOS 6.5 upgrade to CentOS 7 (Linux)
- CentOS 6.6 installation certification system based on the ftp service (Server)
- Oracle database import and export (Database)
- How to choose the correct HTTP status code (Server)
- Examples 14 grep command (Linux)
- IIS virtual host of safety knowledge (Linux)
- Expand an existing RAID arrays and remove the failed disk in a RAID (Linux)
- Close common port to protect server security (Linux)
- Linux System Getting Started Tutorial: mounted directly in Linux LVM partition (Linux)
- Linux firewall rules example Extracts (Linux)
- Examples of safety testing system notes for RedHat Linux (Linux)
- PCM audio under Linux (Linux)
- To disable the function by pressing Ctrl-Alt-Del to restart the system in RHEL / CentOS 5/6 (Linux)
- Debian users to install FFmpeg 2.2.2 (Linux)
- CentOS7 set boot directly into the command line interface (Linux)
- Linux Fundamentals of the text, data flow processing orders (Linux)
- Redis 3.0.3 Cluster Setup (Database)
- Rails project prepared some tips small experience (Linux)
- What factors affect the performance of Java calls (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.