{"cells":[{"metadata":{},"cell_type":"markdown","source":"# TSNE Map of low resolution training data \n\nThis notebook reads all training images, resizes them to 10x10 thumbnails and creates a nice colorful embedding using tSNE.\n\n## used libs..."},{"metadata":{"_uuid":"a0ce3e6c-22ff-4274-ab9d-0f7466e44b21","_cell_guid":"1f38c2bd-6856-4eda-a2d7-1134b54a3bc4","trusted":true},"cell_type":"code","source":"import math, re, os\nimport tensorflow as tf\nimport numpy as np\nfrom kaggle_datasets import KaggleDatasets\nimport PIL\nfrom sklearn.manifold import TSNE\nfrom sklearn.decomposition import PCA","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Read training data \n\nThe training data is provided as multiple TFRecord files containing jpg images. We read them using a tf.data dataset pipeline and resize all of them to 10x10x3 values on the fly using dataset.map(). The 10x10x3 arrays produced by the pipeline are collected into a list and finally stacked forming a (num_pics, 10, 10, 3) tensor/array.\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"LABELED_TFREC_FORMAT = {\n    \"image\": tf.io.FixedLenFeature([], tf.string), # tf.string means bytestring\n    \"class\": tf.io.FixedLenFeature([], tf.int64),  # shape [] means single element\n}\n\ndef parseItem(item):\n    rec = tf.io.parse_single_example(item, LABELED_TFREC_FORMAT)\n    img = rec['image']\n    img = tf.image.decode_jpeg(img, channels=3)\n    img = tf.image.resize(img, (10,10), antialias=True)\n    img = tf.cast(img, tf.uint8)\n    return img\n\nfilename_pattern = KaggleDatasets().get_gcs_path() + '/tfrecords-jpeg-192x192/train/*.tfrec'\nfilenames = tf.io.gfile.glob(filename_pattern)\nds = tf.data.TFRecordDataset(filenames).map(parseItem)\nthumbnails = np.stack([img for img in iter(ds)])\n\nthumbnails.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Show a few of the very tiny images :-)"},{"metadata":{"trusted":true},"cell_type":"code","source":"mosaic = PIL.Image.new(mode='RGB', size=(800, 200))\ni = 0\nfor ix in range(80):\n    for iy in range(20):\n        t = PIL.Image.fromarray(thumbnails[i,:,:,:])\n        mosaic.paste(t, (ix*10, iy*10))\n        i = i + 1\n\nmosaic","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Creating a 2D embedding using tSNE\n\nBefore calling tSNE the data is transformed with PCA. This improves the output of tSNE on most datasets. After bumping up the perplexity parameter to 250 a nice embedding is retrieved."},{"metadata":{"trusted":true},"cell_type":"code","source":"(n, dimx, dimy, chan) = thumbnails.shape\ndata = thumbnails.reshape((n, dimx * dimy * chan))\ndata = PCA(n_components=32).fit_transform(data)\n\nembedding = TSNE(n_components=2, verbose=2, perplexity=250).fit_transform(data)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Draw embedding"},{"metadata":{"trusted":true},"cell_type":"code","source":"(n, w) = embedding.shape\n\nimg = PIL.Image.new(mode='RGB', size=(800, 800))    \nfor i in range(n):\n    img2 = PIL.Image.fromarray(thumbnails[i])\n    x = math.floor(embedding[i,0]*16 + 400)\n    y = math.floor(embedding[i,1]*16 + 400)\n    img.paste(img2, (x, y))\n\nimg","execution_count":null,"outputs":[]}],"metadata":{"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":4}