{"cells":[{"metadata":{"trusted":true,"_uuid":"21b7b7df726c096417e12e5275f4c303e7e3659d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"!ls","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a3550c22477f74448d5a40f04b870fa3ce52ada2"},"cell_type":"markdown","source":"peeking into the input directory"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"!ls ../input","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e93405246703f0fa4df2c1ba3b43575482d36c82"},"cell_type":"markdown","source":"peeking into the train directory"},{"metadata":{"trusted":true,"_uuid":"1fd33f5d95bd7461021f6674f09b2e9bbd84fe63"},"cell_type":"code","source":"!ls ../input/train/ | head -5","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3eb2a58c760066d8ab31ceba362deb8c980fa45a"},"cell_type":"markdown","source":"looking into the first 2 rows of `train.csv`"},{"metadata":{"trusted":true,"_uuid":"7b4f0212689ab94e3031c434efb4a0b90269a0e4"},"cell_type":"code","source":"!head -n 2 ../input/train.csv","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"70575ebef61bf1eda078ed1b00d91344d8fe794e"},"cell_type":"markdown","source":"## here we are following the PyTorch tutorial to import datasets with built in functions\nDataset class\n-------------\n\n``torch.utils.data.Dataset`` is an abstract class representing a\ndataset.\nYour custom dataset should inherit ``Dataset`` and override the following\nmethods:\n\n-  ``__len__`` so that ``len(dataset)`` returns the size of the dataset.\n-  ``__getitem__`` to support the indexing such that ``dataset[i]`` can\n   be used to get $i$\\ th sample\n\nLet's create a dataset class for our whale identificatin dataset. We will\nread the csv in ``__init__`` but leave the reading of images to\n``__getitem__``. This is memory efficient because all the images are not\nstored in the memory at once but read as required.\n\nSample of our dataset will be a dict\n``{'image': image, 'id': ids}``. Our datset will take an\noptional argument ``transform`` so that any required processing can be\napplied on the sample. We will see the usefulness of ``transform`` in the\nnext section.\n\n\n\n"},{"metadata":{"trusted":true,"_uuid":"005bdebe62a6c80dea23de8b55c5c213eb2a049e"},"cell_type":"code","source":"from __future__ import print_function, division\nimport os\nimport torch\nimport pandas as pd\nfrom skimage import io, transform\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms, utils\n\n# Ignore warnings\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"575d57e96b73df2a62cdc7bfea1f7ba4bf173cdb"},"cell_type":"markdown","source":"Visualizing the image"},{"metadata":{"trusted":true,"_uuid":"a22cbd0ddc6b9d0466775c573e4389ff27ca8ad3"},"cell_type":"code","source":"train_data = pd.read_csv(\"../input/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"79696e3a0a10992b4bc559b7ed02540df3eed55e"},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2e2b35a09116cc2396d645a6ab4e8e1923b01297"},"cell_type":"code","source":"image_name = train_data.iloc[0,0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"15cd7e8a35bbdb7aec8eabcafbeb18e772081c15"},"cell_type":"code","source":"plt.title(str(train_data.iloc[0,1]))\nplt.imshow(io.imread(os.path.join(\"../input/train/\",image_name)))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"66d210beee8088cd3d9a1339d69c450ad49ebb43"},"cell_type":"code","source":"#type of the variable that stores id\ntype(train_data.iloc[0,1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"904a8bd36099fe8771862c7614e824195c91aca0"},"cell_type":"code","source":"class WhaleIdDataset(Dataset):\n    \"\"\"the whale identification dataset\"\"\"\n    def __init__(self, csv, rootDir, transform=None ):\n        \"\"\"\n        Args:\n            csv (string): path to the file with Id\n            rootDir (string): path to the root directory\n            transform (callable,optional): optional \n                transforms to be applied on the samples.\n        \"\"\"\n        self.data_frame = pd.read_csv(csv)\n        self.root_dir = rootDir\n        self.transform = transform\n        \n    def __len__(self):\n        return len(self.data_frame)\n    \n    def __getitem__(self,idx):\n        image_name = self.data_frame.iloc[idx,0]\n        image_path = os.path.join(self.root_dir,\n                                  image_name)\n        image = io.imread(image_path)\n        Id = self.data_frame.iloc[idx,1]\n        sample = {\"image\": image,\"id\": Id}\n        \n        if self.transform:\n            sample = self.transform(sample)\n        \n        return sample \n        ","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c2b0b81f5ced7d1549cc168bf4f0f5fb25ca15a3"},"cell_type":"markdown","source":"loaded the dataset (initiated the class)"},{"metadata":{"trusted":true,"_uuid":"dd6653d261878e2aa24b1401486c3ff27c568420"},"cell_type":"code","source":"whaleDataset = WhaleIdDataset(\"../input/train.csv\",\n                              \"../input/train/\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a9e20f5471d4823f94b634019fa4bb12fdb5fd94"},"cell_type":"markdown","source":"accessing an element"},{"metadata":{"trusted":true,"_uuid":"d8618779ad06d5881472ee50454f889aed1381fe"},"cell_type":"code","source":"whaleDataset[1][\"image\"].shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9ea34b90a3c610234a97a4e68620e61720f31278"},"cell_type":"markdown","source":"iterating over some values in the dataset"},{"metadata":{"trusted":true,"_uuid":"1cceb43568d7ea911a4335183fa423a01c9dfd43"},"cell_type":"code","source":"def show_image(image, id):\n    plt.title(id)\n    plt.imshow(image)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ee5543b6401d97eaf68d5b21e74817fe100c8f45"},"cell_type":"code","source":"fig = plt.figure()\nfor i in range(3):\n    a = fig.add_subplot(1, 3, i+1)\n    a.axis(\"off\")\n    a.set_title(whaleDataset[i][\"id\"])\n    plt.imshow(whaleDataset[i][\"image\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d5ae13ddc029709aa76946f9f2a3a462e8feb22"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"edccb9174b3214a750477b257ec584a76e6ee13d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"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"}},"nbformat":4,"nbformat_minor":1}