imagenet_split.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os.path as osp
  15. import random
  16. from .utils import list_files, is_pic
  17. import paddlex.utils.logging as logging
  18. def split_imagenet_dataset(dataset_dir, val_percent, test_percent, save_dir):
  19. all_files = list_files(dataset_dir)
  20. label_list = list()
  21. train_image_anno_list = list()
  22. val_image_anno_list = list()
  23. test_image_anno_list = list()
  24. for file in all_files:
  25. if not is_pic(file):
  26. continue
  27. label, image_name = osp.split(file)
  28. if label not in label_list:
  29. label_list.append(label)
  30. label_list = sorted(label_list)
  31. for i in range(len(label_list)):
  32. image_list = list_files(osp.join(dataset_dir, label_list[i]))
  33. image_anno_list = list()
  34. for img in image_list:
  35. image_anno_list.append([osp.join(label_list[i], img), i])
  36. random.shuffle(image_anno_list)
  37. image_num = len(image_anno_list)
  38. val_num = int(image_num * val_percent)
  39. test_num = int(image_num * test_percent)
  40. train_num = image_num - val_num - test_num
  41. train_image_anno_list += image_anno_list[:train_num]
  42. val_image_anno_list += image_anno_list[train_num:train_num + val_num]
  43. test_image_anno_list += image_anno_list[train_num + val_num:]
  44. with open(
  45. osp.join(save_dir, 'train_list.txt'), mode='w',
  46. encoding='utf-8') as f:
  47. for x in train_image_anno_list:
  48. file, label = x
  49. f.write('{} {}\n'.format(file, label))
  50. with open(
  51. osp.join(save_dir, 'val_list.txt'), mode='w',
  52. encoding='utf-8') as f:
  53. for x in val_image_anno_list:
  54. file, label = x
  55. f.write('{} {}\n'.format(file, label))
  56. if len(test_image_anno_list):
  57. with open(
  58. osp.join(save_dir, 'test_list.txt'), mode='w',
  59. encoding='utf-8') as f:
  60. for x in test_image_anno_list:
  61. file, label = x
  62. f.write('{} {}\n'.format(file, label))
  63. with open(
  64. osp.join(save_dir, 'labels.txt'), mode='w', encoding='utf-8') as f:
  65. for l in sorted(label_list):
  66. f.write('{}\n'.format(l))
  67. return len(train_image_anno_list), len(val_image_anno_list), len(
  68. test_image_anno_list)