@@ -0,0 +1,1626 @@
+#!/usr/bin/env python%0A# -*- coding:utf-8 -*-%0A%0A'''%0ADirectory structure%0ATRAIN_DIR: %0A  label0:%0A    img0001.png%0A    img0002.png%0A    img0003.png%0A  label1:%0A    img0001.png%0A    img0002.png%0A    .%0A    .%0A    .%0A  label9:%0A    img0001.png%0A'''%0A%0Aimport cv2, os%0Aimport numpy as np%0A%0ALABEL_MAGIC_NUMBER = 2049%0AIMAGE_MAGIC_NUMBER = 2051%0ATRAIN_DIR = %22data%22%0A%0Adef _make32(val):%0A  # Big endian%0A  return %5Bval %3E%3E i & 0xff for i in %5B24,16,8,0%5D%5D%0A%0Aimg_data = %5B%5D%0Adata_label = %5B%5D%0Adata_size = %7B%7D%0A%0Afor dirname in next(os.walk(TRAIN_DIR))%5B1%5D:%0A  data_label.append(dirname)%0A%0A  files = next(os.walk(TRAIN_DIR + %22/%22 + dirname))%5B2%5D%0A  data_size%5Bdirname%5D = len(files)%0A%0A  for filename in files:%0A    img_file = TRAIN_DIR + %22/%22 + dirname + %22/%22 + filename%0A    #print(img_file)%0A    img = cv2.imread(img_file)%0A    img = cv2.resize(img, (28, 28))%0A    imgg = cv2.cvtColor(img, cv2.cv.CV_BGR2GRAY)%0A%0A    img_data = np.r_%5Bimg_data, imgg%5B:,:%5D.reshape(imgg.size)%5D%0A%0A%0A# make a train label data%0A# make header%0Aldata = _make32(LABEL_MAGIC_NUMBER)%0Aldata = np.r_%5Bldata, _make32(sum(data_size.values()))%5D%0A%0A# write value%0Afor i,v in enumerate(data_label):%0A  ldata = np.r_%5Bldata, %5Bi%5D*data_size%5Bv%5D%5D%0A%0Anp.array(ldata, dtype=np.uint8).tofile(TRAIN_DIR + %22/labels.idx1-ubyte%22)%0A%0A# make a train image data%0A# make header%0Aidata = _make32(IMAGE_MAGIC_NUMBER)%0Aidata = np.r_%5Bidata, _make32(sum(data_size.values()))%5D%0Aidata = np.r_%5Bidata, _make32(28)%5D%0Aidata = np.r_%5Bidata, _make32(28)%5D%0Aidata = np.r_%5Bidata, img_data%5D%0A%0Anp.array(idata, dtype=np.uint8).tofile(TRAIN_DIR + %22/images.idx3-ubyte%22)%0A%0Awith open(TRAIN_DIR + %22/label_name.txt%22, 'w') as f:%0A  f.write(%22,%22.join(%5B%22%5C%22%22 + x + %22%5C%22%22 for x in data_label%5D))%0A%0A

Add a utility file to make a train data for MNIST sample.