We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Invalid hash error on ImageNet dataset
In my environment, md5 value of `meta.bin` used in ImageNet dataset is different from the value defined in `imagenet.py`.

`meta.bin` is generated by `torch.save` in the code. I found python2 and3 generate different files.

md5sum hashes are as follows.

- (defined) `7e0d3cf156177e4fc47011cdd30ce706`
- (Python 2.7.16, Ubuntu) `a36fd93cf3900286d99e24ad0a73ce04`
- (Python 3.7.3, Ubuntu) `ca981e8aac175178e80e7949d90ee85c`

https://github.com/pytorch/vision/blob/9a481d0bec2700763a799ff148fe2e083b575441/torchvision/datasets/imagenet.py#L23-L26

https://github.com/pytorch/vision/blob/9a481d0bec2700763a799ff148fe2e083b575441/torchvision/datasets/imagenet.py#L117-L118

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `torchvision/datasets/imagenet.py`
Content:
```
1 from __future__ import print_function
2 import os
3 import shutil
4 import torch
5 from .folder import ImageFolder
6 from .utils import check_integrity, download_url
7 
8 ARCHIVE_DICT = {
9     'train': {
10         'url': 'http://www.image-net.org/challenges/LSVRC/2012/nnoupb/ILSVRC2012_img_train.tar',
11         'md5': '1d675b47d978889d74fa0da5fadfb00e',
12     },
13     'val': {
14         'url': 'http://www.image-net.org/challenges/LSVRC/2012/nnoupb/ILSVRC2012_img_val.tar',
15         'md5': '29b22e2961454d5413ddabcf34fc5622',
16     },
17     'devkit': {
18         'url': 'http://www.image-net.org/challenges/LSVRC/2012/nnoupb/ILSVRC2012_devkit_t12.tar.gz',
19         'md5': 'fa75699e90414af021442c21a62c3abf',
20     }
21 }
22 
23 META_DICT = {
24     'filename': 'meta.bin',
25     'md5': '7e0d3cf156177e4fc47011cdd30ce706',
26 }
27 
28 
29 class ImageNet(ImageFolder):
30     """`ImageNet <http://image-net.org/>`_ 2012 Classification Dataset.
31 
32     Args:
33         root (string): Root directory of the ImageNet Dataset.
34         split (string, optional): The dataset split, supports ``train``, or ``val``.
35         download (bool, optional): If true, downloads the dataset from the internet and
36             puts it in root directory. If dataset is already downloaded, it is not
37             downloaded again.
38         transform (callable, optional): A function/transform that  takes in an PIL image
39             and returns a transformed version. E.g, ``transforms.RandomCrop``
40         target_transform (callable, optional): A function/transform that takes in the
41             target and transforms it.
42         loader (callable, optional): A function to load an image given its path.
43 
44      Attributes:
45         classes (list): List of the class names.
46         class_to_idx (dict): Dict with items (class_name, class_index).
47         wnids (list): List of the WordNet IDs.
48         wnid_to_idx (dict): Dict with items (wordnet_id, class_index).
49         imgs (list): List of (image path, class_index) tuples
50         targets (list): The class_index value for each image in the dataset
51     """
52 
53     def __init__(self, root, split='train', download=False, **kwargs):
54         root = self.root = os.path.expanduser(root)
55         self.split = self._verify_split(split)
56 
57         if download:
58             self.download()
59         wnid_to_classes = self._load_meta_file()[0]
60 
61         super(ImageNet, self).__init__(self.split_folder, **kwargs)
62         self.root = root
63 
64         idcs = [idx for _, idx in self.imgs]
65         self.wnids = self.classes
66         self.wnid_to_idx = {wnid: idx for idx, wnid in zip(idcs, self.wnids)}
67         self.classes = [wnid_to_classes[wnid] for wnid in self.wnids]
68         self.class_to_idx = {cls: idx
69                              for clss, idx in zip(self.classes, idcs)
70                              for cls in clss}
71 
72     def download(self):
73         if not self._check_meta_file_integrity():
74             tmpdir = os.path.join(self.root, 'tmp')
75 
76             archive_dict = ARCHIVE_DICT['devkit']
77             download_and_extract_tar(archive_dict['url'], self.root,
78                                      extract_root=tmpdir,
79                                      md5=archive_dict['md5'])
80             devkit_folder = _splitexts(os.path.basename(archive_dict['url']))[0]
81             meta = parse_devkit(os.path.join(tmpdir, devkit_folder))
82             self._save_meta_file(*meta)
83 
84             shutil.rmtree(tmpdir)
85 
86         if not os.path.isdir(self.split_folder):
87             archive_dict = ARCHIVE_DICT[self.split]
88             download_and_extract_tar(archive_dict['url'], self.root,
89                                      extract_root=self.split_folder,
90                                      md5=archive_dict['md5'])
91 
92             if self.split == 'train':
93                 prepare_train_folder(self.split_folder)
94             elif self.split == 'val':
95                 val_wnids = self._load_meta_file()[1]
96                 prepare_val_folder(self.split_folder, val_wnids)
97         else:
98             msg = ("You set download=True, but a folder '{}' already exist in "
99                    "the root directory. If you want to re-download or re-extract the "
100                    "archive, delete the folder.")
101             print(msg.format(self.split))
102 
103     @property
104     def meta_file(self):
105         return os.path.join(self.root, META_DICT['filename'])
106 
107     def _check_meta_file_integrity(self):
108         return check_integrity(self.meta_file, META_DICT['md5'])
109 
110     def _load_meta_file(self):
111         if self._check_meta_file_integrity():
112             return torch.load(self.meta_file)
113         else:
114             raise RuntimeError("Meta file not found or corrupted.",
115                                "You can use download=True to create it.")
116 
117     def _save_meta_file(self, wnid_to_class, val_wnids):
118         torch.save((wnid_to_class, val_wnids), self.meta_file)
119 
120     def _verify_split(self, split):
121         if split not in self.valid_splits:
122             msg = "Unknown split {} .".format(split)
123             msg += "Valid splits are {{}}.".format(", ".join(self.valid_splits))
124             raise ValueError(msg)
125         return split
126 
127     @property
128     def valid_splits(self):
129         return 'train', 'val'
130 
131     @property
132     def split_folder(self):
133         return os.path.join(self.root, self.split)
134 
135     def extra_repr(self):
136         return "Split: {split}".format(**self.__dict__)
137 
138 
139 def extract_tar(src, dest=None, gzip=None, delete=False):
140     import tarfile
141 
142     if dest is None:
143         dest = os.path.dirname(src)
144     if gzip is None:
145         gzip = src.lower().endswith('.gz')
146 
147     mode = 'r:gz' if gzip else 'r'
148     with tarfile.open(src, mode) as tarfh:
149         tarfh.extractall(path=dest)
150 
151     if delete:
152         os.remove(src)
153 
154 
155 def download_and_extract_tar(url, download_root, extract_root=None, filename=None,
156                              md5=None, **kwargs):
157     download_root = os.path.expanduser(download_root)
158     if extract_root is None:
159         extract_root = download_root
160     if filename is None:
161         filename = os.path.basename(url)
162 
163     if not check_integrity(os.path.join(download_root, filename), md5):
164         download_url(url, download_root, filename=filename, md5=md5)
165 
166     extract_tar(os.path.join(download_root, filename), extract_root, **kwargs)
167 
168 
169 def parse_devkit(root):
170     idx_to_wnid, wnid_to_classes = parse_meta(root)
171     val_idcs = parse_val_groundtruth(root)
172     val_wnids = [idx_to_wnid[idx] for idx in val_idcs]
173     return wnid_to_classes, val_wnids
174 
175 
176 def parse_meta(devkit_root, path='data', filename='meta.mat'):
177     import scipy.io as sio
178 
179     metafile = os.path.join(devkit_root, path, filename)
180     meta = sio.loadmat(metafile, squeeze_me=True)['synsets']
181     nums_children = list(zip(*meta))[4]
182     meta = [meta[idx] for idx, num_children in enumerate(nums_children)
183             if num_children == 0]
184     idcs, wnids, classes = list(zip(*meta))[:3]
185     classes = [tuple(clss.split(', ')) for clss in classes]
186     idx_to_wnid = {idx: wnid for idx, wnid in zip(idcs, wnids)}
187     wnid_to_classes = {wnid: clss for wnid, clss in zip(wnids, classes)}
188     return idx_to_wnid, wnid_to_classes
189 
190 
191 def parse_val_groundtruth(devkit_root, path='data',
192                           filename='ILSVRC2012_validation_ground_truth.txt'):
193     with open(os.path.join(devkit_root, path, filename), 'r') as txtfh:
194         val_idcs = txtfh.readlines()
195     return [int(val_idx) for val_idx in val_idcs]
196 
197 
198 def prepare_train_folder(folder):
199     for archive in [os.path.join(folder, archive) for archive in os.listdir(folder)]:
200         extract_tar(archive, os.path.splitext(archive)[0], delete=True)
201 
202 
203 def prepare_val_folder(folder, wnids):
204     img_files = sorted([os.path.join(folder, file) for file in os.listdir(folder)])
205 
206     for wnid in set(wnids):
207         os.mkdir(os.path.join(folder, wnid))
208 
209     for wnid, img_file in zip(wnids, img_files):
210         shutil.move(img_file, os.path.join(folder, wnid, os.path.basename(img_file)))
211 
212 
213 def _splitexts(root):
214     exts = []
215     ext = '.'
216     while ext:
217         root, ext = os.path.splitext(root)
218         exts.append(ext)
219     return root, ''.join(reversed(exts))
220
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/torchvision/datasets/imagenet.py b/torchvision/datasets/imagenet.py
--- a/torchvision/datasets/imagenet.py
+++ b/torchvision/datasets/imagenet.py
@@ -20,11 +20,6 @@
     }
 }
 
-META_DICT = {
-    'filename': 'meta.bin',
-    'md5': '7e0d3cf156177e4fc47011cdd30ce706',
-}
-
 
 class ImageNet(ImageFolder):
     """`ImageNet <http://image-net.org/>`_ 2012 Classification Dataset.
@@ -70,7 +65,7 @@
                              for cls in clss}
 
     def download(self):
-        if not self._check_meta_file_integrity():
+        if not check_integrity(self.meta_file):
             tmpdir = os.path.join(self.root, 'tmp')
 
             archive_dict = ARCHIVE_DICT['devkit']
@@ -102,13 +97,10 @@
 
     @property
     def meta_file(self):
-        return os.path.join(self.root, META_DICT['filename'])
-
-    def _check_meta_file_integrity(self):
-        return check_integrity(self.meta_file, META_DICT['md5'])
+        return os.path.join(self.root, 'meta.bin')
 
     def _load_meta_file(self):
-        if self._check_meta_file_integrity():
+        if check_integrity(self.meta_file):
             return torch.load(self.meta_file)
         else:
             raise RuntimeError("Meta file not found or corrupted.",

{"golden_diff": "diff --git a/torchvision/datasets/imagenet.py b/torchvision/datasets/imagenet.py\n--- a/torchvision/datasets/imagenet.py\n+++ b/torchvision/datasets/imagenet.py\n@@ -20,11 +20,6 @@\n     }\n }\n \n-META_DICT = {\n-    'filename': 'meta.bin',\n-    'md5': '7e0d3cf156177e4fc47011cdd30ce706',\n-}\n-\n \n class ImageNet(ImageFolder):\n     \"\"\"`ImageNet <http://image-net.org/>`_ 2012 Classification Dataset.\n@@ -70,7 +65,7 @@\n                              for cls in clss}\n \n     def download(self):\n-        if not self._check_meta_file_integrity():\n+        if not check_integrity(self.meta_file):\n             tmpdir = os.path.join(self.root, 'tmp')\n \n             archive_dict = ARCHIVE_DICT['devkit']\n@@ -102,13 +97,10 @@\n \n     @property\n     def meta_file(self):\n-        return os.path.join(self.root, META_DICT['filename'])\n-\n-    def _check_meta_file_integrity(self):\n-        return check_integrity(self.meta_file, META_DICT['md5'])\n+        return os.path.join(self.root, 'meta.bin')\n \n     def _load_meta_file(self):\n-        if self._check_meta_file_integrity():\n+        if check_integrity(self.meta_file):\n             return torch.load(self.meta_file)\n         else:\n             raise RuntimeError(\"Meta file not found or corrupted.\",\n", "issue": "Invalid hash error on ImageNet dataset\nIn my environment, md5 value of `meta.bin` used in ImageNet dataset is different from the value defined in `imagenet.py`.\r\n\r\n`meta.bin` is generated by `torch.save` in the code. I found python2 and3 generate different files.\r\n\r\nmd5sum hashes are as follows.\r\n\r\n- (defined) `7e0d3cf156177e4fc47011cdd30ce706`\r\n- (Python 2.7.16, Ubuntu) `a36fd93cf3900286d99e24ad0a73ce04`\r\n- (Python 3.7.3, Ubuntu) `ca981e8aac175178e80e7949d90ee85c`\r\n\r\nhttps://github.com/pytorch/vision/blob/9a481d0bec2700763a799ff148fe2e083b575441/torchvision/datasets/imagenet.py#L23-L26\r\n\r\nhttps://github.com/pytorch/vision/blob/9a481d0bec2700763a799ff148fe2e083b575441/torchvision/datasets/imagenet.py#L117-L118\n", "before_files": [{"content": "from __future__ import print_function\nimport os\nimport shutil\nimport torch\nfrom .folder import ImageFolder\nfrom .utils import check_integrity, download_url\n\nARCHIVE_DICT = {\n    'train': {\n        'url': 'http://www.image-net.org/challenges/LSVRC/2012/nnoupb/ILSVRC2012_img_train.tar',\n        'md5': '1d675b47d978889d74fa0da5fadfb00e',\n    },\n    'val': {\n        'url': 'http://www.image-net.org/challenges/LSVRC/2012/nnoupb/ILSVRC2012_img_val.tar',\n        'md5': '29b22e2961454d5413ddabcf34fc5622',\n    },\n    'devkit': {\n        'url': 'http://www.image-net.org/challenges/LSVRC/2012/nnoupb/ILSVRC2012_devkit_t12.tar.gz',\n        'md5': 'fa75699e90414af021442c21a62c3abf',\n    }\n}\n\nMETA_DICT = {\n    'filename': 'meta.bin',\n    'md5': '7e0d3cf156177e4fc47011cdd30ce706',\n}\n\n\nclass ImageNet(ImageFolder):\n    \"\"\"`ImageNet <http://image-net.org/>`_ 2012 Classification Dataset.\n\n    Args:\n        root (string): Root directory of the ImageNet Dataset.\n        split (string, optional): The dataset split, supports ``train``, or ``val``.\n        download (bool, optional): If true, downloads the dataset from the internet and\n            puts it in root directory. If dataset is already downloaded, it is not\n            downloaded again.\n        transform (callable, optional): A function/transform that  takes in an PIL image\n            and returns a transformed version. E.g, ``transforms.RandomCrop``\n        target_transform (callable, optional): A function/transform that takes in the\n            target and transforms it.\n        loader (callable, optional): A function to load an image given its path.\n\n     Attributes:\n        classes (list): List of the class names.\n        class_to_idx (dict): Dict with items (class_name, class_index).\n        wnids (list): List of the WordNet IDs.\n        wnid_to_idx (dict): Dict with items (wordnet_id, class_index).\n        imgs (list): List of (image path, class_index) tuples\n        targets (list): The class_index value for each image in the dataset\n    \"\"\"\n\n    def __init__(self, root, split='train', download=False, **kwargs):\n        root = self.root = os.path.expanduser(root)\n        self.split = self._verify_split(split)\n\n        if download:\n            self.download()\n        wnid_to_classes = self._load_meta_file()[0]\n\n        super(ImageNet, self).__init__(self.split_folder, **kwargs)\n        self.root = root\n\n        idcs = [idx for _, idx in self.imgs]\n        self.wnids = self.classes\n        self.wnid_to_idx = {wnid: idx for idx, wnid in zip(idcs, self.wnids)}\n        self.classes = [wnid_to_classes[wnid] for wnid in self.wnids]\n        self.class_to_idx = {cls: idx\n                             for clss, idx in zip(self.classes, idcs)\n                             for cls in clss}\n\n    def download(self):\n        if not self._check_meta_file_integrity():\n            tmpdir = os.path.join(self.root, 'tmp')\n\n            archive_dict = ARCHIVE_DICT['devkit']\n            download_and_extract_tar(archive_dict['url'], self.root,\n                                     extract_root=tmpdir,\n                                     md5=archive_dict['md5'])\n            devkit_folder = _splitexts(os.path.basename(archive_dict['url']))[0]\n            meta = parse_devkit(os.path.join(tmpdir, devkit_folder))\n            self._save_meta_file(*meta)\n\n            shutil.rmtree(tmpdir)\n\n        if not os.path.isdir(self.split_folder):\n            archive_dict = ARCHIVE_DICT[self.split]\n            download_and_extract_tar(archive_dict['url'], self.root,\n                                     extract_root=self.split_folder,\n                                     md5=archive_dict['md5'])\n\n            if self.split == 'train':\n                prepare_train_folder(self.split_folder)\n            elif self.split == 'val':\n                val_wnids = self._load_meta_file()[1]\n                prepare_val_folder(self.split_folder, val_wnids)\n        else:\n            msg = (\"You set download=True, but a folder '{}' already exist in \"\n                   \"the root directory. If you want to re-download or re-extract the \"\n                   \"archive, delete the folder.\")\n            print(msg.format(self.split))\n\n    @property\n    def meta_file(self):\n        return os.path.join(self.root, META_DICT['filename'])\n\n    def _check_meta_file_integrity(self):\n        return check_integrity(self.meta_file, META_DICT['md5'])\n\n    def _load_meta_file(self):\n        if self._check_meta_file_integrity():\n            return torch.load(self.meta_file)\n        else:\n            raise RuntimeError(\"Meta file not found or corrupted.\",\n                               \"You can use download=True to create it.\")\n\n    def _save_meta_file(self, wnid_to_class, val_wnids):\n        torch.save((wnid_to_class, val_wnids), self.meta_file)\n\n    def _verify_split(self, split):\n        if split not in self.valid_splits:\n            msg = \"Unknown split {} .\".format(split)\n            msg += \"Valid splits are {{}}.\".format(\", \".join(self.valid_splits))\n            raise ValueError(msg)\n        return split\n\n    @property\n    def valid_splits(self):\n        return 'train', 'val'\n\n    @property\n    def split_folder(self):\n        return os.path.join(self.root, self.split)\n\n    def extra_repr(self):\n        return \"Split: {split}\".format(**self.__dict__)\n\n\ndef extract_tar(src, dest=None, gzip=None, delete=False):\n    import tarfile\n\n    if dest is None:\n        dest = os.path.dirname(src)\n    if gzip is None:\n        gzip = src.lower().endswith('.gz')\n\n    mode = 'r:gz' if gzip else 'r'\n    with tarfile.open(src, mode) as tarfh:\n        tarfh.extractall(path=dest)\n\n    if delete:\n        os.remove(src)\n\n\ndef download_and_extract_tar(url, download_root, extract_root=None, filename=None,\n                             md5=None, **kwargs):\n    download_root = os.path.expanduser(download_root)\n    if extract_root is None:\n        extract_root = download_root\n    if filename is None:\n        filename = os.path.basename(url)\n\n    if not check_integrity(os.path.join(download_root, filename), md5):\n        download_url(url, download_root, filename=filename, md5=md5)\n\n    extract_tar(os.path.join(download_root, filename), extract_root, **kwargs)\n\n\ndef parse_devkit(root):\n    idx_to_wnid, wnid_to_classes = parse_meta(root)\n    val_idcs = parse_val_groundtruth(root)\n    val_wnids = [idx_to_wnid[idx] for idx in val_idcs]\n    return wnid_to_classes, val_wnids\n\n\ndef parse_meta(devkit_root, path='data', filename='meta.mat'):\n    import scipy.io as sio\n\n    metafile = os.path.join(devkit_root, path, filename)\n    meta = sio.loadmat(metafile, squeeze_me=True)['synsets']\n    nums_children = list(zip(*meta))[4]\n    meta = [meta[idx] for idx, num_children in enumerate(nums_children)\n            if num_children == 0]\n    idcs, wnids, classes = list(zip(*meta))[:3]\n    classes = [tuple(clss.split(', ')) for clss in classes]\n    idx_to_wnid = {idx: wnid for idx, wnid in zip(idcs, wnids)}\n    wnid_to_classes = {wnid: clss for wnid, clss in zip(wnids, classes)}\n    return idx_to_wnid, wnid_to_classes\n\n\ndef parse_val_groundtruth(devkit_root, path='data',\n                          filename='ILSVRC2012_validation_ground_truth.txt'):\n    with open(os.path.join(devkit_root, path, filename), 'r') as txtfh:\n        val_idcs = txtfh.readlines()\n    return [int(val_idx) for val_idx in val_idcs]\n\n\ndef prepare_train_folder(folder):\n    for archive in [os.path.join(folder, archive) for archive in os.listdir(folder)]:\n        extract_tar(archive, os.path.splitext(archive)[0], delete=True)\n\n\ndef prepare_val_folder(folder, wnids):\n    img_files = sorted([os.path.join(folder, file) for file in os.listdir(folder)])\n\n    for wnid in set(wnids):\n        os.mkdir(os.path.join(folder, wnid))\n\n    for wnid, img_file in zip(wnids, img_files):\n        shutil.move(img_file, os.path.join(folder, wnid, os.path.basename(img_file)))\n\n\ndef _splitexts(root):\n    exts = []\n    ext = '.'\n    while ext:\n        root, ext = os.path.splitext(root)\n        exts.append(ext)\n    return root, ''.join(reversed(exts))\n", "path": "torchvision/datasets/imagenet.py"}], "after_files": [{"content": "from __future__ import print_function\nimport os\nimport shutil\nimport torch\nfrom .folder import ImageFolder\nfrom .utils import check_integrity, download_url\n\nARCHIVE_DICT = {\n    'train': {\n        'url': 'http://www.image-net.org/challenges/LSVRC/2012/nnoupb/ILSVRC2012_img_train.tar',\n        'md5': '1d675b47d978889d74fa0da5fadfb00e',\n    },\n    'val': {\n        'url': 'http://www.image-net.org/challenges/LSVRC/2012/nnoupb/ILSVRC2012_img_val.tar',\n        'md5': '29b22e2961454d5413ddabcf34fc5622',\n    },\n    'devkit': {\n        'url': 'http://www.image-net.org/challenges/LSVRC/2012/nnoupb/ILSVRC2012_devkit_t12.tar.gz',\n        'md5': 'fa75699e90414af021442c21a62c3abf',\n    }\n}\n\n\nclass ImageNet(ImageFolder):\n    \"\"\"`ImageNet <http://image-net.org/>`_ 2012 Classification Dataset.\n\n    Args:\n        root (string): Root directory of the ImageNet Dataset.\n        split (string, optional): The dataset split, supports ``train``, or ``val``.\n        download (bool, optional): If true, downloads the dataset from the internet and\n            puts it in root directory. If dataset is already downloaded, it is not\n            downloaded again.\n        transform (callable, optional): A function/transform that  takes in an PIL image\n            and returns a transformed version. E.g, ``transforms.RandomCrop``\n        target_transform (callable, optional): A function/transform that takes in the\n            target and transforms it.\n        loader (callable, optional): A function to load an image given its path.\n\n     Attributes:\n        classes (list): List of the class names.\n        class_to_idx (dict): Dict with items (class_name, class_index).\n        wnids (list): List of the WordNet IDs.\n        class_to_idx (dict): Dict with items (wordnet_id, wordnet_id_index).\n        imgs (list): List of (image path, class_index) tuples\n        targets (list): The class_index value for each image in the dataset\n    \"\"\"\n\n    def __init__(self, root, split='train', download=False, **kwargs):\n        root = self.root = os.path.expanduser(root)\n        self.split = self._verify_split(split)\n\n        if download:\n            self.download()\n        wnid_to_classes = self._load_meta_file()[0]\n\n        super(ImageNet, self).__init__(self.split_folder, **kwargs)\n        self.root = root\n\n        idcs = [idx for _, idx in self.imgs]\n        self.wnids = self.classes\n        self.wnid_to_idx = {wnid: idx for idx, wnid in zip(idcs, self.wnids)}\n        self.classes = [wnid_to_classes[wnid] for wnid in self.wnids]\n        self.class_to_idx = {cls: idx\n                             for clss, idx in zip(self.classes, idcs)\n                             for cls in clss}\n\n    def download(self):\n        if not check_integrity(self.meta_file):\n            tmpdir = os.path.join(self.root, 'tmp')\n\n            archive_dict = ARCHIVE_DICT['devkit']\n            download_and_extract_tar(archive_dict['url'], self.root,\n                                     extract_root=tmpdir,\n                                     md5=archive_dict['md5'])\n            devkit_folder = _splitexts(os.path.basename(archive_dict['url']))[0]\n            meta = parse_devkit(os.path.join(tmpdir, devkit_folder))\n            self._save_meta_file(*meta)\n\n            shutil.rmtree(tmpdir)\n\n        if not os.path.isdir(self.split_folder):\n            archive_dict = ARCHIVE_DICT[self.split]\n            download_and_extract_tar(archive_dict['url'], self.root,\n                                     extract_root=self.split_folder,\n                                     md5=archive_dict['md5'])\n\n            if self.split == 'train':\n                prepare_train_folder(self.split_folder)\n            elif self.split == 'val':\n                val_wnids = self._load_meta_file()[1]\n                prepare_val_folder(self.split_folder, val_wnids)\n        else:\n            msg = (\"You set download=True, but a folder '{}' already exist in \"\n                   \"the root directory. If you want to re-download or re-extract the \"\n                   \"archive, delete the folder.\")\n            print(msg.format(self.split))\n\n    @property\n    def meta_file(self):\n        return os.path.join(self.root, 'meta.bin')\n\n    def _load_meta_file(self):\n        if check_integrity(self.meta_file):\n            return torch.load(self.meta_file)\n        else:\n            raise RuntimeError(\"Meta file not found or corrupted.\",\n                               \"You can use download=True to create it.\")\n\n    def _save_meta_file(self, wnid_to_class, val_wnids):\n        torch.save((wnid_to_class, val_wnids), self.meta_file)\n\n    def _verify_split(self, split):\n        if split not in self.valid_splits:\n            msg = \"Unknown split {} .\".format(split)\n            msg += \"Valid splits are {{}}.\".format(\", \".join(self.valid_splits))\n            raise ValueError(msg)\n        return split\n\n    @property\n    def valid_splits(self):\n        return 'train', 'val'\n\n    @property\n    def split_folder(self):\n        return os.path.join(self.root, self.split)\n\n    def extra_repr(self):\n        return \"Split: {split}\".format(**self.__dict__)\n\n\ndef extract_tar(src, dest=None, gzip=None, delete=False):\n    import tarfile\n\n    if dest is None:\n        dest = os.path.dirname(src)\n    if gzip is None:\n        gzip = src.lower().endswith('.gz')\n\n    mode = 'r:gz' if gzip else 'r'\n    with tarfile.open(src, mode) as tarfh:\n        tarfh.extractall(path=dest)\n\n    if delete:\n        os.remove(src)\n\n\ndef download_and_extract_tar(url, download_root, extract_root=None, filename=None,\n                             md5=None, **kwargs):\n    download_root = os.path.expanduser(download_root)\n    if extract_root is None:\n        extract_root = extract_root\n    if filename is None:\n        filename = os.path.basename(url)\n\n    if not check_integrity(os.path.join(download_root, filename), md5):\n        download_url(url, download_root, filename=filename, md5=md5)\n\n    extract_tar(os.path.join(download_root, filename), extract_root, **kwargs)\n\n\ndef parse_devkit(root):\n    idx_to_wnid, wnid_to_classes = parse_meta(root)\n    val_idcs = parse_val_groundtruth(root)\n    val_wnids = [idx_to_wnid[idx] for idx in val_idcs]\n    return wnid_to_classes, val_wnids\n\n\ndef parse_meta(devkit_root, path='data', filename='meta.mat'):\n    import scipy.io as sio\n\n    metafile = os.path.join(devkit_root, path, filename)\n    meta = sio.loadmat(metafile, squeeze_me=True)['synsets']\n    nums_children = list(zip(*meta))[4]\n    meta = [meta[idx] for idx, num_children in enumerate(nums_children)\n            if num_children == 0]\n    idcs, wnids, classes = list(zip(*meta))[:3]\n    classes = [tuple(clss.split(', ')) for clss in classes]\n    idx_to_wnid = {idx: wnid for idx, wnid in zip(idcs, wnids)}\n    wnid_to_classes = {wnid: clss for wnid, clss in zip(wnids, classes)}\n    return idx_to_wnid, wnid_to_classes\n\n\ndef parse_val_groundtruth(devkit_root, path='data',\n                          filename='ILSVRC2012_validation_ground_truth.txt'):\n    with open(os.path.join(devkit_root, path, filename), 'r') as txtfh:\n        val_idcs = txtfh.readlines()\n    return [int(val_idx) for val_idx in val_idcs]\n\n\ndef prepare_train_folder(folder):\n    for archive in [os.path.join(folder, archive) for archive in os.listdir(folder)]:\n        extract_tar(archive, os.path.splitext(archive)[0], delete=True)\n\n\ndef prepare_val_folder(folder, wnids):\n    img_files = sorted([os.path.join(folder, file) for file in os.listdir(folder)])\n\n    for wnid in set(wnids):\n        os.mkdir(os.path.join(folder, wnid))\n\n    for wnid, img_file in zip(wnids, img_files):\n        shutil.move(img_file, os.path.join(folder, wnid, os.path.basename(img_file)))\n\n\ndef _splitexts(root):\n    exts = []\n    ext = '.'\n    while ext:\n        root, ext = os.path.splitext(root)\n        exts.append(ext)\n    return root, ''.join(reversed(exts))\n", "path": "torchvision/datasets/imagenet.py"}]}