Browse Source

modified uniform part creation. added functions for random part selection

Dimitri Korsch 7 năm trước cách đây
mục cha
commit
0b206d68e4
3 tập tin đã thay đổi với 46 bổ sung20 xóa
  1. 1 1
      nabirds/__init__.py
  2. 29 7
      nabirds/dataset/utils.py
  3. 16 12
      nabirds/display.py

+ 1 - 1
nabirds/__init__.py

@@ -1,4 +1,4 @@
 from .dataset import Dataset
 from .annotations import NAB_Annotations, CUB_Annotations
 
-__version__ = "0.1.4"
+__version__ = "0.1.5"

+ 29 - 7
nabirds/dataset/utils.py

@@ -6,8 +6,7 @@ def __expand_parts(p):
 	return p[:, 0], p[:, 1:3], p[:, 3].astype(bool)
 
 
-
-def uniform_part_locs(im, ratio=DEFAULT_RATIO, round_op=np.floor):
+def uniform_parts(im, ratio=DEFAULT_RATIO, round_op=np.floor):
 	h, w, c = im.shape
 
 	part_w = round_op(w * ratio).astype(np.int32)
@@ -15,17 +14,16 @@ def uniform_part_locs(im, ratio=DEFAULT_RATIO, round_op=np.floor):
 
 	n, m = w // part_w, h // part_h
 
-	idxs = np.arange(n*m)
-	locs = np.zeros((2, n*m), dtype=np.int32)
-
+	parts = np.ones((n*m, 4), dtype=int)
+	parts[:, 0] = np.arange(n*m)
 
 	for x in range(n):
 		for y in range(m):
 			i = y * n + x
 			x0, y0 = x * part_w, y * part_h
-			locs[:, i] = [x0 + part_w // 2, y0 + part_h // 2]
+			parts[i, 1:3] = [x0 + part_w // 2, y0 + part_h // 2]
 
-	return idxs, locs
+	return parts
 
 def visible_part_locs(p):
 	idxs, locs, vis = __expand_parts(p)
@@ -66,3 +64,27 @@ def reveal_parts(im, xy, ratio=DEFAULT_RATIO):
 		res[y0:y0+crop_h, x0:x0+crop_w] = im[y0:y0+crop_h, x0:x0+crop_w]
 
 	return res
+
+def random_select(idxs, xy, part_crops, *args, **kw):
+	rnd_idxs = random_idxs(np.arange(len(idxs)), *args, **kw)
+	idxs = idxs[rnd_idxs]
+	xy = xy[:, rnd_idxs]
+
+	selected_mask = np.bincount(idxs, minlength=len(part_crops)).astype(bool)
+	p_crops = part_crops.copy()
+	p_crops[np.logical_not(selected_mask)] = 0
+
+	return idxs, xy, p_crops
+
+def random_idxs(idxs, rnd=None, n_parts=None):
+
+	if rnd is None or isinstance(rnd, int):
+		rnd = np.random.RandomState(rnd)
+	else:
+		assert isinstance(rnd, np.random.RandomState), \
+			"'rnd' should be either a random seed or a RandomState instance!"
+
+	n_parts = n_parts or rnd.randint(1, len(idxs))
+	res = rnd.choice(idxs, n_parts, replace=False)
+	res.sort()
+	return res

+ 16 - 12
nabirds/display.py

@@ -7,9 +7,10 @@ import numpy as np
 
 from annotations import NAB_Annotations, CUB_Annotations
 from dataset import Dataset
-from dataset.utils import reveal_parts, \
-	visible_part_locs, visible_crops, \
-	uniform_part_locs, crops
+from dataset.utils import reveal_parts, uniform_parts, \
+	random_select, \
+	visible_part_locs, visible_crops
+
 
 import matplotlib.pyplot as plt
 
@@ -42,31 +43,34 @@ def main(args):
 		im, parts, label = data[i]
 
 		if args.uniform_parts:
-			idxs, xy = uniform_part_locs(im, ratio=args.ratio)
-		else:
-			idxs, xy = visible_part_locs(parts)
+			parts = uniform_parts(im, ratio=args.ratio)
+
+		idxs, xy = visible_part_locs(parts)
+		part_crops = visible_crops(im, parts, ratio=args.ratio)
 
 		logging.debug(label)
 		logging.debug(idxs)
+		logging.debug(xy)
 
 		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")
 
+		if args.rnd:
+			idxs, xy, part_crops = random_select(idxs, xy, part_crops)
+
 		ax = fig1.add_subplot(2,1,2)
+		ax.set_title("{}selected parts".format("randomly " if args.rnd else ""))
 		ax.imshow(reveal_parts(im, xy, ratio=args.ratio))
 		ax.scatter(*xy, marker="x", c=idxs)
 		ax.axis("off")
 
-		if args.uniform_parts:
-			part_crops = crops(im, xy, ratio=args.ratio)
-		else:
-			part_crops = visible_crops(im, parts, ratio=args.ratio)
-
 		fig2 = plt.figure(figsize=(16,9))
-		n_crops = len(part_crops)
+
+		n_crops = part_crops.shape[0]
 		rows = int(np.ceil(np.sqrt(n_crops)))
 		cols = int(np.ceil(n_crops / rows))