Skip to content

Instantly share code, notes, and snippets.

@dgrahn
Last active February 3, 2020 15:30
Show Gist options
  • Save dgrahn/e370bfec4e1b957bc378f42a965c912a to your computer and use it in GitHub Desktop.
Save dgrahn/e370bfec4e1b957bc378f42a965c912a to your computer and use it in GitHub Desktop.
Full MNIST Single Image.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Full MNIST Single Image.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyMMfOdJiWTOatfX9WlIbuV0",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/dgrahn/e370bfec4e1b957bc378f42a965c912a/full-mnist-single-image.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SdHLOb3njb95",
"colab_type": "text"
},
"source": [
"# MNIST Single Image\n",
"This code combines MNIST training data into a single image.\n",
"\n",
"First import appropriate libraries and download MNIST."
]
},
{
"cell_type": "code",
"metadata": {
"id": "ZfvbgHJJ-zAA",
"colab_type": "code",
"outputId": "eb5b7233-0802-455a-c47f-86af2fa7998c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
}
},
"source": [
"%tensorflow_version 2.x\n",
"import tensorflow as tf\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"\n",
"(train_images, train_labels), (_, _) = tf.keras.datasets.mnist.load_data()\n",
"print(train_images.shape, train_labels.shape)\n",
"\n",
"mnist = pd.DataFrame()#{ 'x': train_images, 'y': train_labels })\n",
"mnist['y'] = train_labels\n",
"mnist['x'] = train_images.tolist()\n",
"mnist.x = mnist.x.map(np.array)"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"TensorFlow 2.x selected.\n",
"(60000, 28, 28) (60000,)\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lkRdTOvVmLvm",
"colab_type": "text"
},
"source": [
"MNIST takes up about 59 columns and 1125 rows. This will be a tall image."
]
},
{
"cell_type": "code",
"metadata": {
"id": "BpbDccqrBAKC",
"colab_type": "code",
"colab": {}
},
"source": [
"from PIL import Image\n",
"\n",
"COLS = 59\n",
"ROWS = 1125\n",
"SIZE = 28\n",
"\n",
"full = Image.new('L', (COLS * SIZE, ROWS * SIZE), (0,))"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "UDpy4WJSn9Ds",
"colab_type": "text"
},
"source": [
"Now composite each digit into their own columns."
]
},
{
"cell_type": "code",
"metadata": {
"id": "gvo4uzJLE_uf",
"colab_type": "code",
"outputId": "5309e57e-8b11-464b-ed63-b4a6b6f00bfc",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 187
}
},
"source": [
"pixels = full.load()\n",
"col = -1\n",
"\n",
"for i in range(10):\n",
" digits = mnist[mnist.y == i].x\n",
" print('Compositing', i, '->', digits.shape[0])\n",
"\n",
" for j in range(digits.shape[0]):\n",
" row = j % ROWS\n",
" if row == 0: col += 1\n",
" top = row * SIZE\n",
" left = col * SIZE\n",
" bottom = top + SIZE\n",
" right = left + SIZE\n",
"\n",
" for i2, x in enumerate(range(top, bottom)):\n",
" for j2, y in enumerate(range(left, right)):\n",
" pixels[y, x] = int(digits.iloc[j][i2, j2]),"
],
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": [
"Compositing 0 -> 5923\n",
"Compositing 1 -> 6742\n",
"Compositing 2 -> 5958\n",
"Compositing 3 -> 6131\n",
"Compositing 4 -> 5842\n",
"Compositing 5 -> 5421\n",
"Compositing 6 -> 5918\n",
"Compositing 7 -> 6265\n",
"Compositing 8 -> 5851\n",
"Compositing 9 -> 5949\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4MW2lgtXowcS",
"colab_type": "text"
},
"source": [
"Finally, put it all in an image. You can download this using a right-click > save."
]
},
{
"cell_type": "code",
"metadata": {
"id": "s2bOoc3rLJMs",
"colab_type": "code",
"outputId": "55820f5d-94f8-4b05-ccf7-949bc49f5e97",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
}
},
"source": [
"dpi = SIZE * 4\n",
"w, h = full.size\n",
"\n",
"plt.figure(figsize=(w / dpi, h / dpi))\n",
"plt.axis('off')\n",
"plt.imshow(full, aspect='auto', cmap='gray', vmin=0, vmax=255)\n",
"plt.show()"
],
"execution_count": 6,
"outputs": [
{
"output_type": "display_data",
"data": {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment