prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Import ImageNet dataset
  ### Steps to reproduce problem
  1. Download and extract ImageNet dataset for image classification: [link](https://www.kaggle.com/c/imagenet-object-localization-challenge/data);
  2. Add the loaded dataset into a Datumaro project;
  3. Run `datum info`.
  
  ### Current behaviour
  ImageNet dataset has ~1.2m images, but in the `info` output we can see that imported dataset has only 69647, and also these images have wrong labels.
  
  ### Expected behaviour
  Correct import.
  
  ### Environment
  `git log -1`: 7e35c8
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `datumaro/plugins/imagenet_format.py`
  Content:
  ```
  1 # Copyright (C) 2020 Intel Corporation
  2 #
  3 # SPDX-License-Identifier: MIT
  4 
  5 import logging as log
  6 import os
  7 import os.path as osp
  8 
  9 from datumaro.components.converter import Converter
  10 from datumaro.components.extractor import (
  11     AnnotationType, DatasetItem, Importer, Label, LabelCategories,
  12     SourceExtractor,
  13 )
  14 from datumaro.util.image import find_images
  15 
  16 
  17 class ImagenetPath:
  18     IMAGE_DIR_NO_LABEL = 'no_label'
  19 
  20 
  21 class ImagenetExtractor(SourceExtractor):
  22     def __init__(self, path, subset=None):
  23         assert osp.isdir(path), path
  24         super().__init__(subset=subset)
  25 
  26         self._categories = self._load_categories(path)
  27         self._items = list(self._load_items(path).values())
  28 
  29     def _load_categories(self, path):
  30         label_cat = LabelCategories()
  31         for dirname in sorted(os.listdir(path)):
  32             if dirname != ImagenetPath.IMAGE_DIR_NO_LABEL:
  33                 label_cat.add(dirname)
  34         return { AnnotationType.label: label_cat }
  35 
  36     def _load_items(self, path):
  37         items = {}
  38 
  39         for image_path in find_images(path, recursive=True, max_depth=1):
  40             label = osp.basename(osp.dirname(image_path))
  41             image_name = osp.splitext(osp.basename(image_path))[0]
  42             if image_name.startswith(label + '_'):
  43                 image_name = image_name[len(label) + 1:]
  44 
  45             item = items.get(image_name)
  46             if item is None:
  47                 item = DatasetItem(id=image_name, subset=self._subset,
  48                     image=image_path)
  49                 items[image_name] = item
  50             annotations = item.annotations
  51 
  52             if label != ImagenetPath.IMAGE_DIR_NO_LABEL:
  53                 label = self._categories[AnnotationType.label].find(label)[0]
  54                 annotations.append(Label(label=label))
  55 
  56         return items
  57 
  58 
  59 class ImagenetImporter(Importer):
  60     @classmethod
  61     def find_sources(cls, path):
  62         if not osp.isdir(path):
  63             return []
  64         return [{ 'url': path, 'format': 'imagenet' }]
  65 
  66 
  67 class ImagenetConverter(Converter):
  68     DEFAULT_IMAGE_EXT = '.jpg'
  69 
  70     def apply(self):
  71         if 1 < len(self._extractor.subsets()):
  72             log.warning("ImageNet format only supports exporting a single "
  73                 "subset, subset information will not be used.")
  74 
  75         subset_dir = self._save_dir
  76         extractor = self._extractor
  77         labels = {}
  78         for item in self._extractor:
  79             labels = set(p.label for p in item.annotations
  80                 if p.type == AnnotationType.label)
  81 
  82             for label in labels:
  83                 label_name = extractor.categories()[AnnotationType.label][label].name
  84                 self._save_image(item, osp.join(subset_dir, label_name,
  85                     '%s_%s' %  (label_name, self._make_image_filename(item))))
  86 
  87             if not labels:
  88                 self._save_image(item, osp.join(subset_dir,
  89                     ImagenetPath.IMAGE_DIR_NO_LABEL,
  90                     ImagenetPath.IMAGE_DIR_NO_LABEL + '_' + \
  91                     self._make_image_filename(item)))
  92
  ```
  --- 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()
  
  ```
  
golden_diff: diff --git a/datumaro/plugins/imagenet_format.py b/datumaro/plugins/imagenet_format.py
  --- a/datumaro/plugins/imagenet_format.py
  +++ b/datumaro/plugins/imagenet_format.py
  @@ -39,14 +39,13 @@
           for image_path in find_images(path, recursive=True, max_depth=1):
               label = osp.basename(osp.dirname(image_path))
               image_name = osp.splitext(osp.basename(image_path))[0]
  -            if image_name.startswith(label + '_'):
  -                image_name = image_name[len(label) + 1:]
   
  -            item = items.get(image_name)
  +            item_id = osp.join(label, image_name)
  +            item = items.get(item_id)
               if item is None:
  -                item = DatasetItem(id=image_name, subset=self._subset,
  +                item = DatasetItem(id=item_id, subset=self._subset,
                       image=image_path)
  -                items[image_name] = item
  +                items[item_id] = item
               annotations = item.annotations
   
               if label != ImagenetPath.IMAGE_DIR_NO_LABEL:
  @@ -68,6 +67,13 @@
       DEFAULT_IMAGE_EXT = '.jpg'
   
       def apply(self):
  +
  +        def _get_dir_name(id_parts, label_name):
  +            if 1 < len(id_parts) and id_parts[0] == label_name:
  +                return ''
  +            else:
  +                return label_name
  +
           if 1 < len(self._extractor.subsets()):
               log.warning("ImageNet format only supports exporting a single "
                   "subset, subset information will not be used.")
  @@ -76,16 +82,15 @@
           extractor = self._extractor
           labels = {}
           for item in self._extractor:
  +            id_parts = item.id.split('/')
               labels = set(p.label for p in item.annotations
                   if p.type == AnnotationType.label)
   
               for label in labels:
                   label_name = extractor.categories()[AnnotationType.label][label].name
  -                self._save_image(item, osp.join(subset_dir, label_name,
  -                    '%s_%s' %  (label_name, self._make_image_filename(item))))
  +                self._save_image(item, subdir=osp.join(subset_dir,
  +                    _get_dir_name(id_parts, label_name)))
   
               if not labels:
  -                self._save_image(item, osp.join(subset_dir,
  -                    ImagenetPath.IMAGE_DIR_NO_LABEL,
  -                    ImagenetPath.IMAGE_DIR_NO_LABEL + '_' + \
  -                    self._make_image_filename(item)))
  +                self._save_image(item, subdir=osp.join(subset_dir,
  +                     _get_dir_name(id_parts, ImagenetPath.IMAGE_DIR_NO_LABEL)))
  
verification_info: {"golden_diff": "diff --git a/datumaro/plugins/imagenet_format.py b/datumaro/plugins/imagenet_format.py\n--- a/datumaro/plugins/imagenet_format.py\n+++ b/datumaro/plugins/imagenet_format.py\n@@ -39,14 +39,13 @@\n         for image_path in find_images(path, recursive=True, max_depth=1):\n             label = osp.basename(osp.dirname(image_path))\n             image_name = osp.splitext(osp.basename(image_path))[0]\n-            if image_name.startswith(label + '_'):\n-                image_name = image_name[len(label) + 1:]\n \n-            item = items.get(image_name)\n+            item_id = osp.join(label, image_name)\n+            item = items.get(item_id)\n             if item is None:\n-                item = DatasetItem(id=image_name, subset=self._subset,\n+                item = DatasetItem(id=item_id, subset=self._subset,\n                     image=image_path)\n-                items[image_name] = item\n+                items[item_id] = item\n             annotations = item.annotations\n \n             if label != ImagenetPath.IMAGE_DIR_NO_LABEL:\n@@ -68,6 +67,13 @@\n     DEFAULT_IMAGE_EXT = '.jpg'\n \n     def apply(self):\n+\n+        def _get_dir_name(id_parts, label_name):\n+            if 1 < len(id_parts) and id_parts[0] == label_name:\n+                return ''\n+            else:\n+                return label_name\n+\n         if 1 < len(self._extractor.subsets()):\n             log.warning(\"ImageNet format only supports exporting a single \"\n                 \"subset, subset information will not be used.\")\n@@ -76,16 +82,15 @@\n         extractor = self._extractor\n         labels = {}\n         for item in self._extractor:\n+            id_parts = item.id.split('/')\n             labels = set(p.label for p in item.annotations\n                 if p.type == AnnotationType.label)\n \n             for label in labels:\n                 label_name = extractor.categories()[AnnotationType.label][label].name\n-                self._save_image(item, osp.join(subset_dir, label_name,\n-                    '%s_%s' %  (label_name, self._make_image_filename(item))))\n+                self._save_image(item, subdir=osp.join(subset_dir,\n+                    _get_dir_name(id_parts, label_name)))\n \n             if not labels:\n-                self._save_image(item, osp.join(subset_dir,\n-                    ImagenetPath.IMAGE_DIR_NO_LABEL,\n-                    ImagenetPath.IMAGE_DIR_NO_LABEL + '_' + \\\n-                    self._make_image_filename(item)))\n+                self._save_image(item, subdir=osp.join(subset_dir,\n+                     _get_dir_name(id_parts, ImagenetPath.IMAGE_DIR_NO_LABEL)))\n", "issue": "Import ImageNet dataset\n### Steps to reproduce problem\r\n1. Download and extract ImageNet dataset for image classification: [link](https://www.kaggle.com/c/imagenet-object-localization-challenge/data);\r\n2. Add the loaded dataset into a Datumaro project;\r\n3. Run `datum info`.\r\n\r\n### Current behaviour\r\nImageNet dataset has ~1.2m images, but in the `info` output we can see that imported dataset has only 69647, and also these images have wrong labels.\r\n\r\n### Expected behaviour\r\nCorrect import.\r\n\r\n### Environment\r\n`git log -1`: 7e35c8\n", "before_files": [{"content": "# Copyright (C) 2020 Intel Corporation\n#\n# SPDX-License-Identifier: MIT\n\nimport logging as log\nimport os\nimport os.path as osp\n\nfrom datumaro.components.converter import Converter\nfrom datumaro.components.extractor import (\n    AnnotationType, DatasetItem, Importer, Label, LabelCategories,\n    SourceExtractor,\n)\nfrom datumaro.util.image import find_images\n\n\nclass ImagenetPath:\n    IMAGE_DIR_NO_LABEL = 'no_label'\n\n\nclass ImagenetExtractor(SourceExtractor):\n    def __init__(self, path, subset=None):\n        assert osp.isdir(path), path\n        super().__init__(subset=subset)\n\n        self._categories = self._load_categories(path)\n        self._items = list(self._load_items(path).values())\n\n    def _load_categories(self, path):\n        label_cat = LabelCategories()\n        for dirname in sorted(os.listdir(path)):\n            if dirname != ImagenetPath.IMAGE_DIR_NO_LABEL:\n                label_cat.add(dirname)\n        return { AnnotationType.label: label_cat }\n\n    def _load_items(self, path):\n        items = {}\n\n        for image_path in find_images(path, recursive=True, max_depth=1):\n            label = osp.basename(osp.dirname(image_path))\n            image_name = osp.splitext(osp.basename(image_path))[0]\n            if image_name.startswith(label + '_'):\n                image_name = image_name[len(label) + 1:]\n\n            item = items.get(image_name)\n            if item is None:\n                item = DatasetItem(id=image_name, subset=self._subset,\n                    image=image_path)\n                items[image_name] = item\n            annotations = item.annotations\n\n            if label != ImagenetPath.IMAGE_DIR_NO_LABEL:\n                label = self._categories[AnnotationType.label].find(label)[0]\n                annotations.append(Label(label=label))\n\n        return items\n\n\nclass ImagenetImporter(Importer):\n    @classmethod\n    def find_sources(cls, path):\n        if not osp.isdir(path):\n            return []\n        return [{ 'url': path, 'format': 'imagenet' }]\n\n\nclass ImagenetConverter(Converter):\n    DEFAULT_IMAGE_EXT = '.jpg'\n\n    def apply(self):\n        if 1 < len(self._extractor.subsets()):\n            log.warning(\"ImageNet format only supports exporting a single \"\n                \"subset, subset information will not be used.\")\n\n        subset_dir = self._save_dir\n        extractor = self._extractor\n        labels = {}\n        for item in self._extractor:\n            labels = set(p.label for p in item.annotations\n                if p.type == AnnotationType.label)\n\n            for label in labels:\n                label_name = extractor.categories()[AnnotationType.label][label].name\n                self._save_image(item, osp.join(subset_dir, label_name,\n                    '%s_%s' %  (label_name, self._make_image_filename(item))))\n\n            if not labels:\n                self._save_image(item, osp.join(subset_dir,\n                    ImagenetPath.IMAGE_DIR_NO_LABEL,\n                    ImagenetPath.IMAGE_DIR_NO_LABEL + '_' + \\\n                    self._make_image_filename(item)))\n", "path": "datumaro/plugins/imagenet_format.py"}], "after_files": [{"content": "# Copyright (C) 2020 Intel Corporation\n#\n# SPDX-License-Identifier: MIT\n\nimport logging as log\nimport os\nimport os.path as osp\n\nfrom datumaro.components.converter import Converter\nfrom datumaro.components.extractor import (\n    AnnotationType, DatasetItem, Importer, Label, LabelCategories,\n    SourceExtractor,\n)\nfrom datumaro.util.image import find_images\n\n\nclass ImagenetPath:\n    IMAGE_DIR_NO_LABEL = 'no_label'\n\n\nclass ImagenetExtractor(SourceExtractor):\n    def __init__(self, path, subset=None):\n        assert osp.isdir(path), path\n        super().__init__(subset=subset)\n\n        self._categories = self._load_categories(path)\n        self._items = list(self._load_items(path).values())\n\n    def _load_categories(self, path):\n        label_cat = LabelCategories()\n        for dirname in sorted(os.listdir(path)):\n            if dirname != ImagenetPath.IMAGE_DIR_NO_LABEL:\n                label_cat.add(dirname)\n        return { AnnotationType.label: label_cat }\n\n    def _load_items(self, path):\n        items = {}\n\n        for image_path in find_images(path, recursive=True, max_depth=1):\n            label = osp.basename(osp.dirname(image_path))\n            image_name = osp.splitext(osp.basename(image_path))[0]\n\n            item_id = osp.join(label, image_name)\n            item = items.get(item_id)\n            if item is None:\n                item = DatasetItem(id=item_id, subset=self._subset,\n                    image=image_path)\n                items[item_id] = item\n            annotations = item.annotations\n\n            if label != ImagenetPath.IMAGE_DIR_NO_LABEL:\n                label = self._categories[AnnotationType.label].find(label)[0]\n                annotations.append(Label(label=label))\n\n        return items\n\n\nclass ImagenetImporter(Importer):\n    @classmethod\n    def find_sources(cls, path):\n        if not osp.isdir(path):\n            return []\n        return [{ 'url': path, 'format': 'imagenet' }]\n\n\nclass ImagenetConverter(Converter):\n    DEFAULT_IMAGE_EXT = '.jpg'\n\n    def apply(self):\n\n        def _get_dir_name(id_parts, label_name):\n            if 1 < len(id_parts) and id_parts[0] == label_name:\n                return ''\n            else:\n                return label_name\n\n        if 1 < len(self._extractor.subsets()):\n            log.warning(\"ImageNet format only supports exporting a single \"\n                \"subset, subset information will not be used.\")\n\n        subset_dir = self._save_dir\n        extractor = self._extractor\n        labels = {}\n        for item in self._extractor:\n            id_parts = item.id.split('/')\n            labels = set(p.label for p in item.annotations\n                if p.type == AnnotationType.label)\n\n            for label in labels:\n                label_name = extractor.categories()[AnnotationType.label][label].name\n                self._save_image(item, subdir=osp.join(subset_dir,\n                    _get_dir_name(id_parts, label_name)))\n\n            if not labels:\n                self._save_image(item, subdir=osp.join(subset_dir,\n                     _get_dir_name(id_parts, ImagenetPath.IMAGE_DIR_NO_LABEL)))\n", "path": "datumaro/plugins/imagenet_format.py"}]}