Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def op_script(data: torch.Tensor) -> torch.Tensor:
return kornia.bgr_to_rgb(data)
data = torch.tensor([[[1., 1.],
[1., 1.]],
[[2., 2.],
[2., 2.]],
[[3., 3.],
[3., 3.]]]) # 3x2x2
data = data.to(device)
actual = op_script(data)
expected = kornia.bgr_to_rgb(data)
assert_allclose(actual, expected)
from matplotlib import pyplot as plt
import cv2
import numpy as np
import torch
import kornia
import torchvision
#############################
# We use OpenCV to load an image to memory represented in a numpy.ndarray
img_bgr: np.ndarray = cv2.imread('./data/drslump.jpg', cv2.IMREAD_COLOR)
#############################
# Convert the numpy array to torch
x_bgr: torch.Tensor = kornia.image_to_tensor(img_bgr)
x_rgb: torch.Tensor = kornia.bgr_to_rgb(x_bgr)
#############################
# Create batch and normalize
x_rgb = x_rgb.expand(2, -1, -1, -1) # 4xCxHxW
x_rgb = x_rgb.float() / 255.
def imshow(input: torch.Tensor):
out: torch.Tensor = torchvision.utils.make_grid(input, nrow=2, padding=1)
out_np: np.ndarray = kornia.tensor_to_image(out)
plt.imshow(out_np)
plt.axis('off')
#############################
# Show original
imshow(x_rgb)
import cv2
import numpy as np
import matplotlib.pyplot as plt
#############################
# We use OpenCV to load an image to memory represented in a numpy.ndarray
img_bgr: np.ndarray = cv2.imread('./data/arturito.jpeg') # HxWxC
#############################
# The image is convert to a 4D torch tensor
x_bgr: torch.tensor = kornia.image_to_tensor(img_bgr) # 1xCxHxW
#############################
# Once with a torch tensor we can use any Kornia operator
x_rgb: torch.tensor = kornia.bgr_to_rgb(x_bgr) # 1xCxHxW
#############################
# Convert back to numpy to visualize
img_rgb: np.ndarray = kornia.tensor_to_image(x_rgb.byte()) # HxWxC
#############################
# We use Matplotlib to visualize de results
fig, axs = plt.subplots(1, 2, figsize=(32, 16))
axs = axs.ravel()
axs[0].axis('off')
axs[0].imshow(img_bgr)
axs[1].axis('off')
axs[1].imshow(img_rgb)
from matplotlib import pyplot as plt
import cv2
import numpy as np
import torch
import kornia
import torchvision
#############################
# We use OpenCV to load an image to memory represented in a numpy.ndarray
img_bgr: np.ndarray = cv2.imread('./data/ninja_turtles.jpg', cv2.IMREAD_COLOR)
#############################
# Convert the numpy array to torch
x_bgr: torch.Tensor = kornia.image_to_tensor(img_bgr)
x_rgb: torch.Tensor = kornia.bgr_to_rgb(x_bgr)
#############################
# Create batch and normalize
x_rgb = x_rgb.expand(4, -1, -1, -1) # 4xCxHxW
x_rgb = x_rgb.float() / 255.
def imshow(input: torch.Tensor):
out: torch.Tensor = torchvision.utils.make_grid(input, nrow=2, padding=5)
out_np: np.ndarray = kornia.tensor_to_image(out)
plt.imshow(out_np)
plt.axis('off')
#############################
# Show original
imshow(x_rgb)
from matplotlib import pyplot as plt
import cv2
import numpy as np
import torch
import kornia
import torchvision
#############################
# We use OpenCV to load an image to memory represented in a numpy.ndarray
img_bgr: np.ndarray = cv2.imread('./data/doraemon.png', cv2.IMREAD_COLOR)
#############################
# Convert the numpy array to torch
x_bgr: torch.Tensor = kornia.image_to_tensor(img_bgr)
x_rgb: torch.Tensor = kornia.bgr_to_rgb(x_bgr)
#############################
# Create batch and normalize
x_rgb = x_rgb.expand(2, -1, -1, -1) # 4xCxHxW
x_gray = kornia.rgb_to_grayscale(x_rgb.float() / 255.)
def imshow(input: torch.Tensor):
out: torch.Tensor = torchvision.utils.make_grid(input, nrow=2, padding=1)
out_np: np.ndarray = kornia.tensor_to_image(out)
plt.imshow(out_np)
plt.axis('off')
#############################
# Show original
imshow(x_rgb)
def imshow(input: torch.Tensor):
out: torch.Tensor = torchvision.utils.make_grid(input, nrow=2, padding=5)
out_np: np.ndarray = kornia.tensor_to_image(out)
plt.imshow(out_np)
plt.axis('off')
#############################
# 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.)
imshow(xb_gray)
#############################
# Convert RGB to HSV
xb_hsv = kornia.rgb_to_hsv(xb_rgb.float() / 255.)
imshow(xb_hsv[:, 2:3])