{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Loading the cropped dogs seamlessly with Pytorch\n\nIn this competition, there are many pictures with multiple dogs, humans and other stuff that can disturb our GANs.\n\nWhat want are the DOGGOS.\n\nThis code was made for kernel using Pytorch."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport xml.etree.ElementTree as ET\n\nimport torch\nimport torchvision\n\n# for testing only\nfrom tqdm import tqdm_notebook as tqdm\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"See the structure of the annotation. It is a classic XML with the bbox at `annotation/object/bndbox****`."},{"metadata":{"trusted":true},"cell_type":"code","source":"!cat ../input/annotation/Annotation/n02085620-Chihuahua/n02085620_10074","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# This loader will use the underlying loader plus crop the image based on the annotation\ndef doggo_loader(path):\n    img = torchvision.datasets.folder.default_loader(path) # default loader\n    \n    # Get bounding box\n    annotation_basename = os.path.splitext(os.path.basename(path))[0]\n    annotation_dirname = next(dirname for dirname in os.listdir('../input/annotation/Annotation/') if dirname.startswith(annotation_basename.split('_')[0]))\n    annotation_filename = os.path.join('../input/annotation/Annotation', annotation_dirname, annotation_basename)\n    tree = ET.parse(annotation_filename)\n    root = tree.getroot()\n    objects = root.findall('object')\n    for o in objects:\n        bndbox = o.find('bndbox')\n        xmin = int(bndbox.find('xmin').text)\n        ymin = int(bndbox.find('ymin').text)\n        xmax = int(bndbox.find('xmax').text)\n        ymax = int(bndbox.find('ymax').text)\n    bbox = (xmin, ymin, xmax, ymax)\n    \n    # return cropped image\n    return img.crop(bbox)\n\n\n# The dataset (example)\ndataset = torchvision.datasets.ImageFolder(\n    '../input/all-dogs/',\n    loader=doggo_loader, # THE CUSTOM LOADER\n    transform=torchvision.transforms.Compose([\n        torchvision.transforms.Resize(64),\n        torchvision.transforms.CenterCrop(64),\n        torchvision.transforms.ToTensor(),\n    ]) # some transformations, add your data preprocessing here\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Check that it all loads without a bug\nfor i in tqdm(range(len(dataset))):\n    _ = dataset[i]\nprint('Ok.')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Check that we get only the CUTE DOGS OH YES WHOS THE GOOD DOGGO ITS YOU\nn = 10\n_, axes = plt.subplots(figsize=(4*n, 4*n), ncols=n, nrows=n)\nfor i, ax in enumerate(axes.flatten()):\n    ax.imshow(dataset[i][0].permute(1, 2, 0).detach().numpy())\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# benchmark this loader vs vanilla\n\ndataset_vanilla = torchvision.datasets.ImageFolder(\n    '../input/all-dogs/',\n    transform=torchvision.transforms.Compose([\n        torchvision.transforms.Resize(64),\n        torchvision.transforms.CenterCrop(64),\n        torchvision.transforms.ToTensor(),\n    ])\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%timeit\ndataset[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%timeit\ndataset_vanilla[0]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Hope it'll be usefull !\n\nDon't forget to +1 if you'll use it :)\n\nCheers,\nGuillaume"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}