()
| 44 | return list(raw_buf[8:].astype(np.int32).reshape(num_labels)) |
| 45 | |
| 46 | def main(): |
| 47 | parser = argparse.ArgumentParser(description="Extracts 10 PGM files from the MNIST dataset", formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 48 | parser.add_argument("-o", "--output", help="Path to the output directory.", default=os.getcwd()) |
| 49 | |
| 50 | args, _ = parser.parse_known_args() |
| 51 | |
| 52 | with urllib.request.urlopen("http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz") as res: |
| 53 | data = load_mnist_data(gzip.decompress(res.read())) |
| 54 | |
| 55 | with urllib.request.urlopen("http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz") as res: |
| 56 | labels = load_mnist_labels(gzip.decompress(res.read())) |
| 57 | |
| 58 | output_dir = args.output |
| 59 | |
| 60 | # Find one image for each digit. |
| 61 | for i in range(10): |
| 62 | index = labels.index(i) |
| 63 | image = Image.fromarray(data[index], mode="L") |
| 64 | path = os.path.join(output_dir, "{:}.pgm".format(i)) |
| 65 | image.save(path) |
| 66 | |
| 67 | if __name__ == '__main__': |
| 68 | main() |
no test coverage detected