{"metadata": {"language_info": {"codemirror_mode": {"version": 3, "name": "ipython"}, "nbconvert_exporter": "python", "file_extension": ".py", "version": "3.6.1", "name": "python", "pygments_lexer": "ipython3", "mimetype": "text/x-python"}, "kernelspec": {"language": "python", "name": "python3", "display_name": "Python 3"}}, "nbformat_minor": 1, "cells": [{"metadata": {"_uuid": "7b05592e9c8c363a82eb523d5f83195e733c5024", "collapsed": true, "_cell_guid": "0ed30743-a817-424d-b2f5-61fd81058b5d"}, "outputs": [], "cell_type": "code", "source": ["#Below are how I convert the images and their corresponding masks into tfrecords file.\n", "#This is inspired from Daniil's great blog on ML and CV. http://warmspringwinds.github.io/tensorflow/tf-slim/2016/12/21/tfrecords-guide/\n", "#Note that I am not able to write the converted tfrecords file here in Kaggle so the 2nd and 3rd blocks raised error here. \n", "#However, you can run them in your local machine. Any hints on how to write to Kaggle's data directory will be appreciated. "], "execution_count": null}, {"metadata": {"_uuid": "127ad4b7c19e87ff2fb180d1030dbbe4ea430a94", "_kg_hide-input": false, "collapsed": true, "_kg_hide-output": true, "_cell_guid": "519501a2-749b-4240-94e2-957f055d2cc9"}, "outputs": [], "cell_type": "code", "source": ["import tensorflow as tf\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import matplotlib.image as mpimg\n", "import os\n", "\n", "#construct list for image paths and their corresponding masks in the same order\n", "#image_path = \"../data/train/*.jpg\"\n", "image_path = []\n", "for filename in os.listdir(\"../input/train/\"):\n", "    image_path.append(\"../input/train/\"+filename)\n", "\n", "mask_path = []\n", "for i, filename in enumerate(image_path):\n", "    mask_path.append(filename[0:13]+\"_masks/\" + filename[14:-4] + \"_mask.gif\")"], "execution_count": null}, {"metadata": {"_uuid": "d14d3460a74e19b631dc8571bf21164cf4522444", "collapsed": true, "_cell_guid": "eeff0142-e9c9-40f9-a771-c87e1a757d99"}, "outputs": [], "cell_type": "code", "source": ["# path to where the tfrecords will be stored (change to your customized path).\n", "# I can't run this here in Kaggle because of permission. \n", "# Any comment on how to write temporary files in Kaggle will be appreciated.\n", "tfrecords_filename = '../input/train/training_data.tfrecords'\n", "#get a writer for the tfrecord file.\n", "writer = tf.python_io.TFRecordWriter(tfrecords_filename)\n", "#write data/masks into tfrecords\n", "for i in range(len(image_path)):\n", "    img = np.array(mpimg.imread(image_path[i]))\n", "    mask = np.array(mpimg.imread(mask_path[i]))[:,:,0]\n", "\n", "    height = img.shape[0]\n", "    width = img.shape[1]\n", "    img_raw = img.tostring()\n", "    mask_raw = mask.tostring()\n", "    #save the heights and widths as well so, which \n", "    #are needed when decoding from tfrecords back to images\n", "    example = tf.train.Example(features=tf.train.Features(feature={\n", "                                                          'height': _int64_feature(height),\n", "                                                          'width': _int64_feature(width),\n", "                                                          'image_raw': _bytes_feature(img_raw),\n", "                                                          'mask_raw': _bytes_feature(mask_raw)}\n", "                                                          ))\n", "    writer.write(example.SerializeToString())\n", "writer.close()"], "execution_count": null}, {"metadata": {"_uuid": "ac14b9fd2086f4c76f68f7a7d32d58e33ca9a625", "collapsed": true, "_cell_guid": "c576608e-3b8d-4ead-8bb6-a83c402c6bf1"}, "outputs": [], "cell_type": "code", "source": ["#run the following to verify the created tfrecord file.\n", "record_iterator = tf.python_io.tf_record_iterator(path=tfrecords_filename)\n", "for string_record in record_iterator:\n", "    example = tf.train.Example()\n", "    example.ParseFromString(string_record)\n", "    height = int(example.features.feature['height'].int64_list.value[0])\n", "    width = int(example.features.feature['width'].int64_list.value[0])\n", "    img_string = (example.features.feature['image_raw'].bytes_list.value[0])\n", "    mask_string = (example.features.feature['mask_raw'].bytes_list.value[0])\n", "    img_1d = np.fromstring(img_string, dtype=np.uint8)\n", "    mask_1d = np.fromstring(mask_string, dtype=np.uint8)\n", "    #reshape back to their original shape from a 1D array read from tfrecords\n", "    img = img_1d.reshape((height, width, -1))\n", "    mask = mask_1d.reshape((height, width))\n", "    plt.imshow(img)\n", "    plt.show()\n", "    plt.imshow(mask)\n", "    plt.show()"], "execution_count": null}], "nbformat": 4}