用kafka接收消息
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.8KB

  1. """Prepare MS COCO datasets"""
  2. import os
  3. import sys
  4. import argparse
  5. import zipfile
  6. # TODO: optim code
  7. cur_path = os.path.abspath(os.path.dirname(__file__))
  8. root_path = os.path.split(os.path.split(os.path.split(cur_path)[0])[0])[0]
  9. sys.path.append(root_path)
  10. from core.utils import download, makedirs, try_import_pycocotools
  11. _TARGET_DIR = os.path.expanduser('~/.torch/datasets/coco')
  12. def parse_args():
  13. parser = argparse.ArgumentParser(
  14. description='Initialize MS COCO dataset.',
  15. epilog='Example: python mscoco.py --download-dir ~/mscoco',
  16. formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  17. parser.add_argument('--download-dir', type=str, default='~/mscoco/', help='dataset directory on disk')
  18. parser.add_argument('--no-download', action='store_true', help='disable automatic download if set')
  19. parser.add_argument('--overwrite', action='store_true',
  20. help='overwrite downloaded files if set, in case they are corrupted')
  21. args = parser.parse_args()
  22. return args
  23. def download_coco(path, overwrite=False):
  24. _DOWNLOAD_URLS = [
  25. ('http://images.cocodataset.org/zips/train2017.zip',
  26. '10ad623668ab00c62c096f0ed636d6aff41faca5'),
  27. ('http://images.cocodataset.org/annotations/annotations_trainval2017.zip',
  28. '8551ee4bb5860311e79dace7e79cb91e432e78b3'),
  29. ('http://images.cocodataset.org/zips/val2017.zip',
  30. '4950dc9d00dbe1c933ee0170f5797584351d2a41'),
  31. # ('http://images.cocodataset.org/annotations/stuff_annotations_trainval2017.zip',
  32. # '46cdcf715b6b4f67e980b529534e79c2edffe084'),
  33. # test2017.zip, for those who want to attend the competition.
  34. # ('http://images.cocodataset.org/zips/test2017.zip',
  35. # '4e443f8a2eca6b1dac8a6c57641b67dd40621a49'),
  36. ]
  37. makedirs(path)
  38. for url, checksum in _DOWNLOAD_URLS:
  39. filename = download(url, path=path, overwrite=overwrite, sha1_hash=checksum)
  40. # extract
  41. with zipfile.ZipFile(filename) as zf:
  42. zf.extractall(path=path)
  43. if __name__ == '__main__':
  44. args = parse_args()
  45. path = os.path.expanduser(args.download_dir)
  46. if not os.path.isdir(path) or not os.path.isdir(os.path.join(path, 'train2017')) \
  47. or not os.path.isdir(os.path.join(path, 'val2017')) \
  48. or not os.path.isdir(os.path.join(path, 'annotations')):
  49. if args.no_download:
  50. raise ValueError(('{} is not a valid directory, make sure it is present.'
  51. ' Or you should not disable "--no-download" to grab it'.format(path)))
  52. else:
  53. download_coco(path, overwrite=args.overwrite)
  54. # make symlink
  55. makedirs(os.path.expanduser('~/.torch/datasets'))
  56. if os.path.isdir(_TARGET_DIR):
  57. os.remove(_TARGET_DIR)
  58. os.symlink(path, _TARGET_DIR)
  59. try_import_pycocotools()