display.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/usr/bin/env python
  2. if __name__ != '__main__': raise Exception("Do not import me!")
  3. from argparse import ArgumentParser
  4. import logging
  5. import numpy as np
  6. from annotations import NAB_Annotations, CUB_Annotations
  7. from dataset import Dataset
  8. from dataset.utils import reveal_parts, uniform_parts, \
  9. random_select, \
  10. visible_part_locs, visible_crops
  11. import matplotlib.pyplot as plt
  12. def init_logger(args):
  13. fmt = "%(levelname)s - [%(asctime)s] %(filename)s:%(lineno)d [%(funcName)s]: %(message)s"
  14. logging.basicConfig(
  15. format=fmt,
  16. level=getattr(logging, args.loglevel.upper(), logging.DEBUG),
  17. filename=args.logfile or None,
  18. filemode="w")
  19. def plot_crops(crops, title, scatter_mid=False):
  20. fig = plt.figure(figsize=(16,9))
  21. fig.suptitle(title, fontsize=16)
  22. n_crops = crops.shape[0]
  23. rows = int(np.ceil(np.sqrt(n_crops)))
  24. cols = int(np.ceil(n_crops / rows))
  25. for j, crop in enumerate(crops, 1):
  26. ax = fig.add_subplot(rows, cols, j)
  27. ax.imshow(crop)
  28. ax.axis("off")
  29. if scatter_mid:
  30. middle_h, middle_w = crop.shape[0] / 2, crop.shape[1] / 2
  31. ax.scatter(middle_w, middle_h, marker="x")
  32. def main(args):
  33. init_logger(args)
  34. annotation_cls = dict(
  35. nab=NAB_Annotations,
  36. cub=CUB_Annotations)
  37. logging.info("Loading \"{}\" annnotations from \"{}\"".format(args.dataset, args.data))
  38. annot = annotation_cls.get(args.dataset.lower())(args.data)
  39. subset = args.subset.lower()
  40. uuids = getattr(annot, "{}_uuids".format(subset))
  41. features = args.features[0 if subset == "train" else 1]
  42. data = Dataset(
  43. uuids=uuids, annotations=annot,
  44. features=features,
  45. uniform_parts=args.uniform_parts,
  46. crop_to_bb=args.crop_to_bb,
  47. crop_uniform=args.crop_uniform,
  48. parts_in_bb=args.parts_in_bb,
  49. rnd_select=args.rnd,
  50. ratio=args.ratio,
  51. seed=args.seed
  52. )
  53. n_images = len(data)
  54. logging.info("Found {} images in the {} subset".format(n_images, subset))
  55. for i in range(n_images):
  56. if i + 1 <= args.start: continue
  57. im, parts, label = data[i]
  58. idxs, xy = visible_part_locs(parts)
  59. part_crops = visible_crops(im, parts, ratio=args.ratio)
  60. if args.rnd:
  61. selected = parts[:, -1].astype(bool)
  62. parts[selected, -1] = 0
  63. parts[np.logical_not(selected), -1] = 1
  64. action_crops = visible_crops(im, parts, ratio=args.ratio)
  65. logging.debug(label)
  66. logging.debug(idxs)
  67. logging.debug(xy)
  68. fig1 = plt.figure(figsize=(16,9))
  69. ax = fig1.add_subplot(2,1,1)
  70. ax.imshow(im)
  71. ax.set_title("Visible Parts")
  72. ax.scatter(*xy, marker="x", c=idxs)
  73. ax.axis("off")
  74. ax = fig1.add_subplot(2,1,2)
  75. ax.set_title("{}selected parts".format("randomly " if args.rnd else ""))
  76. ax.imshow(reveal_parts(im, xy, ratio=args.ratio))
  77. # ax.scatter(*xy, marker="x", c=idxs)
  78. ax.axis("off")
  79. plot_crops(part_crops, "Selected parts")
  80. if args.rnd:
  81. plot_crops(action_crops, "Actions")
  82. plt.show()
  83. plt.close()
  84. if i+1 >= args.start + args.n_images: break
  85. parser = ArgumentParser()
  86. parser.add_argument("data",
  87. help="Folder containing the dataset with images and annotation files",
  88. type=str)
  89. parser.add_argument("--dataset",
  90. help="Possible datasets: NAB, CUB",
  91. choices=["cub", "nab"],
  92. default="nab", type=str)
  93. parser.add_argument("--features",
  94. help="pre-extracted train and test features",
  95. default=[None, None],
  96. nargs=2, type=str)
  97. parser.add_argument("--subset",
  98. help="Possible subsets: train, test",
  99. choices=["train", "test"],
  100. default="train", type=str)
  101. parser.add_argument("--start", "-s",
  102. help="Image id to start with",
  103. type=int, default=0)
  104. parser.add_argument("--n_images", "-n",
  105. help="Number of images to display",
  106. type=int, default=10)
  107. parser.add_argument("--ratio",
  108. help="Part extraction ratio",
  109. type=float, default=.2)
  110. parser.add_argument("--rnd",
  111. help="select random subset of present parts",
  112. action="store_true")
  113. parser.add_argument("--uniform_parts", "-u",
  114. help="Do not use GT parts, but sample parts uniformly from the image",
  115. action="store_true")
  116. parser.add_argument("--crop_to_bb",
  117. help="Crop image to the bounding box",
  118. action="store_true")
  119. parser.add_argument("--crop_uniform",
  120. help="Try to extend the bounding box to same height and width",
  121. action="store_true")
  122. parser.add_argument("--parts_in_bb",
  123. help="Only display parts, that are inside the bounding box",
  124. action="store_true")
  125. parser.add_argument(
  126. '--logfile', type=str, default='',
  127. help='File for logging output')
  128. parser.add_argument(
  129. '--loglevel', type=str, default='INFO',
  130. help='logging level. see logging module for more information')
  131. parser.add_argument(
  132. '--seed', type=int, default=12311123,
  133. help='random seed')
  134. main(parser.parse_args())