display.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.annotation 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(root_or_infofile=args.data, parts=args.parts, load_strict=False)
  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. mask = data.labels == args.only_class
  39. logging.info(f"Showing only {mask.sum()} images from class {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())