|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +# |
| 19 | + |
| 20 | +import numpy as np |
| 21 | +import os |
| 22 | +import sys |
| 23 | +import gzip |
| 24 | +import codecs |
| 25 | + |
| 26 | + |
| 27 | +def check_dataset_exist(dirpath): |
| 28 | + if not os.path.exists(dirpath): |
| 29 | + print( |
| 30 | + 'The MNIST dataset does not exist. Please download the mnist dataset using python data/download_mnist.py' |
| 31 | + ) |
| 32 | + sys.exit(0) |
| 33 | + return dirpath |
| 34 | + |
| 35 | + |
| 36 | +def load_dataset(dir_path): |
| 37 | + dir_path = check_dataset_exist(dirpath=dir_path) |
| 38 | + train_x_path = os.path.join(dir_path, 'train-images-idx3-ubyte.gz') # need to change to local disk |
| 39 | + train_y_path = os.path.join(dir_path, 'train-labels-idx1-ubyte.gz') # need to change to local disk |
| 40 | + valid_x_path = os.path.join(dir_path, 't10k-images-idx3-ubyte.gz') # need to change to local disk |
| 41 | + valid_y_path = os.path.join(dir_path, 't10k-labels-idx1-ubyte.gz') # need to change to local disk |
| 42 | + |
| 43 | + train_x = read_image_file(check_dataset_exist(train_x_path)).astype( |
| 44 | + np.float32) |
| 45 | + train_y = read_label_file(check_dataset_exist(train_y_path)).astype( |
| 46 | + np.float32) |
| 47 | + valid_x = read_image_file(check_dataset_exist(valid_x_path)).astype( |
| 48 | + np.float32) |
| 49 | + valid_y = read_label_file(check_dataset_exist(valid_y_path)).astype( |
| 50 | + np.float32) |
| 51 | + return train_x, train_y, valid_x, valid_y |
| 52 | + |
| 53 | + |
| 54 | +def read_label_file(path): |
| 55 | + with gzip.open(path, 'rb') as f: |
| 56 | + data = f.read() |
| 57 | + assert get_int(data[:4]) == 2049 |
| 58 | + length = get_int(data[4:8]) |
| 59 | + parsed = np.frombuffer(data, dtype=np.uint8, offset=8).reshape((length)) |
| 60 | + return parsed |
| 61 | + |
| 62 | + |
| 63 | +def get_int(b): |
| 64 | + return int(codecs.encode(b, 'hex'), 16) |
| 65 | + |
| 66 | + |
| 67 | +def read_image_file(path): |
| 68 | + with gzip.open(path, 'rb') as f: |
| 69 | + data = f.read() |
| 70 | + assert get_int(data[:4]) == 2051 |
| 71 | + length = get_int(data[4:8]) |
| 72 | + num_rows = get_int(data[8:12]) |
| 73 | + num_cols = get_int(data[12:16]) |
| 74 | + parsed = np.frombuffer(data, dtype=np.uint8, offset=16).reshape( |
| 75 | + (length, 1, num_rows, num_cols)) |
| 76 | + return parsed |
| 77 | + |
| 78 | + |
| 79 | +def normalize(train_x, val_x): |
| 80 | + train_x /= 255 |
| 81 | + val_x /= 255 |
| 82 | + return train_x, val_x |
| 83 | + |
| 84 | + |
| 85 | +def load(dir_path='/tmp/mnist'): |
| 86 | + train_x, train_y, val_x, val_y = load_dataset(dir_path) |
| 87 | + train_x, val_x = normalize(train_x, val_x) |
| 88 | + train_x = train_x.astype(np.float32) |
| 89 | + val_x = val_x.astype(np.float32) |
| 90 | + train_y = train_y.astype(np.int32) |
| 91 | + val_y = val_y.astype(np.int32) |
| 92 | + return train_x, train_y, val_x, val_y |
0 commit comments