display.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 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. annot = AnnotationType.new_annotation(args)
  15. kwargs = {}
  16. if annot.info is None:
  17. kwargs = dict(
  18. part_rescale_size=args.rescale_size,
  19. uniform_parts=args.uniform_parts,
  20. ratio=args.ratio,
  21. )
  22. data = annot.new_dataset(
  23. args.subset,
  24. center_cropped=not args.no_center_crop,
  25. crop_to_bb=args.crop_to_bb,
  26. crop_uniform=args.crop_uniform,
  27. parts_in_bb=args.parts_in_bb,
  28. rnd_select=args.rnd,
  29. seed=args.seed,
  30. **kwargs
  31. )
  32. logging.info(f"Loaded {len(data)} {args.subset} images")
  33. if args.only_class >= 0:
  34. mask = data.labels == args.only_class
  35. logging.info(f"Showing only {mask.sum()} images from class {args.only_class}")
  36. idxs = np.where(mask)[0]
  37. else:
  38. start = max(args.start, 0)
  39. n_images = min(args.n_images, len(data) - start)
  40. end = max(start, start + n_images)
  41. logging.info(f"Showing only images {start} - {end}")
  42. idxs = range(start, end)
  43. for i in idxs:
  44. im, parts, label = data[i]
  45. fig1, axs = plt.subplots(1, 1, figsize=(16,9))
  46. axs = [axs]
  47. axs[0].axis("off")
  48. axs[0].set_title("Visible Parts")
  49. axs[0].imshow(im)
  50. if not args.crop_to_bb and not args.no_bboxes:
  51. data.plot_bounding_box(i, axs[0])
  52. # axs[1].axis("off")
  53. # axs[1].set_title("{}selected parts".format("randomly " if args.rnd else ""))
  54. # axs[1].imshow(parts.reveal(im, ratio=data.ratio))
  55. if not args.no_parts:
  56. parts.plot(im=im, ax=axs[0], ratio=data.ratio, linewidth=3)
  57. if data.uniform_parts:
  58. crop_names = None
  59. else:
  60. crop_names = list(data._annot.part_names.values())
  61. part_crops = parts.visible_crops(im, ratio=data.ratio)
  62. if args.rnd:
  63. parts.invert_selection()
  64. action_crops = parts.visible_crops(im, ratio=data.ratio)
  65. plot_crops(part_crops, f"{args.parts}: Selected parts", names=crop_names)
  66. if args.rnd:
  67. plot_crops(action_crops, f"{args.parts}: Actions", names=crop_names)
  68. plt.show()
  69. plt.close()
  70. main(parser.parse_args())