def op_script(data: torch.Tensor) -> torch.Tensor: return kornia.rgb_to_hsv(data) data = torch.tensor([[[[21., 22.], [22., 22.]], [[13., 14.], [14., 14.]], [[8., 8.], [8., 8.]]]]) # 3x2x2 actual = op_script(data) expected = kornia.rgb_to_hsv(data) assert_allclose(actual, expected)
def augment(im, aug_type): ''' Augment images as per given augmentation type. ''' if aug_type == 'normal': im = im elif aug_type == 'rotated': rot_angle = np.random.choice([-90, 90, 180]) im = kornia_affine(im, rot_angle, 'rotate') elif aug_type == 'hsv': adjustFactor = np.random.choice( [-0.1, -0.075, -0.05, 0.05, 0.075, 0.1]) hue, sat, value = torch.chunk(kornia.rgb_to_hsv(im), chunks=3, dim=-3) adjust_mat = (torch.ones(1, 512, 512) * adjustFactor).cuda() hueNew = hue + hue * adjust_mat.cuda() hueNew = torch.clamp(hueNew, -2 * np.pi, 2 * np.pi) satNew = sat + sat * adjust_mat.cuda() satNew = torch.clamp(satNew, -2 * np.pi, 2 * np.pi) new_im = torch.cat([hueNew, satNew, value], dim=-3) im = kornia.hsv_to_rgb(new_im) elif aug_type == 'gauss_noise': im = augment_gaussian_noise(im) elif aug_type == 'mirror': im = torch.flip(im, [-2]) return im
def __call__(self,x): x = kornia.rgb_to_hsv(x) return x
############################# # Create a batch of images xb_bgr = torch.cat([x_bgr, hflip(x_bgr), vflip(x_bgr), rot180(x_bgr)]) imshow(xb_bgr) ############################# # Convert BGR to RGB xb_rgb = kornia.bgr_to_rgb(xb_bgr) imshow(xb_rgb) ############################# # Convert RGB to grayscale # NOTE: image comes in torch.uint8, and kornia assumes floating point type xb_gray = kornia.rgb_to_grayscale(xb_rgb.float() / 255.0) imshow(xb_gray) ############################# # Convert RGB to HSV xb_hsv = kornia.rgb_to_hsv(xb_rgb.float() / 255.0) imshow(xb_hsv[:, 2:3]) ############################# # Convert RGB to YUV # NOTE: image comes in torch.uint8, and kornia assumes floating point type yuv = kornia.rgb_to_yuv(xb_rgb.float() / 255.0) y_channel = torchvision.utils.make_grid(yuv, nrow=2)[0, :, :] plt.imshow(y_channel, cmap='gray', vmin=0, vmax=1) # Displaying only y channel plt.axis('off') plt.show()
def _rgb_to_hsv(img: torch.Tensor) -> torch.Tensor: return kornia.rgb_to_hsv(img)