preprocess_imagenet_validation_data.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/python
  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. """Process the ImageNet Challenge bounding boxes for TensorFlow model training.
  17. Associate the ImageNet 2012 Challenge validation data set with labels.
  18. The raw ImageNet validation data set is expected to reside in JPEG files
  19. located in the following directory structure.
  20. data_dir/ILSVRC2012_val_00000001.JPEG
  21. data_dir/ILSVRC2012_val_00000002.JPEG
  22. ...
  23. data_dir/ILSVRC2012_val_00050000.JPEG
  24. This script moves the files into a directory structure like such:
  25. data_dir/n01440764/ILSVRC2012_val_00000293.JPEG
  26. data_dir/n01440764/ILSVRC2012_val_00000543.JPEG
  27. ...
  28. where 'n01440764' is the unique synset label associated with
  29. these images.
  30. This directory reorganization requires a mapping from validation image
  31. number (i.e. suffix of the original file) to the associated label. This
  32. is provided in the ImageNet development kit via a Matlab file.
  33. In order to make life easier and divorce ourselves from Matlab, we instead
  34. supply a custom text file that provides this mapping for us.
  35. Sample usage:
  36. ./preprocess_imagenet_validation_data.py ILSVRC2012_img_val \
  37. imagenet_2012_validation_synset_labels.txt
  38. """
  39. from __future__ import absolute_import
  40. from __future__ import division
  41. from __future__ import print_function
  42. import os
  43. import os.path
  44. import sys
  45. if __name__ == '__main__':
  46. if len(sys.argv) < 3:
  47. print('Invalid usage\n'
  48. 'usage: preprocess_imagenet_validation_data.py '
  49. '<validation data dir> <validation labels file>')
  50. sys.exit(-1)
  51. data_dir = sys.argv[1]
  52. validation_labels_file = sys.argv[2]
  53. # Read in the 50000 synsets associated with the validation data set.
  54. labels = [l.strip() for l in open(validation_labels_file).readlines()]
  55. unique_labels = set(labels)
  56. # Make all sub-directories in the validation data dir.
  57. for label in unique_labels:
  58. labeled_data_dir = os.path.join(data_dir, label)
  59. os.makedirs(labeled_data_dir)
  60. # Move all of the image to the appropriate sub-directory.
  61. for i in range(len(labels)):
  62. basename = 'ILSVRC2012_val_000%.5d.JPEG' % (i + 1)
  63. original_filename = os.path.join(data_dir, basename)
  64. if not os.path.exists(original_filename):
  65. print('Failed to find: ' % original_filename)
  66. sys.exit(-1)
  67. new_filename = os.path.join(data_dir, labels[i], basename)
  68. os.rename(original_filename, new_filename)