|
@@ -10,9 +10,13 @@ except ImportError:
|
|
|
|
|
|
import yaml
|
|
|
import logging
|
|
|
+import numpy as np
|
|
|
+import matplotlib.pyplot as plt
|
|
|
+
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
|
from nabirds import CUB_Annotations, Dataset
|
|
|
+from nabirds.dataset import utils
|
|
|
|
|
|
|
|
|
def init_logger(args):
|
|
@@ -24,22 +28,85 @@ def init_logger(args):
|
|
|
filemode="w")
|
|
|
|
|
|
|
|
|
+def plot_crops(crops, title, scatter_mid=False, names=None):
|
|
|
+
|
|
|
+ n_crops = crops.shape[0]
|
|
|
+ rows = int(np.ceil(np.sqrt(n_crops)))
|
|
|
+ cols = int(np.ceil(n_crops / rows))
|
|
|
+
|
|
|
+ fig, axs = plt.subplots(rows, cols, figsize=(16,9))
|
|
|
+ fig.suptitle(title, fontsize=16)
|
|
|
+ for i, crop in enumerate(crops):
|
|
|
+ ax = axs[np.unravel_index(i, axs.shape)]
|
|
|
+ if names is not None:
|
|
|
+ ax.set_title(names[i])
|
|
|
+ ax.imshow(crop)
|
|
|
+ ax.axis("off")
|
|
|
+ if scatter_mid:
|
|
|
+ middle_h, middle_w = crop.shape[0] / 2, crop.shape[1] / 2
|
|
|
+ ax.scatter(middle_w, middle_h, marker="x")
|
|
|
+
|
|
|
+
|
|
|
def main(args):
|
|
|
init_logger(args)
|
|
|
|
|
|
- annot = CUB_Annotations.from_info_file(
|
|
|
+ annot = CUB_Annotations(
|
|
|
args.info, args.parts, args.feature_model)
|
|
|
+
|
|
|
logging.info("Loaded data from \"{}\"".format(annot.root))
|
|
|
|
|
|
uuids = getattr(annot, "{}_uuids".format(args.subset))
|
|
|
|
|
|
- data = Dataset(
|
|
|
- uuids=uuids, annotations=annot,
|
|
|
+ data = annot.new_dataset(
|
|
|
+ args.subset,
|
|
|
+
|
|
|
+ uniform_parts=args.uniform_parts,
|
|
|
+
|
|
|
+ crop_to_bb=args.crop_to_bb,
|
|
|
+ crop_uniform=args.crop_uniform,
|
|
|
+
|
|
|
+ parts_in_bb=args.parts_in_bb,
|
|
|
+
|
|
|
+ rnd_select=args.rnd,
|
|
|
seed=args.seed
|
|
|
)
|
|
|
|
|
|
logging.info("Loaded {} {} images".format(len(data), args.subset))
|
|
|
|
|
|
+ start = max(args.start, 0)
|
|
|
+ n_images = min(args.n_images, len(data) - start)
|
|
|
+
|
|
|
+ for i in range(start, max(start, start + n_images)):
|
|
|
+ im, parts, label = data[i]
|
|
|
+
|
|
|
+ idxs, xy = utils.visible_part_locs(parts)
|
|
|
+ part_crops = utils.visible_crops(im, parts, ratio=data.ratio)
|
|
|
+ if args.rnd:
|
|
|
+ selected = parts[:, -1].astype(bool)
|
|
|
+ parts[selected, -1] = 0
|
|
|
+ parts[np.logical_not(selected), -1] = 1
|
|
|
+ action_crops = utils.visible_crops(im, parts, ratio=data.ratio)
|
|
|
+
|
|
|
+ fig1 = plt.figure(figsize=(16,9))
|
|
|
+ ax = fig1.add_subplot(2,1,1)
|
|
|
+ ax.imshow(im)
|
|
|
+ ax.set_title("Visible Parts")
|
|
|
+ ax.scatter(*xy, marker="x", c=idxs)
|
|
|
+ ax.axis("off")
|
|
|
+
|
|
|
+ ax = fig1.add_subplot(2,1,2)
|
|
|
+ ax.set_title("{}selected parts".format("randomly " if args.rnd else ""))
|
|
|
+ ax.imshow(utils.reveal_parts(im, xy, ratio=data.ratio))
|
|
|
+ # ax.scatter(*xy, marker="x", c=idxs)
|
|
|
+ ax.axis("off")
|
|
|
+ crop_names = list(data._annot.part_names.values())
|
|
|
+ plot_crops(part_crops, "Selected parts", names=crop_names)
|
|
|
+
|
|
|
+ if args.rnd:
|
|
|
+ plot_crops(action_crops, "Actions")
|
|
|
+
|
|
|
+ plt.show()
|
|
|
+ plt.close()
|
|
|
|
|
|
|
|
|
parser = ArgumentParser()
|
|
@@ -50,15 +117,47 @@ parser.add_argument("--parts", "-p",
|
|
|
choices=["GT", "GT2", "NAC", "L1_pred", "L1_full"]
|
|
|
)
|
|
|
|
|
|
-parser.add_argument("--feature_model",
|
|
|
+parser.add_argument("--feature_model", "-fm",
|
|
|
choices=["inception", "inception_tf", "resnet"]
|
|
|
)
|
|
|
|
|
|
-parser.add_argument("--subset", "-s",
|
|
|
+parser.add_argument("--subset", "-sub",
|
|
|
help="Possible subsets: train, test",
|
|
|
choices=["train", "test"],
|
|
|
default="train", type=str)
|
|
|
|
|
|
+
|
|
|
+parser.add_argument("--start", "-s",
|
|
|
+ help="Image id to start with",
|
|
|
+ type=int, default=0)
|
|
|
+
|
|
|
+parser.add_argument("--n_images", "-n",
|
|
|
+ help="Number of images to display",
|
|
|
+ type=int, default=10)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+parser.add_argument("--rnd",
|
|
|
+ help="select random subset of present parts",
|
|
|
+ action="store_true")
|
|
|
+
|
|
|
+parser.add_argument("--uniform_parts", "-u",
|
|
|
+ help="Do not use GT parts, but sample parts uniformly from the image",
|
|
|
+ action="store_true")
|
|
|
+
|
|
|
+parser.add_argument("--crop_to_bb",
|
|
|
+ help="Crop image to the bounding box",
|
|
|
+ action="store_true")
|
|
|
+
|
|
|
+parser.add_argument("--crop_uniform",
|
|
|
+ help="Try to extend the bounding box to same height and width",
|
|
|
+ action="store_true")
|
|
|
+
|
|
|
+parser.add_argument("--parts_in_bb",
|
|
|
+ help="Only display parts, that are inside the bounding box",
|
|
|
+ action="store_true")
|
|
|
+
|
|
|
+
|
|
|
parser.add_argument(
|
|
|
'--logfile', type=str, default='',
|
|
|
help='File for logging output')
|