Bananaware

qcount

Jan 24th, 2021
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. '''Quality Counter - 5/ago/2020
  2.  
  3. Counts how many "Good", "Bad" and "Whatever" images are in each subfolder.
  4. Run this from the folder containing the subfolders with the labeled images (as generated by quality-labeler.py)
  5.  
  6. Sample output:
  7. Jersey_1 | Good: 337 | Bad: 181 | +-: 149 | 51% 27% 22% (337/667)
  8. Jersey_2 | Good: 116 | Bad: 141 | +-: 37 | 39% 48% 13% (116/294)
  9. Jersey_3 | Good: 309 | Bad: 116 | +-: 73 | 62% 23% 15% (309/498)
  10. Total | Good: 762 | Bad: 438 | +-: 259 | 52% 30% 18% (762/1459)
  11.  
  12. Puruna_1 | Good: 62 | Bad: 776 | +-: 37 | 7.1% 89% 4.2% (62/875)
  13. Puruna_2 | Good: 89 | Bad: 1132 | +-: 75 | 6.9% 87% 5.8% (89/1296)
  14. Puruna_3 | Good: 70 | Bad: 1656 | +-: 67 | 3.9% 92% 3.7% (70/1793)
  15. Total | Good: 221 | Bad: 3564 | +-: 179 | 5.6% 90% 4.5% (221/3964)
  16.  
  17. USP-P1 | Good: 161 | Bad: 50 | +-: 124 | 48% 15% 37% (161/335)
  18. USP-P2 | Good: 211 | Bad: 56 | +-: 43 | 68% 18% 14% (211/310)
  19. USP-P3 | Good: 230 | Bad: 67 | +-: 45 | 67% 20% 13% (230/342)
  20. Total | Good: 602 | Bad: 173 | +-: 212 | 61% 18% 21% (602/987)
  21.  
  22. Total | Good: 1585 | Bad: 4175 | +-: 650 | 25% 65% 10% (1585/6410)
  23. '''
  24.  
  25. import os
  26. import fnmatch
  27. import shutil
  28. import re
  29. from itertools import groupby
  30.  
  31.  
  32. MODE = 1 # 0 = All subfolders, 1 = Predetermined split
  33. COUNT_AUG = False # Counts Horizontally Flipped folders created for Data Augmentation
  34. AUG_SUFFIX = "_aug" # Which folders are used is defined later
  35. COUNT_MIDS = True # Counts labels over 1
  36. GROUP_MIDS = True # Groups labels over 1 as being 0
  37. NUM_CLASSES = 4
  38. SPLIT_KEY = 'Z'
  39.  
  40.  
  41.  
  42. local_count = part_count = total_count = [0] * NUM_CLASSES
  43. DF = './' # Data folder
  44.  
  45.  
  46. # split lists. train, valid, test
  47. splits_dict = {}
  48.  
  49. # full sample
  50. splits_dict['X'] = [[DF+'Puruna_1', DF+'Jersey_3', DF+'USP-P2'],
  51. [DF+'Puruna_3', DF+'Jersey_2', DF+'USP-P3'],
  52. [DF+'Puruna_2', DF+'Jersey_1', DF+'USP-P1']]
  53.  
  54. splits_dict['Z'] = [[DF+'Puruna_3', DF+'Jersey_3', DF+'Nelore~USP-P3'],
  55. [DF+'Puruna_2', DF+'Jersey_1', DF+'Nelore~USP-P2'],
  56. [DF+'Puruna_1', DF+'Jersey_2', DF+'Nelore~USP-P1']]
  57.  
  58. splits_dict['Z_part'] = [[DF+'Puruna_1', DF+'Jersey_2', DF+'Nelore~USP-P1']]
  59.  
  60. splits_dict['G'] = [[DF+'Puruna_1', DF+'Jersey_2', DF+'Nelore~USP-P2'],
  61. [DF+'Puruna_3', DF+'Jersey_1', DF+'Nelore~USP-P3'],
  62. [DF+'Puruna_2', DF+'Jersey_3', DF+'Nelore~USP-P1']]
  63.  
  64.  
  65. # intra-sensor
  66. splits_dict['IS1'] = [[DF+'Jersey_1-P1', DF+'Puruna_1'],
  67. [DF+'Jersey_1-P2', DF+'Puruna_2'],
  68. [DF+'Jersey_1-P3', DF+'Puruna_3']]
  69.  
  70. # intra-classe
  71. splits_dict['IC1'] = [[DF+'Jersey_1'],
  72. [DF+'Jersey_2'],
  73. [DF+'Jersey_3']]
  74.  
  75. splits_dict['IC2'] = [[DF+'Puruna_1'],
  76. [DF+'Puruna_2'],
  77. [DF+'Puruna_3']]
  78.  
  79.  
  80. # test. small sample para carregar rapido e testar mudancas no codigo
  81. splits_dict['test'] = [[DF+'Nelore~USP-P1'],
  82. [DF+'Nelore~USP-P2'],
  83. [DF+'Nelore~USP-P3']]
  84.  
  85.  
  86. if MODE == 0:
  87. subfolders_init = [f.path for f in os.scandir(DF) if f.is_dir()]
  88. subfolders_init.sort()
  89. if not COUNT_AUG:
  90. subfolders_init = [i for i in subfolders_init if not i.endswith(AUG_SUFFIX)]
  91. subfolders = [list(i) for j, i in groupby(subfolders_init, lambda a: re.split('|'.join(map(re.escape, ('_', '-'))), a.split('/')[-1], 0)[0])] # Splits on _ and -
  92.  
  93. if MODE == 1:
  94. subfolders = splits_dict[SPLIT_KEY]
  95. if COUNT_AUG: # Looks for hflip folders and adds them
  96. for split in subfolders:
  97. for s in split:
  98. if os.path.isdir(s + AUG_SUFFIX): # Check if hflip folder exists
  99. split.append(s + AUG_SUFFIX)
  100.  
  101.  
  102. def print_results(folder_name, counter):
  103. good = counter[1]
  104. bad = counter[0]
  105. whatever = counter[2]
  106. artifact = counter[3]
  107.  
  108. total = good+bad
  109. if COUNT_MIDS:
  110. total += whatever
  111. total += artifact
  112.  
  113. good_ratio = (float(good)/total)*100
  114. if COUNT_MIDS:
  115. if GROUP_MIDS:
  116. bad += whatever
  117. bad += artifact
  118. bad_ratio = (float(bad)/total)*100
  119. print("%20s | Good: %-4d | Bad: %-4d | %.4g%% %.4g%% (%d/%d)" % (folder_name, good, bad, good_ratio, bad_ratio, good, total))
  120. else:
  121. bad_ratio = (float(bad)/total)*100
  122. whatever_ratio = (float(whatever)/total)*100
  123. artifact_ratio = (float(artifact)/total)*100
  124. print("%20s | Good: %-5d | Bad: %-5d | +-: %-5d | A: %-5d | %.2g%% %.2g%% %.2g%% %.2g%% (%d/%d)" % (folder_name, good, bad, whatever, artifact, good_ratio, bad_ratio, whatever_ratio, artifact_ratio, good, total))
  125. else:
  126. bad_ratio = (float(bad)/total)*100
  127. print("%20s | Good: %-4d | Bad: %-4d | %.4g%% %.4g%% (%d/%d)" % (folder_name, good, bad, good_ratio, bad_ratio, good, total))
  128.  
  129. for split in subfolders:
  130. part_count = [0] * NUM_CLASSES
  131.  
  132. for s in split:
  133. list_files = []
  134. for root, dirnames, filenames in os.walk(s):
  135. for filename in fnmatch.filter(filenames, '*.[Jj][Pp][Gg]'): # Case insensitive with regards to the file extension
  136. list_files.append(os.path.join(root, filename))
  137. list_files.sort()
  138.  
  139. local_count = [0] * NUM_CLASSES
  140.  
  141. for f in list_files:
  142. local_count[int(f.split('/')[-2])] += 1
  143.  
  144. for i, pos_count in enumerate(local_count):
  145. part_count[i] += pos_count
  146.  
  147. if sum(local_count) > 0: # Ignores empty subfolders
  148. print_results(s.split('/')[-1], local_count)
  149.  
  150. if sum(part_count) > 0:
  151. print_results("Total", part_count)
  152. print()
  153. for i, pos_count in enumerate(part_count):
  154. total_count[i] += pos_count
  155.  
  156. if sum(total_count) > 0:
  157. print_results("Total", total_count)
  158.  
Advertisement
Add Comment
Please, Sign In to add comment