|
| 1 | +""" |
| 2 | + Import the USPS Handwritten Digits Dataset |
| 3 | + Source: https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/ |
| 4 | + multiclass.html#usps |
| 5 | + (and: https://ieeexplore.ieee.org/document/291440) |
| 6 | + Description: Handwritten text recognition image database. |
| 7 | +
|
| 8 | + ~~~ Important note ~~~ |
| 9 | + Please cite the following paper when using or referencing the dataset: |
| 10 | + Hull, J. J. (1994). A database for handwritten text recognition |
| 11 | + research. IEEE Transactions on pattern analysis and machine |
| 12 | + intelligence, 16(5), 550-554. |
| 13 | +""" |
| 14 | + |
| 15 | +from tensorflow.keras.utils import get_file |
| 16 | +import logging |
| 17 | +from sklearn.datasets import load_svmlight_file |
| 18 | +import bz2 |
| 19 | + |
| 20 | + |
| 21 | +def warn_citation(): |
| 22 | + """Warns about citation requirements |
| 23 | + # Returns |
| 24 | + Void |
| 25 | + """ |
| 26 | + logging.warning(("Please cite the following paper when using or" |
| 27 | + " referencing this Extra Keras Dataset:")) |
| 28 | + logging.warning( |
| 29 | + ("Hull, J. J. (1994). A database for handwritten text " |
| 30 | + "recognition research. IEEE Transactions on pattern analysis and " |
| 31 | + "machine intelligence, 16(5), 550-554.") |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def decompress(path): |
| 36 | + """Decompresses BZ2 data into another file""" |
| 37 | + bz_zip = bz2.BZ2File(path) |
| 38 | + decompressed_data = bz_zip.read() |
| 39 | + new_path = path[:-4] |
| 40 | + open(new_path, 'wb').write(decompressed_data) |
| 41 | + return new_path |
| 42 | + |
| 43 | + |
| 44 | +def load_to_numpy(path): |
| 45 | + """Loads LIBSVM data into NumPY format""" |
| 46 | + data = load_svmlight_file(path) |
| 47 | + return (data[0].toarray(), data[1]) |
| 48 | + |
| 49 | + |
| 50 | +def load_data( |
| 51 | + path="usps.bz2", |
| 52 | + path_testing="usps-testing.bz2" |
| 53 | +): |
| 54 | + """Loads the USPS Handwritten Digits Dataset. |
| 55 | + # Arguments |
| 56 | + path: path where to cache the USPS data locally |
| 57 | + (relative to ~/.keras/datasets). |
| 58 | + path_testing: path where to cache the USPS testing data locally |
| 59 | + (relative to ~/.keras/datasets). |
| 60 | + # Returns |
| 61 | + Tuple of Numpy arrays: `(input_train, target_train), |
| 62 | + (input_test, target_test)`. |
| 63 | + Input structure: 16x16 image with a digit |
| 64 | + Target structure: number in the 0.0 - 9.0 range |
| 65 | +
|
| 66 | + """ |
| 67 | + # Log about loading |
| 68 | + logging.basicConfig(level=logging.INFO) |
| 69 | + logging.info('Loading dataset = usps') |
| 70 | + |
| 71 | + # Download data |
| 72 | + path = get_file( |
| 73 | + path, |
| 74 | + origin=("https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/" |
| 75 | + "datasets/multiclass/usps.bz2") |
| 76 | + ) |
| 77 | + path_testing = get_file( |
| 78 | + path_testing, |
| 79 | + origin=("https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/" |
| 80 | + "datasets/multiclass/usps.t.bz2") |
| 81 | + ) |
| 82 | + |
| 83 | + # Decompress data |
| 84 | + decompress_train = decompress(path) |
| 85 | + decompress_test = decompress(path_testing) |
| 86 | + |
| 87 | + # Load LIBSVM data into NumPy array |
| 88 | + (input_train, target_train) = load_to_numpy(decompress_train) |
| 89 | + (input_test, target_test) = load_to_numpy(decompress_test) |
| 90 | + |
| 91 | + # Reshape data |
| 92 | + input_train = input_train.reshape(input_train.shape[0], 16, 16) |
| 93 | + input_test = input_test.reshape(input_test.shape[0], 16, 16) |
| 94 | + |
| 95 | + # Correct targets (e.g. number 3 is now returned as 4.0) |
| 96 | + target_train = target_train - 1 |
| 97 | + target_test = target_test - 1 |
| 98 | + |
| 99 | + # Warn about citation |
| 100 | + warn_citation() |
| 101 | + |
| 102 | + # Return data |
| 103 | + return (input_train, target_train), (input_test, target_test) |
0 commit comments