dataset.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from imageio import imread
  2. import numpy as np
  3. class Dataset(object):
  4. def __init__(self, uuids, annotations):
  5. super(Dataset, self).__init__()
  6. self.uuids = uuids
  7. self._annot = annotations
  8. def __len__(self):
  9. return len(self.uuids)
  10. def _get(self, method, i):
  11. return getattr(self._annot, method)(self.uuids[i])
  12. def get_example(self, i, mode="RGB"):
  13. methods = ["image", "parts", "label"]
  14. im_path, parts, label = [self._get(m, i) for m in methods]
  15. return imread(im_path, pilmode=mode), parts, label
  16. __getitem__ = get_example
  17. # some convention functions
  18. DEFAULT_RATIO = np.sqrt(49 / 400)
  19. def __expand_parts(p):
  20. return p[:, 0], p[:, 1:3], p[:, 3].astype(bool)
  21. def uniform_part_locs(im, ratio=DEFAULT_RATIO, round_op=np.floor):
  22. h, w, c = im.shape
  23. part_w = round_op(w * ratio).astype(np.int32)
  24. part_h = round_op(h * ratio).astype(np.int32)
  25. n, m = w // part_w, h // part_h
  26. idxs = np.arange(n*m)
  27. locs = np.zeros((2, n*m), dtype=np.int32)
  28. for x in range(n):
  29. for y in range(m):
  30. i = y * n + x
  31. x0, y0 = x * part_w, y * part_h
  32. locs[:, i] = [x0 + part_w // 2, y0 + part_h // 2]
  33. return idxs, locs
  34. def visible_part_locs(p):
  35. idxs, locs, vis = __expand_parts(p)
  36. return idxs[vis], locs[vis].T
  37. def crops(im, xy, ratio=DEFAULT_RATIO, padding_mode="edge"):
  38. assert im.ndim == 3, "Only RGB images are currently supported!"
  39. h, w, c = im.shape
  40. crop_h, crop_w = int(h * ratio), int(w * ratio)
  41. crops = np.zeros((xy.shape[1], crop_h, crop_w, c), dtype=im.dtype)
  42. pad_h, pad_w = crop_h // 2, crop_w // 2
  43. padded_im = np.pad(im, [(pad_h, pad_h), (pad_w, pad_w), [0,0]], mode=padding_mode)
  44. for i, (x, y) in enumerate(xy.T):
  45. x0, y0 = x - crop_w // 2 + pad_w, y - crop_h // 2 + pad_h
  46. crops[i] = padded_im[y0:y0+crop_h, x0:x0+crop_w]
  47. return crops
  48. def visible_crops(im, p, *args, **kw):
  49. idxs, locs, vis = __expand_parts(p)
  50. parts = crops(im, locs[vis].T, *args, **kw)
  51. res = np.zeros((len(idxs),) + parts.shape[1:], dtype=parts.dtype)
  52. res[vis] = parts
  53. return res
  54. def reveal_parts(im, xy, ratio=DEFAULT_RATIO):
  55. h, w, c = im.shape
  56. crop_h, crop_w = int(h * ratio), int(w * ratio)
  57. res = np.zeros_like(im)
  58. for x, y in xy.T:
  59. x0, y0 = max(x - crop_w // 2, 0), max(y - crop_h // 2, 0)
  60. res[y0:y0+crop_h, x0:x0+crop_w] = im[y0:y0+crop_h, x0:x0+crop_w]
  61. return res