MCPcopy Create free account
hub / github.com/DragonisCV/RAM / tensor2img_fast

Function tensor2img_fast

ram/utils/img_util.py:97–111  ·  view source on GitHub ↗

This implementation is slightly faster than tensor2img. It now only supports torch tensor with shape (1, c, h, w). Args: tensor (Tensor): Now only support torch tensor with (1, c, h, w). rgb2bgr (bool): Whether to change rgb to bgr. Default: True. min_max (tuple[int]

(tensor, rgb2bgr=True, min_max=(0, 1))

Source from the content-addressed store, hash-verified

95
96
97def tensor2img_fast(tensor, rgb2bgr=True, min_max=(0, 1)):
98 """This implementation is slightly faster than tensor2img.
99 It now only supports torch tensor with shape (1, c, h, w).
100
101 Args:
102 tensor (Tensor): Now only support torch tensor with (1, c, h, w).
103 rgb2bgr (bool): Whether to change rgb to bgr. Default: True.
104 min_max (tuple[int]): min and max values for clamp.
105 """
106 output = tensor.squeeze(0).detach().clamp_(*min_max).permute(1, 2, 0)
107 output = (output - min_max[0]) / (min_max[1] - min_max[0]) * 255
108 output = output.type(torch.uint8).cpu().numpy()
109 if rgb2bgr:
110 output = cv2.cvtColor(output, cv2.COLOR_RGB2BGR)
111 return output
112
113
114def imfrombytes(content, flag='color', float32=False):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected