download_and_preprocess_imagenet.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/bash
  2. # Copyright 2016 Google Inc. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # ==============================================================================
  16. # Script to download and preprocess ImageNet Challenge 2012
  17. # training and validation data set.
  18. #
  19. # The final output of this script are sharded TFRecord files containing
  20. # serialized Example protocol buffers. See build_imagenet_data.py for
  21. # details of how the Example protocol buffers contain the ImageNet data.
  22. #
  23. # The final output of this script appears as such:
  24. #
  25. # data_dir/train-00000-of-01024
  26. # data_dir/train-00001-of-01024
  27. # ...
  28. # data_dir/train-00127-of-01024
  29. #
  30. # and
  31. #
  32. # data_dir/validation-00000-of-00128
  33. # data_dir/validation-00001-of-00128
  34. # ...
  35. # data_dir/validation-00127-of-00128
  36. #
  37. # Note that this script may take several hours to run to completion. The
  38. # conversion of the ImageNet data to TFRecords alone takes 2-3 hours depending
  39. # on the speed of your machine. Please be patient.
  40. #
  41. # **IMPORTANT**
  42. # To download the raw images, the user must create an account with image-net.org
  43. # and generate a username and access_key. The latter two are required for
  44. # downloading the raw images.
  45. #
  46. # usage:
  47. # ./download_and_preprocess_imagenet.sh [data-dir]
  48. set -e
  49. if [ -z "$1" ]; then
  50. echo "usage download_and_preprocess_imagenet.sh [data dir]"
  51. exit
  52. fi
  53. # Create the output and temporary directories.
  54. DATA_DIR="${1%/}"
  55. SCRATCH_DIR="${DATA_DIR}/raw-data/"
  56. mkdir -p "${DATA_DIR}"
  57. mkdir -p "${SCRATCH_DIR}"
  58. WORK_DIR="$0.runfiles/inception"
  59. # Download the ImageNet data.
  60. LABELS_FILE="${WORK_DIR}/data/imagenet_lsvrc_2015_synsets.txt"
  61. DOWNLOAD_SCRIPT="${WORK_DIR}/data/download_imagenet.sh"
  62. "${DOWNLOAD_SCRIPT}" "${SCRATCH_DIR}" "${LABELS_FILE}"
  63. # Note the locations of the train and validation data.
  64. TRAIN_DIRECTORY="${SCRATCH_DIR}train/"
  65. VALIDATION_DIRECTORY="${SCRATCH_DIR}validation/"
  66. # Preprocess the validation data by moving the images into the appropriate
  67. # sub-directory based on the label (synset) of the image.
  68. echo "Organizing the validation data into sub-directories."
  69. PREPROCESS_VAL_SCRIPT="${WORK_DIR}/data/preprocess_imagenet_validation_data.py"
  70. VAL_LABELS_FILE="${WORK_DIR}/data/imagenet_2012_validation_synset_labels.txt"
  71. "${PREPROCESS_VAL_SCRIPT}" "${VALIDATION_DIRECTORY}" "${VAL_LABELS_FILE}"
  72. # Convert the XML files for bounding box annotations into a single CSV.
  73. echo "Extracting bounding box information from XML."
  74. BOUNDING_BOX_SCRIPT="${WORK_DIR}/data/process_bounding_boxes.py"
  75. BOUNDING_BOX_FILE="${SCRATCH_DIR}/imagenet_2012_bounding_boxes.csv"
  76. BOUNDING_BOX_DIR="${SCRATCH_DIR}bounding_boxes/"
  77. "${BOUNDING_BOX_SCRIPT}" "${BOUNDING_BOX_DIR}" "${LABELS_FILE}" \
  78. | sort >"${BOUNDING_BOX_FILE}"
  79. echo "Finished downloading and preprocessing the ImageNet data."
  80. # Build the TFRecords version of the ImageNet data.
  81. BUILD_SCRIPT="${WORK_DIR}/build_imagenet_data"
  82. OUTPUT_DIRECTORY="${DATA_DIR}"
  83. IMAGENET_METADATA_FILE="${WORK_DIR}/data/imagenet_metadata.txt"
  84. "${BUILD_SCRIPT}" \
  85. --train_directory="${TRAIN_DIRECTORY}" \
  86. --validation_directory="${VALIDATION_DIRECTORY}" \
  87. --output_directory="${OUTPUT_DIRECTORY}" \
  88. --imagenet_metadata_file="${IMAGENET_METADATA_FILE}" \
  89. --labels_file="${LABELS_FILE}" \
  90. --bounding_box_file="${BOUNDING_BOX_FILE}"