def parse_images(self): if self.args.training or self.args.validation: self.fetch_mnist_data() if self.args.training: self.sets['training'] = {'images': [], 'correct_answers': []} x_training = self.mnist_ds['training']['default'][:] y_training = self.mnist_ds['training']['targets'][:] for x in x_training[0]: self.sets['training']['images'].append(x) for y in y_training[0]: self.sets['training']['correct_answers'].append(y[0]) if self.args.validation: self.sets['validation'] = {'images': [], 'correct_answers': []} x_validation = self.mnist_ds['validation']['default'][:] y_validation = self.mnist_ds['validation']['targets'][:] for x in x_validation[0]: self.sets['validation']['images'].append(x) for y in y_validation[0]: self.sets['validation']['correct_answers'].append(y[0]) if self.args.demo: """ import pickle demo_set_filename = 'demo_python2' demo_set = pickle.load(open(demo_set_filename, "rb")) """ from demo import load_flat_text_cases demo_inputs, demo_targets = load_flat_text_cases( 'demo100_text.txt') for i in range(len(demo_inputs)): demo_inputs[i] = map(lambda value: value / 255.0, demo_inputs[i]) adapted_image_array = [] for row_idx in range(28): adapted_image_array.append([]) for col_idx in range(28): adapted_image_array[row_idx].append( demo_inputs[i][row_idx * 28 + col_idx]) demo_inputs[i] = adapted_image_array self.sets['demo'] = { 'images': demo_inputs, 'correct_answers': demo_targets } if self.args.input: self.sets['input'] = {'images': []} if self.args.input.endswith('.png'): image_array = img_helper.read_image(self.args.input) self.sets['input']['images'].append(image_array) elif self.args.input: f = open(self.args.input) lines = [] for line in f: lines.append(line.strip()) f.close() if 'png' in lines[0]: for file_name in lines: if file_name.endswith('.png'): image_array = img_helper.read_image(file_name) self.sets['input']['images'].append(image_array) else: print('Failed to parse custom input')
import os from demo import load_flat_text_cases import numpy as np """ This python file reads the flat mnist txt files, preprocesses it and finally saves it in a format that the training module can read """ data_dir = '.' mnist_training_file_path = os.path.join(data_dir, 'all_flat_mnist_training_cases_text.txt') mnist_validation_file_path = os.path.join(data_dir, 'all_flat_mnist_testing_cases_text.txt') hdf_file = os.path.join(data_dir, 'MNIST.hdf5') print("Extracting and preprocessing MNIST data...") training_inputs, training_targets = load_flat_text_cases(mnist_training_file_path) for i in range(len(training_inputs)): training_inputs[i] = map(lambda value: value / 255.0, training_inputs[i]) # normalize training_inputs = np.array(training_inputs).reshape((1, 60000, 28, 28, 1)) training_targets = np.array(training_targets).reshape((1, 60000, 1)) validation_inputs, validation_targets = load_flat_text_cases(mnist_validation_file_path) for i in range(len(validation_inputs)): validation_inputs[i] = map(lambda value: value / 255.0, validation_inputs[i]) # normalize validation_inputs = np.array(validation_inputs).reshape((1, 10000, 28, 28, 1)) validation_targets = np.array(validation_targets).reshape((1, 10000, 1)) print("Creating HDF5 dataset...") f = h5py.File(hdf_file, 'w')
def parse_images(self): if self.args.training or self.args.validation: self.fetch_mnist_data() if self.args.training: self.sets["training"] = {"images": [], "correct_answers": []} x_training = self.mnist_ds["training"]["default"][:] y_training = self.mnist_ds["training"]["targets"][:] for x in x_training[0]: self.sets["training"]["images"].append(x) for y in y_training[0]: self.sets["training"]["correct_answers"].append(y[0]) if self.args.validation: self.sets["validation"] = {"images": [], "correct_answers": []} x_validation = self.mnist_ds["validation"]["default"][:] y_validation = self.mnist_ds["validation"]["targets"][:] for x in x_validation[0]: self.sets["validation"]["images"].append(x) for y in y_validation[0]: self.sets["validation"]["correct_answers"].append(y[0]) if self.args.demo: """ import pickle demo_set_filename = 'demo_python2' demo_set = pickle.load(open(demo_set_filename, "rb")) """ from demo import load_flat_text_cases demo_inputs, demo_targets = load_flat_text_cases("demo100_text.txt") for i in range(len(demo_inputs)): demo_inputs[i] = map(lambda value: value / 255.0, demo_inputs[i]) adapted_image_array = [] for row_idx in range(28): adapted_image_array.append([]) for col_idx in range(28): adapted_image_array[row_idx].append(demo_inputs[i][row_idx * 28 + col_idx]) demo_inputs[i] = adapted_image_array self.sets["demo"] = {"images": demo_inputs, "correct_answers": demo_targets} if self.args.input: self.sets["input"] = {"images": []} if self.args.input.endswith(".png"): image_array = img_helper.read_image(self.args.input) self.sets["input"]["images"].append(image_array) elif self.args.input: f = open(self.args.input) lines = [] for line in f: lines.append(line.strip()) f.close() if "png" in lines[0]: for file_name in lines: if file_name.endswith(".png"): image_array = img_helper.read_image(file_name) self.sets["input"]["images"].append(image_array) else: print("Failed to parse custom input")