Meehai commited on
Commit
595564c
·
1 Parent(s): 0449410

little fixes

Browse files
neo_reader/multitask_dataset.py CHANGED
@@ -28,7 +28,7 @@ class NpzRepresentation:
28
 
29
  def save_to_disk(self, data: tr.Tensor, path: Path):
30
  """stores this item to the disk which can then be loaded via `load_from_disk`"""
31
- np.save(path, data.cpu().numpy(), allow_pickle=False)
32
 
33
  def plot_fn(self, x: tr.Tensor) -> np.ndarray:
34
  """very basic implementation of converting this representation to a viewable image. You should overwrite this"""
@@ -37,7 +37,7 @@ class NpzRepresentation:
37
  assert len(x.shape) == 3, x.shape # guaranteed to be (H, W, C) at this point
38
  if x.shape[-1] != 3: x = x[..., 0:1]
39
  if x.shape[-1] == 1: x = x.repeat(1, 1, 3)
40
- x = x.nan_to_num(0).cpu().numpy() # guaranteed to be (H, W, 3) at this point hopefully
41
  _min, _max = x.min((0, 1), keepdims=True), x.max((0, 1), keepdims=True)
42
  if x.dtype != np.uint8: x = np.nan_to_num((x - _min) / (_max - _min) * 255, 0).astype(np.uint8)
43
  return x
@@ -119,7 +119,7 @@ class MultiTaskDataset(Dataset):
119
  for task_name in self.task_names:
120
  t = self.task_types[task_name]
121
  try:
122
- t = t(task_name)
123
  except Exception:
124
  pass
125
  self._tasks.append(t)
 
28
 
29
  def save_to_disk(self, data: tr.Tensor, path: Path):
30
  """stores this item to the disk which can then be loaded via `load_from_disk`"""
31
+ np.save(path, data.cpu().detach().numpy(), allow_pickle=False)
32
 
33
  def plot_fn(self, x: tr.Tensor) -> np.ndarray:
34
  """very basic implementation of converting this representation to a viewable image. You should overwrite this"""
 
37
  assert len(x.shape) == 3, x.shape # guaranteed to be (H, W, C) at this point
38
  if x.shape[-1] != 3: x = x[..., 0:1]
39
  if x.shape[-1] == 1: x = x.repeat(1, 1, 3)
40
+ x = x.nan_to_num(0).cpu().detach().numpy() # guaranteed to be (H, W, 3) at this point hopefully
41
  _min, _max = x.min((0, 1), keepdims=True), x.max((0, 1), keepdims=True)
42
  if x.dtype != np.uint8: x = np.nan_to_num((x - _min) / (_max - _min) * 255, 0).astype(np.uint8)
43
  return x
 
119
  for task_name in self.task_names:
120
  t = self.task_types[task_name]
121
  try:
122
+ t = t(task_name) # hack for not isinstance(self.task_types, NpzRepresentation) but callable
123
  except Exception:
124
  pass
125
  self._tasks.append(t)
neo_reader/neo_node.py CHANGED
@@ -33,7 +33,7 @@ def _act_to_cmap(act_file: Path) -> np.ndarray:
33
  return rgb_colors
34
 
35
  class NEONode(NpzRepresentation):
36
- """NEO nodes implementation in ngclib repository"""
37
  def __init__(self, node_type: str, name: str):
38
  self.node_type = node_type
39
  self.name = name
@@ -45,11 +45,9 @@ class NEONode(NpzRepresentation):
45
  def load_from_disk(self, path: Path) -> tr.Tensor:
46
  data = np.load(path, allow_pickle=False)
47
  y = data if isinstance(data, np.ndarray) else data["arr_0"] # in case on npz, we need this as well
48
- if y.shape[0] == 1: # pylint: disable=unsubscriptable-object
49
- y = y[0] # pylint: disable=unsubscriptable-object
50
- if len(y.shape) == 2:
51
- y = np.expand_dims(y, axis=-1)
52
- y[np.isnan(y)] = 0
53
  return tr.from_numpy(y).float()
54
 
55
  @overrides
@@ -57,9 +55,9 @@ class NEONode(NpzRepresentation):
57
  return super().save_to_disk(data.clip(0, 1), path)
58
 
59
  def plot_fn(self, x: tr.Tensor) -> np.ndarray:
60
- y = np.clip(x.cpu().numpy(), 0, 1)
61
  y = y * 255
62
- y[y == 0] = 255
63
  y = y.astype(np.uint).squeeze()
64
  y_rgb = self.cmap[y].astype(np.uint8)
65
  return y_rgb
 
33
  return rgb_colors
34
 
35
  class NEONode(NpzRepresentation):
36
+ """NEO nodes implementation"""
37
  def __init__(self, node_type: str, name: str):
38
  self.node_type = node_type
39
  self.name = name
 
45
  def load_from_disk(self, path: Path) -> tr.Tensor:
46
  data = np.load(path, allow_pickle=False)
47
  y = data if isinstance(data, np.ndarray) else data["arr_0"] # in case on npz, we need this as well
48
+ y = y[0] if y.shape[0] == 1 else y # pylint: disable=unsubscriptable-object
49
+ y = np.expand_dims(y, axis=-1) if len(y.shape) == 2 else y
50
+ y[y == 0] = float("nan")
 
 
51
  return tr.from_numpy(y).float()
52
 
53
  @overrides
 
55
  return super().save_to_disk(data.clip(0, 1), path)
56
 
57
  def plot_fn(self, x: tr.Tensor) -> np.ndarray:
58
+ y = np.clip(x.cpu().detach().numpy(), 0, 1)
59
  y = y * 255
60
+ y[np.isnan(y)] = 255
61
  y = y.astype(np.uint).squeeze()
62
  y_rgb = self.cmap[y].astype(np.uint8)
63
  return y_rgb