{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport torch\nimport torchvision\nimport os\nfrom torch.utils.data import Dataset, DataLoader\nfrom PIL import Image\nimport math\n\n\n\nclass HotelDataset(Dataset):\n    #Have to define a path parameter\n    \n    def __init__(self, root_dir=\"../input/hotel-id-to-combat-human-trafficking-2022-fgvc9/\", data_path=\"train_images/\", min_images=1, max_images=None, max_entries=None, transform=None):\n        MAX_WINDOWS_FILENAME_CHAR_LENGTH = 260\n        self.root_dir = root_dir\n        self.data_path = data_path\n        self.transform = transform\n        self.full_data_path = os.path.join(root_dir, data_path)\n        \n        #We expect the following structure:\n        #full_data_path contains only folders, each folder representing a hotel, and having an unique name\n        #inside the hotel folders there are only image files\n        \n        dirs = os.listdir(self.full_data_path)\n        self.num_labels = len(dirs)\n        self.total_files = 0\n        for directory in dirs:\n            files=os.listdir(os.path.join(self.full_data_path,directory))\n            files_number = len(files)\n            if max_images != None and files_number > max_images:\n                dirs.remove(directory)\n                continue\n            if files_number < min_images:\n                dirs.remove(directory)\n                continue\n            self.total_files += files_number\n            if max_entries != None and max_entries >= self.total_files:\n                dirs = dirs[:max_entries]\n                break\n        \n        self.hotel_data = np.chararray([self.total_files,2], itemsize=MAX_WINDOWS_FILENAME_CHAR_LENGTH)\n        iterator = 0\n        for directory in dirs:\n            files=os.listdir(os.path.join(self.full_data_path,directory))\n            for f in files:\n                self.hotel_data[iterator] = [f, directory]\n            \n    def __getitem__(self, index):\n        label = self.hotel_data[index,1].decode()\n        hotel_image_id = self.hotel_data[index,0].decode()\n        image_path = os.path.join(self.full_data_path, label, hotel_image_id)\n        \n        image = Image.open(image_path)\n        item = image\n        if self.transform != None:\n            item = self.transform(image)\n        return item, label\n        \n    def __len__(self):\n        return self.total_files\n#test = np.chararray([50,2], itemsize=MAX_WINDOWS_FILENAME_CHAR_LENGTH)\n#test[:] = \"testSomeStringLol\"\n#print(test)\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n#import os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Testing the dataset below","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport torch\nimport torchvision\nfrom torch.utils.data import Dataset, DataLoader\nfrom PIL import Image\nimport math\n\ndataset = HotelDataset(max_entries=500)\n\nprint(dataset[0])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Testing transforms. Transforms can be composed, also some of them do have functions such as random rotation.","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport torch\nimport torchvision\nfrom torch.utils.data import Dataset, DataLoader\nfrom PIL import Image\nimport math\n\nIMAGE_SIZE_WIDTH = 250\nIMAGE_SIZE_HEIGHT = 250\n\ntrain_transforms = torchvision.transforms.Compose([\n    torchvision.transforms.Resize((IMAGE_SIZE_WIDTH, IMAGE_SIZE_HEIGHT)),\n    torchvision.transforms.RandomHorizontalFlip()\n])\n\ndataset = HotelDataset(max_entries=500, transform=train_transforms)\n\nprint(dataset[0])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}