display.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python
  2. if __name__ != '__main__': raise Exception("Do not import me!")
  3. import sys
  4. sys.path.insert(0, "..")
  5. import logging
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8. from argparse import ArgumentParser
  9. from cvdatasets.annotations import AnnotationType
  10. from utils import parser, plot_crops
  11. def main(args):
  12. assert args.dataset in AnnotationType, \
  13. f"AnnotationType is not known: \"{args.dataset}\""
  14. annotation_cls = AnnotationType[args.dataset].value
  15. logging.info(f"Loading \"{args.dataset}\" annnotations from \"{args.data}\"")
  16. annot = annotation_cls.new(args)
  17. kwargs = {}
  18. if annot.info is None:
  19. # features = args.features[0 if args.subset == "train" else 1]
  20. kwargs = dict(
  21. part_rescale_size=args.rescale_size,
  22. # features=features,
  23. uniform_parts=args.uniform_parts,
  24. ratio=args.ratio,
  25. )
  26. data = annot.new_dataset(
  27. args.subset,
  28. center_cropped=not args.no_center_crop,
  29. crop_to_bb=args.crop_to_bb,
  30. crop_uniform=args.crop_uniform,
  31. parts_in_bb=args.parts_in_bb,
  32. rnd_select=args.rnd,
  33. seed=args.seed,
  34. **kwargs
  35. )
  36. logging.info(f"Loaded {len(data)} {args.subset} images")
  37. if args.only_class >= 0:
  38. logging.info(f"Showing only images from class {args.only_class}")
  39. mask = data.labels == args.only_class
  40. idxs = np.where(mask)[0]
  41. else:
  42. start = max(args.start, 0)
  43. n_images = min(args.n_images, len(data) - start)
  44. end = max(start, start + n_images)
  45. logging.info(f"Showing only images {start} - {end}")
  46. idxs = range(start, end)
  47. for i in idxs:
  48. im, parts, label = data[i]
  49. fig1, axs = plt.subplots(1, 1, figsize=(16,9))
  50. axs = [axs]
  51. axs[0].axis("off")
  52. axs[0].set_title("Visible Parts")
  53. axs[0].imshow(im)
  54. if not args.crop_to_bb and not args.no_bboxes:
  55. data.plot_bounding_box(i, axs[0])
  56. # axs[1].axis("off")
  57. # axs[1].set_title("{}selected parts".format("randomly " if args.rnd else ""))
  58. # axs[1].imshow(parts.reveal(im, ratio=data.ratio))
  59. if not args.no_parts:
  60. parts.plot(im=im, ax=axs[0], ratio=data.ratio, linewidth=3)
  61. if data.uniform_parts:
  62. crop_names = None
  63. else:
  64. crop_names = list(data._annot.part_names.values())
  65. part_crops = parts.visible_crops(im, ratio=data.ratio)
  66. if args.rnd:
  67. parts.invert_selection()
  68. action_crops = parts.visible_crops(im, ratio=data.ratio)
  69. plot_crops(part_crops, f"{args.parts}: Selected parts", names=crop_names)
  70. if args.rnd:
  71. plot_crops(action_crops, f"{args.parts}: Actions", names=crop_names)
  72. plt.show()
  73. plt.close()
  74. main(parser.parse_args())