{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Reading package lists... Done\n", "Building dependency tree \n", "Reading state information... Done\n", "wget is already the newest version (1.19.4-1ubuntu2.1).\n", "0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.\n", "Files already extracted\n" ] } ], "source": [ "!bash download.sh" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/root/miniconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88\n", " return f(*args, **kwds)\n", "/root/miniconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88\n", " return f(*args, **kwds)\n", "/root/miniconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88\n", " return f(*args, **kwds)\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import os\n", "import scipy.misc\n", "import tensorflow as tf\n", "from tensorflow.contrib.tensorboard.plugins import projector\n", "\n", "from collections import Counter\n", "\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
LabelDescription
00T-shirt/top
11Trouser
22Pullover
33Dress
44Coat
55Sandal
66Shirt
77Sneaker
88Bag
99Ankle boot
\n", "
" ], "text/plain": [ " Label Description\n", "0 0 T-shirt/top\n", "1 1 Trouser\n", "2 2 Pullover\n", "3 3 Dress\n", "4 4 Coat\n", "5 5 Sandal\n", "6 6 Shirt\n", "7 7 Sneaker\n", "8 8 Bag\n", "9 9 Ankle boot" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "labels = pd.read_csv('labels.tsv', sep='\\t')\n", "label_dict = dict(zip(labels.Label, labels.Description))\n", "labels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Following code to extract the data was adapted from [here](https://github.com/zalandoresearch/fashion-mnist/blob/master/utils/mnist_reader.py) (the official repository for Fashion Mnist)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(9, 6000),\n", " (0, 6000),\n", " (3, 6000),\n", " (2, 6000),\n", " (7, 6000),\n", " (5, 6000),\n", " (1, 6000),\n", " (6, 6000),\n", " (4, 6000),\n", " (8, 6000)]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open('./data/train-images-idx3-ubyte', 'rb') as imgpath:\n", " images = np.frombuffer(imgpath.read(), dtype=np.uint8,\n", " offset=16).reshape((-1,28,28))\n", " \n", "with open('./data/train-labels-idx1-ubyte', 'rb') as imgpath:\n", " im_labels = np.frombuffer(imgpath.read(), dtype=np.uint8,\n", " offset=8)\n", " \n", "Counter(im_labels).most_common()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "!mkdir -p ./logs/\n", "!mkdir -p ./logs/1\n", "PATH = os.getcwd()\n", "LOG_DIR = PATH+'/logs/1/'" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "with open(os.path.join(LOG_DIR, 'metadata.tsv'), 'w') as f:\n", " f.write('Class\\tName\\n')\n", " for num, name in zip(im_labels, [label_dict[l] for l in im_labels]):\n", " f.write('{}\\t{}\\n'.format(num,name))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/root/miniconda3/lib/python3.6/site-packages/ipykernel_launcher.py:29: DeprecationWarning: `imsave` is deprecated!\n", "`imsave` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.\n", "Use ``imageio.imwrite`` instead.\n" ] } ], "source": [ "def images_to_sprite(data):\n", " \"\"\"Creates the sprite image along with any necessary padding\n", " Args:\n", " data: NxHxW[x3] tensor containing the images.\n", " Returns:\n", " data: Properly shaped HxWx3 image with any necessary padding.\n", " \"\"\"\n", " if len(data.shape) == 3:\n", " data = np.tile(data[...,np.newaxis], (1,1,1,3))\n", " data = data.astype(np.float32)\n", " min = np.min(data.reshape((data.shape[0], -1)), axis=1)\n", " data = (data.transpose(1,2,3,0) - min).transpose(3,0,1,2)\n", " max = np.max(data.reshape((data.shape[0], -1)), axis=1)\n", " data = (data.transpose(1,2,3,0) / max).transpose(3,0,1,2)\n", "\n", " n = int(np.ceil(np.sqrt(data.shape[0])))\n", " padding = ((0, n ** 2 - data.shape[0]), (0, 0),\n", " (0, 0)) + ((0, 0),) * (data.ndim - 3)\n", " data = np.pad(data, padding, mode='constant',\n", " constant_values=0)\n", " # Tile the individual thumbnails into an image.\n", " data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3)\n", " + tuple(range(4, data.ndim + 1)))\n", " data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])\n", " data = (data * 255).astype(np.uint8)\n", " return data\n", "\n", "sprite = images_to_sprite(images)\n", "scipy.misc.imsave(os.path.join(LOG_DIR, 'sprite.png'), sprite)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The sprite image is a square image that you write all your original images into. The remaining images are put in as blank (black)." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(6860, 6860, 3)\n", "6858.571279792899\n" ] } ], "source": [ "print(sprite.shape)\n", "print(np.sqrt(60000)*28)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "features = tf.Variable(images.reshape((len(images), -1)), name='features')\n", "\n", "with tf.Session() as sess:\n", " saver = tf.train.Saver([features])\n", "\n", " sess.run(features.initializer)\n", " saver.save(sess, os.path.join(LOG_DIR, 'images_4_classes.ckpt'))\n", " \n", " config = projector.ProjectorConfig()\n", " # One can add multiple embeddings.\n", " embedding = config.embeddings.add()\n", " embedding.tensor_name = features.name\n", " # Link this tensor to its metadata file (e.g. labels).\n", " embedding.metadata_path = os.path.join(LOG_DIR, 'metadata.tsv')\n", " # Comment out if you don't want sprites\n", " embedding.sprite.image_path = os.path.join(LOG_DIR, 'sprite.png')\n", " embedding.sprite.single_image_dim.extend([images.shape[1], images.shape[1]])\n", " # Saves a config file that TensorBoard will read during startup.\n", " projector.visualize_embeddings(tf.summary.FileWriter(LOG_DIR), config)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/root/miniconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88\n", " return f(*args, **kwds)\n", "/root/miniconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88\n", " return f(*args, **kwds)\n", "/root/miniconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88\n", " return f(*args, **kwds)\n", "TensorBoard 1.10.0 at http://a666cd6efd0d:6006 (Press CTRL+C to quit)\n" ] } ], "source": [ "!tensorboard --logdir=./logs/1/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[click here](http://localhost:6006/) to open tensorboard." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }