|
| 1 | +from __future__ import print_function |
| 2 | +from PIL import Image |
| 3 | +from os.path import join |
| 4 | +import os |
| 5 | +import torch.utils.data as data |
| 6 | +from .utils import download_url, check_integrity, list_dir, list_files |
| 7 | + |
| 8 | + |
| 9 | +class Omniglot(data.Dataset): |
| 10 | + """`Omniglot <https://github.com/brendenlake/omniglot>`_ Dataset. |
| 11 | + Args: |
| 12 | + root (string): Root directory of dataset where directory |
| 13 | + ``omniglot-py`` exists. |
| 14 | + background (bool, optional): If True, creates dataset from the "background" set, otherwise |
| 15 | + creates from the "evaluation" set. This terminology is defined by the authors. |
| 16 | + transform (callable, optional): A function/transform that takes in an PIL image |
| 17 | + and returns a transformed version. E.g, ``transforms.RandomCrop`` |
| 18 | + target_transform (callable, optional): A function/transform that takes in the |
| 19 | + target and transforms it. |
| 20 | + download (bool, optional): If true, downloads the dataset zip files from the internet and |
| 21 | + puts it in root directory. If the zip files are already downloaded, they are not |
| 22 | + downloaded again. |
| 23 | + """ |
| 24 | + folder = 'omniglot-py' |
| 25 | + download_url_prefix = 'https://github.com/brendenlake/omniglot/raw/master/python' |
| 26 | + zips_md5 = { |
| 27 | + 'images_background': '68d2efa1b9178cc56df9314c21c6e718', |
| 28 | + 'images_evaluation': '6b91aef0f799c5bb55b94e3f2daec811' |
| 29 | + } |
| 30 | + |
| 31 | + def __init__(self, root, background=True, |
| 32 | + transform=None, target_transform=None, |
| 33 | + download=False): |
| 34 | + self.root = join(os.path.expanduser(root), self.folder) |
| 35 | + self.background = background |
| 36 | + self.transform = transform |
| 37 | + self.target_transform = target_transform |
| 38 | + |
| 39 | + if download: |
| 40 | + self.download() |
| 41 | + |
| 42 | + if not self._check_integrity(): |
| 43 | + raise RuntimeError('Dataset not found or corrupted.' + |
| 44 | + ' You can use download=True to download it') |
| 45 | + |
| 46 | + self.target_folder = join(self.root, self._get_target_folder()) |
| 47 | + self._alphabets = list_dir(self.target_folder) |
| 48 | + self._characters = sum([[join(a, c) for c in list_dir(join(self.target_folder, a))] |
| 49 | + for a in self._alphabets], []) |
| 50 | + self._character_images = [[(image, idx) for image in list_files(join(self.target_folder, character), '.png')] |
| 51 | + for idx, character in enumerate(self._characters)] |
| 52 | + self._flat_character_images = sum(self._character_images, []) |
| 53 | + |
| 54 | + def __len__(self): |
| 55 | + return len(self._flat_character_images) |
| 56 | + |
| 57 | + def __getitem__(self, index): |
| 58 | + """ |
| 59 | + Args: |
| 60 | + index (int): Index |
| 61 | +
|
| 62 | + Returns: |
| 63 | + tuple: (image, target) where target is index of the target character class. |
| 64 | + """ |
| 65 | + image_name, character_class = self._flat_character_images[index] |
| 66 | + image_path = join(self.target_folder, self._characters[character_class], image_name) |
| 67 | + image = Image.open(image_path, mode='r').convert('L') |
| 68 | + |
| 69 | + if self.transform: |
| 70 | + image = self.transform(image) |
| 71 | + |
| 72 | + if self.target_transform: |
| 73 | + character_class = self.target_transform(character_class) |
| 74 | + |
| 75 | + return image, character_class |
| 76 | + |
| 77 | + def _check_integrity(self): |
| 78 | + zip_filename = self._get_target_folder() |
| 79 | + if not check_integrity(join(self.root, zip_filename + '.zip'), self.zips_md5[zip_filename]): |
| 80 | + return False |
| 81 | + return True |
| 82 | + |
| 83 | + def download(self): |
| 84 | + import zipfile |
| 85 | + |
| 86 | + if self._check_integrity(): |
| 87 | + print('Files already downloaded and verified') |
| 88 | + return |
| 89 | + |
| 90 | + filename = self._get_target_folder() |
| 91 | + zip_filename = filename + '.zip' |
| 92 | + url = self.download_url_prefix + '/' + zip_filename |
| 93 | + download_url(url, self.root, zip_filename, self.zips_md5[filename]) |
| 94 | + print('Extracting downloaded file: ' + join(self.root, zip_filename)) |
| 95 | + with zipfile.ZipFile(join(self.root, zip_filename), 'r') as zip_file: |
| 96 | + zip_file.extractall(self.root) |
| 97 | + |
| 98 | + def _get_target_folder(self): |
| 99 | + return 'images_background' if self.background else 'images_evaluation' |
0 commit comments