-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgene_dataset.py
97 lines (82 loc) · 3.77 KB
/
gene_dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
#coding=utf-8
# @file : gene_dataset.py
# @time : 9/12/2019 11:09 PM
# @author: shishishu
import argparse
import os
import pickle
import numpy as np
from ast import literal_eval
from conf import config
from lib.preprocess.download_data import download_mnist, parse_idx_data
from lib.preprocess.trans_dump_data import parse_images, parse_labels
parser = argparse.ArgumentParser()
parser.add_argument('--flatten_flag', type=literal_eval, default=False, help='flatten images to one dimension (row * col)')
parser.add_argument('--expand_flag', type=literal_eval, default=True, help='expand labels to two dimensions (one hot)')
class GeneDataset:
def __init__(self, flatten_flag, expand_flag):
self.flatten_flag = flatten_flag
self.expand_flag = expand_flag
self.mnist_dict = config.MNIST_DICT
self.raw_dir = os.path.join(config.DATA_DIR, 'raw')
self.npy_dir = os.path.join(config.DATA_DIR, 'npy')
def download_save_data(self):
for key, val in self.mnist_dict.items():
unzip_file_path = download_mnist(self.raw_dir, val['file_name'], val['file_size'])
data = parse_idx_data(unzip_file_path, val['example_count'])
data_src, data_type = GeneDataset.split_file_name(val['file_name'])
if data_type == 'images':
parse_images(data, self.npy_dir, data_src, self.flatten_flag)
if data_type == 'labels':
parse_labels(data, self.npy_dir, data_src, self.expand_flag)
@staticmethod
def split_file_name(file_name):
data_src_raw, data_type = file_name.split('-')[:2]
if data_src_raw == 'train':
data_src = 'tr'
if data_src_raw == 't10k':
data_src = 'te'
return data_src, data_type
@staticmethod
def load_digits(data_src, flatten_flag=False, expand_flag=True):
data = dict()
data['images'] = GeneDataset.load_npy_images(data_src, flatten_flag)
data['labels'] = GeneDataset.load_npy_labels(data_src, expand_flag)
return data
@staticmethod
def load_npy_images(data_src, flatten_flag=False):
if flatten_flag:
file_name = data_src + '_images_flatten.npy'
else:
file_name = data_src + '_images.npy'
file_path = os.path.join(config.DATA_DIR, 'npy', file_name)
return pickle.load(open(file_path, 'rb'))
@staticmethod
def load_npy_labels(data_src, expand_flag=True):
if expand_flag:
file_name = data_src + '_labels_expand.npy'
else:
file_name = data_src + '_labels.npy'
file_path = os.path.join(config.DATA_DIR, 'npy', file_name)
return pickle.load(open(file_path, 'rb'))
@staticmethod
def convert_npy_to_txt(data_src): # tensorflow input text
images_npy = GeneDataset.load_npy_images(data_src, flatten_flag=True).astype(np.float32)
labels_npy = GeneDataset.load_npy_labels(data_src, expand_flag=True).astype(np.float32)
row_npy = np.hstack((images_npy, labels_npy))
# row_list = row_npy.tolist()
file_name = data_src + '_images_labels.txt'
file_path = os.path.join(config.DATA_DIR, 'text2', file_name)
with open(file_path, 'w', encoding='utf-8') as fw:
for idx in range(row_npy.shape[0]):
row = list(map(str, row_npy[idx]))
fw.writelines(' '.join(row))
fw.write('\n')
return
if __name__ == '__main__':
FLAGS, unparsed = parser.parse_known_args()
# genDater = GeneDataset(FLAGS.flatten_flag, FLAGS.expand_flag)
# genDater.download_save_data()
GeneDataset.convert_npy_to_txt('tr')
GeneDataset.convert_npy_to_txt('te')