使用GPU实现对每个像素点的并行操作

使用GPU实现对每个像素点的并行操作

例如给每个像素点独立添加噪声。

 

原来的方法对于640x480的图像大概需要6秒。

def add_noise(im):

    for i in range(3):

        for x in range(im.shape[0]):

            for y in range(im.shape[1]):

                im[x,y,i] = im[x,y,i] * np.exp(np.random.normal(0, 0.05))

    im = np.clip(im, 0, 255)

 

    return im

 

现在采用torch实现GPU运算。(加速显著,每秒约53)

def add_noise(im):

 

    rand = torch.randn(*im.shape) * 0.2

    rand = torch.exp(rand)

 

    return torch.clip(torch.Tensor(im) * rand, 0, 255).numpy()

 

由于图像像素值变成了浮点数,显示时请使用:

cv2.imshow('frame', frame / 255.)

 

深度学习推荐
深度学习推荐

墨之科技,版权所有 © Copyright 2017-2027

湘ICP备14012786号     邮箱:ai@inksci.com